nondescript/main.nim

87 lines
1.8 KiB
Nim

import vm
import compiler
import config
import os
import strformat
when compileTests:
import tests/hashtable
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 runTests =
when compileTests:
testHashtables()
else:
echo "Nds was compiled without nim tests. Please change the flag and recompile, then tests will be available."
quit 1
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 "--test":
when defined(danger) or defined(release):
echo "WARNING: nds was compiled with -d:danger or -d:release, tests are only supported on -d:debug!"
runTests()
of "--help":
printUsage()
quit 0
else:
echo &"Unsupported flag {arg}."
printUsage()
quit 1
else:
echo "Maximum param count is 1"
quit 1