-
Notifications
You must be signed in to change notification settings - Fork 4
/
microJ1.js
230 lines (222 loc) · 5.24 KB
/
microJ1.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Author: Eyler -- Jan 2002
// 1.9.2002 redesigned
// 15.10 use TreeNode structure
// 22.12 package
// Data structure for mJ1 and mJ2
// Oct 2003 made public
// Mar 2019 JavaScript
class Node {
constructor() {
this.list = []
}
add(x) { //x is Statement
this.list.push(x);
}
addAll(A) { //A is Array
for (let x of A) this.add(x);
}
toTree(indent='') {
indent += ' '
let s = this.toString()
for (let x of this.list) {
let t = (x instanceof Node?
x.toTree(indent) : x.toString())
s += '\n'+indent+t
}
return s
}
}
class Method extends Node {
constructor(name) {
super(); this.name = name;
this.nParams = 0;
this.vars = [];
//for (let v of vars) this.vars.push(v)
}
toString() {
let a = [];
for (let i=0; i<this.nParams; i++)
a.push(this.vars[i])
return this.name+"("+a.join(', ')+")";
}
}
class Variable { //move to Expression
constructor(m, i) { this.met = m; this.ind = i; }
fValue() {
return VM.getValue(this);
}
toString() { return this.met.vars[this.ind]; }
}
class Declaration {
constructor(vars) { this.vars = vars; }
run() { }
toString() {
return "var "+this.vars.join(', ');
}
}
class Assignment {
constructor(v, e) { this.left = v; this.right = e; }
run() { VM.setValue(this.left, this.right.fValue()); }
toString() { return this.left+" = "+this.right; }
}
class PrintStat {
constructor(p, nl) { this.pri = p; this.hasNL = nl; }
run() {
for (let v of this.pri)
printer += v.value();
if (this.hasNL) {
console.log(printer);
printer = '';
}
}
toString() {
let t = this.hasNL? "println" : "print";
return t+" "+this.pri.join(', ');
}
}
class PrintItem {
constructor(e1, e2=null) {
if (typeof e1 == "string") {
this.lit = e1; return;
}
this.exp = e1; this.len = e2;
}
value() {
if (this.lit) return this.lit;
let t = String(this.exp.fValue());
if (this.len == null) return t;
let n = t.length;
let m = Math.round(this.len.fValue());
if (n >= m) return t;
const MAX = 72;
if (m > MAX) m = MAX;
while (t.length < m) t = ' '+t;
return t;
}
toString() {
if (this.lit) return '"'+this.lit+'"';
return (this.len? this.exp+":"+this.len : ""+this.exp);
}
}
//microJ1 Parser
var met //current Method being parsed
var printer = '' //current unfinished line
const metST = new Map() //Symbol Table: name -> Method
function method() {
let name = tok.val;
if (metST.get(name))
error(name+" already defined");
met = new Method(name);
metST.set(name, met);
match(IDENT); match(LEFT);
if (tok.kind == IDENT) identList();
met.nParams = met.vars.length;
match(RIGHT);
match(BEGIN);
let d = declaration();
if (d.vars.length > 0) met.add(d);
let a = statList();
match(END); met.addAll(a);
return met;
}
function identifier() {
let id = tok.val; match(IDENT);
if (met.vars.includes(id))
error(id+" declared twice");
met.vars.push(id); return id;
}
function identList() {
let L = [];
L.push(identifier());
while (tok == COMMA) {
match(COMMA); L.push(identifier());
}
return L;
}
function declaration() {
let a = [];
if (tok == VAR) {
match(VAR); a = identList(); match(SEMICOL);
}
return new Declaration(a);
}
function statList() {
let L = [];
do { //at least one
let f = tok.index;
let s = statement();
//((Node)s).setPosition(f, prev);
L.push(s);
//debug(DBG_MED, s.toString());
} while (tok != END);
return L;
}
function statement() {
switch (tok.kind) {
case IDENT: return assignment();
case PRINTLN:
case PRINT: return printStat();
default: expected("Statement");
}
return null;
}
function assignment() {
let v = variable();
match(ASSIGN);
let e = expression();
match(SEMICOL);
return new Assignment(v, e);
}
//expression() and term() defined in Expression.js
function factor() { //redefined
switch (tok.kind) {
case NUMBER:
let c = tok.val;
match(NUMBER);
return new Constant(c);
case IDENT:
return variable();
case LEFT:
match(LEFT);
let e = expression();
match(RIGHT); return e;
default: expected("Factor");
}
return null;
}
function variable() {
let i = met.vars.indexOf(tok.val);
if (i < 0) error(tok.val+" not declared");
match(IDENT);
return new Variable(met, i);
}
function printStat() {
let hasNL = (tok == PRINTLN);
match(tok);
let a = [];
if (tok != SEMICOL || !hasNL)
a = itemList();
match(SEMICOL);
return new PrintStat(a, hasNL);
}
function itemList() {
let L = [];
L.push(printItem());
while (tok == COMMA) {
match(COMMA); L.push(printItem());
}
return L;
}
function printItem() {
switch (tok.kind) {
case LITERAL:
let s = tok.val;
match(LITERAL);
return new PrintItem(s);
default :
let e1 = expression();
if (tok != COLON) return new PrintItem(e1);
match(COLON);
return new PrintItem(e1, expression());
}
}