nondescript/src/nds.nim

86 lines
1.6 KiB
Nim

import ndspkg/vm
import ndspkg/compv2/parser
import ndspkg/compv2/node
import ndspkg/compv2/emitter
import ndspkg/config
import ndspkg/chunk
import os
import strformat
type Result = enum
rsOK, rsCompileError, rsRuntimeError
proc interpret(name: string, source: string): Result =
let parser = newParser(name, source)
let node = parser.parse()
if parser.hadError:
return rsCompileError
when debugDumpAst:
echo $node
let emitter = newEmitter(name, node)
emitter.emit()
if emitter.hadError:
return rsCompileError
when debugDumpChunk:
emitter.chunk.disassembleChunk()
case emitter.chunk.run():
of irOK:
rsOK
of irRuntimeError:
rsRuntimeError
proc repl =
while true:
try:
let line = ndLineEditor()
if line.len > 0:
discard interpret("repl", line)
except ReadlineInterruptedException:
break
proc runFile(path: string) =
case interpret(path, readFile(path)):
of rsCompileError:
quit 65
of rsRuntimeError:
quit 70
of rsOK:
quit 0
proc printUsage =
echo """Usage:
- Start a repl: nds
- Run a file: nds path/to/script.nds
- Run the nim half of the test suite: nds --test
- Print this message: nds --help or nds -h
"""
const hardcodedPath* = ""
if paramCount() == 0:
if hardcodedPath == "":
repl()
else:
runFile(hardcodedPath)
elif paramCount() == 1:
let arg = paramStr(1)
if arg[0] != '-':
runFile(paramStr(1))
else:
case arg:
of "--help":
printUsage()
quit 0
else:
echo &"Unsupported flag {arg}."
printUsage()
quit 1
else:
echo "Maximum param count is 1."
printUsage()
quit 1