OSDN Git Service

Comparison operations.
authorSimon Forman <sforman@hushmail.com>
Sun, 25 Sep 2022 04:00:26 +0000 (21:00 -0700)
committerSimon Forman <sforman@hushmail.com>
Sun, 25 Sep 2022 04:00:45 +0000 (21:00 -0700)
implementations/Nim/joy.nim
implementations/Ocaml/helloworld/bin/main.ml

index 3679693..3216671 100644 (file)
@@ -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)
index b72217e..4d2589d 100644 (file)
@@ -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