Skip to content

Commit

Permalink
no longer have to copy stdlib functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Destaq committed May 15, 2024
1 parent 5935df4 commit 5f11c64
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 32 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ open-bisect:
clean:
rm -rf _coverage
dune clean

cloc:
cloc --by-file --include-lang=OCaml . --exclude-dir=_build,.git
28 changes: 5 additions & 23 deletions lib/parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ let rec print_string_list lst sep =
let () = print_string (h ^ sep) in
print_string_list t sep

(**Helper function for GitHub Actions, copy of List.find_index.*)
let find_index p =
let rec aux i = function
| [] -> None
| a :: l -> if p a then Some i else aux (i + 1) l
in
aux 0

(**Print out a list of tokens generated by the parser.*)
let print_tokenized tokens =
List.iter
Expand Down Expand Up @@ -50,25 +42,13 @@ let replace_all str old_substring new_substring =
in
replace_helper str old_substring new_substring 0

(*Helper function to ensure that tests pass on ci/cd.*)
let ends_with ~suffix s =
let len_s = String.length s and len_suf = String.length suffix in
let diff = len_s - len_suf in
let rec aux i =
if i = len_suf then true
else if String.unsafe_get s (diff + i) <> String.unsafe_get suffix i then
false
else aux (i + 1)
in
diff >= 0 && aux 0

(**Group and merge tokens based on quotations.*)
let rec quote_grouping acc cur_group in_group tokens =
match tokens with
| [] -> List.rev acc
| h :: t ->
if in_group then
if ends_with ~suffix:"\"" h then
if String.ends_with ~suffix:"\"" h then
quote_grouping
((cur_group ^ " " ^ replace_all h "\"" "") :: acc)
"" false t
Expand All @@ -77,7 +57,8 @@ let rec quote_grouping acc cur_group in_group tokens =
(cur_group ^ " " ^ replace_all h "\"" "")
in_group t
else if
String.starts_with ~prefix:"\"" h && not (ends_with ~suffix:"\"" h)
String.starts_with ~prefix:"\"" h
&& not (String.ends_with ~suffix:"\"" h)
then quote_grouping acc (replace_all h "\"" "") true t
else quote_grouping (replace_all h "\"" "" :: acc) cur_group in_group t

Expand Down Expand Up @@ -172,7 +153,8 @@ let parse_create_table tokens =
check_pk_uniqueness _table_name pk_field.name
(Table.convert_to_value pk_field.col_type
(List.nth row_values
(Option.get (find_index (fun c -> c = pk_field.name) columns))))
(Option.get
(List.find_index (fun c -> c = pk_field.name) columns))))
in
insert_row _table_name columns row_values
else insert_row _table_name columns row_values
Expand Down
10 changes: 1 addition & 9 deletions lib/table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ type column = { name : string; col_type : column_type; primary_key : bool }
type table = { columns : column list; mutable rows : row list }
(**Full table storage.*)

(**Helper function for GitHub Actions.*)
let find_index p =
let rec aux i = function
| [] -> None
| a :: l -> if p a then Some i else aux (i + 1) l
in
aux 0

(**Convert column type to string.*)
let column_type_to_str c =
match c with
Expand Down Expand Up @@ -210,7 +202,7 @@ let select_rows_table table column_names pred order_column =
in
let order_column_ind =
if order_column <> "" then
find_index
List.find_index
(fun c -> c.name = order_column)
(List.filter (fun c -> List.mem c columns) table.columns)
else (None : int option)
Expand Down

0 comments on commit 5f11c64

Please sign in to comment.