-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCompiler.java
More file actions
54 lines (50 loc) · 2.22 KB
/
Compiler.java
File metadata and controls
54 lines (50 loc) · 2.22 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
package com.compilerprogramming.ezlang.compiler;
import com.compilerprogramming.ezlang.lexer.Lexer;
import com.compilerprogramming.ezlang.parser.Parser;
import com.compilerprogramming.ezlang.semantic.SemaAssignTypes;
import com.compilerprogramming.ezlang.semantic.SemaDefineTypes;
import com.compilerprogramming.ezlang.types.Symbol;
import com.compilerprogramming.ezlang.types.EZType;
import com.compilerprogramming.ezlang.types.TypeDictionary;
import java.util.EnumSet;
public class Compiler {
private void compile(TypeDictionary typeDictionary, EnumSet<Options> options) {
for (Symbol symbol: typeDictionary.getLocalSymbols()) {
if (symbol instanceof Symbol.FunctionTypeSymbol functionSymbol) {
EZType.EZTypeFunction functionType = (EZType.EZTypeFunction) functionSymbol.type;
var function = new CompiledFunction(functionSymbol, typeDictionary, options);
if (options.contains(Options.DUMP_INITIAL_IR))
function.dumpIR(false, "Initial IR");
functionType.code = function;
new Optimizer().optimize(function, options);
}
}
}
public TypeDictionary compileSrc(String src) {
return compileSrc(src, EnumSet.noneOf(Options.class));
}
public TypeDictionary compileSrc(String src, EnumSet<Options> options) {
Parser parser = new Parser();
var program = parser.parse(new Lexer(src));
var typeDict = new TypeDictionary();
var sema = new SemaDefineTypes(typeDict);
sema.analyze(program);
var sema2 = new SemaAssignTypes(typeDict);
sema2.analyze(program);
compile(typeDict, options);
return typeDict;
}
public static String dumpIR(TypeDictionary typeDictionary) {
return dumpIR(typeDictionary, false);
}
public static String dumpIR(TypeDictionary typeDictionary, boolean verbose) {
StringBuilder sb = new StringBuilder();
for (Symbol s: typeDictionary.bindings.values()) {
if (s instanceof Symbol.FunctionTypeSymbol f) {
var function = (CompiledFunction) f.code();
function.toStr(sb, verbose);
}
}
return sb.toString();
}
}