A start of parsing tokens.

This commit is contained in:
Simon Forman 2022-09-23 19:35:15 -07:00
parent 97f715a10d
commit 54d287bc32
1 changed files with 26 additions and 20 deletions

View File

@ -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)