OSDN Git Service

cons
authorsforman <sforman@hushmail.com>
Sat, 29 Jul 2023 16:17:27 +0000 (09:17 -0700)
committersforman <sforman@hushmail.com>
Sat, 29 Jul 2023 16:17:27 +0000 (09:17 -0700)
implementations/Elm/src/Joy.elm

index cdb3829..da79796 100644 (file)
@@ -44,6 +44,7 @@ joy_eval symbol stack expression =
         "xor" -> joy_binary_math_op (Bitwise.xor) stack expression
         "clear" -> Ok ([], expression)
         "concat" -> joy_concat stack expression
+        "cons" -> joy_cons stack expression
         _ -> Err ("Unknown word: " ++ symbol)
 
 
@@ -68,6 +69,17 @@ joy_concat stack expression =
         Err msg -> Err msg
 
 
+joy_cons : JList -> JList -> Result String (JList, JList)
+joy_cons stack expression =
+    case pop_list(stack) of
+        Ok (a, s0) ->
+            case pop_any(s0) of
+                Ok (b, s1) ->
+                    Ok ((push_list (b :: a) s1), expression)
+                Err msg -> Err msg
+        Err msg -> Err msg
+
+
 
 push_int : Int -> JList -> JList
 push_int i stack = (JoyInt i) :: stack