-
Notifications
You must be signed in to change notification settings - Fork 1
/
mn.nim
168 lines (149 loc) · 3.93 KB
/
mn.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import
streams,
strutils,
os,
mnpkg/parser,
mnpkg/value,
mnpkg/scope,
mnpkg/interpreter,
mnpkg/utils,
mnpkg/lang
export
parser,
interpreter,
utils,
value,
scope,
lang
proc stdLib*(i: In) =
i.lang_module
proc interpret*(i: In, s: Stream) =
i.stdLib()
i.open(s, i.filename)
discard i.parser.getToken()
try:
i.interpret()
except CatchableError:
i.error(getCurrentExceptionMsg())
i.close()
proc interpret*(i: In, s: string): MnValue =
i.open(newStringStream(s), i.filename)
discard i.parser.getToken()
try:
result = i.interpret()
except CatchableError:
i.error(getCurrentExceptionMsg())
i.close()
proc mnFile*(filename: string, op = "interpret", main = true): seq[string] {.discardable.}
proc mnStream(s: Stream, filename: string, op = "interpret", main = true): seq[string] {.discardable.}=
var i = newMinInterpreter(filename = filename)
i.pwd = filename.parentDir
i.interpret(s)
newSeq[string](0)
proc mnStr*(buffer: string) =
mnStream(newStringStream(buffer), "input")
proc mnFile*(filename: string, op = "interpret", main = true): seq[string] {.discardable.} =
var fn = filename
if not filename.endsWith(".mn"):
fn &= ".mn"
var fileLines = newSeq[string](0)
var contents = ""
try:
fileLines = fn.readFile().splitLines()
except CatchableError:
stderr.writeLine("Cannot read from file: " & fn)
quit(3)
if fileLines[0].len >= 2 and fileLines[0][0..1] == "#!":
contents = ";;\n" & fileLines[1..fileLines.len-1].join("\n")
else:
contents = fileLines.join("\n")
mnStream(newStringStream(contents), fn, op, main)
when isMainModule:
import
terminal,
parseopt,
mnpkg/meta
var exeName = "mn"
proc printResult(i: In, res: MnValue) =
if res.isNil:
return
if i.stack.len > 0:
let n = $i.stack.len
if res.isQuotation and res.qVal.len > 1:
echo " ("
for item in res.qVal:
echo " " & $item
echo " ".repeat(n.len) & ")"
elif res.isCommand:
echo " [" & res.cmdVal & "]"
else:
echo " $1" % [$i.stack[i.stack.len - 1]]
proc mnSimpleRepl*(i: var MnInterpreter) =
i.stdLib()
var s = newStringStream("")
i.open(s, "<repl>")
var line: string
echo "mn v$#" % pkgVersion
while true:
stdout.write(":: ")
stdout.flushFile()
line = stdin.readLine()
let r = i.interpret($line)
if $line != "":
i.printResult(r)
proc mnSimpleRepl*() =
var i = newMinInterpreter(filename = "<repl>")
i.mnSimpleRepl()
let usage* = """ mn v$version - A truly minimal concatenative programming language
(c) 2021 Fabio Cevasco
Usage:
$exe [options] [filename]
Arguments:
filename A $exe file to interpret or compile
Options:
-e, --evaluate Evaluate a $exe program inline
-h, --help Print this help
-d, --debug Enable debug messages
-v, —-version Print the program version""" % [
"exe", exeName,
"version", pkgVersion,
]
var file, s: string = ""
var args = newSeq[string](0)
var p = initOptParser()
for kind, key, val in getopt(p):
case kind:
of cmdArgument:
args.add key
if file == "":
file = key
of cmdLongOption, cmdShortOption:
case key:
of "debug", "d":
DEBUG = true
of "evaluate", "e":
if file == "":
s = val
of "help", "h":
if file == "":
echo usage
quit(0)
of "version", "v":
if file == "":
echo pkgVersion
quit(0)
else:
discard
else:
discard
var op = "interpret"
if s != "":
mnStr(s)
elif file != "":
mnFile file, op
else:
if isatty(stdin):
mnSimpleRepl()
quit(0)
else:
mnStream newFileStream(stdin), "stdin", op