From 70c5223319e1138a4d5f06ec0104c8a31fce4ea6 Mon Sep 17 00:00:00 2001 From: sforman Date: Sat, 29 Jul 2023 09:06:14 -0700 Subject: [PATCH] clear, concat --- implementations/Elm/src/Joy.elm | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/implementations/Elm/src/Joy.elm b/implementations/Elm/src/Joy.elm index 147f791..cdb3829 100644 --- a/implementations/Elm/src/Joy.elm +++ b/implementations/Elm/src/Joy.elm @@ -42,6 +42,8 @@ joy_eval symbol 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 + "clear" -> Ok ([], expression) + "concat" -> joy_concat stack expression _ -> Err ("Unknown word: " ++ symbol) @@ -55,7 +57,15 @@ joy_binary_math_op op stack 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 + Ok (a, s0) -> + case pop_list(s0) of + Ok (b, s1) -> + Ok ((push_list (b ++ a) s1), expression) + Err msg -> Err msg + Err msg -> Err msg @@ -63,10 +73,18 @@ push_int : Int -> JList -> JList push_int i stack = (JoyInt i) :: stack +push_list : JList -> JList -> JList +push_list el stack = (JoyList el) :: stack + + pop_int : JList -> Result String (Int, JList) pop_int stack = pop_any stack |> andThen isnt_int +pop_list : JList -> Result String (JList, JList) +pop_list stack = pop_any stack |> andThen isnt_list + + pop_any : JList -> Result String (JoyType, JList) pop_any stack = case stack of @@ -86,6 +104,16 @@ isnt_int (item, stack) = +isnt_list : (JoyType, JList) -> Result String (JList, JList) +isnt_list (item, stack) = + case item of + JoyList el -> + Ok (el, stack) + _ -> + Err "Not a list." + + + -- Printer joyTermToString : JoyType -> String -- 2.11.0