-
Notifications
You must be signed in to change notification settings - Fork 0
/
crenshaw_interpreter_port.js
229 lines (200 loc) · 4.01 KB
/
crenshaw_interpreter_port.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
(function(console,process,require) {
'use strict';
String.prototype.splice=function(idx,rem,str) {
return this.slice(0,idx)+str+this.slice(idx+Math.abs(rem));
};
var
string=(true
//Probably considered bad practice, but this is a single purpose script with no async tasks... yet?
? require('fs').readFileSync('test_interpreter.js','utf8')
//For faster tweaking and testing
: '((1+2)*3-(-3))/4'
)
,char_i=-1
,look='' //lookahead Character
,error_marker='[ERR]'
,table={}
,prompt=require('cli-core').prompt
;
function getChar() {
look=string[++char_i]||'';
return look;
}
function error(s) {
throw new Error(
s+'\n'
+'"'
+string
.splice(char_i,0,error_marker)
.slice(Math.max(char_i-10,0),char_i+10+error_marker.length)
+'"'
);
}
function abort(s) {
error(s);
process.exit(-1);
}
function expected(s) {
abort(s+' Expected');
}
function isAlpha(c) {
if (typeof c!=='string') {throw new Error('Expected String, got '+typeof c);}
return (/[a-z]/i).test(c);
}
function isDigit(c) {
if (typeof c!=='string') {throw new Error('Expected String, got '+typeof c);}
return (/[0-9]/).test(c);
}
function isAlNum(c) {
if (typeof c!=='string') {throw new Error('Expected String, got '+typeof c);}
return isAlpha(c)||isDigit(c);
}
function isAddop(c) {
if (typeof c!=='string') {throw new Error('Expected String, got '+typeof c);}
return (/[+\-]/).test(c);
}
function isWhite(c) {
if (typeof c!=='string') {throw new Error('Expected String, got '+typeof c);}
return (/\s/).test(c);
}
function skipWhite() {
while (isWhite(look)) {
getChar();
}
}
function newLine() {skipWhite();}
function match(x) {
if (look===x) {
getChar();
skipWhite();
}
else {expected("'"+x+"'");}
}
function getName() {
if (!isAlpha(look)) {expected('Name');}
var ret='';
while (isAlNum(look)) {
ret+=look;
getChar();
}
skipWhite();
return ret;
}
function getNum() {
if (!isDigit(look)) {expected('Integer');}
var ret='';
while (isDigit(look)) {
ret+=look;
getChar();
}
skipWhite();
return +ret;
}
function emit(s) {
console.log('\t'+s);
}
function emitLn(s) {
throw new Error('Stop using this!');
emit(s);
}
function ident() {
var name=getName();
if (look==='(') {
match('(');
match(')');
emitLn('BSR ' + name);
}
else {
emitLn('MOVE ' + name + '(PC),D0');
}
}
function factor() {
var value;
if (look==='(') {
match('(');
value=expression();
match(')');
}
else if (isAlpha(look)) {
value=table[getName()];
}
else {
value=getNum();
}
return value;
}
function term() {
var value=factor();
while (/[*\/]/.test(look)) {
switch (look) {
case '*':
match('*');
value*=factor();
break;
case '/':
match('*');
value/=factor();
break;
default: expected('Mulop');
}
}
return value;
}
function expression() {
var value=0;
if (!isAddop(look)) {value=term();}
while (isAddop(look)) {
switch (look) {
case '+':
match('+');
value+=term();
break;
case '-':
match('-');
value-=term();
break;
default: expected('Addop');
}
}
return value;
}
function assignment() {
var name=getName();
match('=');
table[name]=expression();
}
function input() {
match('?');
var name=getName();
prompt('Specify a value for "'+name+'"',function(err,input) {
table[name]=+input;
});
return table[name];
}
function output() {
match('!');
console.log(table[getName()]);
}
function init() {
getChar();
skipWhite();
}
init();
while (look!=='') {
switch (look) {
case '?': input( ); break;
case '!': output(); break;
default: assignment();
}
match('.');
newLine();
}
if (look&&look!=='\n') {expected('NewLine');}
if (string[string.length-1]!=='\n') {expected('NewLine at EOF');}
if (look!=='') {
error(
'Invalid/Unsupported Code'
);
}
console.log(JSON.stringify(table));
}(console,process,require));