92 lines
1.1 KiB
Io
92 lines
1.1 KiB
Io
input := ""
|
|
stack := List clone
|
|
should_quit := false
|
|
|
|
ops := Map clone
|
|
|
|
doFile("operators.io")
|
|
|
|
fns := Map clone
|
|
|
|
doFile("functions.io")
|
|
|
|
|
|
|
|
isnan := method(x, x asNumber != x asNumber)
|
|
|
|
pop := method(
|
|
stack pop
|
|
)
|
|
push := method(x,
|
|
if(x != nil,
|
|
stack append(x)
|
|
)
|
|
)
|
|
|
|
print_stack := method(
|
|
stack foreach(v, write(v, ", "))
|
|
write("\n")
|
|
)
|
|
|
|
read := method(
|
|
if(should_quit, return)
|
|
|
|
input = File standardInput readLine
|
|
|
|
doall(input)
|
|
|
|
print_stack
|
|
)
|
|
|
|
doall := method(line,
|
|
actions := line splitNoEmpties
|
|
actions foreach(v, doword(v))
|
|
)
|
|
|
|
doword := method(word,
|
|
if(isnan(word), exec(word), push(word asNumber))
|
|
)
|
|
|
|
exec := method(op,
|
|
if(op at(0) asCharacter == "'",
|
|
push(op exSlice(1))
|
|
return
|
|
)
|
|
|
|
if(op == "defun",
|
|
name := pop
|
|
|
|
code := stack clone
|
|
stack empty
|
|
|
|
add_fn(name, block(
|
|
code foreach(v, doword(v))
|
|
))
|
|
|
|
return
|
|
)
|
|
|
|
if(ops hasKey(op),
|
|
action := ops at(op)
|
|
result := action call(pop, pop)
|
|
push(result)
|
|
|
|
return
|
|
)
|
|
|
|
if(fns hasKey(op),
|
|
action := fns at(op)
|
|
action call
|
|
|
|
return
|
|
)
|
|
)
|
|
|
|
if(System args size > 1,
|
|
file := File with(System args at(1)) openForReading
|
|
doall(file contents)
|
|
)
|
|
|
|
print_stack
|
|
while(should_quit == false, read)
|