-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.c
492 lines (392 loc) · 10.3 KB
/
compiler.c
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "compiler.h"
#include "object.h"
#ifdef DEBUG_PRINT_CODE
#include "debug.h"
#endif
Parser parser;
CompilerState* cmpState = NULL;
Chunk* compilingChunk;
static void initCompiler(CompilerState* state) {
state->localCount = 0;
state->scopeDepth = GLOBAL_SCOPE;
cmpState = state;
}
static void beginScope() { cmpState->scopeDepth++; }
static void endScope() { cmpState->scopeDepth--; }
static Chunk* currentChunk() { return compilingChunk; }
static void errorAt(Token* token, const char* message) {
if (parser.panicMode) return;
parser.panicMode = true;
fprintf(stderr, "[line %d] Error", token->line);
if (token->type == TOKEN_EOF) {
fprintf(stderr, " at end");
} else if (token->type == TOKEN_ERROR) {
// Nothing.
} else {
fprintf(stderr, " at '%.*s'", token->length, token->start);
}
fprintf(stderr, ": %s\n", message);
parser.hadError = true;
}
static void errorAtCurrent(const char* message) {
errorAt(&parser.current, message);
}
static void error(const char* message) { errorAt(&parser.previous, message); }
static void advance() {
parser.previous = parser.current;
for (;;) {
parser.current = scanToken();
if (parser.current.type != TOKEN_ERROR) break;
errorAtCurrent(parser.current.start);
}
}
static void consume(TokenType type, const char* message) {
if (parser.current.type == type) {
advance();
return;
}
errorAtCurrent(message);
}
static void synchronize() {
parser.panicMode = false;
while (parser.current.type != TOKEN_EOF) {
if (parser.previous.type == TOKEN_SEMICOLON) return;
switch (parser.current.type) {
case TOKEN_CLASS:
case TOKEN_FUN:
case TOKEN_VAR:
case TOKEN_FOR:
case TOKEN_IF:
case TOKEN_WHILE:
case TOKEN_PRINT:
case TOKEN_RETURN:
return;
default:
// Do nothing.
;
}
advance();
}
}
static bool check(TokenType type) { return parser.current.type == type; }
static bool match(TokenType type) {
if (!check(type)) return false;
advance();
return true;
}
static void emitByte(uint8_t byte) {
writeChunk(currentChunk(), byte, parser.previous.line);
}
static void emitBytes(uint8_t byte1, uint8_t byte2) {
emitByte(byte1);
emitByte(byte2);
}
static int emitJump(uint8_t instruction) {
emitByte(instruction);
// 16-bit offset
emitByte(0xff);
emitByte(0xff);
return currentChunk()->count - 2;
}
static uint8_t makeConstant(Value value) {
int constant = addConstant(currentChunk(), value);
if (constant > UINT8_MAX) {
error("Too many constants in one chunk.");
return 0;
}
return (uint8_t)constant;
}
static void parsePrecedence(Precedence precedence) {
advance();
ParseFn prefixRule = PARSE_RULE(parser.previous.type)->prefix;
if (prefixRule == NULL) {
error("Expect expression.");
return;
}
bool canAssign = precedence <= PREC_ASSIGNMENT;
prefixRule(canAssign);
while (precedence <= PARSE_RULE(parser.current.type)->precedence) {
advance();
ParseFn infixRule = PARSE_RULE(parser.previous.type)->infix;
infixRule(canAssign);
}
if (canAssign && match(TOKEN_EQUAL)) {
error("Invalid assignment target.");
expression();
}
}
void expression() { parsePrecedence(PREC_ASSIGNMENT); }
void block() {
while (!check(TOKEN_RIGHT_BRACE) && !check(TOKEN_EOF)) {
declaration();
}
consume(TOKEN_RIGHT_BRACE, "Expect '}' after block.");
}
static uint8_t identifierConstant(Token* name) {
return makeConstant(LOX_OBJ(copyString(name->start, name->length)));
}
static void addLocal(Token name) {
if (cmpState->localCount == UINT8_COUNT) {
error("Too many local variables in function."); // Who does this?!
return;
}
Local* local = &cmpState->locals[cmpState->localCount++];
local->name = name;
local->depth = UNINITIALIZED;
}
static bool identifiersEqual(Token* a, Token* b) {
if (a->length != b->length) return false;
return memcmp(a->start, b->start, a->length) == 0;
}
static int resolveLocal(CompilerState* state, Token* name) {
for (int i = state->localCount - 1; i >= 0; i--) {
Local* local = &state->locals[i];
if (identifiersEqual(name, &local->name)) {
if (local->depth == UNINITIALIZED)
error("Cannot read local variable in its own initializer.");
return i;
}
}
return -1;
}
static void declareVariable() {
// Global variables are implicitly declared.
if (cmpState->scopeDepth == GLOBAL_SCOPE) return;
Token* name = &parser.previous;
for (int i = cmpState->localCount - 1; i >= 0; i--) {
Local* local = &cmpState->locals[i];
if (local->depth != UNINITIALIZED && local->depth < cmpState->scopeDepth)
break;
if (identifiersEqual(name, &local->name)) {
error("Variable with this name already declared in this scope.");
}
}
addLocal(*name);
}
static void markInitialized() {
if (cmpState->scopeDepth == 0) return;
cmpState->locals[cmpState->localCount - 1].depth = cmpState->scopeDepth;
}
static uint8_t parseVariable(const char* errorMessage) {
consume(TOKEN_IDENTIFIER, errorMessage);
declareVariable();
if (cmpState->scopeDepth > GLOBAL_SCOPE) return 0;
return identifierConstant(&parser.previous);
}
static void defineVariable(uint8_t global) {
if (cmpState->scopeDepth > GLOBAL_SCOPE) {
markInitialized();
return;
}
emitBytes(OP_DEFINE_GLOBAL, global);
}
static void patchJump(int offset) {
// -2 to adjust for the bytecode for the jump offset itself.
int jump = currentChunk()->count - offset - 2;
if (jump > UINT16_MAX) {
error("Too much code to jump over.");
}
currentChunk()->code[offset] = (jump >> 8) & 0xff;
currentChunk()->code[offset + 1] = jump & 0xff;
}
void and_(bool canAssign) {
int endJump = emitJump(OP_JUMP_IF_FALSE);
emitByte(OP_POP);
parsePrecedence(PREC_AND);
patchJump(endJump);
}
static void varDeclaration() {
uint8_t global = parseVariable("Expect variable name.");
if (match(TOKEN_EQUAL)) {
expression();
} else {
emitByte(OP_NIL);
}
consume(TOKEN_SEMICOLON, "Expect ';' after variable declaration.");
defineVariable(global);
}
void expressionStatement() {
expression();
emitByte(OP_POP);
consume(TOKEN_SEMICOLON, "Expect ';' after expression.");
}
static void ifStatement() {
consume(TOKEN_LEFT_PAREN, "Expect '(' after 'if'.");
expression();
consume(TOKEN_RIGHT_PAREN, "Expect ')' after condition.");
int thenJump = emitJump(OP_JUMP_IF_FALSE);
emitByte(OP_POP);
statement();
int elseJump = emitJump(OP_JUMP);
patchJump(thenJump);
emitByte(OP_POP);
if (match(TOKEN_ELSE)) statement();
patchJump(elseJump);
}
static void printStatement() {
expression();
consume(TOKEN_SEMICOLON, "Expect ';' after value.");
emitByte(OP_PRINT);
}
void declaration() {
if (match(TOKEN_VAR)) {
varDeclaration();
} else {
statement();
}
if (parser.panicMode) synchronize();
}
void statement() {
if (match(TOKEN_PRINT)) {
printStatement();
} else if (match(TOKEN_IF)) {
ifStatement();
} else if (match(TOKEN_LEFT_BRACE)) {
beginScope();
block();
endScope();
} else {
expressionStatement();
}
}
static void namedVariable(Token name, bool canAssign) {
uint8_t getOp, setOp;
int arg = resolveLocal(cmpState, &name);
if (arg != -1) {
getOp = OP_GET_LOCAL;
setOp = OP_SET_LOCAL;
} else {
arg = identifierConstant(&name);
getOp = OP_GET_GLOBAL;
setOp = OP_SET_GLOBAL;
}
if (canAssign && match(TOKEN_EQUAL)) {
expression();
emitBytes(setOp, (uint8_t)arg);
} else {
emitBytes(getOp, (uint8_t)arg);
}
}
void variable(bool canAssign) { namedVariable(parser.previous, canAssign); }
static void emitConstant(Value value) {
emitBytes(OP_CONSTANT, makeConstant(value));
}
void number(bool canAssign) {
double value = strtod(parser.previous.start, NULL);
emitConstant(LOX_NUMBER(value));
}
void or_(bool canAssign) {
int elseJump = emitJump(OP_JUMP_IF_FALSE);
int endJump = emitJump(OP_JUMP);
patchJump(elseJump);
emitByte(OP_POP);
parsePrecedence(PREC_OR);
patchJump(endJump);
}
void string(bool canAssign) {
emitConstant(LOX_OBJ(
copyString(parser.previous.start + 1, parser.previous.length - 2)));
// `+1` & `-2` to trim qoutes.
}
void literal(bool canAssign) {
switch (parser.previous.type) {
case TOKEN_FALSE:
emitByte(OP_FALSE);
break;
case TOKEN_NIL:
emitByte(OP_NIL);
break;
case TOKEN_TRUE:
emitByte(OP_TRUE);
break;
default:
return;
}
}
void binary(bool canAssign) {
TokenType operator= parser.previous.type;
// Compile the right operand.
ParseRule* rule = PARSE_RULE(operator);
parsePrecedence((Precedence)(rule->precedence + 1));
// Emit the operator instruction.
switch (operator) {
case TOKEN_BANG_EQUAL:
emitBytes(OP_EQUAL, OP_NOT);
break;
case TOKEN_EQUAL_EQUAL:
emitByte(OP_EQUAL);
break;
case TOKEN_GREATER:
emitByte(OP_GREATER);
break;
case TOKEN_GREATER_EQUAL:
emitBytes(OP_LESS, OP_NOT);
break;
case TOKEN_LESS:
emitByte(OP_LESS);
break;
case TOKEN_LESS_EQUAL:
emitBytes(OP_GREATER, OP_NOT);
break;
case TOKEN_PLUS:
emitByte(OP_ADD);
break;
case TOKEN_MINUS:
emitByte(OP_SUBTRACT);
break;
case TOKEN_STAR:
emitByte(OP_MULTIPLY);
break;
case TOKEN_SLASH:
emitByte(OP_DIVIDE);
break;
default:
return;
}
}
void unary(bool canAssign) {
TokenType operator= parser.previous.type;
// Compile the operand.
parsePrecedence(PREC_UNARY);
// Emit the operator instruction.
switch (operator) {
case TOKEN_BANG:
emitByte(OP_NOT);
break;
case TOKEN_MINUS:
emitByte(OP_NEGATE);
break;
default:
return;
}
}
void grouping(bool canAssign) {
expression();
consume(TOKEN_RIGHT_PAREN, "Expect ')' after expression.");
}
static void endCompiler() {
emitByte(OP_RETURN);
#ifdef DEBUG_PRINT_CODE
if (!parser.hadError) {
disassembleChunk(currentChunk(), "code");
}
#endif
}
bool compile(const char* source, Chunk* chunk) {
initLexer(source);
CompilerState state;
initCompiler(&state);
compilingChunk = chunk;
parser.hadError = false;
parser.panicMode = false;
advance();
while (!match(TOKEN_EOF)) {
declaration();
}
endCompiler();
return !parser.hadError;
}