public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1 2/3] checker: create checker and populate scope on ast nodes
Date: Sat, 21 Sep 2024 02:20:39 +0200	[thread overview]
Message-ID: <20240921002250.902558-3-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20240921002250.902558-1-johnny@johnnyrichard.com>

The checker will check semantic analysis in the future, now we want to
focus on create the scope (symbol table) and attach to a couple of ast
nodes.

We want the scope to be accessible from the ast nodes to easily resolve
scope on the next compiler pipeline steps.

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 src/ast.h     |   4 ++
 src/checker.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/checker.h |  34 ++++++++++++++
 src/main.c    |   4 ++
 4 files changed, 167 insertions(+)
 create mode 100644 src/checker.c
 create mode 100644 src/checker.h

diff --git a/src/ast.h b/src/ast.h
index 94ee6b4..7d065c6 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -21,6 +21,7 @@
 
 #include "arena.h"
 #include "list.h"
+#include "scope.h"
 #include "string_view.h"
 
 typedef struct ast_node ast_node_t;
@@ -66,6 +67,7 @@ typedef struct ast_var_definition
     string_view_t identifier;
     type_t type;
     ast_node_t *value;
+    scope_t *scope;
 } ast_var_definition_t;
 
 typedef enum
@@ -85,6 +87,7 @@ typedef struct ast_literal
 typedef struct ast_ref
 {
     string_view_t identifier;
+    scope_t *scope;
 } ast_ref_t;
 
 typedef enum ast_binary_op_kind
@@ -118,6 +121,7 @@ typedef struct ast_binary_op
 
 typedef struct ast_return_stmt
 {
+    // FIXME: rename to a meaningful name like expr
     ast_node_t *data;
 } ast_return_stmt_t;
 
diff --git a/src/checker.c b/src/checker.c
new file mode 100644
index 0000000..5925158
--- /dev/null
+++ b/src/checker.c
@@ -0,0 +1,125 @@
+/*
+ * 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);
+
+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_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: {
+            // 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;
+            symbol_t *symbol = symbol_new(checker->arena, id);
+
+            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;
+    }
+}
diff --git a/src/checker.h b/src/checker.h
new file mode 100644
index 0000000..681f405
--- /dev/null
+++ b/src/checker.h
@@ -0,0 +1,34 @@
+/*
+ * 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/>.
+ */
+#ifndef CHECKER_H
+#define CHECKER_H
+
+#include "arena.h"
+#include "ast.h"
+
+typedef struct checker
+{
+    arena_t *arena;
+} checker_t;
+
+checker_t *
+checker_new(arena_t *arena);
+
+void
+checker_check(checker_t *checker, ast_node_t *ast);
+
+#endif /* CHECKER_H */
diff --git a/src/main.c b/src/main.c
index 030f34c..810bf2d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -22,6 +22,7 @@
 #include <string.h>
 
 #include "arena.h"
+#include "checker.h"
 #include "cli.h"
 #include "codegen_linux_aarch64.h"
 #include "codegen_linux_x86_64.h"
@@ -136,6 +137,9 @@ handle_codegen_linux(cli_opts_t *opts)
 
     ast_node_t *ast = parser_parse_program(&parser);
 
+    checker_t *checker = checker_new(&arena);
+    checker_check(checker, ast);
+
     char asm_file[opts->output_bin.size + 3];
     sprintf(asm_file, "" SV_FMT ".s", SV_ARG(opts->output_bin));
 
-- 
2.46.0


  parent reply	other threads:[~2024-09-21  0:22 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-21  0:20 [PATCH olang v1 0/3] compiler: enable full compilation for vars Johnny Richard
2024-09-21  0:20 ` [PATCH olang v1 1/3] scope: create scope data structure Johnny Richard
2024-09-21  0:20 ` Johnny Richard [this message]
2024-09-21  0:20 ` [PATCH olang v1 3/3] codegen: add support scopes and symbols lookups for var Johnny Richard
2024-09-21  0:23   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-21  0:59 ` [PATCH olang v1 0/3] compiler: enable full compilation for vars Carlos Maniero

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240921002250.902558-3-johnny@johnnyrichard.com \
    --to=johnny@johnnyrichard.com \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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