48 lines
838 B
Nim
48 lines
838 B
Nim
import vm
|
|
import compiler
|
|
import os
|
|
import config
|
|
|
|
type Result = enum
|
|
rsOK, rsCompileError, rsRuntimeError
|
|
|
|
proc interpret(source: string): Result =
|
|
let compiler = newCompiler(source)
|
|
compiler.compile()
|
|
if compiler.hadError:
|
|
return rsCompileError
|
|
let vm = newVM(compiler.chunk)
|
|
case vm.run():
|
|
of irOK:
|
|
rsOK
|
|
of irRuntimeError:
|
|
rsRuntimeError
|
|
|
|
|
|
proc repl =
|
|
while true:
|
|
try:
|
|
let line = konLineEditor()
|
|
if line.len > 0:
|
|
discard interpret(line)
|
|
except ReadlineInterruptedException:
|
|
break
|
|
|
|
proc runFile(path: string) =
|
|
case interpret(readFile(path)):
|
|
of rsCompileError:
|
|
quit 65
|
|
of rsRuntimeError:
|
|
quit 70
|
|
of rsOK:
|
|
quit 0
|
|
|
|
if paramCount() == 0:
|
|
repl()
|
|
elif paramCount() == 1:
|
|
runFile(paramStr(1))
|
|
else:
|
|
echo "Maximum param count is 1"
|
|
quit 1
|
|
|