Reuse as_list().

This commit is contained in:
Simon Forman 2022-09-14 11:24:56 -07:00
parent 9f04aa97e2
commit 3d199c1106
1 changed files with 22 additions and 14 deletions

View File

@ -83,7 +83,28 @@ let empty_list = JoyType(kind: joyList, listVal: Nil[JoyType]())
██║ ██║ ██║ ██║██║ ╚════██║
╚██████╔╝ ██║ ██║███████╗███████║
╚═════╝ ╚═╝ ╚═╝╚══════╝╚══════╝
]#
#[
Convert from nodes to values (or raise errors!)
]#
proc as_list(thing: JoyType): JoyListType =
# You've got a node, you want the list or it's an error.
case thing.kind
of joyList:
return thing.listVal
else:
raise newException(ValueError, "Only lists!")
#[
Push values onto stacks wrapping them in the correct node kinds.
]#
@ -135,11 +156,7 @@ proc pop_list(stack: JoyListType): (JoyListType, JoyListType) =
# we want the List.
if stack.isEmpty:
raise newException(ValueError, "Not enough values on stack.")
case stack.head.kind:
of joyList:
return (stack.head.listVal, stack.tail)
else:
raise newException(ValueError, "Not a list.")
return (as_list(stack.head), stack.tail)
proc pop_bool(stack: JoyListType): (bool, JoyListType) =
@ -161,7 +178,6 @@ proc pop_bool(stack: JoyListType): (bool, JoyListType) =
# ███████╗██╗ ██╗██████╗ ██████╗ ███████╗███████╗███████╗██╗ ██████╗ ███╗ ██╗
# ██╔════╝╚██╗██╔╝██╔══██╗██╔══██╗██╔════╝██╔════╝██╔════╝██║██╔═══██╗████╗ ██║
# █████╗ ╚███╔╝ ██████╔╝██████╔╝█████╗ ███████╗███████╗██║██║ ██║██╔██╗ ██║
@ -178,14 +194,6 @@ proc pop_bool(stack: JoyListType): (bool, JoyListType) =
# concatenating them.
proc as_list(thing: JoyType): JoyListType =
case thing.kind
of joyList:
return thing.listVal
else:
raise newException(ValueError, "Only lists!")
proc push_quote(quote: JoyType, expression: JoyType): JoyType =
#[
Put the quoted program onto the stack-of-stacks.