From d94153583c13b7987fa565f5c0bf99bef2e9d521 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 24 Sep 2022 21:00:26 -0700 Subject: [PATCH] Comparison operations. --- implementations/Nim/joy.nim | 2 -- implementations/Ocaml/helloworld/bin/main.ml | 30 ++++++++++++++++++-- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/implementations/Nim/joy.nim b/implementations/Nim/joy.nim index 3679693..3216671 100644 --- a/implementations/Nim/joy.nim +++ b/implementations/Nim/joy.nim @@ -339,7 +339,6 @@ proc dip(stack: JoyListType, expression: JoyListType, dictionary: JoyMapType): ( dictionary ) - proc i(stack: JoyListType, expression: JoyListType, dictionary: JoyMapType): (JoyListType, JoyListType, JoyMapType) = let (body_node, s0) = pop_list_node(stack) return ( @@ -348,7 +347,6 @@ proc i(stack: JoyListType, expression: JoyListType, dictionary: JoyMapType): (Jo dictionary ) - proc loop(stack: JoyListType, expression: JoyListType, dictionary: JoyMapType): (JoyListType, JoyListType, JoyMapType) = let (body_node, s0) = pop_list_node(stack) let (flag, s1) = pop_bool(s0) diff --git a/implementations/Ocaml/helloworld/bin/main.ml b/implementations/Ocaml/helloworld/bin/main.ml index b72217e..4d2589d 100644 --- a/implementations/Ocaml/helloworld/bin/main.ml +++ b/implementations/Ocaml/helloworld/bin/main.ml @@ -96,6 +96,8 @@ let pop_bool : joy_list -> bool * joy_list = | JoyFalse -> (false, tail) | _ -> raise (ValueError "Not a Boolean value.")) +let push_bool b stack = if b then JoyTrue :: stack else JoyFalse :: stack + (* ██████╗ ██████╗ ██╗███╗ ██╗████████╗███████╗██████╗ ██╔══██╗██╔══██╗██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗ @@ -255,9 +257,7 @@ let i s e d = let loop s e d = let body, s0 = pop_list s in let flag, s1 = pop_bool s0 in - if flag then - (s1, body @ (JoyList body :: j_loop :: e), d) - else (s1, e, d) + if flag then (s1, body @ (JoyList body :: j_loop :: e), d) else (s1, e, d) (* ██████╗ ██████╗ ██████╗ ███████╗ ██╗ ██╗ ██████╗ ██████╗ ██████╗ ███████╗ @@ -295,6 +295,30 @@ let joy_eval sym stack expression dictionary = let a, s0 = pop_int stack in let b, s1 = pop_int s0 in (JoyInt (b - a) :: s1, expression, dictionary) + | "<" -> + let a, s0 = pop_int stack in + let b, s1 = pop_int s0 in + (push_bool (b < a) s1, expression, dictionary) + | ">" -> + let a, s0 = pop_int stack in + let b, s1 = pop_int s0 in + (push_bool (b > a) s1, expression, dictionary) + | "<=" -> + let a, s0 = pop_int stack in + let b, s1 = pop_int s0 in + (push_bool (b <= a) s1, expression, dictionary) + | ">=" -> + let a, s0 = pop_int stack in + let b, s1 = pop_int s0 in + (push_bool (b >= a) s1, expression, dictionary) + | "!=" -> + let a, s0 = pop_int stack in + let b, s1 = pop_int s0 in + (push_bool (b != a) s1, expression, dictionary) + | "=" -> + let a, s0 = pop_int stack in + let b, s1 = pop_int s0 in + (push_bool (b = a) s1, expression, dictionary) | "branch" -> branch stack expression dictionary | "i" -> i stack expression dictionary | "loop" -> loop stack expression dictionary