-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.c
More file actions
740 lines (660 loc) · 26.7 KB
/
ast.c
File metadata and controls
740 lines (660 loc) · 26.7 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "../includes/ast.h"
#include "../includes/bytecode.h"
Ruja_Ast ast_new() {
Ruja_Ast ast = malloc(sizeof(struct Ruja_Ast_Node));
if (ast == NULL) {
fprintf(stderr, "Failed to allocate memory for ast\n");
return NULL;
}
ast->type = AST_NODE_EMPTY;
return ast;
}
void ast_free(Ruja_Ast ast) {
if (ast == NULL) return;
switch (ast->type) {
case AST_NODE_EMPTY:
break;
case AST_NODE_LITERAL:
token_free(ast->as.literal.tok_literal);
break;
case AST_NODE_IDENTIFIER:
token_free(ast->as.identifier.tok_identifier);
break;
case AST_NODE_UNARY_OP:
token_free(ast->as.unary_op.tok_unary);
ast_free(ast->as.unary_op.expression);
break;
case AST_NODE_BINARY_OP:
token_free(ast->as.binary_op.tok_binary);
ast_free(ast->as.binary_op.left_expression);
ast_free(ast->as.binary_op.right_expression);
break;
case AST_NODE_TERNARY_OP:
token_free(ast->as.ternary_op.tok_ternary.tok_question);
token_free(ast->as.ternary_op.tok_ternary.tok_colon);
ast_free(ast->as.ternary_op.condition);
ast_free(ast->as.ternary_op.true_expression);
ast_free(ast->as.ternary_op.false_expression);
break;
case AST_NODE_EXPRESSION:
ast_free(ast->as.expr.expression);
break;
case AST_NODE_STMT_ASSIGN:
token_free(ast->as.assign.tok_assign);
ast_free(ast->as.assign.identifier);
ast_free(ast->as.assign.expression);
break;
case AST_NODE_STMT_TYPED_DECL:
token_free(ast->as.typed_decl.tok_dtype);
ast_free(ast->as.typed_decl.identifier);
break;
case AST_NODE_STMT_TYPED_DECL_ASSIGN:
token_free(ast->as.typed_decl_assign.tok_dtype);
token_free(ast->as.typed_decl_assign.tok_assign);
ast_free(ast->as.typed_decl_assign.identifier);
ast_free(ast->as.typed_decl_assign.expression);
break;
case AST_NODE_STMT_INFERRED_DECL_ASSIGN:
token_free(ast->as.inferred_decl_assign.tok_assign);
ast_free(ast->as.inferred_decl_assign.identifier);
ast_free(ast->as.inferred_decl_assign.expression);
break;
case AST_NODE_STMT_IF:
token_free(ast->as.if_branch.tok_if);
ast_free(ast->as.if_branch.condition);
ast_free(ast->as.if_branch.body);
ast_free(ast->as.if_branch.next_branch);
break;
case AST_NODE_STMT_ELIF:
token_free(ast->as.elif_branch.tok_elif);
ast_free(ast->as.elif_branch.condition);
ast_free(ast->as.elif_branch.body);
ast_free(ast->as.elif_branch.next_branch);
break;
case AST_NODE_STMT_ELSE:
token_free(ast->as.else_branch.tok_else);
ast_free(ast->as.else_branch.body);
break;
case AST_NODE_RANGED_ITER:
ast_free(ast->as.ranged_iter.start_expr);
ast_free(ast->as.ranged_iter.end_expr);
ast_free(ast->as.ranged_iter.step_expr);
break;
case AST_NODE_STMT_FOR:
token_free(ast->as.for_loop.tok_for);
token_free(ast->as.for_loop.tok_in);
ast_free(ast->as.for_loop.identifier);
ast_free(ast->as.for_loop.iter);
ast_free(ast->as.for_loop.body);
break;
case AST_NODE_STMT_WHILE:
token_free(ast->as.while_loop.tok_while);
ast_free(ast->as.while_loop.condition);
ast_free(ast->as.while_loop.body);
break;
case AST_NODE_STMT_STRUCT_MEMBER:
token_free(ast->as.struct_member.tok_dtype);
ast_free(ast->as.struct_member.identifier);
ast_free(ast->as.struct_member.next_member);
break;
case AST_NODE_STMT_STRUCT_DEF:
token_free(ast->as.struct_def.tok_struct);
ast_free(ast->as.struct_def.identifier);
ast_free(ast->as.struct_def.members);
break;
case AST_NODE_STMTS:
ast_free(ast->as.stmts.statement);
ast_free(ast->as.stmts.next);
break;
}
free(ast);
}
Ruja_Ast ast_new_literal(Ruja_Token* literal_token) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_LITERAL;
ast->as.literal.tok_literal = literal_token;
literal_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_identifier(Ruja_Token* identifier_token) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_IDENTIFIER;
ast->as.identifier.tok_identifier = identifier_token;
identifier_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_unary_op(Ruja_Token* unary_token, Ruja_Ast expression) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_UNARY_OP;
ast->as.unary_op.tok_unary = unary_token;
ast->as.unary_op.expression = expression;
unary_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_binary_op(Ruja_Token* binary_token, Ruja_Ast left_expression, Ruja_Ast right_expression) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_BINARY_OP;
ast->as.binary_op.tok_binary = binary_token;
ast->as.binary_op.left_expression = left_expression;
ast->as.binary_op.right_expression = right_expression;
binary_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_ternary_op(Ruja_Token* tok_question, Ruja_Token* tok_colon, Ruja_Ast condition, Ruja_Ast true_expression, Ruja_Ast false_expression) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_TERNARY_OP;
ast->as.ternary_op.tok_ternary.tok_question = tok_question;
ast->as.ternary_op.tok_ternary.tok_colon = tok_colon;
ast->as.ternary_op.condition = condition;
ast->as.ternary_op.true_expression = true_expression;
ast->as.ternary_op.false_expression = false_expression;
tok_question->in_ast = true;
return ast;
}
Ruja_Ast ast_new_expression(Ruja_Ast expression) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_EXPRESSION;
ast->as.expr.expression = expression;
return ast;
}
Ruja_Ast ast_new_assign(Ruja_Token* assign_token, Ruja_Ast identifier, Ruja_Ast expression) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMT_ASSIGN;
ast->as.assign.tok_assign = assign_token;
ast->as.assign.identifier = identifier;
ast->as.assign.expression = expression;
assign_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_typed_decl(Ruja_Token* dtype_token, Ruja_Ast identifier) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMT_TYPED_DECL;
ast->as.typed_decl.tok_dtype = dtype_token;
ast->as.typed_decl.identifier = identifier;
dtype_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_typed_decl_assign(Ruja_Token* dtype_token, Ruja_Token* assign_token, Ruja_Ast identifier, Ruja_Ast expression) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMT_TYPED_DECL_ASSIGN;
ast->as.typed_decl_assign.tok_dtype = dtype_token;
ast->as.typed_decl_assign.tok_assign = assign_token;
ast->as.typed_decl_assign.identifier = identifier;
ast->as.typed_decl_assign.expression = expression;
dtype_token->in_ast = true;
assign_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_inferred_decl_assign(Ruja_Token* assign_token, Ruja_Ast identifier, Ruja_Ast expression) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMT_INFERRED_DECL_ASSIGN;
ast->as.inferred_decl_assign.tok_assign = assign_token;
ast->as.inferred_decl_assign.identifier = identifier;
ast->as.inferred_decl_assign.expression = expression;
assign_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_if_stmt(Ruja_Token* if_token, Ruja_Ast condition, Ruja_Ast body, Ruja_Ast next_branch) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMT_IF;
ast->as.if_branch.tok_if = if_token;
ast->as.if_branch.condition = condition;
ast->as.if_branch.body = body;
ast->as.if_branch.next_branch = next_branch;
if_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_elif_stmt(Ruja_Token* elif_token, Ruja_Ast condition, Ruja_Ast body, Ruja_Ast else_stmt) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMT_ELIF;
ast->as.elif_branch.tok_elif = elif_token;
ast->as.elif_branch.condition = condition;
ast->as.elif_branch.body = body;
ast->as.elif_branch.next_branch = else_stmt;
elif_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_else_stmt(Ruja_Token* else_token, Ruja_Ast body) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMT_ELSE;
ast->as.else_branch.tok_else = else_token;
ast->as.else_branch.body = body;
else_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_ranged_iter(Ruja_Ast start_expr, Ruja_Ast end_expr, Ruja_Ast step_expr) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_RANGED_ITER;
ast->as.ranged_iter.start_expr = start_expr;
ast->as.ranged_iter.end_expr = end_expr;
ast->as.ranged_iter.step_expr = step_expr;
return ast;
}
Ruja_Ast ast_new_for_loop(Ruja_Token* for_token, Ruja_Ast identifier, Ruja_Ast iter, Ruja_Ast body) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMT_FOR;
ast->as.for_loop.tok_for = for_token;
ast->as.for_loop.tok_in = NULL;
ast->as.for_loop.identifier = identifier;
ast->as.for_loop.iter = iter;
ast->as.for_loop.body = body;
for_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_while_loop(Ruja_Token* while_token, Ruja_Ast condition, Ruja_Ast body) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMT_WHILE;
ast->as.while_loop.tok_while = while_token;
ast->as.while_loop.condition = condition;
ast->as.while_loop.body = body;
while_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_struct_members(Ruja_Ast identifier_token, Ruja_Ast next_member) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMT_STRUCT_MEMBER;
ast->as.struct_member.tok_dtype = NULL;
ast->as.struct_member.identifier = identifier_token;
ast->as.struct_member.next_member = next_member;
return ast;
}
Ruja_Ast ast_new_struct_def(Ruja_Token* struct_token, Ruja_Ast identifier_token, Ruja_Ast members) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMT_STRUCT_DEF;
ast->as.struct_def.tok_struct = struct_token;
ast->as.struct_def.identifier = identifier_token;
ast->as.struct_def.members = members;
struct_token->in_ast = true;
return ast;
}
Ruja_Ast ast_new_stmt(Ruja_Ast statement, Ruja_Ast next) {
Ruja_Ast ast = ast_new();
if (ast == NULL) return NULL;
ast->type = AST_NODE_STMTS;
ast->as.stmts.statement = statement;
ast->as.stmts.next = next;
return ast;
}
static const char* assign_to_string(Ruja_Token_Kind kind) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-enum"
switch (kind) {
case RUJA_TOK_ASSIGN:
return "=";
case RUJA_TOK_ADD_EQ:
return "+=";
case RUJA_TOK_SUB_EQ:
return "-=";
case RUJA_TOK_MUL_EQ:
return "*=";
case RUJA_TOK_DIV_EQ:
return "/=";
default:
return "unknown";
}
#pragma GCC diagnostic pop
}
static const char* type_to_string(Ruja_Token_Kind kind) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-enum"
switch (kind) {
case RUJA_TOK_TYPE_BOOL:
return "bool";
case RUJA_TOK_TYPE_CHAR:
return "char";
case RUJA_TOK_TYPE_I32:
return "int";
case RUJA_TOK_TYPE_F64:
return "float";
case RUJA_TOK_TYPE_STRING:
return "string";
default:
return "unknown";
}
#pragma GCC diagnostic pop
}
/**
* @brief Convert an ast_unary_op_type to a string
*
* @param type
* @return const char*
*/
const char *unary_token_kind_to_string(Ruja_Token_Kind kind) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-enum"
switch (kind) {
case RUJA_TOK_NOT:
return "not";
case RUJA_TOK_SUB:
return "-";
default:
return "unknown";
}
#pragma GCC diagnostic pop
}
/**
* @brief Convert an ast_binary_op_type to a string
*
* @param type
* @return const char*
*/
const char *binary_token_kind_to_string(Ruja_Token_Kind kind) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-enum"
switch (kind) {
case RUJA_TOK_ADD:
return "+";
case RUJA_TOK_SUB:
return "-";
case RUJA_TOK_MUL:
return "*";
case RUJA_TOK_DIV:
return "/";
case RUJA_TOK_AND:
return "and";
case RUJA_TOK_OR:
return "or";
case RUJA_TOK_EQ:
return "==";
case RUJA_TOK_NE:
return "!=";
case RUJA_TOK_LT:
return "<";
case RUJA_TOK_LE:
return "<=";
case RUJA_TOK_GT:
return ">";
case RUJA_TOK_GE:
return ">=";
default:
return "unknown";
}
#pragma GCC diagnostic pop
}
/**
* @brief Increment an id and return the new value
*
*
* @param type
* @return size_t The new value of the id
*/
static size_t increment(size_t* id) {
return ++(*id);
}
/**
* @brief Print an ast node to a file in dot format
*
* @param file The file pointer
* @param id The id of the node
* @param label The label of the node
* @param color The color of the node
* @param style The style of the node
*/
static void dot_node(FILE* file, size_t id, const char* label, const char* color, const char* style) {
fprintf(file, " %zu [label=\"%s\", fillcolor=\"%s\", style=\"%s\"];\n", id, label, color?color:"black", style?style:"");
}
static void print_token_word(FILE* file, Ruja_Token* token) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch-enum"
switch (token->kind) {
case RUJA_TOK_INT: fprintf(file, "%ld", strtol(token->start, NULL, 10)); break;
case RUJA_TOK_FLOAT: fprintf(file, "%lf", strtod(token->start, NULL)); break;
case RUJA_TOK_CHAR: fprintf(file, "'%c'", *(token->start)); break;
case RUJA_TOK_TRUE: fprintf(file, "%.*s", (int) token->length, token->start); break;
case RUJA_TOK_FALSE: fprintf(file, "%.*s", (int) token->length, token->start); break;
case RUJA_TOK_ID: fprintf(file, "%.*s", (int) token->length, token->start); break;
case RUJA_TOK_STRING: fprintf(file, "\\\"%.*s\\\"", (int) token->length, token->start); break;
case RUJA_TOK_NIL: fprintf(file, "%.*s", (int) token->length, token->start); break;
default: fprintf(file, "UNKNOWN");
}
#pragma GCC diagnostic pop
}
/**
* @brief Print an ast word node to a file in dot format
*
* @param file The file pointer
* @param id The id of the node
* @param word The word of the node
* @param color The color of the node
* @param style The style of the node
*/
static void dot_node_word(FILE* file, size_t id, Ruja_Token* token, const char* color, const char* style) {
fprintf(file, " %zu [label=\"", id);
print_token_word(file, token);
fprintf(file, "\", fillcolor=\"%s\", style=\"%s\"];\n", color?color:"black", style?style:"");
}
/**
* @brief Print an ast arrow to a file in dot format
*
* @param file The file pointer
* @param from The id of the node the arrow is coming from
* @param to The id of the node the arrow is going to
* @param label The label of the arrow
*/
static void dot_arrow(FILE* file, size_t from, size_t to, const char* label) {
fprintf(file, " %zu -> %zu [label=\"%s\"];\n", from, to, label);
}
/**
* @brief Internal recursive function to print an ast to a file in dot format
*
* @param ast The ast to print
* @param file The file pointer
*/
static void ast_dot_internal(Ruja_Ast ast, FILE* file, size_t* id) {
// Dark colors
#define DARK_BLUE "#0000CC"
#define DARK_GREEN "#00CC00"
#define DARK_RED "#CC0000"
// Light colors
#define LOOP_COLOR "#F391F6"
#define BRANCH_COLOR "#91F6F6"
#define STATEMENT_COLOR "#F6CD91"
#define EXPRESSION_COLOR "#CCE6FF"
#define LITERAL_COLOR "#CCFFCC"
#define ARITHMETIC_COLOR "#FFCCCC"
#define IDENTIFIER_COLOR "#FFFFCC"
if (ast == NULL) return;
size_t root_id = *id;
switch (ast->type) {
case AST_NODE_EMPTY:
dot_node(file, root_id, "Empty", DARK_RED, "filled");
break;
case AST_NODE_LITERAL:
dot_node(file, root_id, "Literal", EXPRESSION_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "value");
dot_node_word(file, *id, ast->as.literal.tok_literal, LITERAL_COLOR, "filled");
break;
case AST_NODE_IDENTIFIER:
dot_node(file, root_id, "Identifier", EXPRESSION_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "name");
dot_node_word(file, *id, ast->as.literal.tok_literal, IDENTIFIER_COLOR, "filled");
break;
case AST_NODE_UNARY_OP:
dot_node(file, root_id, "UnaryOp", EXPRESSION_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "type");
dot_node(file, *id, unary_token_kind_to_string(ast->as.unary_op.tok_unary->kind), ARITHMETIC_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "expression");
ast_dot_internal(ast->as.unary_op.expression, file, id);
break;
case AST_NODE_BINARY_OP:
dot_node(file, root_id, "BinaryOp", EXPRESSION_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "left_expression");
ast_dot_internal(ast->as.binary_op.left_expression, file, id);
dot_arrow(file, root_id, increment(id), "type");
dot_node(file, *id, binary_token_kind_to_string(ast->as.binary_op.tok_binary->kind), ARITHMETIC_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "right_expression");
ast_dot_internal(ast->as.binary_op.right_expression, file, id);
break;
case AST_NODE_TERNARY_OP:
dot_node(file, root_id, "TernaryOp", EXPRESSION_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "condition");
ast_dot_internal(ast->as.ternary_op.condition, file, id);
dot_arrow(file, root_id, increment(id), "true_expression");
ast_dot_internal(ast->as.ternary_op.true_expression, file, id);
dot_arrow(file, root_id, increment(id), "false_expression");
ast_dot_internal(ast->as.ternary_op.false_expression, file, id);
break;
case AST_NODE_EXPRESSION:
dot_node(file, root_id, "Expression", EXPRESSION_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "expression");
ast_dot_internal(ast->as.expr.expression, file, id);
break;
case AST_NODE_STMT_ASSIGN:
dot_node(file, root_id, "Assignment", STATEMENT_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "assign_type");
dot_node(file, *id, assign_to_string(ast->as.assign.tok_assign->kind), ARITHMETIC_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "identifier");
ast_dot_internal(ast->as.assign.identifier, file, id);
dot_arrow(file, root_id, increment(id), "expression");
ast_dot_internal(ast->as.assign.expression, file, id);
break;
case AST_NODE_STMT_TYPED_DECL:
dot_node(file, root_id, "TypedDeclaration", STATEMENT_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "type");
dot_node(file, *id, type_to_string(ast->as.typed_decl.tok_dtype->kind), ARITHMETIC_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "identifier");
ast_dot_internal(ast->as.typed_decl.identifier, file, id);
break;
case AST_NODE_STMT_TYPED_DECL_ASSIGN:
dot_node(file, root_id, "TypedDeclarationAssignment", STATEMENT_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "type");
dot_node(file, *id, type_to_string(ast->as.typed_decl_assign.tok_dtype->kind), ARITHMETIC_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "assign");
dot_node(file, *id, assign_to_string(ast->as.typed_decl_assign.tok_assign->kind), ARITHMETIC_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "identifier");
ast_dot_internal(ast->as.typed_decl_assign.identifier, file, id);
dot_arrow(file, root_id, increment(id), "expression");
ast_dot_internal(ast->as.typed_decl_assign.expression, file, id);
break;
case AST_NODE_STMT_INFERRED_DECL_ASSIGN:
dot_node(file, root_id, "InferredDeclarationAssignment", STATEMENT_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "identifier");
ast_dot_internal(ast->as.inferred_decl_assign.identifier, file, id);
dot_arrow(file, root_id, increment(id), "assign");
dot_node(file, *id, assign_to_string(ast->as.inferred_decl_assign.tok_assign->kind), ARITHMETIC_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "expression");
ast_dot_internal(ast->as.inferred_decl_assign.expression, file, id);
break;
case AST_NODE_STMT_IF:
dot_node(file, root_id, "IfBranch", BRANCH_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "condition");
ast_dot_internal(ast->as.if_branch.condition, file, id);
dot_arrow(file, root_id, increment(id), "body");
ast_dot_internal(ast->as.if_branch.body, file, id);
if (ast->as.if_branch.next_branch != NULL) {
dot_arrow(file, root_id, increment(id), "next");
ast_dot_internal(ast->as.if_branch.next_branch, file, id);
}
break;
case AST_NODE_STMT_ELIF:
dot_node(file, root_id, "ElifBranch", BRANCH_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "condition");
ast_dot_internal(ast->as.elif_branch.condition, file, id);
dot_arrow(file, root_id, increment(id), "body");
ast_dot_internal(ast->as.elif_branch.body, file, id);
if (ast->as.elif_branch.next_branch != NULL) {
dot_arrow(file, root_id, increment(id), "next");
ast_dot_internal(ast->as.elif_branch.next_branch, file, id);
}
break;
case AST_NODE_STMT_ELSE:
dot_node(file, root_id, "ElseBranch", BRANCH_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "body");
ast_dot_internal(ast->as.else_branch.body, file, id);
break;
case AST_NODE_RANGED_ITER:
dot_node(file, root_id, "RangedIteration", STATEMENT_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "start");
ast_dot_internal(ast->as.ranged_iter.start_expr, file, id);
dot_arrow(file, root_id, increment(id), "end");
ast_dot_internal(ast->as.ranged_iter.end_expr, file, id);
if (ast->as.ranged_iter.step_expr != NULL) {
dot_arrow(file, root_id, increment(id), "step");
ast_dot_internal(ast->as.ranged_iter.step_expr, file, id);
}
break;
case AST_NODE_STMT_FOR:
dot_node(file, root_id, "ForLoop", LOOP_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "identifier");
ast_dot_internal(ast->as.for_loop.identifier, file, id);
dot_arrow(file, root_id, increment(id), "iterable");
ast_dot_internal(ast->as.for_loop.iter, file, id);
dot_arrow(file, root_id, increment(id), "body");
ast_dot_internal(ast->as.for_loop.body, file, id);
break;
case AST_NODE_STMT_WHILE:
dot_node(file, root_id, "WhileLoop", LOOP_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "condition");
ast_dot_internal(ast->as.while_loop.condition, file, id);
dot_arrow(file, root_id, increment(id), "body");
ast_dot_internal(ast->as.while_loop.body, file, id);
break;
case AST_NODE_STMT_STRUCT_MEMBER:
dot_node(file, root_id, "StructMember", STATEMENT_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "type");
dot_node(file, *id, type_to_string(ast->as.struct_member.tok_dtype->kind), ARITHMETIC_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "identifier");
ast_dot_internal(ast->as.struct_member.identifier, file, id);
if (ast->as.struct_member.next_member != NULL) {
dot_arrow(file, root_id, increment(id), "next");
ast_dot_internal(ast->as.struct_member.next_member, file, id);
}
break;
case AST_NODE_STMT_STRUCT_DEF:
dot_node(file, root_id, "StructDefinition", STATEMENT_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "identifier");
ast_dot_internal(ast->as.struct_def.identifier, file, id);
dot_arrow(file, root_id, increment(id), "members");
ast_dot_internal(ast->as.struct_def.members, file, id);
break;
case AST_NODE_STMTS:
dot_node(file, root_id, "Statements", STATEMENT_COLOR, "filled");
dot_arrow(file, root_id, increment(id), "statement");
ast_dot_internal(ast->as.stmts.statement, file, id);
if (ast->as.stmts.next != NULL) {
dot_arrow(file, root_id, increment(id), "next");
ast_dot_internal(ast->as.stmts.next, file, id);
}
break;
}
// Dark colors
#undef DARK_BLUE
#undef DARK_GREEN
#undef DARK_RED
// Light colors
#undef LOOP_COLOR
#undef BRANCH_COLOR
#undef STATEMENT_COLOR
#undef EXPRESSION_COLOR
#undef LITERAL_COLOR
#undef ARITHMETIC_COLOR
#undef IDENTIFIER_COLOR
}
void ast_dot(Ruja_Ast ast, FILE *file) {
fprintf(file, "digraph ast {\n");
fprintf(file, " graph [rankdir=LR];\n");
fprintf(file, " node [shape=box];\n");
size_t id = 0;
ast_dot_internal(ast, file, &id);
fprintf(file, "}\n");
}