Minor cleanup.
This commit is contained in:
parent
3d199c1106
commit
9e5d699b0f
|
|
@ -1,6 +1,7 @@
|
||||||
import rdstdin, strutils
|
import rdstdin, strutils
|
||||||
import bigints, fp
|
import bigints, fp
|
||||||
|
|
||||||
|
# TODO: rewrite expression to use list instead of list node.
|
||||||
|
|
||||||
type
|
type
|
||||||
|
|
||||||
|
|
@ -83,12 +84,8 @@ let empty_list = JoyType(kind: joyList, listVal: Nil[JoyType]())
|
||||||
██║ ██║ ██║ ██║██║ ╚════██║
|
██║ ██║ ██║ ██║██║ ╚════██║
|
||||||
╚██████╔╝ ██║ ██║███████╗███████║
|
╚██████╔╝ ██║ ██║███████╗███████║
|
||||||
╚═════╝ ╚═╝ ╚═╝╚══════╝╚══════╝
|
╚═════╝ ╚═╝ ╚═╝╚══════╝╚══════╝
|
||||||
]#
|
|
||||||
|
|
||||||
|
Convert from nodes to values or raise errors.
|
||||||
#[
|
|
||||||
|
|
||||||
Convert from nodes to values (or raise errors!)
|
|
||||||
|
|
||||||
]#
|
]#
|
||||||
|
|
||||||
|
|
@ -101,12 +98,17 @@ proc as_list(thing: JoyType): JoyListType =
|
||||||
raise newException(ValueError, "Only lists!")
|
raise newException(ValueError, "Only lists!")
|
||||||
|
|
||||||
|
|
||||||
|
proc as_int(i: JoyType): BigInt =
|
||||||
|
# You've got a node, you want the int or it's an error.
|
||||||
|
case i.kind
|
||||||
|
of joyInt:
|
||||||
|
return i.intVal
|
||||||
|
else:
|
||||||
|
raise newException(ValueError, "Not an integer.")
|
||||||
|
|
||||||
|
|
||||||
#[
|
#[
|
||||||
Push values onto stacks wrapping them in the correct node kinds.
|
Push values onto stacks wrapping them in the correct node kinds.
|
||||||
|
|
||||||
]#
|
]#
|
||||||
|
|
||||||
proc push_int(n: BigInt, stack: JoyListType): JoyListType =
|
proc push_int(n: BigInt, stack: JoyListType): JoyListType =
|
||||||
|
|
@ -124,19 +126,13 @@ proc push_bool(a: bool, stack: JoyListType): JoyListType =
|
||||||
|
|
||||||
|
|
||||||
#[
|
#[
|
||||||
|
|
||||||
Pop values from stacks.
|
Pop values from stacks.
|
||||||
|
|
||||||
]#
|
]#
|
||||||
|
|
||||||
proc pop_int(stack: JoyListType): (BigInt, JoyListType) =
|
proc pop_int(stack: JoyListType): (BigInt, JoyListType) =
|
||||||
if stack.isEmpty:
|
if stack.isEmpty:
|
||||||
raise newException(ValueError, "Not enough values on stack.")
|
raise newException(ValueError, "Not enough values on stack.")
|
||||||
case stack.head.kind:
|
return (as_int(stack.head), stack.tail)
|
||||||
of joyInt:
|
|
||||||
return (stack.head.intVal, stack.tail)
|
|
||||||
else:
|
|
||||||
raise newException(ValueError, "Not an integer.")
|
|
||||||
|
|
||||||
|
|
||||||
proc pop_list_node(stack: JoyListType): (JoyType, JoyListType) =
|
proc pop_list_node(stack: JoyListType): (JoyType, JoyListType) =
|
||||||
|
|
@ -177,26 +173,27 @@ proc pop_bool(stack: JoyListType): (bool, JoyListType) =
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[
|
||||||
|
███████╗██╗ ██╗██████╗ ██████╗ ███████╗███████╗███████╗██╗ ██████╗ ███╗ ██╗
|
||||||
|
██╔════╝╚██╗██╔╝██╔══██╗██╔══██╗██╔════╝██╔════╝██╔════╝██║██╔═══██╗████╗ ██║
|
||||||
|
█████╗ ╚███╔╝ ██████╔╝██████╔╝█████╗ ███████╗███████╗██║██║ ██║██╔██╗ ██║
|
||||||
|
██╔══╝ ██╔██╗ ██╔═══╝ ██╔══██╗██╔══╝ ╚════██║╚════██║██║██║ ██║██║╚██╗██║
|
||||||
|
███████╗██╔╝ ██╗██║ ██║ ██║███████╗███████║███████║██║╚██████╔╝██║ ╚████║
|
||||||
|
╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝
|
||||||
|
|
||||||
# ███████╗██╗ ██╗██████╗ ██████╗ ███████╗███████╗███████╗██╗ ██████╗ ███╗ ██╗
|
As elegant as it is to model the expression as a stack, it's not very
|
||||||
# ██╔════╝╚██╗██╔╝██╔══██╗██╔══██╗██╔════╝██╔════╝██╔════╝██║██╔═══██╗████╗ ██║
|
ficient, as concatenating definitions and other quoted programs to
|
||||||
# █████╗ ╚███╔╝ ██████╔╝██████╔╝█████╗ ███████╗███████╗██║██║ ██║██╔██╗ ██║
|
the expression is a common and expensive operation.
|
||||||
# ██╔══╝ ██╔██╗ ██╔═══╝ ██╔══██╗██╔══╝ ╚════██║╚════██║██║██║ ██║██║╚██╗██║
|
|
||||||
# ███████╗██╔╝ ██╗██║ ██║ ██║███████╗███████║███████║██║╚██████╔╝██║ ╚████║
|
Instead, let's keep a stack of sub-expressions, reading from them
|
||||||
# ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═══╝
|
one-by-one, and prepending new sub-expressions to the stack rather than
|
||||||
#
|
concatenating them.
|
||||||
# As elegant as it is to model the expression as a stack, it's not very
|
]#
|
||||||
# efficient, as concatenating definitions and other quoted programs to
|
|
||||||
# the expression is a common and expensive operation.
|
|
||||||
#
|
|
||||||
# Instead, let's keep a stack of sub-expressions, reading from them
|
|
||||||
# one-by-one, and prepending new sub-expressions to the stack rather than
|
|
||||||
# concatenating them.
|
|
||||||
|
|
||||||
|
|
||||||
proc push_quote(quote: JoyType, expression: JoyType): JoyType =
|
proc push_quote(quote: JoyType, expression: JoyType): JoyType =
|
||||||
#[
|
#[
|
||||||
Put the quoted program onto the stack-of-stacks.
|
Put the quoted program NODE onto the stack-of-stacks.
|
||||||
]#
|
]#
|
||||||
let ql = as_list(quote)
|
let ql = as_list(quote)
|
||||||
if ql.isEmpty:
|
if ql.isEmpty:
|
||||||
|
|
@ -207,7 +204,7 @@ proc push_quote(quote: JoyType, expression: JoyType): JoyType =
|
||||||
|
|
||||||
proc push_quote_list(quote: JoyListType, expression: JoyType): JoyType =
|
proc push_quote_list(quote: JoyListType, expression: JoyType): JoyType =
|
||||||
#[
|
#[
|
||||||
Put the quoted program onto the stack-of-stacks.
|
Put the quoted program LIST onto the stack-of-stacks.
|
||||||
]#
|
]#
|
||||||
return push_quote(JoyType(kind: joyList, listVal: quote), expression)
|
return push_quote(JoyType(kind: joyList, listVal: quote), expression)
|
||||||
|
|
||||||
|
|
@ -336,6 +333,16 @@ proc branch(stack: JoyListType, expression: JoyType, dictionary: JoyMapType): (
|
||||||
return (s2, push_quote(false_body_node, expression), dictionary)
|
return (s2, push_quote(false_body_node, expression), dictionary)
|
||||||
|
|
||||||
|
|
||||||
|
#[
|
||||||
|
██████╗ ██████╗ ██████╗ ███████╗ ██╗ ██╗ ██████╗ ██████╗ ██████╗ ███████╗
|
||||||
|
██╔════╝██╔═══██╗██╔══██╗██╔════╝ ██║ ██║██╔═══██╗██╔══██╗██╔══██╗██╔════╝
|
||||||
|
██║ ██║ ██║██████╔╝█████╗ ██║ █╗ ██║██║ ██║██████╔╝██║ ██║███████╗
|
||||||
|
██║ ██║ ██║██╔══██╗██╔══╝ ██║███╗██║██║ ██║██╔══██╗██║ ██║╚════██║
|
||||||
|
╚██████╗╚██████╔╝██║ ██║███████╗ ╚███╔███╔╝╚██████╔╝██║ ██║██████╔╝███████║
|
||||||
|
╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝ ╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚══════╝
|
||||||
|
]#
|
||||||
|
|
||||||
|
|
||||||
proc clear(stack: JoyListType, expression: JoyType, dictionary: JoyMapType): (
|
proc clear(stack: JoyListType, expression: JoyType, dictionary: JoyMapType): (
|
||||||
JoyListType, JoyType, JoyMapType) =
|
JoyListType, JoyType, JoyMapType) =
|
||||||
return (empty_list.listVal, expression, dictionary)
|
return (empty_list.listVal, expression, dictionary)
|
||||||
|
|
@ -428,12 +435,6 @@ proc joy_eval(sym: string, stack: JoyListType, expression: JoyType,
|
||||||
return (stack, expression, dictionary)
|
return (stack, expression, dictionary)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# = ≡ eq
|
|
||||||
# != ≡ ne
|
|
||||||
# <> ≡ ne
|
|
||||||
|
|
||||||
|
|
||||||
proc joy(stack: JoyListType, expression: JoyType, dictionary: JoyMapType): (
|
proc joy(stack: JoyListType, expression: JoyType, dictionary: JoyMapType): (
|
||||||
JoyListType, JoyMapType) =
|
JoyListType, JoyMapType) =
|
||||||
var s = stack
|
var s = stack
|
||||||
|
|
@ -453,13 +454,7 @@ proc joy(stack: JoyListType, expression: JoyType, dictionary: JoyMapType): (
|
||||||
return (s, d)
|
return (s, d)
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let stack = empty_list.listVal
|
|
||||||
let dict = newMap[string, JoyListType]()
|
|
||||||
#let exp = text_to_expression("2 3 add 23 mul 45 gt")
|
#let exp = text_to_expression("2 3 add 23 mul 45 gt")
|
||||||
#let exp = text_to_expression("2 3 false [add] [mul] branch")
|
#let exp = text_to_expression("2 3 false [add] [mul] branch")
|
||||||
#let exp = text_to_expression("2 3 true [add] [mul] branch")
|
#let exp = text_to_expression("2 3 true [add] [mul] branch")
|
||||||
|
|
@ -468,8 +463,8 @@ let dict = newMap[string, JoyListType]()
|
||||||
|
|
||||||
#echo print_stack(s)
|
#echo print_stack(s)
|
||||||
|
|
||||||
var s = stack
|
var s = empty_list.listVal
|
||||||
var d = dict
|
var d = newMap[string, JoyListType]()
|
||||||
var exp: JoyType
|
var exp: JoyType
|
||||||
while true:
|
while true:
|
||||||
try:
|
try:
|
||||||
|
|
@ -477,12 +472,10 @@ while true:
|
||||||
except IOError:
|
except IOError:
|
||||||
break
|
break
|
||||||
try:
|
try:
|
||||||
(s, d) = joy(s, exp, dict)
|
(s, d) = joy(s, exp, d)
|
||||||
except:
|
except:
|
||||||
echo getCurrentExceptionMsg()
|
echo getCurrentExceptionMsg()
|
||||||
echo print_stack(s)
|
echo print_stack(s)
|
||||||
continue
|
|
||||||
echo print_stack(s)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue