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 | JoyInt of int
| JoyList of joy_type list | JoyList of joy_type list
(* type joy_list = joy_type list *) type joy_list = joy_type list
let joy_true = JoyTrue let joy_true = JoyTrue
let joy_false = JoyFalse let joy_false = JoyFalse
@ -36,8 +36,7 @@ let rec tokenize1 str start last =
let rec tokenize0 str start acc = let rec tokenize0 str start acc =
if start >= String.length str then acc if start >= String.length str then acc
else else
let ch = String.get str start in match String.get str start with
match ch with
| '[' -> Left_bracket :: tokenize0 str (start + 1) acc | '[' -> Left_bracket :: tokenize0 str (start + 1) acc
| ']' -> Right_bracket :: tokenize0 str (start + 1) acc | ']' -> Right_bracket :: tokenize0 str (start + 1) acc
| ' ' -> 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 tokenize str = tokenize0 str 0 []
(*
let token_to_string token = let token_to_string token =
match token with match token with
| Left_bracket -> "[" | Left_bracket -> "["
| Right_bracket -> "]" | Right_bracket -> "]"
| Token str -> str | 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 text_to_expression str =
let tokens = tokenize str in let tokens = tokenize str in
tokens 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 (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 () = print_endline (joy_to_string dummy) *)
let s = String.concat " " (List.map token_to_string (tokenize "1 [2]3"))
let () = let () =
print_endline s; print_endline (expression_to_string (parse (tokenize "true [ false]true"))) ;
print_endline (joy_to_string dummy) print_endline (joy_to_string dummy)