about summary refs log tree commit diff
path: root/boot/transpile.c
blob: b1171d76abf51fdf694f4af006b1bfcd02259f16 (plain)
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
/*
 * transpiler from catskill to the c programming language.
 * takes a direct catskill syntax tree and produces a c source file,
 * without an intermediate representation.
 *
 * Copyright (c) 2025-2026, Mel G. <mel@rnrd.eu>
 *
 * SPDX-License-Identifier: MPL-2.0
 */

#pragma once

#include "catboot.h"

struct Transpile_Context
{
    bool in_function;
    struct String function_name;
    struct String function_return_type;
    bool main_function_found;
    bool main_function_takes_args;
};

#define CONTEXT_START(name)        \
    context = transpiler->context; \
    transpiler->context.name = true;

#define CONTEXT_END(name) transpiler->context.name = context.name;

struct Transpiler
{
    FILE* output;
    struct Transpile_Context context;
};

#define TRANSPILER_PREAMBLE                                 \
    DATA_FOR_VISIT(struct Transpiler, transpiler)           \
    struct Transpile_Context context = transpiler->context; \
    (void)context;

void
transpiler_new(struct Transpiler* transpiler, FILE* output)
{
    *transpiler = (struct Transpiler){
        .output = output,
        .context = {
            .in_function = false,
            .function_name = string_empty(),
            .function_return_type = string_empty(),
            .main_function_found = false,
            .main_function_takes_args = false,
        },
    };
}

void
transpiler_visit_type_node(struct Visit* visit, struct Type_Node* node)
{
    TRANSPILER_PREAMBLE

    if (!node || node->type == TYPE_NODE_NONE) {
        fprintf(transpiler->output, "void");
        return;
    }

    switch (node->type) {
    case TYPE_NODE_NAME: {
        struct String name = node->value.name.name;
        if (strcmp(name.data, "int") == 0) {
            fprintf(transpiler->output, "integer");
        } else if (strcmp(name.data, "string") == 0) {
            fprintf(transpiler->output, "struct String");
        } else if (strcmp(name.data, "bool") == 0) {
            fprintf(transpiler->output, "bool");
        } else if (strcmp(name.data, "float") == 0) {
            fprintf(transpiler->output, "real");
        } else if (strcmp(name.data, "uint") == 0) {
            fprintf(transpiler->output, "uint");
        } else if (strcmp(name.data, "byte") == 0) {
            fprintf(transpiler->output, "byte");
        } else if (strcmp(name.data, "ascii") == 0) {
            fprintf(transpiler->output, "ascii");
        } else {
            fprintf(transpiler->output, "%.*s", (int)name.length, name.data);
        }
        break;
    }
    default:
        fprintf(transpiler->output, "/* unknown type %d */", node->type);
        break;
    }
}

void
transpiler_visit_function_header_node(struct Visit* visit, struct Function_Header_Node* header)
{
    TRANSPILER_PREAMBLE

    fprintf(transpiler->output, "(");
    struct Type_Node* param = header->parameters_type_and_name;
    if (!param) {
        fprintf(transpiler->output, "void");
    } else {
        bool first = true;
        while (param) {
            if (!first) { fprintf(transpiler->output, ", "); }
            VISIT(visit_type_node, param);
            fprintf(
                transpiler->output, " %.*s", (int)param->value_name.length, param->value_name.data);
            first = false;
            param = param->next;
        }
    }
    fprintf(transpiler->output, ")");
}

void
transpiler_visit_block_node(struct Visit* visit, struct Block_Node* node)
{
    TRANSPILER_PREAMBLE

    fprintf(transpiler->output, "{\n");
    FOR_EACH (struct Statement*, statement, node->statements) { VISIT(visit_statement, statement); }
    fprintf(transpiler->output, "}\n");
}

void
transpiler_visit_argument_group_node(struct Visit* visit, struct Argument_Group_Node* node)
{
    TRANSPILER_PREAMBLE

    struct Expression* arg = node->arguments;
    bool first = true;
    while (arg) {
        if (!first) { fprintf(transpiler->output, ", "); }
        VISIT(visit_expression, arg);
        first = false;
        arg = arg->next;
    }
}

void
transpiler_visit_statement_declaration(struct Visit* visit, struct Statement* stmt)
{
    TRANSPILER_PREAMBLE

    struct Statement_Value_Declaration* declaration = &stmt->value.declaration;
    struct Expression* initializer = declaration->inner.initializer;

    if (initializer && initializer->kind == EXPRESSION_FUNCTION) {
        struct Expression_Function* fun = &initializer->value.function;
        struct Function_Header_Node* header = &fun->header;

        VISIT(visit_type_node, header->return_type);
        struct String name = *array_at(struct String, &declaration->inner.names, 0);
        fprintf(transpiler->output, " %.*s", (int)name.length, name.data);
        VISIT(visit_function_header_node, header);
        fprintf(transpiler->output, " ");
        VISIT(visit_block_node, &fun->body);
    } else {
        if (declaration->kind == STATEMENT_DECLARATION_CONSTANT) {
            fprintf(transpiler->output, "const ");
        }
        VISIT(visit_type_node, declaration->inner.type);
        struct String name = *array_at(struct String, &declaration->inner.names, 0);
        fprintf(transpiler->output, " %.*s", (int)name.length, name.data);
        if (initializer) {
            fprintf(transpiler->output, " = ");
            VISIT(visit_expression, initializer);
        }
    }
}

void
transpiler_visit_statement_return(struct Visit* visit, struct Statement* stmt)
{
    TRANSPILER_PREAMBLE

    fprintf(transpiler->output, "return");
    if (stmt->value.return_value.value) {
        fprintf(transpiler->output, " ");
        VISIT(visit_expression, stmt->value.return_value.value);
    }
}

void
transpiler_visit_statement_conditional(struct Visit* visit, struct Statement* stmt)
{
    TRANSPILER_PREAMBLE

    struct Statement_Value_Conditional* conditional = &stmt->value.conditional;
    for (uint i = 0; i < conditional->condition_count; ++i) {
        struct Statement_Conditional_Branch* branch = &conditional->conditions[i];
        if (i > 0) { fprintf(transpiler->output, "else "); }
        if (branch->when) {
            fprintf(transpiler->output, "if (");
            VISIT(visit_expression, branch->when);
            fprintf(transpiler->output, ") ");
        }
        VISIT(visit_block_node, &branch->then);
    }
}

void
transpiler_visit_statement(struct Visit* visit, struct Statement* statement)
{
    TRANSPILER_PREAMBLE

    switch (statement->kind) {
    case STATEMENT_EXPRESSION:
        VISIT(visit_expression, statement->value.expression.inner);
        if (transpiler->context.in_function) fprintf(transpiler->output, ";\n");
        break;
    case STATEMENT_DECLARATION:
        VISIT(visit_statement_declaration, statement);
        if (transpiler->context.in_function) fprintf(transpiler->output, ";\n");
        break;
    case STATEMENT_RETURN:
        VISIT(visit_statement_return, statement);
        if (transpiler->context.in_function) fprintf(transpiler->output, ";\n");
        break;
    case STATEMENT_BLOCK:
        VISIT(visit_block_node, &statement->value.block.inner);
        break;
    default:
        walk_statement(visit, statement);
        break;
    }
}

void
transpiler_visit_expression_integer_literal(struct Visit* visit, struct Expression* expr)
{
    TRANSPILER_PREAMBLE

    fprintf(transpiler->output, "%ld", expr->value.integer_literal.value);
}

void
transpiler_visit_expression_float_literal(struct Visit* visit, struct Expression* expr)
{
    TRANSPILER_PREAMBLE

    fprintf(transpiler->output, "%f", expr->value.float_literal.value);
}

void
transpiler_visit_expression_string_literal(struct Visit* visit, struct Expression* expr)
{
    TRANSPILER_PREAMBLE

    fprintf(transpiler->output, "\"%.*s\"", (int)expr->value.string_literal.value.length,
            expr->value.string_literal.value.data);
}

void
transpiler_visit_expression_boolean_literal(struct Visit* visit, struct Expression* expr)
{
    TRANSPILER_PREAMBLE

    fprintf(transpiler->output, "%s", expr->value.bool_literal.value ? "true" : "false");
}

void
transpiler_visit_expression_name(struct Visit* visit, struct Expression* expr)
{
    TRANSPILER_PREAMBLE

    fprintf(
        transpiler->output, "%.*s", (int)expr->value.name.name.length, expr->value.name.name.data);
}

void
transpiler_visit_expression_unary_operation(struct Visit* visit, struct Expression* expr)
{
    TRANSPILER_PREAMBLE

    fprintf(
        transpiler->output, "(%s", unary_operation_to_string(expr->value.unary_operator.operation));
    VISIT(visit_expression, expr->value.unary_operator.operand);
    fprintf(transpiler->output, ")");
}

void
transpiler_visit_expression_binary_operation(struct Visit* visit, struct Expression* expr)
{
    TRANSPILER_PREAMBLE
    struct Expression_Binary_Operator* bin_op = &expr->value.binary_operator;

    if (bin_op->operation == BINARY_ASSIGN && bin_op->right_operand->kind == EXPRESSION_FUNCTION) {
        struct Expression* fun_expr = bin_op->right_operand;
        struct Expression_Function* fun = &fun_expr->value.function;
        struct Function_Header_Node* header = &fun->header;
        struct Expression* name_expr = bin_op->left_operand;

        VISIT(visit_type_node, header->return_type);
        fprintf(transpiler->output, " ");

        // check if this is a main function assignment
        if (name_expr && name_expr->kind == EXPRESSION_NAME) {
            struct Expression_Name* name = &name_expr->value.name;
            if (string_equals_c_str(name->name, "main")) {
                transpiler->context.main_function_found = true;
                transpiler->context.main_function_takes_args =
                    header->parameters_type_and_name != nil;
                fprintf(transpiler->output, "catskill_main");
            } else {
                VISIT(visit_expression, name_expr);
            }
        } else {
            VISIT(visit_expression, name_expr);
        }
        VISIT(visit_function_header_node, header);
        fprintf(transpiler->output, " ");

        CONTEXT_START(in_function);
        VISIT(visit_block_node, &fun->body);
        CONTEXT_END(in_function);
    } else {
        enum Binary_Operation op = expr->value.binary_operator.operation;
        if (op == BINARY_ASSIGN_AND || op == BINARY_ASSIGN_OR) {
            fprintf(transpiler->output, "(");
            VISIT(visit_expression, expr->value.binary_operator.left_operand);
            fprintf(transpiler->output, " = ");
            VISIT(visit_expression, expr->value.binary_operator.left_operand);
            if (op == BINARY_ASSIGN_AND)
                fprintf(transpiler->output, " && ");
            else
                fprintf(transpiler->output, " || ");
            VISIT(visit_expression, expr->value.binary_operator.right_operand);
            fprintf(transpiler->output, ")");
        } else {
            fprintf(transpiler->output, "(");
            VISIT(visit_expression, expr->value.binary_operator.left_operand);
            fprintf(transpiler->output, " %s ", binary_operation_to_string(op));
            VISIT(visit_expression, expr->value.binary_operator.right_operand);
            fprintf(transpiler->output, ")");
        }
    }
}

void
transpiler_visit_expression_call(struct Visit* visit, struct Expression* expr)
{
    TRANSPILER_PREAMBLE

    VISIT(visit_expression, expr->value.call.subject);
    fprintf(transpiler->output, "(");
    VISIT(visit_argument_group_node, &expr->value.call.argument_group);
    fprintf(transpiler->output, ")");
}

void
transpiler_visit_expression(struct Visit* visit, struct Expression* expression)
{
    TRANSPILER_PREAMBLE

    switch (expression->kind) {
    case EXPRESSION_INTEGER_LITERAL:
        VISIT(visit_expression_integer_literal, expression);
        break;
    case EXPRESSION_FLOAT_LITERAL:
        VISIT(visit_expression_float_literal, expression);
        break;
    case EXPRESSION_STRING_LITERAL:
        VISIT(visit_expression_string_literal, expression);
        break;
    case EXPRESSION_BOOLEAN_LITERAL:
        VISIT(visit_expression_boolean_literal, expression);
        break;
    case EXPRESSION_NAME:
        VISIT(visit_expression_name, expression);
        break;
    case EXPRESSION_UNARY_OPERATION:
        VISIT(visit_expression_unary_operation, expression);
        break;
    case EXPRESSION_BINARY_OPERATION:
        VISIT(visit_expression_binary_operation, expression);
        break;
    case EXPRESSION_CALL:
        VISIT(visit_expression_call, expression);
        break;
    default:
        walk_expression(visit, expression);
        break;
    }
}

void
transpiler_visit_tree(struct Visit* visit, struct Tree* tree)
{
    TRANSPILER_PREAMBLE

    // include the catskill runtime core library
    // which provides all the necessary types and functions
    // for transpiled catskill programs.
    // other headers can be included by the user
    // with the pragma `| c-header "header.h"`.
    // TODO: for now we just reference the path to it in this repo
    // exactly and tell the backend in `./build.c` to look for includes there,
    // but in the real implementation we should embed all runtime files into
    // this executable and then write them out into the temporary build directory.
    fprintf(transpiler->output, "#include \"core.c\"\n");

    FOR_EACH (struct Statement*, statement, tree->top_level_statements) {
        VISIT(visit_statement, statement);
    }

    // check if we found a main function and define the appropriate macro for the runtime
    // TODO: create a nice lookup table of all the functions and types we found through
    // the catskill source.
    if (transpiler->context.main_function_found) {
        if (transpiler->context.main_function_takes_args) {
            fprintf(transpiler->output, "\n#define CATSKILL_MAIN_TAKES_ARGS\n");
        }
        fprintf(transpiler->output, "#include \"runtime.c\"\n");
    }
}

struct Visit_Table transpiler_visit_functions = {
    .visit_tree = transpiler_visit_tree,
    .visit_statement = transpiler_visit_statement,
    .visit_statement_declaration = transpiler_visit_statement_declaration,
    .visit_statement_conditional = transpiler_visit_statement_conditional,
    .visit_statement_return = transpiler_visit_statement_return,
    .visit_expression = transpiler_visit_expression,
    .visit_expression_integer_literal = transpiler_visit_expression_integer_literal,
    .visit_expression_float_literal = transpiler_visit_expression_float_literal,
    .visit_expression_string_literal = transpiler_visit_expression_string_literal,
    .visit_expression_boolean_literal = transpiler_visit_expression_boolean_literal,
    .visit_expression_name = transpiler_visit_expression_name,
    .visit_expression_unary_operation = transpiler_visit_expression_unary_operation,
    .visit_expression_binary_operation = transpiler_visit_expression_binary_operation,
    .visit_expression_call = transpiler_visit_expression_call,
    .visit_type_node = transpiler_visit_type_node,
    .visit_function_header_node = transpiler_visit_function_header_node,
    .visit_block_node = transpiler_visit_block_node,
    .visit_argument_group_node = transpiler_visit_argument_group_node,
};

int
transpiler_catskill_to_c(struct Transpiler* transpiler, struct Tree* tree)
{
    struct Visit visit = { .table = &transpiler_visit_functions, .user_data = transpiler };
    visit_table_fill_defaults(visit.table);

    walk(&visit, tree);

    return 0;
}