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
109 lines (102 loc) · 2.63 KB
/
compiler.grace
File metadata and controls
109 lines (102 loc) · 2.63 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
#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 "buildinfo" as buildinfo
import "mgcollections" as mgcollections
import "interactive" as interactive
import "identifierresolution" as identifierresolution
import "mirrors" as mirrors
import "inlining" as inlining
util.parseargs
def targets = ["lex", "parse", "grace", "processed-ast",
"imports", "c", "js", "grace"]
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)
}
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 (util.target == "js") then {
genjs.processDialect(values)
}
if (util.extensions.contains("Plugin")) then {
mirrors.loadDynamicModule(util.extensions.get("Plugin")).processAST(values)
}
values := identifierresolution.resolve(values)
values := inlining.process(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 { _ ->
io.error.write("minigrace: no such target '" ++ util.target ++ "'")
sys.exit(1)
}