-
Notifications
You must be signed in to change notification settings - Fork 11
/
compiler.grace
122 lines (113 loc) · 2.95 KB
/
compiler.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
#pragma DefaultVisibility=public
import "io" as io
import "sys" as sys
import "unicode" as unicode
import "util" as util
import "lexer" as lexer
import "ast" as ast
import "parser" as parser
import "genc" as genc
import "genes" as genes
import "genjson" as genjson
import "buildinfo" as buildinfo
import "mgcollections" as mgcollections
import "interactive" as interactive
import "identifierresolution" as identifierresolution
import "mirrors" as mirrors
util.parseargs
def targets = ["lex", "parse", "grace", "processed-ast",
"imports", "c", "ecmascript"]
if (util.target == "help") then {
print("Valid targets:")
for (targets) do {t->
print(" {t}")
}
sys.exit(0)
}
if (util.interactive) then {
interactive.startRepl
sys.exit(0)
}
var tokens := lexer.Lexer.new.lexfile(util.infile)
if (util.target == "lex") then {
// Print the lexed tokens and quit.
for (tokens) do { v ->
print(v.kind ++ ": " ++ v.value)
if (util.verbosity > 30) then {
print(" [line: {v.line} position: {v.linePos} indent: {v.indent}]")
}
}
sys.exit(0)
}
// JSON wants to keep hold of the last token in the file
if (util.target == "json") then {
if (tokens.size > 0) then {
genjson.saveToken(tokens.last)
}
}
var values := parser.parse(tokens)
if (util.target == "parse") then {
// Parse mode pretty-prints the source's AST and quits.
for (values) do { v ->
print(v.pretty(0))
}
sys.exit(0)
}
if (util.target == "grace") then {
for (values) do { v ->
print(v.toGrace(0))
}
sys.exit(0)
}
if (util.target == "c") then {
genc.processImports(values)
}
if (("js" | "ecmascript" | "es").match(util.target)) then {
genes.processDialect(values)
}
if (util.target == "json") then {
genes.processDialect(values)
}
if (util.extensions.contains("Plugin")) then {
mirrors.loadDynamicModule(util.extensions.get("Plugin")).processAST(values)
}
values := identifierresolution.resolve(values)
if (util.target == "processed-ast") then {
for (values) do { v ->
print(v.pretty(0))
}
sys.exit(0)
}
if (util.target == "imports") then {
def imps = mgcollections.set.new
def vis = object {
inherits ast.baseVisitor
method visitImport(o) -> Boolean {
imps.add(o.value)
}
}
for (values) do {v->
v.accept(vis)
}
for (imps) do {im->
print(im)
}
sys.exit(0)
}
// Perform the actual compilation
match(util.target)
case { "c" ->
genc.compile(values, util.outfile, util.modname, util.runmode,
util.buildtype)
}
case { "js" | "ecmascript" | "es" ->
genes.compile(values, util.outfile, util.modname, util.runmode,
util.buildtype, util.gracelibPath)
}
case { "json" ->
genjson.generate(values, util.outfile)
}
case { _ ->
io.error.write("minigrace: no such target '" ++ util.target ++ "'\n")
sys.exit(1)
}