Order is preserved.

This commit is contained in:
Simon Forman 2022-09-24 09:01:16 -07:00
parent c96cd6e296
commit 0d88895ead
1 changed files with 26 additions and 32 deletions

View File

@ -72,52 +72,47 @@ let rec parse : token list -> joy_list = fun tokens ->
*) *)
(* Get the prefix of the list as joy type and return rest of list*) (* Get the prefix of the list as joy type and return rest of list*)
let rec expect_right_bracket tokens acc = let rec expect_right_bracket tokens acc =
match tokens with match tokens with
| [] -> raise (ParseError "Missing closing bracket.") | [] -> raise (ParseError "Missing closing bracket.")
| head :: tail -> | head :: tail -> (
match head with match head with
| Right_bracket -> acc, tail | Right_bracket -> (acc, tail)
| Left_bracket -> | Left_bracket ->
(* extract the sub-list *) (* extract the sub-list *)
let sub_list, rest = expect_right_bracket tail [] in let sub_list, rest = expect_right_bracket tail [] in
(* continue looking for the expected "]" *) (* continue looking for the expected "]" *)
let el, rrest = expect_right_bracket rest acc in let el, rrest = expect_right_bracket rest acc in
JoyList sub_list :: el, rrest (JoyList sub_list :: el, rrest)
| Token tok -> | Token tok -> (
let el, rest = expect_right_bracket tail acc in let el, rest = expect_right_bracket tail acc in
match tok with match tok with
| "true" -> joy_true :: el, rest | "true" -> (joy_true :: el, rest)
| "false"-> joy_false :: el, rest | "false" -> (joy_false :: el, rest)
| _ -> (JoySymbol tok) :: el, rest | _ -> (JoySymbol tok :: el, rest)))
let foo head tail = let foo head tail =
match head with match head with
| Left_bracket -> | Left_bracket ->
let el, rest = expect_right_bracket tail [] in let el, rest = expect_right_bracket tail [] in
JoyList el, rest (JoyList el, rest)
| Right_bracket -> raise (ParseError "Extra closing bracket.") | Right_bracket -> raise (ParseError "Extra closing bracket.")
| Token tok -> | Token tok -> (
match tok with match tok with
| "true" -> joy_true, tail | "true" -> (joy_true, tail)
| "false"-> joy_false, tail | "false" -> (joy_false, tail)
| _ -> JoySymbol tok, tail | _ -> (JoySymbol tok, tail))
let rec parse0 tokens acc = let rec parse0 tokens acc =
match tokens with match tokens with
| [] -> acc | [] -> acc
| head :: tail -> | head :: tail ->
let item, rest = foo head tail in let item, rest = foo head tail in
item :: parse0 rest acc item :: parse0 rest acc
let parse : token list -> joy_list = fun tokens -> parse0 tokens [] let parse : token list -> joy_list = fun tokens -> parse0 tokens []
(* (*
let text_to_expression str = let text_to_expression str =
@ -130,8 +125,7 @@ let s = String.concat " " (List.map token_to_string (tokenize "1 Pat [2]3"))
(* let () = print_endline (joy_to_string dummy) *) (* let () = print_endline (joy_to_string dummy) *)
let () = let () =
print_endline (expression_to_string (parse (tokenize "1[2[3]4]5[][][[]]"))) ; print_endline (expression_to_string (parse (tokenize "1 2 3[4 5 6[7 8]9 10]11[][][[]]")));
print_endline (expression_to_string (parse (tokenize "true [ false]true"))) ; print_endline (expression_to_string (parse (tokenize "true [ false]true")));
print_endline (joy_to_string dummy) print_endline (joy_to_string dummy)