-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
compiler.c
2233 lines (1866 loc) · 59 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "compiler.h"
#include "common.h"
#include "config.h"
#include "memory.h"
#include "object.h"
#include "pathinfo.h"
#include "scanner.h"
#include "util.h"
#ifdef _WIN32
#include "win32.h"
#endif
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"
static b_blob *current_blob(b_parser *p) {
return &p->compiler->function->blob;
}
static void error_at(b_parser *p, b_token *t, const char *message,
va_list args) {
fflush(stdout); // flush out anything on stdout first
// do not cascade error
// suppress error if already in panic mode
if (p->panic_mode)
return;
p->panic_mode = true;
fprintf(stderr, "SyntaxError:\n");
fprintf(stderr, " File: %s, Line: %d\n", p->current_file, t->line);
fprintf(stderr, " Error");
if (t->type == EOF_TOKEN) {
fprintf(stderr, " at end");
} else if (t->type == ERROR_TOKEN) {
// do nothing
} else {
if (t->length == 1 && *t->start == '\n') {
fprintf(stderr, " at newline");
} else {
fprintf(stderr, " at '%.*s'", t->length, t->start);
}
}
fprintf(stderr, ": ");
vfprintf(stderr, message, args);
fputs("\n", stderr);
p->had_error = true;
}
static void error(b_parser *p, const char *message, ...) {
va_list args;
va_start(args, message);
error_at(p, &p->previous, message, args);
va_end(args);
}
static void error_at_current(b_parser *p, const char *message, ...) {
va_list args;
va_start(args, message);
error_at(p, &p->current, message, args);
va_end(args);
}
static void advance(b_parser *p) {
p->previous = p->current;
for (;;) {
p->current = scan_token(p->scanner);
if (p->current.type != ERROR_TOKEN)
break;
error_at_current(p, p->current.start);
}
}
static void consume(b_parser *p, b_tkn_type t, const char *message) {
if (p->current.type == t) {
advance(p);
return;
}
error_at_current(p, message);
}
static bool check_number(b_parser *p) {
if (p->previous.type == REG_NUMBER_TOKEN ||
p->previous.type == OCT_NUMBER_TOKEN ||
p->previous.type == BIN_NUMBER_TOKEN ||
p->previous.type == HEX_NUMBER_TOKEN)
return true;
return false;
}
static bool check(b_parser *p, b_tkn_type t) { return p->current.type == t; }
static bool match(b_parser *p, b_tkn_type t) {
if (!check(p, t))
return false;
advance(p);
return true;
}
static void consume_statement_end(b_parser *p) {
// allow block last statement to omit statement end
if (p->in_block && check(p, RBRACE_TOKEN))
return;
if (match(p, SEMICOLON_TOKEN)) {
while (match(p, SEMICOLON_TOKEN) || match(p, NEWLINE_TOKEN))
;
return;
}
if (match(p, EOF_TOKEN))
return;
consume(p, NEWLINE_TOKEN, "end of statement expected");
while (match(p, SEMICOLON_TOKEN) || match(p, NEWLINE_TOKEN))
;
}
static void ignore_whitespace(b_parser *p) {
while (match(p, NEWLINE_TOKEN))
;
}
static int get_code_args_count(const uint8_t *bytecode,
const b_value *constants, int ip) {
b_code code = (b_code)bytecode[ip];
// @TODO: handle up values gracefully...
switch (code) {
case OP_EQUAL:
case OP_GREATER:
case OP_LESS:
case OP_NIL:
case OP_TRUE:
case OP_FALSE:
case OP_ADD:
case OP_SUBTRACT:
case OP_MULTIPLY:
case OP_DIVIDE:
case OP_F_DIVIDE:
case OP_REMINDER:
case OP_POW:
case OP_NEGATE:
case OP_NOT:
case OP_ECHO:
case OP_POP:
case OP_CLOSE_UP_VALUE:
case OP_DUP:
case OP_RETURN:
case OP_INHERIT:
case OP_GET_SUPER:
case OP_AND:
case OP_OR:
case OP_XOR:
case OP_LSHIFT:
case OP_RSHIFT:
case OP_BIT_NOT:
case OP_ONE:
case OP_SET_INDEX:
case OP_ASSERT:
case OP_DIE:
case OP_POP_TRY:
case OP_RANGE:
case OP_STRINGIFY:
case OP_CHOICE:
case OP_EMPTY:
return 0;
case OP_CALL:
case OP_SUPER_INVOKE_SELF:
case OP_GET_INDEX:
return 1;
case OP_DEFINE_GLOBAL:
case OP_GET_GLOBAL:
case OP_SET_GLOBAL:
case OP_GET_LOCAL:
case OP_SET_LOCAL:
case OP_GET_UP_VALUE:
case OP_SET_UP_VALUE:
case OP_JUMP_IF_FALSE:
case OP_JUMP:
case OP_BREAK_PL:
case OP_LOOP:
case OP_CONSTANT:
case OP_POP_N:
case OP_CLASS:
case OP_GET_PROPERTY:
case OP_SET_PROPERTY:
case OP_LIST:
case OP_DICT:
case OP_CALL_IMPORT:
case OP_FINISH_MODULE:
case OP_SWITCH:
return 2;
case OP_INVOKE:
case OP_SUPER_INVOKE:
case OP_METHOD:
case OP_CLASS_PROPERTY:
return 3;
case OP_TRY:
return 6;
case OP_CLOSURE: {
int constant = (bytecode[ip + 1] << 8) | bytecode[ip + 2];
b_obj_func *fn = AS_FUNCTION(constants[constant]);
// There is two byte for the constant, then three for each up value.
return 2 + (fn->up_value_count * 3);
}
default:
return 0;
}
return 0;
}
static void emit_byte(b_parser *p, uint8_t byte) {
write_blob(p->vm, current_blob(p), byte, p->previous.line);
}
static void emit_short(b_parser *p, uint16_t byte) {
write_blob(p->vm, current_blob(p), (byte >> 8) & 0xff, p->previous.line);
write_blob(p->vm, current_blob(p), byte & 0xff, p->previous.line);
}
static void emit_bytes(b_parser *p, uint8_t byte, uint8_t byte2) {
write_blob(p->vm, current_blob(p), byte, p->previous.line);
write_blob(p->vm, current_blob(p), byte2, p->previous.line);
}
static void emit_byte_and_short(b_parser *p, uint8_t byte, uint16_t byte2) {
write_blob(p->vm, current_blob(p), byte, p->previous.line);
write_blob(p->vm, current_blob(p), (byte2 >> 8) & 0xff, p->previous.line);
write_blob(p->vm, current_blob(p), byte2 & 0xff, p->previous.line);
}
/* static void emit_byte_and_long(b_parser *p, uint8_t byte, uint16_t byte2) {
write_blob(p->vm, current_blob(p), byte, p->previous.line);
write_blob(p->vm, current_blob(p), (byte2 >> 16) & 0xff, p->previous.line);
write_blob(p->vm, current_blob(p), (byte2 >> 8) & 0xff, p->previous.line);
write_blob(p->vm, current_blob(p), byte2 & 0xff, p->previous.line);
} */
static void emit_loop(b_parser *p, int loop_start) {
emit_byte(p, OP_LOOP);
int offset = current_blob(p)->count - loop_start + 2;
if (offset > UINT16_MAX)
error(p, "loop body too large");
emit_byte(p, (offset >> 8) & 0xff);
emit_byte(p, offset & 0xff);
}
static void emit_return(b_parser *p) {
if (p->compiler->type == TYPE_INITIALIZER) {
emit_byte_and_short(p, OP_GET_LOCAL, 0);
} else {
emit_byte(p, OP_NIL);
}
emit_byte(p, OP_RETURN);
}
static int make_constant(b_parser *p, b_value value) {
int constant = add_constant(p->vm, current_blob(p), value);
if (constant >= UINT16_MAX) {
error(p, "too many constants in current scope");
return 0;
}
return constant;
}
static void emit_constant(b_parser *p, b_value value) {
int constant = make_constant(p, value);
emit_byte_and_short(p, OP_CONSTANT, (uint16_t)constant);
}
static int emit_jump(b_parser *p, uint8_t instruction) {
emit_byte(p, instruction);
// placeholders
emit_byte(p, 0xff);
emit_byte(p, 0xff);
return current_blob(p)->count - 2;
}
static int emit_switch(b_parser *p) {
emit_byte(p, OP_SWITCH);
// placeholders
emit_byte(p, 0xff);
emit_byte(p, 0xff);
return current_blob(p)->count - 2;
}
static int emit_try(b_parser *p) {
emit_byte(p, OP_TRY);
// type placeholders
emit_byte(p, 0xff);
emit_byte(p, 0xff);
// handler placeholders
emit_byte(p, 0xff);
emit_byte(p, 0xff);
// finally placeholders
emit_byte(p, 0xff);
emit_byte(p, 0xff);
return current_blob(p)->count - 6;
}
static void patch_switch(b_parser *p, int offset, int constant) {
current_blob(p)->code[offset] = (constant >> 8) & 0xff;
current_blob(p)->code[offset + 1] = constant & 0xff;
}
static void patch_try(b_parser *p, int offset, int type, int address, int finally) {
// patch type
current_blob(p)->code[offset] = (type >> 8) & 0xff;
current_blob(p)->code[offset + 1] = type & 0xff;
// patch address
current_blob(p)->code[offset + 2] = (address >> 8) & 0xff;
current_blob(p)->code[offset + 3] = address & 0xff;
// patch finally
current_blob(p)->code[offset + 4] = (finally >> 8) & 0xff;
current_blob(p)->code[offset + 5] = finally & 0xff;
}
static void patch_jump(b_parser *p, int offset) {
// -2 to adjust the bytecode for the offset itself
int jump = current_blob(p)->count - offset - 2;
if (jump > UINT16_MAX) {
error(p, "body of conditional block too large");
}
current_blob(p)->code[offset] = (jump >> 8) & 0xff;
current_blob(p)->code[offset + 1] = jump & 0xff;
}
static void init_compiler(b_parser *p, b_compiler *compiler, b_func_type type) {
compiler->enclosing = p->compiler;
compiler->function = NULL;
compiler->type = type;
compiler->local_count = 0;
compiler->scope_depth = 0;
compiler->handler_count = 0;
compiler->function = new_function(p->vm);
compiler->function->file = p->current_file;
p->compiler = compiler;
if (type != TYPE_SCRIPT) {
p->compiler->function->name =
copy_string(p->vm, p->previous.start, p->previous.length);
}
// claiming slot zero for use in class methods
b_local *local = &p->compiler->locals[p->compiler->local_count++];
local->depth = 0;
local->is_captured = false;
if (type != TYPE_FUNCTION) {
local->name.start = "self";
local->name.length = 4;
} else {
local->name.start = "";
local->name.length = 0;
}
}
static int identifier_constant(b_parser *p, b_token *name) {
return make_constant(p,
OBJ_VAL(copy_string(p->vm, name->start, name->length)));
}
static bool identifiers_equal(b_token *a, b_token *b) {
if (a->length != b->length)
return false;
return memcmp(a->start, b->start, a->length) == 0;
}
static int resolve_local(b_parser *p, b_compiler *compiler, b_token *name) {
for (int i = compiler->local_count - 1; i >= 0; i--) {
b_local *local = &compiler->locals[i];
if (identifiers_equal(&local->name, name)) {
if (local->depth == -1) {
error(p, "cannot read local variable in it's own initializer");
}
return i;
}
}
return -1;
}
static int add_up_value(b_parser *p, b_compiler *compiler, uint16_t index,
bool is_local) {
int up_value_count = compiler->function->up_value_count;
for (int i = 0; i < up_value_count; i++) {
b_up_value *up_value = &compiler->up_values[i];
if (up_value->index == index && up_value->is_local == is_local) {
return i;
}
}
if (up_value_count == UINT8_COUNT) {
error(p, "too many closure variables in function");
return 0;
}
compiler->up_values[up_value_count].is_local = is_local;
compiler->up_values[up_value_count].index = index;
return compiler->function->up_value_count++;
}
static int resolve_up_value(b_parser *p, b_compiler *compiler, b_token *name) {
if (compiler->enclosing == NULL)
return -1;
int local = resolve_local(p, compiler->enclosing, name);
if (local != -1) {
compiler->enclosing->locals[local].is_captured = true;
return add_up_value(p, compiler, (uint16_t)local, true);
}
int up_value = resolve_up_value(p, compiler->enclosing, name);
if (up_value != -1) {
return add_up_value(p, compiler, (uint16_t)up_value, false);
}
return -1;
}
static int add_local(b_parser *p, b_token name) {
if (p->compiler->local_count == UINT8_COUNT) {
// we've reached maximum local variables per scope
error(p, "too many local variables in scope");
return -1;
}
b_local *local = &p->compiler->locals[p->compiler->local_count++];
local->name = name;
local->depth = -1;
local->is_captured = false;
return p->compiler->local_count;
}
static void declare_variable(b_parser *p) {
// global variables are implicitly declared...
if (p->compiler->scope_depth == 0)
return;
b_token *name = &p->previous;
for (int i = p->compiler->local_count - 1; i >= 0; i--) {
b_local *local = &p->compiler->locals[i];
if (local->depth != -1 && local->depth < p->compiler->scope_depth) {
break;
}
if (identifiers_equal(name, &local->name)) {
error(p, "%.*s already declared in current scope", name->length,
name->start);
}
}
add_local(p, *name);
}
static int parse_variable(b_parser *p, const char *message) {
consume(p, IDENTIFIER_TOKEN, message);
declare_variable(p);
if (p->compiler->scope_depth > 0) // we are in a local scope...
return 0;
return identifier_constant(p, &p->previous);
}
static void mark_initialized(b_parser *p) {
if (p->compiler->scope_depth == 0)
return;
p->compiler->locals[p->compiler->local_count - 1].depth =
p->compiler->scope_depth;
}
static void define_variable(b_parser *p, int global) {
if (p->compiler->scope_depth > 0) { // we are in a local scope...
mark_initialized(p);
return;
}
emit_byte_and_short(p, OP_DEFINE_GLOBAL, global);
}
static b_token synthetic_token(const char *name) {
b_token token;
token.start = name;
token.length = (int)strlen(name);
return token;
}
static b_obj_func *end_compiler(b_parser *p) {
emit_return(p);
b_obj_func *function = p->compiler->function;
if (!p->had_error && p->vm->should_print_bytecode) {
disassemble_blob(current_blob(p), function->name == NULL
? p->current_file
: function->name->chars);
}
p->compiler = p->compiler->enclosing;
return function;
}
static void begin_scope(b_parser *p) { p->compiler->scope_depth++; }
static void end_scope(b_parser *p) {
p->compiler->scope_depth--;
// remove all variables declared in scope while exiting...
while (p->compiler->local_count > 0 &&
p->compiler->locals[p->compiler->local_count - 1].depth >
p->compiler->scope_depth) {
if (p->compiler->locals[p->compiler->local_count - 1].is_captured) {
emit_byte(p, OP_CLOSE_UP_VALUE);
} else {
emit_byte(p, OP_POP);
}
p->compiler->local_count--;
}
}
static void discard_local(b_parser *p, int depth) {
if (p->compiler->scope_depth == -1) {
error(p, "cannot exit top-level scope");
}
for (int i = p->compiler->local_count - 1;
i >= 0 && p->compiler->locals[i].depth > depth; i--) {
if (p->compiler->locals[i].is_captured) {
emit_byte(p, OP_CLOSE_UP_VALUE);
} else {
emit_byte(p, OP_POP);
}
}
}
static void end_loop(b_parser *p) {
// find all OP_BREAK_PL placeholder and replace with the appropriate jump...
int i = p->innermost_loop_start;
while (i < p->compiler->function->blob.count) {
if (p->compiler->function->blob.code[i] == OP_BREAK_PL) {
p->compiler->function->blob.code[i] = OP_JUMP;
patch_jump(p, i + 1);
} else {
i += 1 + get_code_args_count(p->compiler->function->blob.code,
p->compiler->function->blob.constants.values,
i);
}
}
}
// --> Forward declarations start
static void expression(b_parser *p);
static void statement(b_parser *p);
static void declaration(b_parser *p);
static void anonymous(b_parser *p, bool can_assign);
static b_parse_rule *get_rule(b_tkn_type type);
static void parse_precedence(b_parser *p, b_precedence precedence);
// --> Forward declarations end
static void binary(b_parser *p, bool can_assign) {
b_tkn_type op = p->previous.type;
// compile the right operand
b_parse_rule *rule = get_rule(op);
parse_precedence(p, (b_precedence)(rule->precedence + 1));
// emit the operator instruction
switch (op) {
case PLUS_TOKEN:
emit_byte(p, OP_ADD);
break;
case MINUS_TOKEN:
emit_byte(p, OP_SUBTRACT);
break;
case MULTIPLY_TOKEN:
emit_byte(p, OP_MULTIPLY);
break;
case DIVIDE_TOKEN:
emit_byte(p, OP_DIVIDE);
break;
case PERCENT_TOKEN:
emit_byte(p, OP_REMINDER);
break;
case POW_TOKEN:
emit_byte(p, OP_POW);
break;
case FLOOR_TOKEN:
emit_byte(p, OP_F_DIVIDE);
break;
// equality
case EQUAL_EQ_TOKEN:
emit_byte(p, OP_EQUAL);
break;
case BANG_EQ_TOKEN:
emit_bytes(p, OP_EQUAL, OP_NOT);
break;
case GREATER_TOKEN:
emit_byte(p, OP_GREATER);
break;
case GREATER_EQ_TOKEN:
emit_bytes(p, OP_LESS, OP_NOT);
break;
case LESS_TOKEN:
emit_byte(p, OP_LESS);
break;
case LESS_EQ_TOKEN:
emit_bytes(p, OP_GREATER, OP_NOT);
break;
// bitwise
case AMP_TOKEN:
emit_byte(p, OP_AND);
break;
case BAR_TOKEN:
emit_byte(p, OP_OR);
break;
case XOR_TOKEN:
emit_byte(p, OP_XOR);
break;
case LSHIFT_TOKEN:
emit_byte(p, OP_LSHIFT);
break;
case RSHIFT_TOKEN:
emit_byte(p, OP_RSHIFT);
break;
// range
case RANGE_TOKEN:
emit_byte(p, OP_RANGE);
break;
default:
break;
}
}
static uint8_t argument_list(b_parser *p) {
uint8_t arg_count = 0;
if (!check(p, RPAREN_TOKEN)) {
do {
ignore_whitespace(p);
expression(p);
if (arg_count == MAX_FUNCTION_PARAMETERS) {
error(p, "cannot have more than %d arguments to a function",
MAX_FUNCTION_PARAMETERS);
}
arg_count++;
} while (match(p, COMMA_TOKEN));
}
ignore_whitespace(p);
consume(p, RPAREN_TOKEN, "expected ')' after argument list");
return arg_count;
}
static void call(b_parser *p, bool can_assign) {
uint8_t arg_count = argument_list(p);
emit_bytes(p, OP_CALL, arg_count);
}
static void literal(b_parser *p, bool can_assign) {
switch (p->previous.type) {
case NIL_TOKEN:
emit_byte(p, OP_NIL);
break;
case TRUE_TOKEN:
emit_byte(p, OP_TRUE);
break;
case FALSE_TOKEN:
emit_byte(p, OP_FALSE);
break;
default:
return;
}
}
static void parse_assignment(b_parser *p, uint8_t real_op, uint8_t get_op,
uint8_t set_op, int arg) {
if(get_op == OP_GET_PROPERTY) {
emit_byte(p, OP_DUP);
}
if (arg != -1) {
emit_byte_and_short(p, get_op, arg);
} else {
emit_bytes(p, get_op, 1);
}
expression(p);
emit_byte(p, real_op);
if (arg != -1) {
emit_byte_and_short(p, set_op, (uint16_t)arg);
} else {
emit_byte(p, set_op);
}
}
static void assignment(b_parser *p, uint8_t get_op, uint8_t set_op, int arg, bool can_assign) {
if (can_assign && match(p, EQUAL_TOKEN)) {
expression(p);
if (arg != -1) {
emit_byte_and_short(p, set_op, (uint16_t)arg);
} else {
emit_byte(p, set_op);
}
} else if (can_assign && match(p, PLUS_EQ_TOKEN)) {
parse_assignment(p, OP_ADD, get_op, set_op, arg);
} else if (can_assign && match(p, MINUS_EQ_TOKEN)) {
parse_assignment(p, OP_SUBTRACT, get_op, set_op, arg);
} else if (can_assign && match(p, MULTIPLY_EQ_TOKEN)) {
parse_assignment(p, OP_MULTIPLY, get_op, set_op, arg);
} else if (can_assign && match(p, DIVIDE_EQ_TOKEN)) {
parse_assignment(p, OP_DIVIDE, get_op, set_op, arg);
} else if (can_assign && match(p, POW_EQ_TOKEN)) {
parse_assignment(p, OP_POW, get_op, set_op, arg);
} else if (can_assign && match(p, PERCENT_EQ_TOKEN)) {
parse_assignment(p, OP_REMINDER, get_op, set_op, arg);
} else if (can_assign && match(p, FLOOR_EQ_TOKEN)) {
parse_assignment(p, OP_F_DIVIDE, get_op, set_op, arg);
} else if (can_assign && match(p, AMP_EQ_TOKEN)) {
parse_assignment(p, OP_AND, get_op, set_op, arg);
} else if (can_assign && match(p, BAR_EQ_TOKEN)) {
parse_assignment(p, OP_OR, get_op, set_op, arg);
} else if (can_assign && match(p, TILDE_EQ_TOKEN)) {
parse_assignment(p, OP_BIT_NOT, get_op, set_op, arg);
} else if (can_assign && match(p, XOR_EQ_TOKEN)) {
parse_assignment(p, OP_XOR, get_op, set_op, arg);
} else if (can_assign && match(p, LSHIFT_EQ_TOKEN)) {
parse_assignment(p, OP_LSHIFT, get_op, set_op, arg);
} else if (can_assign && match(p, RSHIFT_EQ_TOKEN)) {
parse_assignment(p, OP_RSHIFT, get_op, set_op, arg);
} else if (can_assign && match(p, INCREMENT_TOKEN)) {
if(get_op == OP_GET_PROPERTY) {
emit_byte(p, OP_DUP);
}
if (arg != -1) {
emit_byte_and_short(p, get_op, arg);
} else {
emit_bytes(p, get_op, 1);
}
emit_bytes(p, OP_ONE, OP_ADD);
emit_byte_and_short(p, set_op, (uint16_t)arg);
} else if (can_assign && match(p, DECREMENT_TOKEN)) {
if(get_op == OP_GET_PROPERTY) {
emit_byte(p, OP_DUP);
}
if (arg != -1) {
emit_byte_and_short(p, get_op, arg);
} else {
emit_bytes(p, get_op, 1);
}
emit_bytes(p, OP_ONE, OP_SUBTRACT);
emit_byte_and_short(p, set_op, (uint16_t)arg);
} else {
if (arg != -1) {
if(get_op == OP_GET_INDEX) {
emit_bytes(p, get_op, (uint8_t)0);
} else {
emit_byte_and_short(p, get_op, (uint16_t)arg);
}
} else {
emit_bytes(p, get_op, (uint8_t)0);
}
}
}
static void dot(b_parser *p, bool can_assign) {
ignore_whitespace(p);
consume(p, IDENTIFIER_TOKEN, "expected property name after '.'");
int name = identifier_constant(p, &p->previous);
if (match(p, LPAREN_TOKEN)) {
uint8_t arg_count = argument_list(p);
emit_byte_and_short(p, OP_INVOKE, name);
emit_byte(p, arg_count);
} else {
assignment(p, OP_GET_PROPERTY, OP_SET_PROPERTY, name, can_assign);
}
}
static void named_variable(b_parser *p, b_token name, bool can_assign) {
uint8_t get_op, set_op;
int arg = resolve_local(p, p->compiler, &name);
if (arg != -1) {
get_op = OP_GET_LOCAL;
set_op = OP_SET_LOCAL;
} else if ((arg = resolve_up_value(p, p->compiler, &name)) != -1) {
get_op = OP_GET_UP_VALUE;
set_op = OP_SET_UP_VALUE;
} else {
arg = identifier_constant(p, &name);
get_op = OP_GET_GLOBAL;
set_op = OP_SET_GLOBAL;
}
assignment(p, get_op, set_op, arg, can_assign);
}
static void list(b_parser *p, bool can_assign) {
emit_byte(p, OP_NIL); // placeholder for the list
int count = 0;
if (!check(p, RBRACKET_TOKEN)) {
do {
ignore_whitespace(p);
expression(p);
ignore_whitespace(p);
count++;
} while (match(p, COMMA_TOKEN));
}
ignore_whitespace(p);
consume(p, RBRACKET_TOKEN, "expected ']' at end of list");
emit_byte_and_short(p, OP_LIST, count);
}
static void dictionary(b_parser *p, bool can_assign) {
emit_byte(p, OP_NIL); // placeholder for the dictionary
int item_count = 0;
if (!check(p, RBRACE_TOKEN)) {
do {
ignore_whitespace(p);
if (!check(p, RBRACE_TOKEN)) { // allow last pair to end with a comma
if (check(p, IDENTIFIER_TOKEN)) {
consume(p, IDENTIFIER_TOKEN, "");
emit_constant(p, OBJ_VAL(copy_string(p->vm, p->previous.start,
p->previous.length)));
} else {
expression(p);
}
ignore_whitespace(p);
consume(p, COLON_TOKEN, "expected ':' after dictionary key");
ignore_whitespace(p);
expression(p);
item_count++;
}
} while (match(p, COMMA_TOKEN));
}
ignore_whitespace(p);
consume(p, RBRACE_TOKEN, "expected '}' after dictionary");
emit_byte_and_short(p, OP_DICT, item_count);
}
static void indexing(b_parser *p, bool can_assign) {
expression(p);
bool assignable = true;
if (!match(p, RBRACKET_TOKEN)) {
consume(p, COMMA_TOKEN, "expecting ',' or ']'");
if(match(p, RBRACKET_TOKEN)) {
emit_byte(p, OP_NIL);
} else {
expression(p);
consume(p, RBRACKET_TOKEN, "expected ']' after indexing");
}
assignable = false;
} else {
emit_byte(p, OP_EMPTY);
}
assignment(p, OP_GET_INDEX, OP_SET_INDEX, -1, assignable);
}
static void variable(b_parser *p, bool can_assign) {
named_variable(p, p->previous, can_assign);
}
static void self(b_parser *p, bool can_assign) {
if (p->current_class == NULL) {
error(p, "cannot use keyword 'self' outside of a class");
return;
}
variable(p, false);
}
static void parent(b_parser *p, bool can_assign) {
if (p->current_class == NULL) {
error(p, "cannot use keyword 'parent' outside of a class");
} else if (!p->current_class->has_superclass) {
error(p, "cannot use keyword 'parent' in a class without a parent");
}
int name = -1;
bool invoke_self = false;
if(!check(p, LPAREN_TOKEN)) {
consume(p, DOT_TOKEN, "expected '.' or '(' after parent");
consume(p, IDENTIFIER_TOKEN, "expected parent class method name after .");
name = identifier_constant(p, &p->previous);
} else {
invoke_self = true;
}
named_variable(p, synthetic_token("self"), false);
if (match(p, LPAREN_TOKEN)) {
uint8_t arg_count = argument_list(p);
named_variable(p, synthetic_token("parent"), false);
if(!invoke_self) {
emit_byte_and_short(p, OP_SUPER_INVOKE, name);
emit_byte(p, arg_count);
} else {
emit_bytes(p, OP_SUPER_INVOKE_SELF, arg_count);
}
} else {
named_variable(p, synthetic_token("parent"), false);
emit_byte_and_short(p, OP_GET_SUPER, name);
}
}
static void grouping(b_parser *p, bool can_assign) {
ignore_whitespace(p);
expression(p);
ignore_whitespace(p);
consume(p, RPAREN_TOKEN, "expected ')' after grouped expression");
}
static b_value compile_number(b_parser *p) {
if (p->previous.type == BIN_NUMBER_TOKEN) {
long long value = strtoll(p->previous.start + 2, NULL, 2);
return NUMBER_VAL(value);
} else if (p->previous.type == OCT_NUMBER_TOKEN) {
long value = strtol(p->previous.start + 2, NULL, 8);
return NUMBER_VAL(value);
} else if (p->previous.type == HEX_NUMBER_TOKEN) {
long value = strtol(p->previous.start, NULL, 16);
return NUMBER_VAL(value);
} else {
double value = strtod(p->previous.start, NULL);
return NUMBER_VAL(value);
}
}
static void number(b_parser *p, bool can_assign) {
emit_constant(p, compile_number(p));
}
// Reads the next character, which should be a hex digit (0-9, a-f, or A-F) and
// returns its numeric value. If the character isn't a hex digit, returns -1.
static int read_hex_digit(char c) {
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')