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
|
/*
* abstract syntax tree types for a catskill source.
*/
enum Unary_Operation
{
UNARY_NONE,
UNARY_MINUS,
UNARY_NOT,
UNARY_BITWISE_NOT,
};
enum Unary_Operation
unary_operation_from_token(const struct Token* token)
{
switch (token->kind) {
case TOKEN_MINUS:
return UNARY_MINUS;
case TOKEN_NOT:
return UNARY_NOT;
// TODO: tilde token
// case TOKEN_TILDE:
// return UNARY_BITWISE_NOT;
default:
return UNARY_NONE;
}
}
const ascii*
unary_operation_to_string(enum Unary_Operation operation)
{
switch (operation) {
case UNARY_MINUS:
return "-";
case UNARY_NOT:
return "!";
case UNARY_BITWISE_NOT:
return "~";
default:
return "unknown";
}
}
enum Binary_Operation
{
BINARY_NONE,
BINARY_PLUS,
BINARY_MINUS,
BINARY_MULTIPLY,
BINARY_DIVIDE,
BINARY_MODULO,
BINARY_EQUAL,
BINARY_NOT_EQUAL,
BINARY_GREATER_THAN,
BINARY_GREATER_THAN_EQUAL,
BINARY_LESS_THAN,
BINARY_LESS_THAN_EQUAL,
BINARY_AND,
BINARY_OR,
BINARY_BITWISE_AND,
BINARY_BITWISE_OR,
BINARY_BITWISE_XOR,
BINARY_BITWISE_LEFT_SHIFT,
BINARY_BITWISE_RIGHT_SHIFT,
};
enum Binary_Operation
binary_operation_from_token(const struct Token* token)
{
switch (token->kind) {
case TOKEN_PLUS:
return BINARY_PLUS;
case TOKEN_MINUS:
return BINARY_MINUS;
case TOKEN_STAR:
return BINARY_MULTIPLY;
case TOKEN_SLASH:
return BINARY_DIVIDE;
// TODO: percent token
// case TOKEN_PERCENT:
// return BINARY_MODULO;
case TOKEN_EQUAL:
return BINARY_EQUAL;
case TOKEN_NOT_EQUAL:
return BINARY_NOT_EQUAL;
case TOKEN_GREATER:
return BINARY_GREATER_THAN;
case TOKEN_GREATER_EQUAL:
return BINARY_GREATER_THAN_EQUAL;
case TOKEN_LESS:
return BINARY_LESS_THAN;
case TOKEN_LESS_EQUAL:
return BINARY_LESS_THAN_EQUAL;
case TOKEN_AND:
return BINARY_AND;
case TOKEN_OR:
return BINARY_OR;
default:
return BINARY_NONE;
}
}
// return the precedence of the given binary operation.
// lower values are weaker, with 1 being the weakest.
// the values are taken mostly the same as other C-family languages.
uint
binary_operation_precedence(enum Binary_Operation operation)
{
switch (operation) {
// weakest
case BINARY_OR:
return 1;
case BINARY_AND:
return 2;
case BINARY_BITWISE_OR:
return 3;
case BINARY_BITWISE_XOR:
return 4;
case BINARY_BITWISE_AND:
return 5;
case BINARY_EQUAL:
case BINARY_NOT_EQUAL:
return 6;
case BINARY_GREATER_THAN:
case BINARY_GREATER_THAN_EQUAL:
case BINARY_LESS_THAN:
case BINARY_LESS_THAN_EQUAL:
return 7;
case BINARY_BITWISE_LEFT_SHIFT:
case BINARY_BITWISE_RIGHT_SHIFT:
return 8;
case BINARY_PLUS:
case BINARY_MINUS:
return 9;
case BINARY_MULTIPLY:
case BINARY_DIVIDE:
case BINARY_MODULO:
return 10;
// strongest
default:
return 0;
}
}
enum Binary_Operation_Associativity
{
BINARY_ASSOCIATIVITY_NONE,
BINARY_ASSOCIATIVITY_LEFT,
BINARY_ASSOCIATIVITY_RIGHT,
};
enum Binary_Operation_Associativity
binary_operation_associativity(enum Binary_Operation operation)
{
// all operations are left associative by default.
return BINARY_ASSOCIATIVITY_LEFT;
}
const ascii*
binary_operation_to_string(enum Binary_Operation operation)
{
switch (operation) {
case BINARY_PLUS:
return "+";
case BINARY_MINUS:
return "-";
case BINARY_MULTIPLY:
return "*";
case BINARY_DIVIDE:
return "/";
case BINARY_MODULO:
return "%";
case BINARY_EQUAL:
return "==";
case BINARY_NOT_EQUAL:
return "!=";
case BINARY_GREATER_THAN:
return ">";
case BINARY_GREATER_THAN_EQUAL:
return ">=";
case BINARY_LESS_THAN:
return "<";
case BINARY_LESS_THAN_EQUAL:
return "<=";
case BINARY_AND:
return "&&";
case BINARY_OR:
return "||";
case BINARY_BITWISE_AND:
return "&";
case BINARY_BITWISE_OR:
return "|";
case BINARY_BITWISE_XOR:
return "^";
case BINARY_BITWISE_LEFT_SHIFT:
return "<<";
case BINARY_BITWISE_RIGHT_SHIFT:
return ">>";
default:
return "unknown";
}
}
enum Expression_Kind
{
EXPRESSION_NONE,
EXPRESSION_INTEGER_LITERAL,
EXPRESSION_FLOAT_LITERAL,
EXPRESSION_STRING_LITERAL,
EXPRESSION_BOOLEAN_LITERAL,
EXPRESSION_NAME,
EXPRESSION_UNARY_OPERATION,
EXPRESSION_BINARY_OPERATION,
EXPRESSION_GROUP,
EXPRESSION_CALL,
EXPRESSION_SUBSCRIPT,
EXPRESSION_MEMBER,
};
struct Expression_Integer_Literal
{
int64 value; // might not fit entire number given in source.
};
struct Expression_Float_Literal
{
float64 value;
};
struct Expression_String_Literal
{
struct String value;
};
struct Expression_Bool_Literal
{
bool value;
};
struct Expression_Name
{
struct String name;
};
struct Expression_Unary_Operator
{
enum Unary_Operation operation;
struct Expression* operand;
};
struct Expression_Binary_Operator
{
enum Binary_Operation operation;
struct Expression* left_operand;
struct Expression* right_operand;
};
struct Expression_Group
{
struct Expression* inner_expression;
};
struct Expression_Call
{
struct Expression* subject;
struct Expression* arguments; // linked list of expressions.
};
struct Expression_Subscript
{
struct Expression* subject;
struct Expression* index;
};
struct Expression_Member
{
struct Expression* subject;
struct String name;
};
union Expression_Value
{
struct Expression_Integer_Literal integer_literal;
struct Expression_Float_Literal float_literal;
struct Expression_String_Literal string_literal;
struct Expression_Bool_Literal bool_literal;
struct Expression_Name name;
struct Expression_Unary_Operator unary_operator;
struct Expression_Binary_Operator binary_operator;
struct Expression_Group group;
struct Expression_Call call;
struct Expression_Subscript subscript;
struct Expression_Member member;
};
struct Expression
{
enum Expression_Kind kind;
union Expression_Value value;
struct Span span;
struct Cursor location;
// if expression is within a group of multiple expressions,
// points to the next expression within it.
struct Expression* next;
};
REGION(struct Expression, expression)
struct Expression*
expression_new(
enum Expression_Kind kind, union Expression_Value value, struct Span span,
struct Cursor location)
{
check(region_expression_cursor < REGION_SIZE, "out of expression memory");
struct Expression* expression = ®ion_expression[region_expression_cursor++];
*expression = (struct Expression){
.kind = kind,
.value = value,
.span = span,
.location = location,
.next = nil,
};
return expression;
}
void
expression_print(const struct Expression* expression)
{
printf("(expr ");
switch (expression->kind) {
case EXPRESSION_NONE:
printf("none");
break;
case EXPRESSION_INTEGER_LITERAL:
printf("%ld", expression->value.integer_literal.value);
break;
case EXPRESSION_FLOAT_LITERAL:
printf("%lf", expression->value.float_literal.value);
break;
case EXPRESSION_STRING_LITERAL:
printf("\"%s\"", expression->value.string_literal.value.data);
break;
case EXPRESSION_BOOLEAN_LITERAL:
printf("%s", expression->value.bool_literal.value ? "true" : "false");
break;
case EXPRESSION_NAME:
printf("(name %s)", expression->value.name.name.data);
break;
case EXPRESSION_UNARY_OPERATION:
printf("(unary %s ", unary_operation_to_string(expression->value.unary_operator.operation));
expression_print(expression->value.unary_operator.operand);
printf(")");
break;
case EXPRESSION_BINARY_OPERATION:
printf(
"(binary %s ", binary_operation_to_string(expression->value.binary_operator.operation));
expression_print(expression->value.binary_operator.left_operand);
printf(" ");
expression_print(expression->value.binary_operator.right_operand);
printf(")");
break;
case EXPRESSION_GROUP:
printf("(group ");
expression_print(expression->value.group.inner_expression);
printf(")");
break;
case EXPRESSION_CALL:
printf("(call ");
expression_print(expression->value.call.subject);
FOR_EACH(struct Expression*, argument, expression->value.call.arguments)
{
printf(" ");
expression_print(argument);
}
printf(")");
break;
case EXPRESSION_SUBSCRIPT:
printf("(subscript ");
expression_print(expression->value.subscript.subject);
printf(" ");
expression_print(expression->value.subscript.index);
printf(")");
break;
case EXPRESSION_MEMBER:
printf("(member ");
expression_print(expression->value.member.subject);
printf(")");
break;
default:
break;
}
printf(")");
}
enum Statement_Kind
{
STATEMENT_NONE,
STATEMENT_EXPRESSION,
};
union Statement_Value
{
struct Expression* expression;
};
struct Statement
{
enum Statement_Kind kind;
union Statement_Value value;
struct Span span;
struct Cursor location;
// if statement is within a group of multiple statements,
// points to the next statement within it.
struct Statement* next;
};
REGION(struct Statement, statement)
struct Statement*
statement_new(
enum Statement_Kind kind, union Statement_Value value, struct Span span, struct Cursor location)
{
check(region_statement_cursor < REGION_SIZE, "out of statement memory");
struct Statement* statement = ®ion_statement[region_statement_cursor++];
*statement = (struct Statement){
.kind = kind,
.value = value,
.span = span,
.location = location,
.next = nil,
};
return statement;
}
void
statement_print(const struct Statement* statement)
{
printf("(stmt ");
switch (statement->kind) {
case STATEMENT_NONE:
printf("none");
break;
case STATEMENT_EXPRESSION: {
const struct Expression* expression = statement->value.expression;
expression_print(expression);
break;
}
default:
printf("unknown");
break;
}
printf(")");
}
// the top-level tree of a single catskill source file.
struct Tree
{
struct Statement* top_level_statements;
};
void
tree_print(const struct Tree* tree)
{
FOR_EACH(struct Statement*, statement, tree->top_level_statements)
{
statement_print(statement);
printf("\n");
}
}
|