forked from timiscoding/JackCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SymbolTable.js
146 lines (131 loc) · 4.05 KB
/
SymbolTable.js
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
const {Enum} = require('./Enum');
const SymbolTableKinds = new Enum({
NONE: {value: 0, description: 'none'},
STATIC: {value: 1, description: 'static'},
FIELD: {value: 2, description: 'field'},
ARG: {value: 3, description: 'arg'},
VAR: {value: 4, description: 'var'},
CLASS_CONST: {value: 5, description: 'class_const'},
LOCAL_CONST: {value: 6, description: 'LOCAL_const'},
});
const {NONE, STATIC, FIELD, ARG, VAR, CLASS_CONST, LOCAL_CONST} = SymbolTableKinds;
class SymbolTable {
constructor() {
this.maxVar = 0;
this.classTable = {};
this.subroutineTable = {};
this.subScopes = [];
this.varCounts = {
[STATIC]: 0,
[FIELD]: 0,
[ARG]: 0,
[VAR]: 0,
[CLASS_CONST]: 0,
[LOCAL_CONST]: 0
};
}
// reset subroutine symbol table
startSubroutine() {
this.maxVar = 0;
this.added = [];
this.subroutineTable = {};
this.varCounts[ARG] = 0;
this.varCounts[VAR] = 0;
this.varCounts[LOCAL_CONST] = 0;
}
nest() {
this.added.push(0);
this.subScopes.push({});
}
unnest() {
const added = this.added.pop();
this.varCounts[VAR] -= added;
this.subScopes.pop();
}
define(name, type, kind, value) {
if ([STATIC, FIELD].includes(kind)) {
this.classTable[name] = {type, kind, index: this.varCounts[kind.toString()]++};
} else if ([ARG].includes(kind)) {
this.subroutineTable[name] = { type, kind, index: this.varCounts[kind.toString()]++};
} else if ([VAR].includes(kind)) {
if (this.subScopes.length > 0){
this.subScopes[this.subScopes.length - 1][name] = { type, kind, index: this.varCounts[kind.toString()]++};
this.added[this.subScopes.length - 1] += 1;
}
else {
this.subroutineTable[name] = { type, kind, index: this.varCounts[kind.toString()]++};
}
if (this.varCounts[kind.toString()] > this.maxVar) this.maxVar = this.varCounts[kind.toString()];
} else if ([CLASS_CONST].includes(kind)) {
this.classTable[name] = {type, kind, index: value};
this.varCounts[kind.toString()]++;
} else if ([LOCAL_CONST].includes(kind)) {
this.subroutineTable[name] = {type, kind, index: value};
this.varCounts[kind.toString()]++;
}
}
varCount(kind) {
if ([VAR].includes(kind)) {
return this.maxVar;
} else {
return this.varCounts[kind];
}
}
kindOf(name) {
const [nested, scope] = this.isNested(name);
if (nested) {
return this.subScopes[scope][name].kind;
} else if (this.subroutineTable[name]) {
return this.subroutineTable[name].kind;
} else if (this.classTable[name]) {
return this.classTable[name].kind;
} else {
return NONE;
}
}
typeOf(name) {
const [nested, scope] = this.isNested(name);
if (nested) {
return this.subScopes[scope][name].type;
} else if (this.subroutineTable[name]) {
return this.subroutineTable[name].type;
} else if (this.classTable[name]) {
return this.classTable[name].type;
}
}
indexOf(name) {
const [nested, scope] = this.isNested(name);
if (nested && typeof this.subScopes[scope][name].index === 'number') {
return this.subScopes[scope][name].index;
} else if (this.subroutineTable[name] && typeof this.subroutineTable[name].index === 'number') {
return this.subroutineTable[name].index;
} else if (this.classTable[name] && typeof this.classTable[name].index === 'number') {
return this.classTable[name].index;
}
}
isNested(name) {
if (this.subScopes.length > 0) {
let nesting = [false, null];
for (let s = this.subScopes.length - 1; s >= 0; s--) {
if (this.subScopes[s][name]) {
nesting = [true, s];
break;
}
}
return nesting;
} else {
return [false, null];
}
}
exists(name) {
let subscope = false;
this.subScopes.forEach(scope => {
subscope = subscope || name in scope;
});
return subscope || name in this.subroutineTable || name in this.classTable;
}
}
module.exports = {
SymbolTableKinds,
SymbolTable,
};