nondescript/src/nds.nim

98 lines
2.0 KiB
Nim

import ndspkg/vm
import ndspkg/compv2/parser
import ndspkg/compv2/node
import ndspkg/compv2/emitter
import ndspkg/config
when compilerChoice == cmOne:
import ndspkg/compiler/compiler
import ndspkg/compiler/types
import os
import strformat
type Result = enum
rsOK, rsCompileError, rsRuntimeError
proc interpret(name: string, source: string): Result =
when compilerChoice == cmAst:
let parser = newParser(name, source)
let node = parser.parse()
echo $node
elif compilerChoice == cmTwo:
let parser = newParser(name, source)
let node = parser.parse()
if parser.hadError:
return rsCompileError
let emitter = newEmitter(name, node)
emitter.emit()
case emitter.chunk.run():
of irOK:
rsOK
of irRuntimeError:
rsRuntimeError
elif compilerChoice == cmOne:
let compiler = newCompiler(name, source)
compiler.compile()
if compiler.hadError:
return rsCompileError
case compiler.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