public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
3a78a5935fc33e9f07399da234bb1ebfcd572c24 blob 5932 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
 
/*
 * 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 "checker.h"
#include "scope.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>

static void
populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast);

static void
populate_types(checker_t *checker, ast_node_t *ast);

checker_t *
checker_new(arena_t *arena)
{
    assert(arena);

    checker_t *checker = (checker_t *)arena_alloc(arena, sizeof(checker_t));
    if (checker == NULL) {
        fprintf(stderr, "[FATAL] Out of memory: checker_new: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }
    checker->arena = arena;
    return checker;
}

void
checker_check(checker_t *checker, ast_node_t *ast)
{
    assert(checker);
    assert(ast);

    scope_t *scope = scope_new(checker->arena);
    populate_types(checker, ast);
    populate_scope(checker, scope, ast);

    // TODO: traverse the ast tree to verify semantics
}

static void
populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
{
    switch (ast->kind) {
        case AST_NODE_PROGRAM: {
            populate_scope(checker, scope, ast->as_program.fn);
            return;
        }

        case AST_NODE_FN_DEF: {
            ast->as_fn_def.scope = scope;
            // FIXME: insert function symbol to scope
            populate_scope(checker, scope, ast->as_fn_def.block);
            return;
        }

        case AST_NODE_IF_STMT: {
            populate_scope(checker, scope, ast->as_if_stmt.cond);
            populate_scope(checker, scope, ast->as_if_stmt.then);

            if (ast->as_if_stmt._else) {
                populate_scope(checker, scope, ast->as_if_stmt._else);
            }

            return;
        }

        case AST_NODE_BINARY_OP: {
            ast_binary_op_t bin_op = ast->as_bin_op;

            populate_scope(checker, scope, bin_op.lhs);
            populate_scope(checker, scope, bin_op.rhs);
            return;
        }

        case AST_NODE_RETURN_STMT: {
            ast_return_stmt_t return_stmt = ast->as_return_stmt;

            populate_scope(checker, scope, return_stmt.data);
            return;
        }

        case AST_NODE_BLOCK: {
            ast_block_t block = ast->as_block;
            scope = scope_push(scope);

            list_item_t *item = list_head(block.nodes);

            while (item != NULL) {
                populate_scope(checker, scope, (ast_node_t *)item->value);
                item = list_next(item);
            }

            return;
        }

        case AST_NODE_VAR_DEF: {
            string_view_t id = ast->as_var_def.identifier;
            type_t type = ast->as_var_def.type;
            symbol_t *symbol = symbol_new(checker->arena, id, type);

            scope_insert(scope, symbol);
            ast->as_var_def.scope = scope;
            return;
        }

        case AST_NODE_REF: {
            ast->as_ref.scope = scope;
            return;
        }

        case AST_NODE_LITERAL:
        case AST_NODE_UNKNOWN:
            return;
    }
}

static void
evaluate_type(type_t *type)
{
    if (string_view_eq_to_cstr(type->id, "u8")) {
        type->kind = TYPE_PRIMITIVE;
        type->as_primitive = TYPE_U8;
        return;
    }
    if (string_view_eq_to_cstr(type->id, "u16")) {
        type->kind = TYPE_PRIMITIVE;
        type->as_primitive = TYPE_U16;
        return;
    }
    if (string_view_eq_to_cstr(type->id, "u32")) {
        type->kind = TYPE_PRIMITIVE;
        type->as_primitive = TYPE_U32;
        return;
    }
    if (string_view_eq_to_cstr(type->id, "u64")) {
        type->kind = TYPE_PRIMITIVE;
        type->as_primitive = TYPE_U64;
        return;
    }

    type->kind = TYPE_USER_DEFINED;
}

static void
populate_types(checker_t *checker, ast_node_t *ast)
{
    switch (ast->kind) {
        case AST_NODE_PROGRAM: {
            populate_types(checker, ast->as_program.fn);
            return;
        }

        case AST_NODE_FN_DEF: {
            evaluate_type(&ast->as_fn_def.return_type);
            populate_types(checker, ast->as_fn_def.block);
            return;
        }

        case AST_NODE_IF_STMT: {
            populate_types(checker, ast->as_if_stmt.cond);
            populate_types(checker, ast->as_if_stmt.then);

            if (ast->as_if_stmt._else) {
                populate_types(checker, ast->as_if_stmt._else);
            }

            return;
        }

        case AST_NODE_BINARY_OP: {
            ast_binary_op_t bin_op = ast->as_bin_op;

            populate_types(checker, bin_op.lhs);
            populate_types(checker, bin_op.rhs);
            return;
        }

        case AST_NODE_RETURN_STMT: {
            populate_types(checker, ast->as_return_stmt.data);
            return;
        }

        case AST_NODE_BLOCK: {
            ast_block_t block = ast->as_block;

            list_item_t *item = list_head(block.nodes);

            while (item != NULL) {
                populate_types(checker, (ast_node_t *)item->value);
                item = list_next(item);
            }

            return;
        }

        case AST_NODE_VAR_DEF: {
            evaluate_type(&ast->as_var_def.type);
            return;
        }

        case AST_NODE_REF:
        case AST_NODE_LITERAL:
        case AST_NODE_UNKNOWN:
            return;
    }
}
debug log:

solving 3a78a59 ...
found 3a78a59 in http://lists.johnnyrichard.com/olang/20240921082437.396691-6-carlos@maniero.me/
found f5068e0 in http://lists.johnnyrichard.com/olang/20240921082437.396691-3-carlos@maniero.me/
found 3b713f7 in https://git.johnnyrichard.com/olang.git
preparing index
index prepared:
100644 3b713f7e772cfc52ca68c00717edfea0f930baa2	src/checker.c

applying [1/2] http://lists.johnnyrichard.com/olang/20240921082437.396691-3-carlos@maniero.me/
diff --git a/src/checker.c b/src/checker.c
index 3b713f7..f5068e0 100644


applying [2/2] http://lists.johnnyrichard.com/olang/20240921082437.396691-6-carlos@maniero.me/
diff --git a/src/checker.c b/src/checker.c
index f5068e0..3a78a59 100644

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

index at:
100644 3a78a5935fc33e9f07399da234bb1ebfcd572c24	src/checker.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