Skip to content

Commit 6059ac4

Browse files
committed
Generate VariableMap
(Might need some more testing)
1 parent 95e142b commit 6059ac4

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

src/pgdp/minijava/Compiler.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package pgdp.minijava;
2+
3+
public class Compiler {
4+
public static void main(String[] args) {
5+
compileFromFile("resources/input.java");
6+
}
7+
8+
public static void compileFromFile(String filePath) {
9+
SyntaxTreeNode node = Parser.parseFromFile(filePath);
10+
String out = Emitter.emit(node);
11+
}
12+
}

src/pgdp/minijava/Emitter.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package pgdp.minijava;
22

3+
import java.util.ArrayList;
34
import java.util.HashMap;
5+
import java.util.List;
46
import java.util.Map;
57

68
public class Emitter {
@@ -9,7 +11,44 @@ private Emitter() {
911
}
1012

1113
public static String emit(SyntaxTreeNode node) {
12-
return emitCode(node, new HashMap<>());
14+
Map<String, Integer> variableMap = generateVariableMap(node, new HashMap<>());
15+
return emitCode(node, variableMap);
16+
}
17+
18+
private static Map<String, Integer> generateVariableMap(SyntaxTreeNode node, Map<String, Integer> variableMap) {
19+
Map<String, Integer> out = new HashMap<>(variableMap);
20+
List<Map<String, Integer>> variableMaps = new ArrayList<>();
21+
int freeID = 0;
22+
for (int i = 0; i < node.getNumberChildren(); i++) {
23+
SyntaxTreeNode child = node.getChild(i);
24+
if(child.getType() == SyntaxTreeNode.Type.DECL) {
25+
for (int j = 0; j < child.getNumberChildren(); j++) {
26+
SyntaxTreeNode n = child.getChild(j);
27+
if(n.getType() == SyntaxTreeNode.Type.NAME){
28+
if(variableMap.containsKey(n.getValue())) {
29+
throw new IllegalStateException(n.getValue() + " has already been defined in this context");
30+
} else {
31+
out.put(n.getValue(), freeID++);
32+
}
33+
}
34+
}
35+
} else if(child.getType() == SyntaxTreeNode.Type.NAME) {
36+
if(!(variableMap.containsKey(child.getValue()) || out.containsKey(child.getValue()))) {
37+
throw new IllegalStateException(child.getValue() + " hasn't been defined in this context");
38+
}
39+
} else {
40+
variableMaps.add(generateVariableMap(child, out));
41+
}
42+
}
43+
return mergeVariableMaps(out, variableMaps);
44+
}
45+
46+
private static Map<String, Integer> mergeVariableMaps(Map<String, Integer> variableMap, List<Map<String, Integer>> list) {
47+
for (Map<String, Integer> map : list) {
48+
final int size = variableMap.size();
49+
map.forEach(((s, integer) -> {if(!variableMap.containsKey(s)) variableMap.put(s, integer + size);} ));
50+
}
51+
return variableMap;
1352
}
1453

1554
private static String emitCode(SyntaxTreeNode node, Map<String, Integer> variableMap) {
@@ -26,6 +65,7 @@ private static String emitCode(SyntaxTreeNode node, Map<String, Integer> variabl
2665
case STMT -> null;
2766
case LABEL -> emitLabel(node, variableMap);
2867
case SYMBOL -> null;
68+
case FUNCCALL -> null;
2969
};
3070
}
3171

@@ -45,8 +85,4 @@ private static String emitLabel(SyntaxTreeNode node, Map<String, Integer> variab
4585
private static String emitDeclaration(SyntaxTreeNode node, Map<String, Integer> variableMap) {
4686
return "";
4787
}
48-
49-
private static class CodeUnit {
50-
51-
}
5288
}

src/pgdp/minijava/Parser.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@ private static int parseStatement(List<Token> tokens, int pos, SyntaxTreeNode ro
7878
} else if(current.getContentAsString().equals("{")){
7979
node.addChild(new SyntaxTreeNode(SyntaxTreeNode.Type.SYMBOL, tokens.get(pos++).getContentAsString()));
8080
pos = parseLine(tokens, pos, node);
81-
while(!(tokens.get(pos).getTokenType() == TokenType.SEPARATOR && tokens.get(pos).getContentAsString().equals("}"))) {
81+
while(pos < tokens.size() && !(tokens.get(pos).getTokenType() == TokenType.SEPARATOR && tokens.get(pos).getContentAsString().equals("}"))) {
8282
pos = parseLine(tokens, pos, node);
83-
System.out.println(pos);
8483
}
8584
node.addChild(new SyntaxTreeNode(SyntaxTreeNode.Type.SYMBOL, tokens.get(pos++).getContentAsString()));
8685
}
@@ -271,10 +270,6 @@ private static int parseExpression(List<Token> tokens, int pos, SyntaxTreeNode r
271270
return pos;
272271
}
273272

274-
public static void main(String[] args) {
275-
Parser.parseFromFile("resources/input.java");
276-
}
277-
278273
private final static List<String> types = List.of(
279274
"int",
280275
"double",

0 commit comments

Comments
 (0)