From 54d287bc32f22dc55c282c47156d6c92a44b3ea0 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Fri, 23 Sep 2022 19:35:15 -0700 Subject: [PATCH] A start of parsing tokens. --- implementations/Ocaml/helloworld/bin/main.ml | 46 ++++++++++++++++------------ 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/implementations/Ocaml/helloworld/bin/main.ml b/implementations/Ocaml/helloworld/bin/main.ml index 005f601..769c27d 100644 --- a/implementations/Ocaml/helloworld/bin/main.ml +++ b/implementations/Ocaml/helloworld/bin/main.ml @@ -5,7 +5,7 @@ type joy_type = | JoyInt of int | JoyList of joy_type list -(* type joy_list = joy_type list *) +type joy_list = joy_type list let joy_true = JoyTrue let joy_false = JoyFalse @@ -36,8 +36,7 @@ let rec tokenize1 str start last = let rec tokenize0 str start acc = if start >= String.length str then acc else - let ch = String.get str start in - match ch with + match String.get str start with | '[' -> Left_bracket :: tokenize0 str (start + 1) acc | ']' -> Right_bracket :: tokenize0 str (start + 1) acc | ' ' -> tokenize0 str (start + 1) acc @@ -47,40 +46,47 @@ let rec tokenize0 str start acc = let tokenize str = tokenize0 str 0 [] +(* let token_to_string token = match token with | Left_bracket -> "[" | Right_bracket -> "]" | Token str -> str +*) + + +let rec parse : token list -> joy_list = fun tokens -> + match tokens with + | [] -> [] + | head :: tail -> + match head with + | Left_bracket -> zero :: parse tail + | Right_bracket -> JoyInt 1 :: parse tail + | Token tok -> + match tok with + | "true" -> joy_true :: parse tail + | "false"-> joy_false :: parse tail + | _ -> JoySymbol tok :: parse tail + + + -(* -let char_tok ch acc = - match ch with - | '[' -> Left_bracket :: acc - | ']' -> Right_bracket :: acc - | ' ' -> acc - | x -> (Token x) :: acc -let tokenize str = - String.fold_right char_tok str [] + + +(* let text_to_expression str = let tokens = tokenize str in tokens - -let token_to_string token = - match token with - | Left_bracket -> "[" - | Right_bracket -> "]" - | Token x -> Char.escaped x let s = String.concat "" (List.map token_to_string (text_to_expression "1 [2]3" )) +let s = String.concat " " (List.map token_to_string (tokenize "1 Pat [2]3")) *) (* let () = print_endline (joy_to_string dummy) *) -let s = String.concat " " (List.map token_to_string (tokenize "1 [2]3")) let () = - print_endline s; + print_endline (expression_to_string (parse (tokenize "true [ false]true"))) ; print_endline (joy_to_string dummy) -- 2.11.0