From 8159e102d35699937541a42c813538f01f3bdd7e Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 24 Sep 2022 21:19:35 -0700 Subject: [PATCH] coms, swap --- implementations/Ocaml/helloworld/bin/main.ml | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/implementations/Ocaml/helloworld/bin/main.ml b/implementations/Ocaml/helloworld/bin/main.ml index 4d2589d..10c13ed 100644 --- a/implementations/Ocaml/helloworld/bin/main.ml +++ b/implementations/Ocaml/helloworld/bin/main.ml @@ -276,6 +276,17 @@ let concat s e d = | JoyList tos :: JoyList second :: s0 -> (JoyList (second @ tos) :: s0, e, d) | _ -> raise (ValueError "some damn thing.") +let cons s e d = + let body, s0 = pop_list s in + match s0 with + | item :: s1 -> (JoyList (item :: body) :: s1, e, d) + | [] -> raise (StackUnderflow "Not enough values on stack.") + +let swap s e d = + match s with + | tos :: second :: s0 -> (second :: tos :: s0, e, d) + | _ :: [] | [] -> raise (StackUnderflow "Not enough values on stack.") + (* ██╗███╗ ██╗████████╗███████╗██████╗ ██████╗ ██████╗ ███████╗████████╗███████╗██████╗ ██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔════╝╚══██╔══╝██╔════╝██╔══██╗ @@ -287,11 +298,11 @@ let concat s e d = let joy_eval sym stack expression dictionary = match sym with - | "+" -> + | "+" | "add" -> let a, s0 = pop_int stack in let b, s1 = pop_int s0 in (JoyInt (a + b) :: s1, expression, dictionary) - | "-" -> + | "-" | "sub" -> let a, s0 = pop_int stack in let b, s1 = pop_int s0 in (JoyInt (b - a) :: s1, expression, dictionary) @@ -325,6 +336,8 @@ let joy_eval sym stack expression dictionary = | "dip" -> dip stack expression dictionary | "clear" -> ([], expression, dictionary) | "concat" -> concat stack expression dictionary + | "cons" -> cons stack expression dictionary + | "swap" -> swap stack expression dictionary | _ -> let func = dictionary sym in (stack, func @ expression, dictionary) -- 2.11.0