import ndspkg/vm import ndspkg/compiler import ndspkg/config import os import strformat type Result = enum rsOK, rsCompileError, rsRuntimeError proc interpret(name: string, source: string): Result = 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