nondescript/main.nim

87 lines
1.8 KiB
Nim
Raw Normal View History

2022-01-20 21:54:11 +01:00
import vm
import compiler
import config
2022-01-28 22:00:21 +01:00
import os
import strformat
when compileTests:
import tests/hashtable
2022-01-20 21:54:11 +01:00
type Result = enum
rsOK, rsCompileError, rsRuntimeError
2022-01-21 00:18:58 +01:00
proc interpret(name: string, source: string): Result =
let compiler = newCompiler(name, source)
2022-01-20 21:54:11 +01:00
compiler.compile()
if compiler.hadError:
return rsCompileError
2022-01-27 01:36:43 +01:00
case compiler.chunk.run():
2022-01-20 21:54:11 +01:00
of irOK:
rsOK
of irRuntimeError:
rsRuntimeError
proc repl =
while true:
try:
2022-01-27 05:56:09 +01:00
let line = ndLineEditor()
2022-01-20 21:54:11 +01:00
if line.len > 0:
2022-01-21 00:18:58 +01:00
discard interpret("repl", line)
2022-01-20 21:54:11 +01:00
except ReadlineInterruptedException:
break
proc runFile(path: string) =
2022-01-21 00:18:58 +01:00
case interpret(path, readFile(path)):
2022-01-20 21:54:11 +01:00
of rsCompileError:
quit 65
of rsRuntimeError:
quit 70
of rsOK:
quit 0
2022-01-28 22:00:21 +01:00
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
"""
2022-01-22 17:28:53 +01:00
const hardcodedPath* = ""
2022-01-20 21:54:11 +01:00
if paramCount() == 0:
2022-01-22 17:28:53 +01:00
if hardcodedPath == "":
repl()
else:
runFile(hardcodedPath)
2022-01-20 21:54:11 +01:00
elif paramCount() == 1:
2022-01-28 22:00:21 +01:00
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
2022-01-20 21:54:11 +01:00
else:
echo "Maximum param count is 1"
quit 1