forked from mwh/minigrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.grace
454 lines (441 loc) · 14.1 KB
/
util.grace
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#pragma DefaultVisibility=public
import "io" as io
import "sys" as sys
import "buildinfo" as buildinfo
import "mgcollections" as mgcollections
var __compilerRevision := false
var verbosityv := 30
var outfilev := io.output
var infilev := io.input
var modnamev := "stdin_minigrace"
var runmodev := "make"
var buildtypev := "run"
var interactivev := false
var gracelibPathv := false
var linenumv := 1
var lineposv := 1
var vtagv := false
var noexecv := false
var targetv := "c"
var versionNumber := "0.0.8"
var extensionsv := mgcollections.map.new
var recurse := true
var dynamicModule := false
var importDynamic := false
var jobs := 2
var cLines := []
var lines := []
var errno := 0
method runOnNew(b)else(e) {
if ((__compilerRevision != "d5f6522d5c5e3f5b1f40d77502a66954955d0e5a")
&& (__compilerRevision != false)) then {
b.apply
} else {
e.apply
}
}
method parseargs {
var argv := sys.argv
var toStdout := false
if (argv.size > 1) then {
var indices := argv.indices
var arg
var skip := true
for (indices) do { ai->
arg := argv.at(ai)
if (arg.at(1) == "-") then {
match(arg)
case { "-o" ->
if(argv.size < (ai + 1)) then {
io.error.write("minigrace: -o requires argument.\n")
sys.exit(1)
}
outfilev := io.open(argv.at(ai + 1), "w")
skip := true
} case { "--verbose" ->
verbosityv := 40
} case { "--help" ->
printhelp
} case { "--vtag" ->
skip := true
if(argv.size < (ai + 1)) then {
io.error.write("minigrace: --vtag requires argument.\n")
sys.exit(1)
}
vtagv := argv.at(ai + 1)
} case { "--make" ->
runmodev := "make"
buildtypev := "bc"
} case { "--no-recurse" ->
recurse := false
} case { "--dynamic-module" ->
dynamicModule := true
runmodev := "make"
noexecv := true
buildtypev := "bc"
} case { "--import-dynamic" ->
importDynamic := true
} case { "--run" ->
buildtypev := "run"
runmodev := "make"
} case { "--source" ->
buildtypev := "source"
runmodev := "build"
} case { "--native" ->
buildtypev := "native"
} case { "--interactive" ->
interactivev := true
} case { "--noexec" ->
noexecv := true
} case { "--yesexec" ->
noexecv := false
} case { "--stdout" ->
toStdout := true
} case { "-" ->
toStdout := true
} case { "--module" ->
skip := true
if(argv.size < (ai + 1)) then {
io.error.write("minigrace: --module requires argument.\n")
sys.exit(1)
}
modnamev := argv.at(ai + 1)
} case { "--gracelib" ->
skip := true
if(argv.size < (ai + 1)) then {
io.error.write("minigrace: --gracelib requires argument.\n")
sys.exit(1)
}
gracelibPathv := argv.at(ai + 1)
} case { "--target" ->
skip := true
if(argv.size < (ai + 1)) then {
io.error.write("minigrace: --target requires argument.\n")
sys.exit(1)
}
targetv := argv.at(ai + 1)
} case { "-j" ->
skip := true
if(argv.size < (ai + 1)) then {
io.error.write("minigrace: -j requires argument.\n")
sys.exit(1)
}
jobs := argv.at(ai + 1).asNumber
} case { "--version" ->
print("minigrace "
++ "{versionNumber}.{buildinfo.gitgeneration}")
print("git revision " ++ buildinfo.gitrevision)
print("<http://ecs.vuw.ac.nz/~mwh/minigrace/>")
sys.exit(0)
} case { "--help" ->
print "Usage: minigrace <file>.grace"
print "See the documentation for more options."
sys.exit(0)
} case { _ ->
if (arg.at(2) == "X") then {
var ext := arg.substringFrom(3)to(arg.size)
processExtension(ext)
} else {
io.error.write("minigrace: invalid "
++ "argument {arg}.\n")
sys.exit(1)
}
}
} else {
if (skip) then {
skip := false
} else {
var filename := arg
infilev := io.open(filename, "r")
if (modnamev == "stdin_minigrace") then {
var accum := ""
modnamev := ""
for (filename) do { c->
if (c == ".") then {
modnamev := accum
}
accum := accum ++ c
}
}
}
}
}
}
if ((outfilev == io.output) && {!toStdout}) then {
outfilev := match(targetv)
case { "c" -> io.open(modnamev ++ ".c", "w") }
case { "js" -> io.open(modnamev ++ ".js", "w") }
case { _ -> io.output }
}
if (gracelibPathv == false) then {
if (io.exists(sys.execPath ++ "/../lib/minigrace/gracelib.o")) then {
gracelibPathv := sys.execPath ++ "/../lib/minigrace"
} else {
gracelibPathv := sys.execPath
}
}
if (infilev == io.input) then {
if (infilev.isatty) then {
print("minigrace {versionNumber}.{buildinfo.gitgeneration} / "
++ buildinfo.gitrevision)
print "Copyright (C) 2011-2013 Michael Homer"
print("This is free software with ABSOLUTELY NO WARRANTY. "
++ "Say minigrace.w for details.")
print ""
if (interactivev.not) then {
print "Enter a program and press Ctrl-D to execute it."
print ""
}
}
}
}
method log_verbose(s) {
if (verbosityv >= 40) then {
var vtagw := ""
if (false != vtagv) then {
vtagw := "[" ++ vtagv ++ "]"
}
io.error.write("minigrace{vtagw}: {modnamev}: {sys.cputime}/"
++ "{sys.elapsed}: {s}\n")
}
}
method outprint(s) {
outfilev.write(s)
outfilev.write("\n")
}
// This is called by various wrapper methods in the errormessages module.
// The parameters are explained as follows:
// - message: The text of the error message.
// - errlinenum: The line number on which the error occurred.
// - position: A string used to show the position of the error in the error message.
// - arr: The string used to draw an arrow showing the position of the error.
// - spacePos: The position in the error line that a space should be inserted, or false.
// - suggestions: A (possibly empty) list of suggestions to correct the error.
method syntaxError(message, errlinenum, position, arr, spacePos, suggestions) {
generalError("Syntax error: {message}", errlinenum, position, arr, spacePos,
suggestions)
}
method generalError(message, errlinenum, position, arr, spacePos, suggestions) {
if (vtagv) then {
io.error.write("[" ++ vtagv ++ "]")
}
io.error.write("{modnamev}.grace[{errlinenum}{position}]: {message}\n")
if ((errlinenum > 1) && (lines.size > 1)) then {
io.error.write(" {errlinenum - 1}: {lines.at(errlinenum - 1)}\n")
}
if (lines.size >= errlinenum) then {
var line := lines.at(errlinenum)
if(spacePos != false) then {
io.error.write(" {errlinenum}: {line.substringFrom(1)to(spacePos-1)} {line.substringFrom(spacePos)to(line.size)}\n")
} else {
io.error.write(" {errlinenum}: {line}\n")
}
io.error.write("{arr}\n")
}
if (errlinenum < lines.size) then {
io.error.write(" {errlinenum + 1}: {lines.at(errlinenum + 1)}\n")
}
if (suggestions.size > 0) then {
for(suggestions) do { s ->
io.error.write("\nDid you mean:\n")
s.print
}
}
if (interactivev.not) then {
sys.exit(1)
} else {
errno := 1
}
}
method type_error(s) {
if (extensionsv.contains("IgnoreTypes")) then {
return true
}
if (vtagv) then {
io.error.write("[" ++ vtagv ++ "]")
}
io.error.write("{modnamev}.grace:{linenumv}:{lineposv}: Type error: {s}")
io.error.write("\n")
io.error.write(lines.at(linenumv) ++ "\n")
if (interactivev.not) then {
sys.exit(1)
} else {
errno := 1
}
}
method semantic_error(s) {
if (vtagv) then {
io.error.write("[" ++ vtagv ++ "]")
}
io.error.write "{modnamev}.grace:{linenumv}:{lineposv}: Semantic error"
if (s == "") then {
io.error.write "\n"
if (!interactivev) then {
sys.exit(1)
}
}
io.error.write ": {s}\n"
if (linenumv > 1) then {
if (lines.size > 0) then {
io.error.write(" {linenumv - 1}: {lines.at(linenumv - 1)}\n")
}
}
var arr := "----"
for (2..(lineposv + linenumv.asString.size)) do {
arr := arr ++ "-"
}
if (lines.size >= linenumv) then {
io.error.write(" {linenumv}: {lines.at(linenumv)}\n{arr}^\n")
}
if (linenumv < lines.size) then {
io.error.write(" {linenumv + 1}: {lines.at(linenumv + 1)}\n")
}
if (interactivev.not) then {
sys.exit(1)
} else {
errno := 1
}
}
method warning(s) {
io.error.write("{modnamev}.grace:{linenumv}:{lineposv}: warning: {s}")
io.error.write("\n")
}
method verbosity {
verbosityv
}
method outfile {
outfilev
}
method infile {
infilev
}
method modname {
modnamev
}
method runmode {
runmodev
}
method buildtype {
buildtypev
}
method interactive {
interactivev
}
method gracelibPath {
gracelibPathv
}
method setline(l) {
linenumv := l
}
method setPosition(l, p) {
linenumv := l
lineposv := p
}
method linenum {
linenumv
}
method linepos {
lineposv
}
method vtag {
vtagv
}
method noexec {
noexecv
}
method target {
targetv
}
method engine {
"native"
}
method extensions {
extensionsv
}
method processExtension(ext) {
var extn := ""
var extv := true
var seeneq := false
for (ext) do {c->
if (c == "=") then {
seeneq := true
extv := ""
} else {
if (!seeneq) then {
extn := extn ++ c
} else {
extv := extv ++ c
}
}
}
extensionsv.put(extn, extv)
}
method printhelp {
print "Usage: {sys.argv.at(1)} [OPTION]... [FILE]"
print "Compile, process, or run a Grace source file or standard input."
print ""
print "Modes:"
print " --make Compile FILE to a native executable"
print " --run Compile FILE and execute the program [default]"
print " --source Compile FILE to source, but no further"
print " --dynamic-module Compile FILE as a dynamic module (experimental!)"
print " --interactive Launch interactive read-eval-print interpreter"
print ""
print "Options:"
print " --verbose Give more detailed output"
print " --target TGT Choose a non-default compilation target TGT"
print " Use --target help to list supported targets."
print " -o OFILE Output to OFILE instead of default"
print " -j N Spawn at most N concurrent subprocesses"
print " --help This text"
print " --module Override default module name (derived from FILE)"
print " --no-recurse Do not compile imported modules"
print " --stdout Output to standard output rather than a file"
print " --version Print version information"
print ""
print "By default, {sys.argv.at(1)} FILE will compile and execute FILE."
print "More detailed usage information is in the <doc/usage> file in the source tree."
sys.exit(0)
}
method debug(s) {
}
var hexdigits := "0123456789abcdef"
method hex(num) {
var tmp := num
var s := ""
while {tmp > 0} do {
var i := tmp % 16
s := hexdigits.at(i + 1) ++ s
tmp := tmp - i
tmp := tmp / 16
}
s
}
method join(joiner, iterable) {
def ind = iterable.indices
def min = ind.first
var s := ""
for (ind) do {i->
if (i != min) then {
s := s ++ joiner
}
s := s ++ iterable.at(i)
}
s
}
method split(str, by) {
def results = []
def bylen = by.size
var start := 1
var i := 1
def strlen = str.size
while {i <= strlen} do {
if (str.substringFrom(i)to(i+bylen-1) == by) then {
results.push(str.substringFrom(start)to(i-1))
start := i + bylen
}
i := i + 1
}
results.push(str.substringFrom(start)to(strlen))
results
}