Skip to content

Commit f3d7f89

Browse files
Merge pull request #3 from CompilerProgramming/dev
Various cleanups and refactoring
2 parents ecab7f2 + eee8f64 commit f3d7f89

File tree

32 files changed

+876
-534
lines changed

32 files changed

+876
-534
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ The project is under development and subject to change. At this point in time, w
3030

3131
## How can you contribute?
3232

33-
Obviously firstly any contributions that improve the implementation and/or fix bugs are welcome. I am not keen on language extensions at this stage, but eventually
34-
we will be extending the language to explore more advanced features.
33+
The project is educational, with a focus on exploring various compiler algorithms and data structures.
34+
Clarity and simplicity is preferred over a coding style that attempts to do micro optimizations.
3535

36-
I am also interested in creating implementations of this project in C++, Go, Rust, Swift, D, C, etc. If you are interested in working on such a
36+
Contributions that improve the quality of the implementation, add test cases / documentation or fix bugs, are very welcome.
37+
I am not keen on language extensions at this stage, but eventually we will be extending the language to explore more
38+
advanced features.
39+
40+
I am also interested in porting this project to C++, Go, Rust, Swift, D, C, etc. If you are interested in working on such a
3741
port please contact me via [Discussions](https://github.com/orgs/CompilerProgramming/discussions).
3842

3943
## Community Discussions

optvm/src/main/java/com/compilerprogramming/ezlang/bytecode/BytecodeCompiler.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

optvm/src/main/java/com/compilerprogramming/ezlang/bytecode/BasicBlock.java renamed to optvm/src/main/java/com/compilerprogramming/ezlang/compiler/BasicBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.compilerprogramming.ezlang.bytecode;
1+
package com.compilerprogramming.ezlang.compiler;
22

33
import com.compilerprogramming.ezlang.types.Register;
44

optvm/src/main/java/com/compilerprogramming/ezlang/bytecode/BytecodeFunction.java renamed to optvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.compilerprogramming.ezlang.bytecode;
1+
package com.compilerprogramming.ezlang.compiler;
22

33
import com.compilerprogramming.ezlang.exceptions.CompilerException;
44
import com.compilerprogramming.ezlang.parser.AST;
@@ -10,7 +10,7 @@
1010
import java.util.ArrayList;
1111
import java.util.List;
1212

13-
public class BytecodeFunction {
13+
public class CompiledFunction {
1414

1515
public BasicBlock entry;
1616
public BasicBlock exit;
@@ -41,7 +41,7 @@ public class BytecodeFunction {
4141
*/
4242
private List<Operand> virtualStack = new ArrayList<>();
4343

44-
public BytecodeFunction(Symbol.FunctionTypeSymbol functionSymbol) {
44+
public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol) {
4545
AST.FuncDecl funcDecl = (AST.FuncDecl) functionSymbol.functionDecl;
4646
setVirtualRegisters(funcDecl.scope);
4747
this.numLocalRegs = this.nextReg; // Before assigning temps
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.compilerprogramming.ezlang.compiler;
2+
3+
import com.compilerprogramming.ezlang.lexer.Lexer;
4+
import com.compilerprogramming.ezlang.parser.Parser;
5+
import com.compilerprogramming.ezlang.semantic.SemaAssignTypes;
6+
import com.compilerprogramming.ezlang.semantic.SemaDefineTypes;
7+
import com.compilerprogramming.ezlang.types.Symbol;
8+
import com.compilerprogramming.ezlang.types.Type;
9+
import com.compilerprogramming.ezlang.types.TypeDictionary;
10+
11+
import java.util.BitSet;
12+
13+
public class Compiler {
14+
15+
private void compile(TypeDictionary typeDictionary) {
16+
for (Symbol symbol: typeDictionary.getLocalSymbols()) {
17+
if (symbol instanceof Symbol.FunctionTypeSymbol functionSymbol) {
18+
Type.TypeFunction functionType = (Type.TypeFunction) functionSymbol.type;
19+
functionType.code = new CompiledFunction(functionSymbol);
20+
}
21+
}
22+
}
23+
public TypeDictionary compileSrc(String src) {
24+
Parser parser = new Parser();
25+
var program = parser.parse(new Lexer(src));
26+
var typeDict = new TypeDictionary();
27+
var sema = new SemaDefineTypes(typeDict);
28+
sema.analyze(program);
29+
var sema2 = new SemaAssignTypes(typeDict);
30+
sema2.analyze(program);
31+
compile(typeDict);
32+
return typeDict;
33+
}
34+
public String dumpIR(TypeDictionary typeDictionary) {
35+
StringBuilder sb = new StringBuilder();
36+
for (Symbol s: typeDictionary.bindings.values()) {
37+
if (s instanceof Symbol.FunctionTypeSymbol f) {
38+
var functionBuilder = (CompiledFunction) f.code();
39+
BasicBlock.toStr(sb, functionBuilder.entry, new BitSet());
40+
}
41+
}
42+
return sb.toString();
43+
}
44+
45+
}

optvm/src/main/java/com/compilerprogramming/ezlang/bytecode/DominatorTree.java renamed to optvm/src/main/java/com/compilerprogramming/ezlang/compiler/DominatorTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.compilerprogramming.ezlang.bytecode;
1+
package com.compilerprogramming.ezlang.compiler;
22

33
import java.util.ArrayList;
44
import java.util.Comparator;

optvm/src/main/java/com/compilerprogramming/ezlang/bytecode/Instruction.java renamed to optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Instruction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.compilerprogramming.ezlang.bytecode;
1+
package com.compilerprogramming.ezlang.compiler;
22

33
import com.compilerprogramming.ezlang.types.Register;
44
import com.compilerprogramming.ezlang.types.Type;

optvm/src/main/java/com/compilerprogramming/ezlang/bytecode/LoopFinder.java renamed to optvm/src/main/java/com/compilerprogramming/ezlang/compiler/LoopFinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.compilerprogramming.ezlang.bytecode;
1+
package com.compilerprogramming.ezlang.compiler;
22

33
import java.util.*;
44
import java.util.stream.Collectors;

optvm/src/main/java/com/compilerprogramming/ezlang/bytecode/LoopNest.java renamed to optvm/src/main/java/com/compilerprogramming/ezlang/compiler/LoopNest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.compilerprogramming.ezlang.bytecode;
1+
package com.compilerprogramming.ezlang.compiler;
22

33
import java.util.*;
44

optvm/src/main/java/com/compilerprogramming/ezlang/bytecode/Operand.java renamed to optvm/src/main/java/com/compilerprogramming/ezlang/compiler/Operand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.compilerprogramming.ezlang.bytecode;
1+
package com.compilerprogramming.ezlang.compiler;
22

33
import com.compilerprogramming.ezlang.types.Register;
44
import com.compilerprogramming.ezlang.types.Type;

0 commit comments

Comments
 (0)