-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpressions.js
205 lines (188 loc) · 3.79 KB
/
expressions.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
'use strict';
process.on('unhandledException', function(err) {
console.log(err.stack);
});
const CR = '\r';
const cradle = require('./cradle'),
q = require('q');
const emitLn = cradle.emitLn,
getNum = cradle.getNum,
getName = cradle.getName,
init = cradle.init,
finish = cradle.finish,
match = cradle.match,
look = cradle.look,
abort = cradle.abort,
isAlpha = cradle.isAlpha,
expected = cradle.expected;
function ident() {
return getName()
.then((name) => {
let nextChar = look();
if (nextChar === '(') {
return match('(')
.then(() => {
return match(')');
})
.then(() => {
emitLn('BSR ' + name);
});
} else {
emitLn('MOVE ' + name + '(PC),D0');
}
});
}
function factor() {
let nextChar = look();
if (nextChar === '(') {
return match('(')
.then(() => {
return expression();
})
.then(() => {
return match(')');
});
} else if (isAlpha(nextChar)) {
return ident();
} else {
return getNum()
.then((num) => {
emitLn('MOVE #' + num + ',D0');
});
}
}
function term() {
return factor()
.then(() => {
return termTail();
});
}
function multiply() {
return match('*')
.then(() => {
return factor()
})
.then(() => {
emitLn('MULS (SP)+,D0');
});
}
function divide() {
return match('/')
.then(() => {
return factor()
})
.then(() => {
emitLn('MOVE (SP)+,D1');
emitLn('DIVS D1,D0');
});
}
function termTail() {
let nextChar = look();
if (nextChar === '*' || nextChar === '/') {
emitLn('MOVE D0,-(SP)');
return q()
.then(() => {
let nextChar = look();
switch(nextChar) {
case '*':
return multiply();
case '/':
return divide();
}
})
.then(() => {
return termTail();
});
} else {
return;
}
}
function isAddOp(c) {
return (c === '+' || c === '-')
}
function add() {
return match('+')
.then(() => {
return term();
})
.then(() => {
emitLn('ADD (SP)+,D0');
});
}
function subtract() {
return match('-')
.then(() => {
return term()
})
.then(() => {
emitLn('SUB (SP)+,D0');
emitLn('NEG D0');
});
}
function expression() {
let nextChar = look();
return q()
.then(() => {
if (isAddOp(nextChar)) {
emitLn('CLR D0')
} else {
return term();
}
})
.then(() => {
return expressionTail();
});
}
function expressionTail() {
let nextChar = look();
if (isAddOp(nextChar)) {
emitLn('MOVE D0,-(SP)');
return q()
.then(() => {
let nextChar = look();
switch(nextChar) {
case '+':
return add();
case '-':
return subtract();
}
})
.then(() => {
return expressionTail();
});
} else {
return;
}
}
function assignment() {
// Somewhere we need to generate DS directives
// for this to work with the EASy68K assembler.
// Leaving this as an exercise for now to collect variables
// and collect generated code in a buffer so that
// the appropriate header with symbol definitions
// is put before the code in the results emitted
// on stdout
return getName()
.then((name) => {
return match('=')
.then(() => {
return expression()
.then(() => {
emitLn('LEA ' + name + '(PC),A0');
emitLn('MOVE D0,(A0)');
});
});
});
}
return init()
.then(() => {
return assignment();
})
.then(() => {
let nextChar = look();
if (nextChar !== CR) expected('Newline');
return finish();
})
.catch((err) => {
abort(err.stack);
});