public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
6ae8358cb84c2b75d2805b9f20e75c3a61cbed24 blob 15205 bytes (raw)

  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
 
/*
 * Copyright (C) 2024 olang maintainers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "pretty_print_ast.h"
#include "arena.h"
#include "list.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_RESET "\x1b[0m"

// clang-format off
#define PP_IS_BIT_SET(data, index) ((data) & 1 << index)
// clang-format on

typedef struct pretty_print_node
{
    char *name;
    list_t *children;
} pretty_print_node_t;

static bool
stdout_supports_color()
{
    struct stat st;
    fstat(STDOUT_FILENO, &st);
    return S_ISCHR(st.st_mode);
}

static void
pretty_print_print_ident(uint64_t *prefix, size_t level, bool lst_children)
{
    assert(level < 64);

    bool support_color = stdout_supports_color();

    if (support_color) {
        printf(ANSI_COLOR_MAGENTA);
    }

    for (size_t i = 0; i < level; ++i) {

        if (!PP_IS_BIT_SET(*prefix, i)) {
            printf("  ");
            continue;
        }

        bool last_index = i + 1 == level;

        if (!last_index) {
            printf("| ");
        } else if (lst_children) {
            printf("`-");
        } else {
            printf("|-");
        }
    }

    if (support_color) {
        printf(ANSI_COLOR_RESET);
    }
}

static void
pretty_print_tree(pretty_print_node_t *node,
                  uint64_t *prefix,
                  size_t level,
                  bool lst_children)
{
    pretty_print_print_ident(prefix, level, lst_children);

    list_t *list = node->children;
    if (list != NULL)
        (*prefix) |= 1 << level;
    if (lst_children)
        (*prefix) ^= 1 << (level - 1);

    printf("%s\n", node->name);

    size_t size = list_size(list);
    for (size_t i = 0; i < size; ++i) {
        pretty_print_node_t *it =
            (pretty_print_node_t *)list_get(list, i)->value;
        pretty_print_tree(it, prefix, level + 1, i + 1 == size);
    }
}

static pretty_print_node_t *
pretty_print_node_new(arena_t *arena)
{
    pretty_print_node_t *node =
        (pretty_print_node_t *)arena_alloc(arena, sizeof(pretty_print_node_t));
    node->children = (list_t *)arena_alloc(arena, sizeof(list_t));
    list_init(node->children, arena);
    return node;
}

static pretty_print_node_t *
pretty_print_new_fn_param(ast_fn_param_t *param, arena_t *arena)
{
    pretty_print_node_t *node = pretty_print_node_new(arena);
    char name[256];
    sprintf(name,
            "Param_Definition <name:" SV_FMT "> <type:" SV_FMT ">",
            SV_ARG(param->id.name),
            SV_ARG(param->type->id));
    node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
    strcpy(node->name, name);
    return node;
}

static pretty_print_node_t *
ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
{
    switch (ast->kind) {
        case AST_NODE_TRANSLATION_UNIT: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            node->name = "Translation_Unit";

            list_item_t *item = list_head(ast->as_translation_unit.decls);

            while (item != NULL) {
                ast_node_t *decl = (ast_node_t *)item->value;

                pretty_print_node_t *fn_node =
                    ast_node_to_pretty_print_node(decl, arena);
                list_append(node->children, fn_node);

                item = list_next(item);
            }

            return node;
        }
        case AST_NODE_FN_DEF: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            ast_fn_definition_t fn_def = ast->as_fn_def;

            char name[256];
            sprintf(name,
                    "Function_Definition <name:" SV_FMT "> <return:" SV_FMT
                    ">%s",
                    SV_ARG(fn_def.id),
                    SV_ARG(fn_def.return_type->id),
                    fn_def._extern ? " <extern>" : "");
            node->name =
                (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
            strcpy(node->name, name);

            list_item_t *param = list_head(fn_def.params);
            while (param != NULL) {
                list_append(node->children,
                            pretty_print_new_fn_param(param->value, arena));
                param = list_next(param);
            }

            if (fn_def.block != NULL) {
                pretty_print_node_t *block =
                    ast_node_to_pretty_print_node(fn_def.block, arena);
                list_append(node->children, block);
            }
            return node;
        }
        case AST_NODE_FN_CALL: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            ast_fn_call_t fn_call = ast->as_fn_call;

            char name[256];
            sprintf(name,
                    "Function_Call <name:" SV_FMT ">",
                    SV_ARG(fn_call.id.name));
            node->name =
                (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
            strcpy(node->name, name);

            list_item_t *item = list_head(fn_call.args);
            while (item != NULL) {
                list_append(node->children,
                            ast_node_to_pretty_print_node(item->value, arena));
                item = list_next(item);
            }

            return node;
        }
        case AST_NODE_BLOCK: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            ast_block_t block = ast->as_block;

            node->name = "Block";

            size_t block_nodes_size = list_size(block.nodes);
            for (size_t i = 0; i < block_nodes_size; ++i) {
                ast_node_t *ast_node =
                    (ast_node_t *)list_get(block.nodes, i)->value;
                pretty_print_node_t *child =
                    ast_node_to_pretty_print_node(ast_node, arena);
                list_append(node->children, child);
            }
            return node;
        }
        case AST_NODE_RETURN_STMT: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            ast_return_stmt_t return_stmt = ast->as_return_stmt;

            node->name = "Return_Statement";

            pretty_print_node_t *child =
                ast_node_to_pretty_print_node(return_stmt.value, arena);
            list_append(node->children, child);

            return node;
        }
        case AST_NODE_IF_STMT: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            ast_if_stmt_t if_stmt = ast->as_if_stmt;

            node->name = "If_Statement";

            pretty_print_node_t *child =
                ast_node_to_pretty_print_node(if_stmt.cond, arena);
            list_append(node->children, child);

            child = ast_node_to_pretty_print_node(if_stmt.then, arena);
            list_append(node->children, child);

            if (if_stmt._else != NULL) {
                child = ast_node_to_pretty_print_node(if_stmt._else, arena);
                list_append(node->children, child);
            }

            return node;
        }
        case AST_NODE_WHILE_STMT: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            ast_while_stmt_t while_stmt = ast->as_while_stmt;

            node->name = "While_Statement";

            pretty_print_node_t *child =
                ast_node_to_pretty_print_node(while_stmt.cond, arena);
            list_append(node->children, child);

            child = ast_node_to_pretty_print_node(while_stmt.then, arena);
            list_append(node->children, child);

            return node;
        }
        case AST_NODE_LITERAL: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            ast_literal_t literal = ast->as_literal;

            char name[256];
            switch (literal.kind) {
                case AST_LITERAL_U32: {
                    sprintf(
                        name, "Literal <kind:u32> <value:%u>", literal.as_u32);
                    node->name = (char *)arena_alloc(
                        arena, sizeof(char) * (strlen(name) + 1));
                    strcpy(node->name, name);
                    break;
                }
                default:
                    assert(0 && "literal not implemented");
            }

            return node;
        }
        case AST_NODE_VAR_DEF: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            ast_var_definition_t var = ast->as_var_def;

            char name[256];
            sprintf(name,
                    "Var_Definition <name:" SV_FMT "> <kind:" SV_FMT ">",
                    SV_ARG(var.id.name),
                    SV_ARG(var.type->id));
            node->name =
                (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
            strcpy(node->name, name);

            pretty_print_node_t *child =
                ast_node_to_pretty_print_node(var.value, arena);
            list_append(node->children, child);

            return node;
        }
        case AST_NODE_REF: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            ast_ref_t ref = ast->as_ref;

            char name[256];
            sprintf(name, "Reference <name:" SV_FMT ">", SV_ARG(ref.id.name));
            node->name =
                (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
            strcpy(node->name, name);

            return node;
        }
        case AST_NODE_BINARY_OP: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            ast_binary_op_t binop = ast->as_bin_op;

            switch (binop.kind) {
                case AST_BINOP_ADDITION: {
                    node->name = "Binary_Operation (+)";
                    break;
                }
                case AST_BINOP_SUBTRACTION: {
                    node->name = "Binary_Operation (-)";
                    break;
                }
                case AST_BINOP_MULTIPLICATION: {
                    node->name = "Binary_Operation (*)";
                    break;
                }
                case AST_BINOP_DIVISION: {
                    node->name = "Binary_Operation (/)";
                    break;
                }
                case AST_BINOP_REMINDER: {
                    node->name = "Binary_Operation (%)";
                    break;
                }
                case AST_BINOP_BITWISE_LSHIFT: {
                    node->name = "Binary_Operation (<<)";
                    break;
                }
                case AST_BINOP_BITWISE_RSHIFT: {
                    node->name = "Binary_Operation (>>)";
                    break;
                }
                case AST_BINOP_BITWISE_XOR: {
                    node->name = "Binary_Operation (^)";
                    break;
                }
                case AST_BINOP_BITWISE_AND: {
                    node->name = "Binary_Operation (&)";
                    break;
                }
                case AST_BINOP_BITWISE_OR: {
                    node->name = "Binary_Operation (|)";
                    break;
                }
                case AST_BINOP_CMP_LT: {
                    node->name = "Binary_Operation (<)";
                    break;
                }
                case AST_BINOP_CMP_GT: {
                    node->name = "Binary_Operation (>)";
                    break;
                }
                case AST_BINOP_CMP_LEQ: {
                    node->name = "Binary_Operation (<=)";
                    break;
                }
                case AST_BINOP_CMP_GEQ: {
                    node->name = "Binary_Operation (>=)";
                    break;
                }
                case AST_BINOP_CMP_EQ: {
                    node->name = "Binary_Operation (==)";
                    break;
                }
                case AST_BINOP_CMP_NEQ: {
                    node->name = "Binary_Operation (!=)";
                    break;
                }
                case AST_BINOP_LOGICAL_AND: {
                    node->name = "Binary_Operation (&&)";
                    break;
                }
                case AST_BINOP_LOGICAL_OR: {
                    node->name = "Binary_Operation (|)";
                    break;
                }
                case AST_BINOP_ASSIGN: {
                    node->name = "Binary_Operation (=)";
                    break;
                }
                default:
                    assert(false && "binop not implemented");
            }

            pretty_print_node_t *lhs =
                ast_node_to_pretty_print_node(binop.lhs, arena);
            pretty_print_node_t *rhs =
                ast_node_to_pretty_print_node(binop.rhs, arena);

            list_append(node->children, lhs);
            list_append(node->children, rhs);

            return node;
        }

        case AST_NODE_UNARY_OP: {
            pretty_print_node_t *node = pretty_print_node_new(arena);
            ast_unary_op_t unary_op = ast->as_unary_op;

            switch (unary_op.kind) {
                case AST_UNARY_BITWISE_NOT: {
                    node->name = "Unary_Operation (~)";
                    break;
                }
                case AST_UNARY_DEREFERENCE: {
                    node->name = "Unary_Operation (*)";
                    break;
                }
                case AST_UNARY_NEGATIVE: {
                    node->name = "Unary_Operation (-)";
                    break;
                }
                case AST_UNARY_LOGICAL_NOT: {
                    node->name = "Unary_Operation (!)";
                    break;
                }
                case AST_UNARY_POSITIVE: {
                    node->name = "Unary_Operation (+)";
                    break;
                }
                case AST_UNARY_ADDRESSOF: {
                    node->name = "Unary_Operation (&)";
                    break;
                }
            }

            pretty_print_node_t *expr =
                ast_node_to_pretty_print_node(unary_op.operand, arena);
            list_append(node->children, expr);

            return node;
        }

        default: {
            printf("node kind = '%d' not implmented\n", ast->kind);
            assert(false);
        }
    }
    return NULL;
}

void
pretty_print_ast(ast_node_t *ast)
{
    arena_t arena = arena_new(8 * 1024);
    pretty_print_node_t *root = ast_node_to_pretty_print_node(ast, &arena);
    uint64_t prefix = 0;

    pretty_print_tree(root, &prefix, 0, true);

    arena_free(&arena);
}
debug log:

solving 6ae8358 ...
found 6ae8358 in http://lists.johnnyrichard.com/olang/20241024123825.120390-5-carlos@maniero.me/
found f65c253 in http://lists.johnnyrichard.com/olang/20241024123825.120390-4-carlos@maniero.me/
found fdc6b65 in http://lists.johnnyrichard.com/olang/20241024123825.120390-2-carlos@maniero.me/
found bc5d119 in https://git.johnnyrichard.com/olang.git
preparing index
index prepared:
100644 bc5d119800249d3fb4b3c36027bf5fea4f58c86d	src/pretty_print_ast.c

applying [1/3] http://lists.johnnyrichard.com/olang/20241024123825.120390-2-carlos@maniero.me/
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index bc5d119..fdc6b65 100644


applying [2/3] http://lists.johnnyrichard.com/olang/20241024123825.120390-4-carlos@maniero.me/
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index fdc6b65..f65c253 100644


applying [3/3] http://lists.johnnyrichard.com/olang/20241024123825.120390-5-carlos@maniero.me/
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index f65c253..6ae8358 100644

Checking patch src/pretty_print_ast.c...
Applied patch src/pretty_print_ast.c cleanly.
Checking patch src/pretty_print_ast.c...
Applied patch src/pretty_print_ast.c cleanly.
Checking patch src/pretty_print_ast.c...
Applied patch src/pretty_print_ast.c cleanly.

index at:
100644 6ae8358cb84c2b75d2805b9f20e75c3a61cbed24	src/pretty_print_ast.c

Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox