/
Symbol.java
executable file
·150 lines (120 loc) · 3.36 KB
/
Symbol.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Symbol.java */
/* Akshat Singhal and Chris Fry */
/* this is the symbol classes of the Akshat and
Chris Compiler (ACC) */
public abstract class Symbol{
final static String INT = "int";
final static String FLOAT = "float";
final static String INTSTAR = "int*";
final static String FLOATSTAR = "float*";
final static String VOIDSTAR = "void*";
final static String ERROR = "error";
String identifier;
String type;
boolean lvalue;
int linenumber;
}
class VarSymbol extends Symbol{
int arraylength;
public VarSymbol(VarDeclNode varDeclNode){
this.linenumber=varDeclNode.linenumber;
this.lvalue=true;
Node[] children = varDeclNode.getChildren();
if (varDeclNode.children[1].toString().equals("asterisk")) {
this.type = children[0].toString() + "*";
this.identifier = children[2].toString();
this.arraylength=0;
}
else {
this.type = children[0].toString();
this.identifier = children[1].toString();
if (children.length==3) {
this.type=this.type+"*";
this.arraylength = ((NumNode)children[2]).value;
this.lvalue=false;
}
}
}
public VarSymbol(ParamNode paramNode){
this.linenumber=paramNode.linenumber;
this.lvalue=true;
Node[] children = paramNode.getChildren();
if (children[1].toString().equals("asterisk")) {
this.type = children[0].toString() + "*";
this.identifier = children[2].toString();
this.arraylength=0;
}
else {
this.type = children[0].toString();
this.identifier = children[1].toString();
if (children.length==3){
this.type=this.type+"*";
this.arraylength = ((NumNode)children[2]).value;
this.lvalue=false;
}
}
}
public String toString(){
return type+ " " + identifier;
}
}
class FunSymbol extends Symbol{
String signature;
public FunSymbol(FunDeclNode funNode){
ParamsNode params = new ParamsNode(0);
this.linenumber=funNode.linenumber;
if (funNode.children.length==5){
this.type = funNode.children[0].toString()+"*";
this.identifier = funNode.children[2].toString();
params = ((ParamsNode)funNode.children[3]);
}
else {
this.type = funNode.children[0].toString();
this.identifier = funNode.children[1].toString();
params = ((ParamsNode)funNode.children[2]);
}
String sig;
sig = "" + identifier ;
for(int i = 0; i < params.getNumChildren(); i++){
if (params.children[i] instanceof VoidNode)
sig += "_" + "void";// not this isn't right yet
else {
if (params.children[i]!=null && params.children[i].children.length==3)
sig += "_" + params.children[i].children[0] + "*";
else
sig += "_" + params.children[i].children[0] ;
}
}
this.signature = sig;
funNode.signature=sig;
}
public String toString(){
return type+"_"+signature;
}
}
class ErrorSymbol extends Symbol{
final int SYMBOLNOTFOUND = 0;
int errorType=-1;
String[] errorTypes=new String[4];
public ErrorSymbol(){
identifier = "error";
type = ERROR;
}
public ErrorSymbol(int reason) {
errorTypes[0]="Symbol Not Found";
this.errorType = reason;
identifier = "error";
type = ERROR;
}
public String toString(){
return identifier;
}
}
class CallSymbol extends Symbol{
public CallSymbol(PrimaryExprNode node){
String identifier;
String type;
boolean lvalue;
int linenumber;
}
}