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
|
#include "parser.h"
#include "tests/tests.h"
#include "ast.h"
#include "slice.h"
#include <string.h>
enum value_type {
VALUE_IDENT,
VALUE_INT,
VALUE_BOOL,
};
struct value {
enum value_type type;
union {
struct slice ident;
int64_t integer;
bool boolean;
};
};
struct parser *parser;
static void
check_parser_errors(struct parser *parser, const char *file, int line,
const char *func)
{
if (parser->errors->len > 0) {
printf("\n");
size_t i;
char *val;
vector_foreach (parser->errors, i, val) {
printf("parser error %lu: %s\n", i, val);
}
printf(TBLD TRED "FAIL!\n" TRST);
printf("%s:%d: %s: ", file, line, func);
printf("parser encountered errors\n");
abort();
}
}
#define check_parser_errors(p) \
check_parser_errors(p, __FILE__, __LINE__, __func__)
static void
test_return_statement(struct statement *st)
{
struct slice retrn_lit = slice_fullstr("return");
struct slice st_lit = node_token_literal(st);
asserteq(st->type, STATEMENT_RETURN);
asserteq(slice_cmp(&retrn_lit, &st_lit), 0);
}
static void
test_integer_literal(struct expression *expr, int64_t val)
{
char buf[128];
struct slice sval;
asserteq(expr->type, EXPRESSION_INT);
asserteq(expr->integer.value, val);
sprintf(buf, "%ld", val);
sval = slice_fullstr(buf);
asserteq(slice_cmp(&expr->token.literal, &sval), 0);
}
static void
test_identifier(struct expression *expr, struct slice val)
{
asserteq(expr->type, EXPRESSION_IDENT);
}
static void
test_boolean_literal(struct expression *expr, bool val)
{
char *str = val ? "true" : "false";
struct slice sval = slice_fullstr(str);
asserteq(expr->type, EXPRESSION_BOOL);
asserteq(expr->boolean.value, val);
asserteq(slice_cmp(&expr->token.literal, &sval), 0);
}
#define test_literal_expression(expr, expect) _Generic((expect), \
int64_t: test_integer_literal, \
struct slice: test_identifier, \
bool: test_boolean_literal \
)(expr, expect)
static inline void
test_expected(struct expression *expr, struct value v)
{
switch(v.type) {
case VALUE_IDENT:
test_literal_expression(expr, v.ident);
break;
case VALUE_INT:
test_literal_expression(expr, v.integer);
break;
case VALUE_BOOL:
test_literal_expression(expr, v.boolean);
break;
}
}
#define VIDENT(v) \
(struct value){ .type = VALUE_IDENT, .ident = slice_fullstr(v) }
#define VINT(v) \
(struct value){ .type = VALUE_INT, .integer = v }
#define VBOOL(v) \
(struct value){ .type = VALUE_BOOL, .boolean = v }
static inline void
test_infix(struct infix_expression *expr, struct value lval, struct slice *op,
struct value rval)
{
test_expected(expr->left, lval);
asserteq(slice_cmp(&expr->operator, op), 0);
test_expected(expr->right, rval);
}
static void
test_let_statement(struct statement *st, struct slice *name)
{
struct slice let_lit = slice_fullstr("let");
struct slice st_lit = node_token_literal(st);
asserteq(slice_cmp(&let_lit, &st_lit), 0);
asserteq(st->type, STATEMENT_LET);
asserteq(slice_cmp(&st->let.name->value, name), 0);
asserteq(slice_cmp(&st->let.name->token.literal, name), 0);
}
static void
test_let_statements(void)
{
struct {
char *input;
struct slice ident;
struct value val;
} tests[] = {
{ "let x = 5;", slice_fullstr("x"), VINT(5) },
{ "let y = true;", slice_fullstr("y"), VBOOL(true) },
{ "let foo = bar;", slice_fullstr("foo"), VIDENT("bar") },
{ 0 },
};
for (size_t i = 0; tests[i].input != NULL; i++) {
parser_reset(parser, tests[i].input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
test_let_statement(st, &tests[i].ident);
test_expected(st->let.value, tests[i].val);
node_destroy(prog);
}
}
static void
test_return_statements(void)
{
struct {
char *input;
struct slice ident;
struct value val;
} tests[] = {
{ "return 5;", slice_fullstr("x"), VINT(5) },
{ "return true;", slice_fullstr("y"), VBOOL(true) },
{ "return bar;", slice_fullstr("foo"), VIDENT("bar") },
{ 0 },
};
for (size_t i = 0; tests[i].input != NULL; i++) {
parser_reset(parser, tests[i].input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
test_return_statement(st);
test_expected(st->retrn.value, tests[i].val);
node_destroy(prog);
}
}
static void
test_identifier_expression_statements(void)
{
char *input = "foobar;\n";
struct slice val = slice_fullstr("foobar");
parser_reset(parser, input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
asserteq(st->type, STATEMENT_EXPRESSION);
test_literal_expression(st->expr.expr, val);
node_destroy(prog);
}
static void
test_integer_expression_statements(void)
{
char *input = "469;\n";
int64_t val = 469;
parser_reset(parser, input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
asserteq(st->type, STATEMENT_EXPRESSION);
test_literal_expression(st->expr.expr, val);
node_destroy(prog);
}
static void
test_prefix_expression_statements(void)
{
struct {
char *input;
struct slice operator;
struct value val;
} tests[] = {
{ "!5;", slice_fullstr("!"), VINT(5) },
{ "-15;", slice_fullstr("-"), VINT(15) },
{ "!foo;", slice_fullstr("!"), VIDENT("foo") },
{ "-bar;", slice_fullstr("-"), VIDENT("bar") },
{ "!true;", slice_fullstr("!"), VBOOL(true) },
{ "!false;", slice_fullstr("!"), VBOOL(false) },
{ 0 },
};
for (size_t i = 0; tests[i].input != NULL; i++) {
parser_reset(parser, tests[i].input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
asserteq(st->type, STATEMENT_EXPRESSION);
asserteq(st->expr.expr->type, EXPRESSION_PREFIX);
test_expected(st->expr.expr->prefix.right, tests[i].val);
node_destroy(prog);
}
}
static void
test_infix_expression_statements(void)
{
struct {
char *input;
struct value lval;
struct slice operator;
struct value rval;
} tests[] = {
{ "5 + 5;", VINT(5), slice_fullstr("+"), VINT(5) },
{ "5 - 5;", VINT(5), slice_fullstr("-"), VINT(5) },
{ "5 * 5;", VINT(5), slice_fullstr("*"), VINT(5) },
{ "5 / 5;", VINT(5), slice_fullstr("/"), VINT(5) },
{ "5 > 5;", VINT(5), slice_fullstr(">"), VINT(5) },
{ "5 < 5;", VINT(5), slice_fullstr("<"), VINT(5) },
{ "foo + bar;", VIDENT("foo"), slice_fullstr("+"), VIDENT("bar") },
{ "foo - bar;", VIDENT("foo"), slice_fullstr("-"), VIDENT("bar") },
{ "foo * bar;", VIDENT("foo"), slice_fullstr("*"), VIDENT("bar") },
{ "foo / bar;", VIDENT("foo"), slice_fullstr("/"), VIDENT("bar") },
{ "foo > bar;", VIDENT("foo"), slice_fullstr(">"), VIDENT("bar") },
{ "foo < bar;", VIDENT("foo"), slice_fullstr("<"), VIDENT("bar") },
{ "true == true;", VBOOL(true), slice_fullstr("=="), VBOOL(true) },
{ "true != false;", VBOOL(true), slice_fullstr("!="), VBOOL(false) },
{ "false != false;", VBOOL(false), slice_fullstr("!="), VBOOL(false) },
{ 0 },
};
for (size_t i = 0; tests[i].input != NULL; i++) {
parser_reset(parser, tests[i].input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
asserteq(st->type, STATEMENT_EXPRESSION);
asserteq(st->expr.expr->type, EXPRESSION_INFIX);
test_infix(&st->expr.expr->infix, tests[i].lval, &tests[i].operator,
tests[i].rval);
node_destroy(prog);
}
}
static void
test_operator_precedence(void)
{
struct {
char *input;
char *expect;
} tests[] = {
{
"-a * b",
"((-a) * b)",
},
{
"!-a",
"(!(-a))",
},
{
"a + b + c",
"((a + b) + c)",
},
{
"a + b - c",
"((a + b) - c)",
},
{
"a * b * c",
"((a * b) * c)",
},
{
"a * b / c",
"((a * b) / c)",
},
{
"a + b / c",
"(a + (b / c))",
},
{
"a + b * c + d / e - f",
"(((a + (b * c)) + (d / e)) - f)",
},
{
"3 + 4; -5 * 5",
"(3 + 4)((-5) * 5)",
},
{
"5 > 4 == 3 < 4",
"((5 > 4) == (3 < 4))",
},
{
"5 < 4 != 3 > 4",
"((5 < 4) != (3 > 4))",
},
{
"3 + 4 * 5 == 3 * 1 + 4 * 5",
"((3 + (4 * 5)) == ((3 * 1) + (4 * 5)))",
},
{
"true",
"true",
},
{
"false",
"false",
},
{
"3 > 5 == false",
"((3 > 5) == false)",
},
{
"3 < 5 == true",
"((3 < 5) == true)",
},
{
"1 + (2 + 3) + 4",
"((1 + (2 + 3)) + 4)",
},
{
"(5 + 5) * 2",
"((5 + 5) * 2)",
},
{
"2 / (5 + 5)",
"(2 / (5 + 5))",
},
{
"(5 + 5) * 2 * (5 + 5)",
"(((5 + 5) * 2) * (5 + 5))",
},
{
"-(5 + 5)",
"(-(5 + 5))",
},
{
"!(true == true)",
"(!(true == true))",
},
{
"a + add(b * c) + d",
"((a + add((b * c))) + d)",
},
{
"add(a, b, 1, 2 * 3, 4 + 5, add(6, 7 * 8))",
"add(a, b, 1, (2 * 3), (4 + 5), add(6, (7 * 8)))",
},
{
"add(a + b + c * d / f + g)",
"add((((a + b) + ((c * d) / f)) + g))",
},
{ 0 },
};
for (size_t i = 0; tests[i].input != NULL; i++) {
char buf[128];
parser_reset(parser, tests[i].input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
node_sprint(prog, buf);
asserteq(strcmp(tests[i].expect, buf), 0);
node_destroy(prog);
}
}
static void
test_boolean_expression(void)
{
struct {
char *input;
bool value;
} tests[] = {
{ "true;", true },
{ "false;", false },
{ 0 },
};
for (size_t i = 0; tests[i].input != NULL; i++) {
parser_reset(parser, tests[i].input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
asserteq(st->type, STATEMENT_EXPRESSION);
test_literal_expression(st->expr.expr, tests[i].value);
node_destroy(prog);
}
}
static void
test_if_expression(void)
{
char *input = "if (x < y) { x }";
struct value x = VIDENT("x");
struct slice op = slice_fullstr("<");
struct value y = VIDENT("y");
parser_reset(parser, input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
asserteq(st->type, STATEMENT_EXPRESSION);
asserteq(st->expr.expr->type, EXPRESSION_IF);
struct if_expression *ifexpr = &st->expr.expr->cond;
asserteq(ifexpr->condition->type, EXPRESSION_INFIX);
test_infix(&ifexpr->condition->infix, x, &op, y);
asserteq(ifexpr->consequence->type, STATEMENT_BLOCK);
asserteq(ifexpr->consequence->block.statements->len, 1);
struct statement *ifst = ifexpr->consequence->block.statements->values[0];
asserteq(ifst->type, STATEMENT_EXPRESSION);
test_identifier(ifst->expr.expr, x.ident);
asserteq(ifexpr->alternative, NULL);
node_destroy(prog);
}
static void
test_if_else_expression(void)
{
char *input = "if (x < y) { x } else { y }";
struct value x = VIDENT("x");
struct slice op = slice_fullstr("<");
struct value y = VIDENT("y");
parser_reset(parser, input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
asserteq(st->type, STATEMENT_EXPRESSION);
asserteq(st->expr.expr->type, EXPRESSION_IF);
struct if_expression *ifexpr = &st->expr.expr->cond;
asserteq(ifexpr->condition->type, EXPRESSION_INFIX);
test_infix(&ifexpr->condition->infix, x, &op, y);
asserteq(ifexpr->consequence->type, STATEMENT_BLOCK);
asserteq(ifexpr->consequence->block.statements->len, 1);
struct statement *ifst = ifexpr->consequence->block.statements->values[0];
asserteq(ifst->type, STATEMENT_EXPRESSION);
test_identifier(ifst->expr.expr, x.ident);
assertneq(ifexpr->alternative, NULL);
asserteq(ifexpr->alternative->block.statements->len, 1);
asserteq(ifexpr->alternative->type, STATEMENT_BLOCK);
struct statement *elsest = ifexpr->alternative->block.statements->values[0];
asserteq(elsest->type, STATEMENT_EXPRESSION);
test_identifier(elsest->expr.expr, y.ident);
node_destroy(prog);
}
static void
test_func_literal(void)
{
char *input = "fn(x, y) { x + y; }";
struct value x = VIDENT("x");
struct slice op = slice_fullstr("+");
struct value y = VIDENT("y");
parser_reset(parser, input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
asserteq(st->type, STATEMENT_EXPRESSION);
asserteq(st->expr.expr->type, EXPRESSION_FUNC);
struct func_literal *func = &st->expr.expr->func;
asserteq(func->parameters->len, 2);
test_identifier(func->parameters->values[0], x.ident);
test_identifier(func->parameters->values[1], y.ident);
asserteq(func->body->type, STATEMENT_BLOCK);
asserteq(func->body->block.statements->len, 1);
struct statement *fst = func->body->block.statements->values[0];
test_infix(&fst->expr.expr->infix, x, &op, y);
node_destroy(prog);
}
static void
test_func_parameters(void)
{
struct {
char *input;
struct {
struct slice vals[4];
size_t len;
} params;
} tests[] = {
{
"fn() {};",
{ 0 },
},
{
"fn(x) {};",
{
{ slice_fullstr("x") },
1
},
},
{
"fn(x, y) {};",
{
{ slice_fullstr("x"), slice_fullstr("y") },
2,
}
},
{
"fn(x, y, z) {};",
{
{ slice_fullstr("x"), slice_fullstr("y"), slice_fullstr("z") },
3,
}
},
{ 0 },
};
for (size_t i = 0; tests[i].input != NULL; i++) {
parser_reset(parser, tests[i].input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
asserteq(st->type, STATEMENT_EXPRESSION);
asserteq(st->expr.expr->type, EXPRESSION_FUNC);
struct func_literal *func = &st->expr.expr->func;
asserteq(func->parameters->len, tests[i].params.len);
for (size_t j = 0; j < tests[i].params.len; j++) {
struct expression *param = func->parameters->values[j];
asserteq(slice_cmp(¶m->ident.token.literal,
&tests[i].params.vals[j]), 0);
}
node_destroy(prog);
}
}
static void
test_call_expression(void)
{
char *input = "add(1, x * y, add(x, y, z));";
int64_t par1 = 1;
struct value x = VIDENT("x");
struct value y = VIDENT("y");
struct slice op1 = slice_fullstr("*");
parser_reset(parser, input);
struct program *prog = parser_parse_program(parser);
check_parser_errors(parser);
assertneq(prog, NULL);
asserteq(prog->statements->len, 1);
struct statement *st = prog->statements->values[0];
asserteq(st->type, STATEMENT_EXPRESSION);
asserteq(st->expr.expr->type, EXPRESSION_CALL);
struct call_expression *call = &st->expr.expr->call;
test_identifier(call->func, slice_fullstr("add"));
asserteq(call->arguments->len, 3);
struct expression *expr1 = call->arguments->values[0],
*expr2 = call->arguments->values[1],
*expr3 = call->arguments->values[2];
test_literal_expression(expr1, par1);
asserteq(expr2->type, EXPRESSION_INFIX);
test_infix(&expr2->infix, x, &op1, y);
asserteq(expr3->type, EXPRESSION_CALL);
asserteq(expr3->call.arguments->len, 3);
}
static void
init(void)
{
parser = parser_new();
}
static void
cleanup(void)
{
parser_destroy(parser);
}
int
main(void)
{
INIT_TESTS();
init();
RUN_TEST(test_let_statements);
RUN_TEST(test_return_statements);
RUN_TEST(test_identifier_expression_statements);
RUN_TEST(test_integer_expression_statements);
RUN_TEST(test_prefix_expression_statements);
RUN_TEST(test_infix_expression_statements);
RUN_TEST(test_operator_precedence);
RUN_TEST(test_boolean_expression);
RUN_TEST(test_if_expression);
RUN_TEST(test_if_else_expression);
RUN_TEST(test_func_literal);
RUN_TEST(test_func_parameters);
RUN_TEST(test_call_expression);
cleanup();
}
|