io_calculator/main.io

97 lines
1.2 KiB
Io
Raw Normal View History

2023-10-31 16:43:57 +00:00
input := ""
stack := List clone
should_quit := false
ops := Map clone
doFile("operators.io")
2023-10-31 16:43:57 +00:00
fns := Map clone
doFile("functions.io")
2023-10-31 16:43:57 +00:00
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
)
2023-11-01 13:20:36 +00:00
action_queue := List clone
2023-10-31 16:43:57 +00:00
doall := method(line,
2023-11-01 13:20:36 +00:00
action_queue = line splitNoEmpties
while(action_queue size > 0,
action := action_queue removeAt(0)
doword(action)
)
2023-10-31 16:43:57 +00:00
)
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)