OSDN Git Service

Comparison ops.
authorsforman <sforman@hushmail.com>
Sat, 29 Jul 2023 18:07:50 +0000 (11:07 -0700)
committersforman <sforman@hushmail.com>
Sat, 29 Jul 2023 18:07:50 +0000 (11:07 -0700)
implementations/Elm/src/Joy.elm

index 224943f..df52ded 100644 (file)
@@ -46,6 +46,14 @@ joy_eval symbol stack expression =
         "/" -> joy_binary_math_op (//) stack expression
         "%" -> joy_binary_math_op (modBy) stack expression
 
+        "<" -> joy_comparison_op (<) stack expression
+        ">" -> joy_comparison_op (>) stack expression
+        "<=" -> joy_comparison_op (<=) stack expression
+        ">=" -> joy_comparison_op (>=) stack expression
+        "<>" -> joy_comparison_op (/=) stack expression
+        "!=" -> joy_comparison_op (/=) stack expression
+        "=" -> joy_comparison_op (==) stack expression
+
         "and" -> joy_binary_math_op (Bitwise.and) stack expression
         "or" -> joy_binary_math_op (Bitwise.or) stack expression
         "xor" -> joy_binary_math_op (Bitwise.xor) stack expression
@@ -122,6 +130,18 @@ joy_binary_math_op op stack expression =
                 Err msg -> Err msg
         Err msg -> Err msg
 
+
+joy_comparison_op : (Int -> Int -> Bool) -> JList -> JList -> Result String (JList, JList)
+joy_comparison_op op stack expression =
+    case pop_int(stack) of
+        Ok (a, s0) ->
+            case pop_int(s0) of
+                Ok (b, s1) ->
+                    Ok ((push_bool (op b a) s1), expression)
+                Err msg -> Err msg
+        Err msg -> Err msg
+
+
 joy_concat : JList -> JList -> Result String (JList, JList)
 joy_concat stack expression =
     case pop_list(stack) of
@@ -222,6 +242,14 @@ joy_truthy stack expression =
         Err msg -> Err msg
 
 
+push_bool : Bool -> JList -> JList
+push_bool flag stack =
+    if flag then
+        JoyTrue :: stack
+    else
+        JoyFalse :: stack
+
+
 push_int : Int -> JList -> JList
 push_int i stack = (JoyInt i) :: stack