forked from mwh/minigrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.grace
More file actions
137 lines (128 loc) · 3.33 KB
/
compiler.grace
File metadata and controls
137 lines (128 loc) · 3.33 KB
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
#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 "genjs" as genjs
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", "js"]
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.extensions.contains("ClassWrap")) then {
def vl = values
values := []
def inner = []
for (vl) do { v->
if ((v.kind == "import") || (v.kind == "dialect")) then {
values.push(v)
} else {
inner.push(v)
}
}
values.push(ast.methodNode.new(ast.identifierNode.new("new", false),
[ast.signaturePart.new("new")],
[ast.objectNode.new(inner, false)], false))
}
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 (util.target == "js") then {
genjs.processDialect(values)
}
if (util.target == "json") then {
genjs.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.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" ->
genjs.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)
}