-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolTable.java
56 lines (46 loc) · 1.29 KB
/
SymbolTable.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
public class SymbolTable {
/* Symbol Tables implemented as a simple linked list */
Parser parser;
public Obj first;
int adrNext;
public SymbolTable(Parser parser) {
this.parser = parser;
}
public Obj NewObj(String name, int type) {
Obj last;
Obj iterator;
Obj newObj = new Obj();
newObj.name = name;
/* Undefined = 0, Integer = 1, Boolean = 2. Originally used enum but that resulted in ugly code */
newObj.type = type;
iterator = first;
last = null;
// Find to last object in the chain
while(iterator != null) {
if(iterator.name == name) parser.SemErr(name + " already exists in symbol table");
last = iterator;
iterator = iterator.next;
}
// If no symbols found then we are inserting the first symbol
if(last == null) first = newObj; else { last.next = newObj; }
// Assign an address to object and increment next address pointer
newObj.adr = this.adrNext++;
return newObj;
}
public Obj FindObj(String name) {
Obj iter;
iter = first;
while(iter != null) {
if(iter.name.equals(name)) return iter;
iter = iter.next;
}
return null;
}
}
// Symbol in memory
class Obj {
public String name;
public int type;
public int adr; // Object's address in memory
public Obj next;
}