-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.ts
301 lines (243 loc) · 6.19 KB
/
compiler.ts
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import { Op } from './stack-vm';
export enum Type {
Keyword,
Label,
Newline,
Null,
Numeral,
String,
Variable
}
export enum TokenizerState {
Flush,
Word,
ReadChar
}
export interface Token {
type: Type;
value?: string;
}
export interface CharHandlerMap {
[key: string]: Function;
}
function tokenize(program: string) {
let tokens: any[] = [];
let cursor: number = 0;
let state: TokenizerState = TokenizerState.ReadChar;
let token: Token = { type: Type.Null };
while (cursor < program.length) {
let char = program[cursor];
switch (state) {
case TokenizerState.ReadChar:
if (isLetter(char)) {
token = { type: Type.Keyword, value: char };
cursor++;
state = TokenizerState.Word
} else if (isNumeral(char)) {
token = { type: Type.Numeral, value: char };
cursor++;
state = TokenizerState.Word
} else if (isColon(char)) {
token = { type: Type.Label, value: '' };
cursor++;
state = TokenizerState.Word
} else if (isSplat(char)) {
token = { type: Type.Variable, value: '' };
cursor++;
state = TokenizerState.Word
} else if (isQuote(char)) {
token = { type: Type.String, value: '' };
cursor++;
state = TokenizerState.Word
} else if (isNewline(char)) {
token = { type: Type.Newline };
cursor++;
state = TokenizerState.Flush;
} else if (isWhitespace(char)) {
cursor++;
} else {
throw 'Syntax error';
}
break;
case TokenizerState.Word:
if (isLetter(char) || isNumeral(char)) {
token.value = token.value!.concat(char);
cursor++;
} else if (isNewline(char)) {
state = TokenizerState.Flush;
} else if (isWhitespace(char) || isQuote(char)) {
cursor++;
state = TokenizerState.Flush;
} else {
throw 'Syntax error';
}
break;
case TokenizerState.Flush:
tokens.push(token);
state = TokenizerState.ReadChar;
break;
}
}
return tokens;
}
function isLetter(char: string): boolean {
return /[a-zA-Z]/.test(char);
}
function isNumeral(char: string): boolean {
return /[0-9]/.test(char);
}
function isSplat(char: string): boolean {
return char === '*';
}
function isColon(char: string): boolean {
return char === ':';
}
function isQuote(char: string): boolean {
return char === '\'';
}
function isWhitespace(char: string): boolean {
return [' '].indexOf(char) > -1;
}
function isNewline(char: string): boolean {
return /\n/.test(char);
}
interface AST {
type: Syntax,
body?: AST[],
args?: AST[],
value?: number | string
};
enum Syntax {
Call,
Label,
Macro,
NumberLiteral,
Primitive,
Program,
StringLiteral,
Variable
}
function parse(tokens: Token[]): AST {
let cursor = 0;
let ast: AST = {
type: Syntax.Program,
body: []
};
function walk(): AST | null {
let token = tokens[cursor];
switch (token.type) {
case Type.Newline:
cursor++;
return null;
case Type.Keyword: {
let primitives = [
'assign',
'call',
'jump',
'label',
'return'
];
let isPrimitive = primitives.indexOf(token.value!) > -1;
let node: AST = {
type: isPrimitive ? Syntax.Primitive : Syntax.Macro,
value: token.value,
args: []
};
cursor++;
while (cursor < tokens.length) {
let arg = walk();
if (!arg) break; // stop because we hit a newline
node.args!.push(arg);
}
return node;
}
case Type.Label:
cursor++;
return {
type: Syntax.Label,
value: token.value
};
case Type.Variable:
cursor++;
return {
type: Syntax.Variable,
value: token.value
};
case Type.Numeral:
cursor++;
return {
type: Syntax.NumberLiteral,
value: token.value
};
case Type.String:
cursor++;
return {
type: Syntax.StringLiteral,
value: token.value
};
default:
throw 'Wat';
}
}
while (cursor < tokens.length) {
let result = walk();
if (result) {
ast.body!.push(result);
}
}
return ast;
}
function generate(ast: AST): string {
switch (ast.type) {
case Syntax.Program:
if (!ast.body) throw 'AST body not present';
return `[${ast.body.map(generate).join(',')}]`;
case Syntax.Label:
return `[${Op.LABEL},"${ast.value}"]`;
case Syntax.NumberLiteral:
return `${ast.value}`;
case Syntax.Variable:
case Syntax.StringLiteral:
return `"${ast.value}"`;
case Syntax.Primitive: {
if (typeof ast.value !== 'string') throw 'Primitive AST values should be strings';
let opKey = ast.value.toUpperCase();
let op = Op[<any>opKey];
if (op === undefined) throw `Opcode "${opKey}" not found`;
if (ast.args!.length < 1) {
return `[${op}]`;
}
if (ast.args!.length > 1) {
let pushes = ast.args!.map(arg => {
return `[${Op.PUSH},${generate(arg)}]`;
}).join(',');
return `${pushes},[${op}]`;
}
let arg = ast.args![0];
if (arg.type === Syntax.Label) {
return `[${op},"${arg.value}"]`;
}
return `[${op},${generate(arg)}]`;
}
case Syntax.Macro: {
if (typeof ast.value !== 'string') throw 'Macro AST values should be strings';
let opKey = ast.value.toUpperCase();
let op = Op[<any>opKey];
if (op === undefined) throw `Opcode "${opKey}" not found`;
if (ast.args!.length > 0) {
let pushes = ast.args!.map(arg => {
if (arg.type === Syntax.Variable) {
return `[${Op.GET},${generate(arg)}]`;
}
return `[${Op.PUSH},${generate(arg)}]`;
}).join(',');
return `${pushes},[${op}]`;
}
}
default:
throw 'Wat';
}
}
export default function compile(program: string): string {
return generate(parse(tokenize(program)));
}