-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.java
More file actions
62 lines (39 loc) · 1.04 KB
/
Code.java
File metadata and controls
62 lines (39 loc) · 1.04 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
import java.util.*;
public class Code {
private static int label;
private static int temp;
private SymbolTable env;
private String code;
public Code(SymbolTable env, String code) {
this.env =env;
this.code = code;
}
public SymbolTable env() {return env;}
public String code() {return code;}
public String generate (String blockName) {
String genCode;
if(blockName.equals("main"))
genCode = "int ";
else
genCode = "void ";
genCode = genCode + blockName + " () {\n";
genCode = genCode + env.code();
genCode = genCode + code;
if(blockName.equals("main"))
genCode = genCode + " return 0;\n";
genCode = genCode + "}\n";
return genCode;
}
public static void initLabel() {label = 0; }
public static String newLabel() {
String newLabel = "_L" + label;
label++;
return newLabel;
}
public static void initTemp() {temp = 0; }
public static String newTemp() {
String newTemp = "_T" + label;
label++;
return newTemp;
}
}