io_calculator/main.io
2023-11-01 14:20:36 +01:00

97 lines
1.2 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
)
action_queue := List clone
doall := method(line,
action_queue = line splitNoEmpties
while(action_queue size > 0,
action := action_queue removeAt(0)
doword(action)
)
)
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)