clear, concat

This commit is contained in:
sforman 2023-07-29 09:06:14 -07:00
parent af882e2be4
commit 70c5223319
1 changed files with 29 additions and 1 deletions

View File

@ -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