/
Context.java
executable file
·159 lines (130 loc) · 3.93 KB
/
Context.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
150
151
152
153
154
155
156
157
158
159
import java.util.ArrayList;
/*
Akshat Singhal, Chris Fry
Context.java
class for context frame for the semantic analysis
of the Akshat and Chris Compiler (ACC)
SymbolTable:
(symbolTable)
|---------|
|-Context1|-->context1 is special, it is never popped off, it has all the top level var and fun decl's.
|-Context2|
|-Context3|
|---------|
Context:
(symbols)
|--------|
|-Symbol1|
|-Symbol2|
|-Symbol3|
|--------|
*/
public class Context{
ArrayList symbols;
public Context(){
//constructor for Context
symbols = new ArrayList();
}
public ArrayList add(Symbol symbol){
// System.out.println("tried to add symbol [" + symbol.identifier + "] of linenumber " + symbol.linenumber);
ArrayList returnerrors = new ArrayList();
Symbol checksymbol;
if (symbol instanceof FunSymbol) {
checksymbol=getFunbySig(((FunSymbol)symbol).signature);
// System.out.println("found symbol: " + checksymbol);
if (!(checksymbol instanceof ErrorSymbol))
returnerrors.add("[" + symbol.linenumber + "]:duplicate function declaration for " + symbol.identifier);
}
else if (symbol instanceof VarSymbol) {
checksymbol=getVar(symbol.identifier);
// System.out.println("found symbol: " + checksymbol);
if (!(checksymbol instanceof ErrorSymbol))
returnerrors.add("[" + symbol.linenumber + "]:duplicate variable/parameter declaration for " + symbol.identifier);
}
else
returnerrors.add("Compiler error: invalid symbol entered into table");
symbols.add(symbol);
return returnerrors;
}
// returns type if symbol is in context, "" if not
public Symbol getVar(String identifier){
Symbol temp;
for(int i = symbols.size() -1; i >= 0; i--){
if((temp = (Symbol)symbols.get(i)).identifier.equals(identifier) &&
(temp instanceof VarSymbol))
return temp;
/* else
System.out.println("tried to compare " + temp);*/
}
temp = new ErrorSymbol();
return temp;
}
public Symbol getFunbySig(String signature){
Symbol temp;
for(int i = symbols.size() -1; i >= 0; i--){
temp = (Symbol)symbols.get(i);
if((temp instanceof FunSymbol) && ((FunSymbol)temp).signature.equals(signature)) {
return temp;
}
}
temp = new ErrorSymbol();
return temp;
}
public Symbol getFunbyIdent(String identifier){
Symbol temp;
for(int i = symbols.size() -1; i >= 0; i--){
temp = (Symbol)symbols.get(i);
if((temp instanceof FunSymbol) && (temp.identifier.equals(identifier)) ) {
return temp;
}
}
temp = new ErrorSymbol();
return temp;
}
public Symbol removeFunBySig(String signature){
//tries to remove a function by looking for it by signature
//returns an errorsymbol if not found
Symbol temp;
for(int i = symbols.size() -1; i >= 0; i--){
temp = (Symbol)symbols.get(i);
if((temp instanceof FunSymbol) && ((FunSymbol)temp).signature.equals(signature)) {
symbols.remove(i);
return temp;
}
}
temp = new ErrorSymbol();
return temp;
}
/*
//removeFunByIdent is a little ambiguous and possibly useless
public Symbol removeFunbyIdent(String identifier){
//tries to remove a function by looking for it by identifier,
//removes the first such function it can find
//returns an errorsymbol if not found
Symbol temp;
for(int i = symbols.size() -1; i >= 0; i--){
temp = (Symbol)symbols.get(i);
if((temp instanceof FunSymbol) && (temp.identifier == identifier)) {
symbols.remove(i);
return temp;
}
}
temp = new ErrorSymbol();
return temp;
}
*/
public Symbol removeVar(String identifier){
//tries to remove a variable by looking for it by signature
//returns an errorsymbol if not found
Symbol temp;
for(int i = symbols.size() -1; i >= 0; i--){
if((temp = (Symbol)symbols.get(i)).identifier.equals(identifier) &&
(temp instanceof VarSymbol)){
symbols.remove(i);
return temp;
}
}
temp = new ErrorSymbol();
return temp;
}
}