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 *)
+
+(*
+let joy_nil = JoyList []
+██████╗ ██╗ ██████╗████████╗██╗ ██████╗ ███╗ ██╗ █████╗ ██████╗ ██╗ ██╗
+██╔══██╗██║██╔════╝╚══██╔══╝██║██╔═══██╗████╗ ██║██╔══██╗██╔══██╗╚██╗ ██╔╝
+██║ ██║██║██║ ██║ ██║██║ ██║██╔██╗ ██║███████║██████╔╝ ╚████╔╝
+██║ ██║██║██║ ██║ ██║██║ ██║██║╚██╗██║██╔══██║██╔══██╗ ╚██╔╝
+██████╔╝██║╚██████╗ ██║ ██║╚██████╔╝██║ ╚████║██║ ██║██║ ██║ ██║
+╚═════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
+https://stackoverflow.com/questions/13708701/how-to-implement-a-dictionary-as-a-function-in-ocaml
+*)
exception UnknownWordError of string
let d = dict_add empty_dict "foo" []
+
+(*
+██╗ ██╗████████╗██╗██╗ ███████╗
+██║ ██║╚══██╔══╝██║██║ ██╔════╝
+██║ ██║ ██║ ██║██║ ███████╗
+██║ ██║ ██║ ██║██║ ╚════██║
+╚██████╔╝ ██║ ██║███████╗███████║
+ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚══════╝
+*)
+
+exception StackUnderflow of string
+exception ValueError of string
+
+let pop_int : joy_list -> int * joy_list =
+ fun stack ->
+ match stack with
+ | [] -> raise (StackUnderflow "Not enough values on stack.")
+ | head :: tail -> (
+ match head with
+ | JoyInt i -> (i, tail)
+ | _ -> raise (ValueError "Not an integer."))
+
+
(*
██████╗ ██████╗ ██╗███╗ ██╗████████╗███████╗██████╗
██╔══██╗██╔══██╗██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗
██║ ██║ ██║██║██║ ╚████║ ██║ ███████╗██║ ██║
╚═╝ ╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
*)
+
let rec joy_to_string jt =
match jt with
| JoySymbol sym -> sym
let delimiter str i =
i >= String.length str || String.contains "[] " (String.get str i)
-let make_token str index last =
- (Token (String.sub str index (last - index)), last)
+let make_token str index i =
+ (Token (String.sub str index (i - index)), i)
(* string -> int -> int -> token * int *)
let rec tokenize1 str index i =
let text_to_expression text = parse (tokenize text)
(*
+ ██████╗ ██████╗ ██████╗ ███████╗ ██╗ ██╗ ██████╗ ██████╗ ██████╗ ███████╗
+██╔════╝██╔═══██╗██╔══██╗██╔════╝ ██║ ██║██╔═══██╗██╔══██╗██╔══██╗██╔════╝
+██║ ██║ ██║██████╔╝█████╗ ██║ █╗ ██║██║ ██║██████╔╝██║ ██║███████╗
+██║ ██║ ██║██╔══██╗██╔══╝ ██║███╗██║██║ ██║██╔══██╗██║ ██║╚════██║
+╚██████╗╚██████╔╝██║ ██║███████╗ ╚███╔███╔╝╚██████╔╝██║ ██║██████╔╝███████║
+ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚══════╝
+let clear s e d = (Joy_nil, e, d)
+
+*)
+
+
+(*
██╗███╗ ██╗████████╗███████╗██████╗ ██████╗ ██████╗ ███████╗████████╗███████╗██████╗
██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔════╝╚══██╔══╝██╔════╝██╔══██╗
██║██╔██╗ ██║ ██║ █████╗ ██████╔╝██████╔╝██████╔╝█████╗ ██║ █████╗ ██████╔╝
██║██║╚██╗██║ ██║ ██╔══╝ ██╔══██╗██╔═══╝ ██╔══██╗██╔══╝ ██║ ██╔══╝ ██╔══██╗
██║██║ ╚████║ ██║ ███████╗██║ ██║██║ ██║ ██║███████╗ ██║ ███████╗██║ ██║
╚═╝╚═╝ ╚═══╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝
-
-let joy stack expression dictionary = (stack @ expression, dictionary)
*)
-exception StackUnderflow of string
-exception ValueError of string
-
-let pop_int : joy_list -> int * joy_list =
- fun stack ->
- match stack with
- | [] -> raise (StackUnderflow "Not enough values on stack.")
- | head :: tail -> (
- match head with
- | JoyInt i -> (i, tail)
- | _ -> raise (ValueError "Not an integer."))
-
let joy_eval sym stack expression dictionary =
match sym with
| "+" ->
let a, s0 = pop_int stack in
let b, s1 = pop_int s0 in
(JoyInt (b - a) :: s1, expression, dictionary)
+ | "clear" ->
+ ([], expression, dictionary)
| _ ->
let func = dictionary sym in
(stack, func @ expression, dictionary)
joy s e d
| _ -> joy (head :: stack) tail dictionary)
+(*
let expr = text_to_expression "1 2 + 3 4 + 5 6 + 7 8 + 9 10 + 11 + + + + + - "
+*)
+let expr = text_to_expression "1 2 3 4 clear 5"
let s = text_to_expression "23 [18 99] "
let stack, _ = joy s expr d