-
Notifications
You must be signed in to change notification settings - Fork 2
/
Compiler.java
59 lines (52 loc) · 1.09 KB
/
Compiler.java
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
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Compiler {
public List<String> compile(String prog) {
return pass3(pass2(pass1(prog)));
}
/**
* Returns an un-optimized AST
*/
public Ast pass1(String prog) {
Deque<String> tokens = tokenize(prog);
return null;
}
/**
* Returns an AST with constant expressions reduced
*/
public Ast pass2(Ast ast) {
return null;
}
/**
* Returns assembly instructions
*/
public List<String> pass3(Ast ast) {
return null;
}
private static Deque<String> tokenize(String prog) {
Deque<String> tokens = new LinkedList<>();
Pattern pattern = Pattern.compile("[-+*/()\\[\\]]|[a-zA-Z]+|\\d+");
Matcher m = pattern.matcher(prog);
while (m.find()) {
tokens.add(m.group());
}
tokens.add("$"); // end-of-stream
return tokens;
}
class Ast {
List<Op> ops;
}
class Op {
String op;
}
class BinOp extends Op {
T op1;
T op2;
}
class UnOp extends Op {
T op1;
}
}