nondescript/src/nds.nim

74 lines
1.4 KiB
Nim
Raw Normal View History

2022-01-29 22:33:34 +01:00
import ndspkg/vm
import ndspkg/compiler
import ndspkg/config
2022-01-20 21:54:11 +01:00
2022-01-28 22:00:21 +01:00
import os
import strformat
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 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 "--help":
printUsage()
quit 0
else:
echo &"Unsupported flag {arg}."
printUsage()
quit 1
2022-01-20 21:54:11 +01:00
else:
2022-01-29 22:33:34 +01:00
echo "Maximum param count is 1."
printUsage()
2022-01-20 21:54:11 +01:00
quit 1