From a69b98868490fd899879be484969156dd5f9d073 Mon Sep 17 00:00:00 2001 From: sforman Date: Sat, 29 Jul 2023 11:07:50 -0700 Subject: [PATCH] Comparison ops. --- implementations/Elm/src/Joy.elm | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/implementations/Elm/src/Joy.elm b/implementations/Elm/src/Joy.elm index 224943f..df52ded 100644 --- a/implementations/Elm/src/Joy.elm +++ b/implementations/Elm/src/Joy.elm @@ -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