From c7966f7ac8f2408f7a1d314777bec71ac3da8e36 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 24 Sep 2022 12:44:20 -0700 Subject: [PATCH] Working towards interpretation. --- implementations/Ocaml/helloworld/bin/main.ml | 52 +++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/implementations/Ocaml/helloworld/bin/main.ml b/implementations/Ocaml/helloworld/bin/main.ml index 8b00735..251ecc4 100644 --- a/implementations/Ocaml/helloworld/bin/main.ml +++ b/implementations/Ocaml/helloworld/bin/main.ml @@ -5,9 +5,8 @@ type joy_type = | JoyInt of int | JoyList of joy_type list -(* type joy_list = joy_type list -*) + let joy_true = JoyTrue let joy_false = JoyFalse @@ -15,6 +14,19 @@ let j_loop = JoySymbol "loop" let zero = JoyInt 0 let dummy = JoyList [ joy_true; joy_false; j_loop; zero ] + +(* https://stackoverflow.com/questions/13708701/how-to-implement-a-dictionary-as-a-function-in-ocaml *) + +exception UnknownWordError of string + +let empty_dict (key : string) : joy_list = raise (UnknownWordError key) +let dict_add dictionary key value = + fun key' -> if key = key' then value else dictionary key' + +type joy_dict = string -> joy_list + +let d = dict_add empty_dict "foo" [] + (* ██████╗ ██████╗ ██╗███╗ ██╗████████╗███████╗██████╗ ██╔══██╗██╔══██╗██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗ @@ -33,7 +45,14 @@ let rec joy_to_string jt = and expression_to_string el = String.concat " " (List.map joy_to_string el) -(* *) +(* +██╗ ███████╗██╗ ██╗███████╗██████╗ +██║ ██╔════╝╚██╗██╔╝██╔════╝██╔══██╗ +██║ █████╗ ╚███╔╝ █████╗ ██████╔╝ +██║ ██╔══╝ ██╔██╗ ██╔══╝ ██╔══██╗ +███████╗███████╗██╔╝ ██╗███████╗██║ ██║ +╚══════╝╚══════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝ +*) type token = Left_bracket | Right_bracket | Token of string @@ -74,6 +93,7 @@ let () = print_endline s let s = String.concat "" (List.map token_to_string (text_to_expression "1 [2]3" )) *) + (* ██████╗ █████╗ ██████╗ ███████╗███████╗██████╗ ██╔══██╗██╔══██╗██╔══██╗██╔════╝██╔════╝██╔══██╗ @@ -136,9 +156,33 @@ let rec parse0 tokens acc = let parse tokens = parse0 tokens [] let text_to_expression text = parse (tokenize text) +(* + +██╗███╗ ██╗████████╗███████╗██████╗ ██████╗ ██████╗ ███████╗████████╗███████╗██████╗ +██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔════╝╚══██╔══╝██╔════╝██╔══██╗ +██║██╔██╗ ██║ ██║ █████╗ ██████╔╝██████╔╝██████╔╝█████╗ ██║ █████╗ ██████╔╝ +██║██║╚██╗██║ ██║ ██╔══╝ ██╔══██╗██╔═══╝ ██╔══██╗██╔══╝ ██║ ██╔══╝ ██╔══██╗ +██║██║ ╚████║ ██║ ███████╗██║ ██║██║ ██║ ██║███████╗ ██║ ███████╗██║ ██║ +╚═╝╚═╝ ╚═══╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝ + +let joy stack expression dictionary = (stack @ expression, dictionary) +*) + +let joy : joy_list -> joy_list -> joy_dict -> joy_list * joy_dict = fun stack expression dictionary -> + match expression with + | [] -> (stack, dictionary) + | _ -> (*head :: tail ->*) + (stack @ expression, dictionary) + +let expr = text_to_expression "1 2 3[4 5 6[7 8]9 10]11[][][[]]" +let s = text_to_expression "23 [18 99] " +let stack, _ = joy s expr d + let () = - print_endline + print_endline (expression_to_string stack); +(* print_endline (expression_to_string (text_to_expression "1 2 3[4 5 6[7 8]9 10]11[][][[]]")); print_endline (expression_to_string (text_to_expression "true [ false]true")); +*) print_endline (joy_to_string dummy) -- 2.11.0