public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* [PATCH olang v1 0/5] Refactors to support semantics
@ 2024-10-23  2:20 Carlos Maniero
  2024-10-23  2:20 ` [PATCH olang v1 1/5] type_checker: rename checker to type_checker Carlos Maniero
                   ` (5 more replies)
  0 siblings, 6 replies; 9+ messages in thread
From: Carlos Maniero @ 2024-10-23  2:20 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

The semantics is an important part of a compiler but we have postpone
handling it so far. But the cost came, and we started to see some extra
responsibility on the codegen layer because of the lack of semantics.

This patchset does not address semantics per si, but it cover several
tech debts to start supporting it.

Carlos Maniero (5):
  type_checker: rename checker to type_checker
  ast: remove redundancy on ast_node base fields
  ast: unary: rename ast_unary.expr to ast_unary.operand
  ast: remove expr reference from return and var assign nodes
  semantics: add scope to all nodes

 src/ast.c                         |  8 ++--
 src/ast.h                         | 67 ++++++++++++++++---------------
 src/codegen_aarch64.c             |  2 +-
 src/codegen_x86_64.c              | 31 +++++++-------
 src/main.c                        |  2 +-
 src/pretty_print_ast.c            |  4 +-
 src/{checker.c => type_checker.c} | 18 ++++-----
 src/{checker.h => type_checker.h} |  0
 8 files changed, 67 insertions(+), 65 deletions(-)
 rename src/{checker.c => type_checker.c} (93%)
 rename src/{checker.h => type_checker.h} (100%)


base-commit: 2dbf9a9896e5778535bd3dc1d5069a762d3b94fa
-- 
2.46.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH olang v1 1/5] type_checker: rename checker to type_checker
  2024-10-23  2:20 [PATCH olang v1 0/5] Refactors to support semantics Carlos Maniero
@ 2024-10-23  2:20 ` Carlos Maniero
  2024-10-23  2:20 ` [PATCH olang v1 2/5] ast: remove redundancy on ast_node base fields Carlos Maniero
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Carlos Maniero @ 2024-10-23  2:20 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

To void adding all the semantics concerns into a single module, we are
renaming the checker file to type_checker.

Other checkers will be introduced as needed.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/main.c                        | 2 +-
 src/{checker.c => type_checker.c} | 2 +-
 src/{checker.h => type_checker.h} | 0
 3 files changed, 2 insertions(+), 2 deletions(-)
 rename src/{checker.c => type_checker.c} (99%)
 rename src/{checker.h => type_checker.h} (100%)

diff --git a/src/main.c b/src/main.c
index 685d8ce..c99ccfc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -22,7 +22,6 @@
 #include <string.h>
 
 #include "arena.h"
-#include "checker.h"
 #include "cli.h"
 #include "codegen_aarch64.h"
 #include "codegen_x86_64.h"
@@ -30,6 +29,7 @@
 #include "parser.h"
 #include "pretty_print_ast.h"
 #include "string_view.h"
+#include "type_checker.h"
 
 // TODO: find a better solution to define the arena capacity
 #define ARENA_CAPACITY (1024 * 1024)
diff --git a/src/checker.c b/src/type_checker.c
similarity index 99%
rename from src/checker.c
rename to src/type_checker.c
index 02341cc..b426349 100644
--- a/src/checker.c
+++ b/src/type_checker.c
@@ -14,7 +14,7 @@
  * 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 "type_checker.h"
 #include "scope.h"
 #include <assert.h>
 #include <errno.h>
diff --git a/src/checker.h b/src/type_checker.h
similarity index 100%
rename from src/checker.h
rename to src/type_checker.h
-- 
2.46.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH olang v1 2/5] ast: remove redundancy on ast_node base fields
  2024-10-23  2:20 [PATCH olang v1 0/5] Refactors to support semantics Carlos Maniero
  2024-10-23  2:20 ` [PATCH olang v1 1/5] type_checker: rename checker to type_checker Carlos Maniero
@ 2024-10-23  2:20 ` Carlos Maniero
  2024-10-23  2:20 ` [PATCH olang v1 3/5] ast: unary: rename ast_unary.expr to ast_unary.operand Carlos Maniero
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Carlos Maniero @ 2024-10-23  2:20 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

This solution was inspired on cython PyObject which uses macros to avoid
the redundancy.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/ast.h | 54 ++++++++++++++++++++++++++++++------------------------
 1 file changed, 30 insertions(+), 24 deletions(-)

diff --git a/src/ast.h b/src/ast.h
index e7d8ed8..c7a25bf 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -26,6 +26,21 @@
 #include "string_view.h"
 #include "type.h"
 
+#define AST_NODE_BASE                                                          \
+    {                                                                          \
+        ast_node_kind_t kind;                                                  \
+        token_loc_t loc;                                                       \
+    }
+
+/**
+ * Every node within the *ast_node* union must include the base segment.
+ *
+ * This macro must be declared first within any node definition to ensure the
+ * ast_node base segment's integrity.
+ */
+
+#define AST_NODE_HEAD struct AST_NODE_BASE base
+
 typedef union ast_node ast_node_t;
 
 typedef enum
@@ -45,21 +60,17 @@ typedef enum
     AST_NODE_UNKNOWN
 } ast_node_kind_t;
 
-typedef struct ast_node_meta
-{
-    ast_node_kind_t kind;
-    token_loc_t loc;
-} ast_node_meta_t;
+typedef struct ast_node_base AST_NODE_BASE ast_node_base_t;
 
 typedef struct ast_block
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     list_t *nodes;
 } ast_block_t;
 
 typedef struct ast_translation_unit
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     list_t *decls;
 } ast_translation_unit_t;
 
@@ -71,7 +82,7 @@ typedef struct ast_fn_param
 
 typedef struct ast_fn_definition
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     string_view_t id;
     list_t *params;
     type_t *return_type;
@@ -82,7 +93,7 @@ typedef struct ast_fn_definition
 
 typedef struct ast_fn_call
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     string_view_t id;
     list_t *args;
     scope_t *scope;
@@ -90,7 +101,7 @@ typedef struct ast_fn_call
 
 typedef struct ast_var_definition
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     string_view_t id;
     type_t *type;
     ast_node_t *value;
@@ -104,7 +115,7 @@ typedef enum
 
 typedef struct ast_literal
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     ast_literal_kind_t kind;
     union
     {
@@ -114,7 +125,7 @@ typedef struct ast_literal
 
 typedef struct ast_ref
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     string_view_t id;
     scope_t *scope;
 } ast_ref_t;
@@ -144,7 +155,7 @@ typedef enum ast_binary_op_kind
 
 typedef struct ast_binary_op
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     ast_binary_op_kind_t kind;
     ast_node_t *lhs;
     ast_node_t *rhs;
@@ -162,27 +173,27 @@ typedef enum ast_unary_op_kind
 
 typedef struct ast_unary_op
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     ast_unary_op_kind_t kind;
     ast_node_t *expr;
 } ast_unary_op_t;
 
 typedef struct ast_var_assign_stmt
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     ast_node_t *ref;
     ast_node_t *expr;
 } ast_var_assign_stmt_t;
 
 typedef struct ast_return_stmt
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     ast_node_t *expr;
 } ast_return_stmt_t;
 
 typedef struct ast_if_stmt
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     ast_node_t *cond;
     ast_node_t *then;
     ast_node_t *_else;
@@ -190,19 +201,14 @@ typedef struct ast_if_stmt
 
 typedef struct ast_while_stmt
 {
-    ast_node_meta_t meta;
+    AST_NODE_HEAD;
     ast_node_t *cond;
     ast_node_t *then;
 } ast_while_stmt_t;
 
 typedef union ast_node
 {
-    // inlined ast_node_meta_t struct.
-    struct
-    {
-        ast_node_kind_t kind;
-        token_loc_t loc;
-    };
+    struct AST_NODE_BASE;
     ast_translation_unit_t as_translation_unit;
     ast_fn_definition_t as_fn_def;
     ast_fn_call_t as_fn_call;
-- 
2.46.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH olang v1 3/5] ast: unary: rename ast_unary.expr to ast_unary.operand
  2024-10-23  2:20 [PATCH olang v1 0/5] Refactors to support semantics Carlos Maniero
  2024-10-23  2:20 ` [PATCH olang v1 1/5] type_checker: rename checker to type_checker Carlos Maniero
  2024-10-23  2:20 ` [PATCH olang v1 2/5] ast: remove redundancy on ast_node base fields Carlos Maniero
@ 2024-10-23  2:20 ` Carlos Maniero
  2024-10-23  2:20 ` [PATCH olang v1 4/5] ast: remove expr reference from return and var assign nodes Carlos Maniero
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 9+ messages in thread
From: Carlos Maniero @ 2024-10-23  2:20 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

This is a more meaningful name for the given context.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/ast.c              |  4 ++--
 src/ast.h              |  4 ++--
 src/codegen_x86_64.c   | 12 ++++++------
 src/pretty_print_ast.c |  2 +-
 src/type_checker.c     |  2 +-
 5 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/ast.c b/src/ast.c
index d0c6b37..eef01ce 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -137,7 +137,7 @@ ast_node_t *
 ast_new_node_unary_op(arena_t *arena,
                       token_loc_t loc,
                       ast_unary_op_kind_t kind,
-                      ast_node_t *expr)
+                      ast_node_t *operand)
 {
     ast_node_t *node_unary_op =
         (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
@@ -146,7 +146,7 @@ ast_new_node_unary_op(arena_t *arena,
     node_unary_op->kind = AST_NODE_UNARY_OP;
     node_unary_op->loc = loc;
     node_unary_op->as_unary_op.kind = kind;
-    node_unary_op->as_unary_op.expr = expr;
+    node_unary_op->as_unary_op.operand = operand;
 
     return node_unary_op;
 }
diff --git a/src/ast.h b/src/ast.h
index c7a25bf..9d82317 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -175,7 +175,7 @@ typedef struct ast_unary_op
 {
     AST_NODE_HEAD;
     ast_unary_op_kind_t kind;
-    ast_node_t *expr;
+    ast_node_t *operand;
 } ast_unary_op_t;
 
 typedef struct ast_var_assign_stmt
@@ -260,7 +260,7 @@ ast_node_t *
 ast_new_node_unary_op(arena_t *arena,
                       token_loc_t loc,
                       ast_unary_op_kind_t kind,
-                      ast_node_t *expr);
+                      ast_node_t *operand);
 
 ast_node_t *
 ast_new_node_literal_u32(arena_t *arena, token_loc_t loc, uint32_t value);
diff --git a/src/codegen_x86_64.c b/src/codegen_x86_64.c
index deb7e24..933a42d 100644
--- a/src/codegen_x86_64.c
+++ b/src/codegen_x86_64.c
@@ -653,8 +653,8 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
             ast_unary_op_t unary_op = expr_node->as_unary_op;
             switch (unary_op.kind) {
                 case AST_UNARY_BITWISE_NOT: {
-                    size_in_bytes_t expr_bytes =
-                        codegen_x86_64_emit_expression(codegen, unary_op.expr);
+                    size_in_bytes_t expr_bytes = codegen_x86_64_emit_expression(
+                        codegen, unary_op.operand);
 
                     fprintf(codegen->out,
                             "    not %s\n",
@@ -663,10 +663,10 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
                     return expr_bytes;
                 }
                 case AST_UNARY_ADDRESSOF: {
-                    assert(unary_op.expr->kind == AST_NODE_REF &&
+                    assert(unary_op.operand->kind == AST_NODE_REF &&
                            "unsupported unary expression for addressof (&)");
 
-                    ast_ref_t ref = unary_op.expr->as_ref;
+                    ast_ref_t ref = unary_op.operand->as_ref;
 
                     symbol_t *symbol = scope_lookup(ref.scope, ref.id);
                     assert(symbol);
@@ -680,11 +680,11 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
                 }
                 case AST_UNARY_DEREFERENCE: {
                     // FIXME: support dereference of dereference (**)
-                    assert(unary_op.expr->kind == AST_NODE_REF &&
+                    assert(unary_op.operand->kind == AST_NODE_REF &&
                            "unsupported unary expression for dereference (*)");
 
                     return codegen_x86_64_emit_expression(codegen,
-                                                          unary_op.expr);
+                                                          unary_op.operand);
                 }
                 default: {
                     assert(0 && "unsupported unary operation");
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index d476370..d18fa56 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -440,7 +440,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
             }
 
             pretty_print_node_t *expr =
-                ast_node_to_pretty_print_node(unary_op.expr, arena);
+                ast_node_to_pretty_print_node(unary_op.operand, arena);
             list_append(node->children, expr);
 
             return node;
diff --git a/src/type_checker.c b/src/type_checker.c
index b426349..53c2e05 100644
--- a/src/type_checker.c
+++ b/src/type_checker.c
@@ -189,7 +189,7 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
         case AST_NODE_UNARY_OP: {
             ast_unary_op_t unary_op = ast->as_unary_op;
 
-            populate_scope(checker, scope, unary_op.expr);
+            populate_scope(checker, scope, unary_op.operand);
             return;
         }
 
-- 
2.46.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH olang v1 4/5] ast: remove expr reference from return and var assign nodes
  2024-10-23  2:20 [PATCH olang v1 0/5] Refactors to support semantics Carlos Maniero
                   ` (2 preceding siblings ...)
  2024-10-23  2:20 ` [PATCH olang v1 3/5] ast: unary: rename ast_unary.expr to ast_unary.operand Carlos Maniero
@ 2024-10-23  2:20 ` Carlos Maniero
  2024-10-23  2:20 ` [PATCH olang v1 5/5] semantics: add scope to all nodes Carlos Maniero
  2024-10-23 17:34 ` [PATCH olang v1 0/5] Refactors to support semantics Johnny Richard
  5 siblings, 0 replies; 9+ messages in thread
From: Carlos Maniero @ 2024-10-23  2:20 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

We will introduce an new ast_node called expr that will be the base
struct for nodes of kind expression. To avoid misunderstandings this two
nodes that used to have a expr field, now have a field named value.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/ast.c              | 4 ++--
 src/ast.h              | 4 ++--
 src/codegen_aarch64.c  | 2 +-
 src/codegen_x86_64.c   | 4 ++--
 src/pretty_print_ast.c | 2 +-
 src/type_checker.c     | 2 +-
 6 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/ast.c b/src/ast.c
index eef01ce..b96a463 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -180,7 +180,7 @@ ast_new_node_ref(arena_t *arena, token_loc_t loc, string_view_t id)
 }
 
 ast_node_t *
-ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr)
+ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *value)
 {
     ast_node_t *node_return_stmt =
         (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
@@ -188,7 +188,7 @@ ast_new_node_return_stmt(arena_t *arena, token_loc_t loc, ast_node_t *expr)
 
     node_return_stmt->kind = AST_NODE_RETURN_STMT;
     node_return_stmt->loc = loc;
-    node_return_stmt->as_return_stmt.expr = expr;
+    node_return_stmt->as_return_stmt.value = value;
 
     return node_return_stmt;
 }
diff --git a/src/ast.h b/src/ast.h
index 9d82317..7c5e9af 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -182,13 +182,13 @@ typedef struct ast_var_assign_stmt
 {
     AST_NODE_HEAD;
     ast_node_t *ref;
-    ast_node_t *expr;
+    ast_node_t *value;
 } ast_var_assign_stmt_t;
 
 typedef struct ast_return_stmt
 {
     AST_NODE_HEAD;
-    ast_node_t *expr;
+    ast_node_t *value;
 } ast_return_stmt_t;
 
 typedef struct ast_if_stmt
diff --git a/src/codegen_aarch64.c b/src/codegen_aarch64.c
index 1de238d..bc511f0 100644
--- a/src/codegen_aarch64.c
+++ b/src/codegen_aarch64.c
@@ -97,7 +97,7 @@ codegen_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn)
     assert(return_node->kind == AST_NODE_RETURN_STMT);
     ast_return_stmt_t return_stmt = return_node->as_return_stmt;
 
-    ast_node_t *literal_node = return_stmt.expr;
+    ast_node_t *literal_node = return_stmt.value;
     assert(literal_node->kind == AST_NODE_LITERAL);
     ast_literal_t literal_u32 = literal_node->as_literal;
 
diff --git a/src/codegen_x86_64.c b/src/codegen_x86_64.c
index 933a42d..9006250 100644
--- a/src/codegen_x86_64.c
+++ b/src/codegen_x86_64.c
@@ -709,9 +709,9 @@ codegen_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
             case AST_NODE_RETURN_STMT: {
                 ast_return_stmt_t return_stmt = node->as_return_stmt;
 
-                ast_node_t *expr = return_stmt.expr;
+                ast_node_t *value = return_stmt.value;
 
-                codegen_x86_64_emit_expression(codegen, expr);
+                codegen_x86_64_emit_expression(codegen, value);
 
                 fprintf(codegen->out, "    mov %%rbp, %%rsp\n");
                 fprintf(codegen->out, "    pop %%rbp\n");
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index d18fa56..bc5d119 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -221,7 +221,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
             node->name = "Return_Statement";
 
             pretty_print_node_t *child =
-                ast_node_to_pretty_print_node(return_stmt.expr, arena);
+                ast_node_to_pretty_print_node(return_stmt.value, arena);
             list_append(node->children, child);
 
             return node;
diff --git a/src/type_checker.c b/src/type_checker.c
index 53c2e05..235d711 100644
--- a/src/type_checker.c
+++ b/src/type_checker.c
@@ -196,7 +196,7 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
         case AST_NODE_RETURN_STMT: {
             ast_return_stmt_t return_stmt = ast->as_return_stmt;
 
-            populate_scope(checker, scope, return_stmt.expr);
+            populate_scope(checker, scope, return_stmt.value);
             return;
         }
 
-- 
2.46.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH olang v1 5/5] semantics: add scope to all nodes
  2024-10-23  2:20 [PATCH olang v1 0/5] Refactors to support semantics Carlos Maniero
                   ` (3 preceding siblings ...)
  2024-10-23  2:20 ` [PATCH olang v1 4/5] ast: remove expr reference from return and var assign nodes Carlos Maniero
@ 2024-10-23  2:20 ` Carlos Maniero
  2024-10-23  2:21   ` [olang/patches/.build.yml] build success builds.sr.ht
  2024-10-23 17:36   ` [PATCH olang v1 5/5] semantics: add scope to all nodes Johnny Richard
  2024-10-23 17:34 ` [PATCH olang v1 0/5] Refactors to support semantics Johnny Richard
  5 siblings, 2 replies; 9+ messages in thread
From: Carlos Maniero @ 2024-10-23  2:20 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

Instead of manually adding scopes to nodes as needed, now the scope is a
base attribute. This makes the implementation simpler and consistent so
we no longer need to think if some node kind requires or not the scope.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/ast.h            |  5 +----
 src/codegen_x86_64.c | 15 ++++++++-------
 src/type_checker.c   | 12 +++++-------
 3 files changed, 14 insertions(+), 18 deletions(-)

diff --git a/src/ast.h b/src/ast.h
index 7c5e9af..abc12e2 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -30,6 +30,7 @@
     {                                                                          \
         ast_node_kind_t kind;                                                  \
         token_loc_t loc;                                                       \
+        scope_t *scope;                                                        \
     }
 
 /**
@@ -88,7 +89,6 @@ typedef struct ast_fn_definition
     type_t *return_type;
     bool _extern;
     ast_node_t *block;
-    scope_t *scope;
 } ast_fn_definition_t;
 
 typedef struct ast_fn_call
@@ -96,7 +96,6 @@ typedef struct ast_fn_call
     AST_NODE_HEAD;
     string_view_t id;
     list_t *args;
-    scope_t *scope;
 } ast_fn_call_t;
 
 typedef struct ast_var_definition
@@ -105,7 +104,6 @@ typedef struct ast_var_definition
     string_view_t id;
     type_t *type;
     ast_node_t *value;
-    scope_t *scope;
 } ast_var_definition_t;
 
 typedef enum
@@ -127,7 +125,6 @@ typedef struct ast_ref
 {
     AST_NODE_HEAD;
     string_view_t id;
-    scope_t *scope;
 } ast_ref_t;
 
 typedef enum ast_binary_op_kind
diff --git a/src/codegen_x86_64.c b/src/codegen_x86_64.c
index 9006250..616fa2b 100644
--- a/src/codegen_x86_64.c
+++ b/src/codegen_x86_64.c
@@ -143,7 +143,7 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
         case AST_NODE_REF: {
             ast_ref_t ref = expr_node->as_ref;
 
-            symbol_t *symbol = scope_lookup(ref.scope, ref.id);
+            symbol_t *symbol = scope_lookup(expr_node->scope, ref.id);
             assert(symbol);
 
             size_t offset = codegen_x86_64_get_stack_offset(codegen, symbol);
@@ -159,7 +159,7 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
         case AST_NODE_FN_CALL: {
             ast_fn_call_t fn_call = expr_node->as_fn_call;
 
-            symbol_t *symbol = scope_lookup(fn_call.scope, fn_call.id);
+            symbol_t *symbol = scope_lookup(expr_node->scope, fn_call.id);
             assert(symbol);
 
             size_t i = 0;
@@ -597,7 +597,7 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
                     switch (bin_op.lhs->kind) {
                         case AST_NODE_REF: {
                             ast_ref_t ref = bin_op.lhs->as_ref;
-                            scope_t *scope = ref.scope;
+                            scope_t *scope = bin_op.lhs->scope;
 
                             symbol_t *symbol = scope_lookup(scope, ref.id);
                             assert(symbol);
@@ -668,7 +668,8 @@ codegen_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr_node)
 
                     ast_ref_t ref = unary_op.operand->as_ref;
 
-                    symbol_t *symbol = scope_lookup(ref.scope, ref.id);
+                    symbol_t *symbol =
+                        scope_lookup(unary_op.operand->scope, ref.id);
                     assert(symbol);
 
                     size_t offset =
@@ -722,7 +723,7 @@ codegen_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
 
             case AST_NODE_VAR_DEF: {
                 ast_var_definition_t var_def = node->as_var_def;
-                scope_t *scope = var_def.scope;
+                scope_t *scope = node->scope;
 
                 symbol_t *symbol = scope_lookup(scope, var_def.id);
                 assert(symbol);
@@ -908,7 +909,7 @@ codegen_x86_64_emit_function(codegen_x86_64_t *codegen,
 
         ast_fn_param_t *param = item->value;
 
-        symbol_t *symbol = scope_lookup(fn_def->scope, param->id);
+        symbol_t *symbol = scope_lookup(fn_def->base.scope, param->id);
         assert(symbol);
 
         // FIXME: add offset according to the param size
@@ -926,7 +927,7 @@ codegen_x86_64_emit_function(codegen_x86_64_t *codegen,
         ++i;
     }
 
-    size_t local_size = calculate_fn_local_size(fn_def->scope);
+    size_t local_size = calculate_fn_local_size(fn_def->base.scope);
 
     if (local_size != 0) {
         fprintf(codegen->out, "    sub $%ld, %%rsp\n", local_size);
diff --git a/src/type_checker.c b/src/type_checker.c
index 235d711..cc87832 100644
--- a/src/type_checker.c
+++ b/src/type_checker.c
@@ -108,6 +108,8 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
     assert(checker);
     assert(scope);
 
+    ast->scope = scope;
+
     switch (ast->kind) {
         case AST_NODE_TRANSLATION_UNIT: {
             list_item_t *item = list_head(ast->as_translation_unit.decls);
@@ -121,7 +123,7 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
 
         case AST_NODE_FN_DEF: {
             ast_fn_definition_t *fn_def = &ast->as_fn_def;
-            fn_def->scope = scope_push(scope);
+            ast->scope = scope_push(scope);
 
             type_resolve(fn_def->return_type);
             symbol_t *symbol =
@@ -136,20 +138,18 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
                 type_resolve(param->type);
                 symbol_t *symbol =
                     symbol_new(checker->arena, param->id, param->type);
-                scope_insert(fn_def->scope, symbol);
+                scope_insert(ast->scope, symbol);
 
                 item = list_next(item);
             }
 
             if (ast->as_fn_def.block != NULL) {
-                populate_scope(checker, fn_def->scope, ast->as_fn_def.block);
+                populate_scope(checker, ast->scope, ast->as_fn_def.block);
             }
             return;
         }
 
         case AST_NODE_FN_CALL: {
-            ast->as_fn_call.scope = scope;
-
             list_item_t *item = list_head(ast->as_fn_call.args);
 
             while (item != NULL) {
@@ -223,14 +223,12 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
                 symbol_new(checker->arena, id, ast->as_var_def.type);
 
             scope_insert(scope, symbol);
-            ast->as_var_def.scope = scope;
 
             populate_scope(checker, scope, ast->as_var_def.value);
             return;
         }
 
         case AST_NODE_REF: {
-            ast->as_ref.scope = scope;
             return;
         }
 
-- 
2.46.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [olang/patches/.build.yml] build success
  2024-10-23  2:20 ` [PATCH olang v1 5/5] semantics: add scope to all nodes Carlos Maniero
@ 2024-10-23  2:21   ` builds.sr.ht
  2024-10-23 17:36   ` [PATCH olang v1 5/5] semantics: add scope to all nodes Johnny Richard
  1 sibling, 0 replies; 9+ messages in thread
From: builds.sr.ht @ 2024-10-23  2:21 UTC (permalink / raw)
  To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel

olang/patches/.build.yml: SUCCESS in 29s

[Refactors to support semantics][0] from [Carlos Maniero][1]

[0]: https://lists.sr.ht/~johnnyrichard/olang-devel/patches/55597
[1]: mailto:carlos@maniero.me

✓ #1355239 SUCCESS olang/patches/.build.yml https://builds.sr.ht/~johnnyrichard/job/1355239

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH olang v1 0/5] Refactors to support semantics
  2024-10-23  2:20 [PATCH olang v1 0/5] Refactors to support semantics Carlos Maniero
                   ` (4 preceding siblings ...)
  2024-10-23  2:20 ` [PATCH olang v1 5/5] semantics: add scope to all nodes Carlos Maniero
@ 2024-10-23 17:34 ` Johnny Richard
  5 siblings, 0 replies; 9+ messages in thread
From: Johnny Richard @ 2024-10-23 17:34 UTC (permalink / raw)
  To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel

Applied partialy, the 5th patch we can reimplementing. Thanks!

https://builds.sr.ht/~johnnyrichard/job/1355498

To git.sr.ht:~johnnyrichard/olang
   2dbf9a9..f87fb37  main -> main


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH olang v1 5/5] semantics: add scope to all nodes
  2024-10-23  2:20 ` [PATCH olang v1 5/5] semantics: add scope to all nodes Carlos Maniero
  2024-10-23  2:21   ` [olang/patches/.build.yml] build success builds.sr.ht
@ 2024-10-23 17:36   ` Johnny Richard
  1 sibling, 0 replies; 9+ messages in thread
From: Johnny Richard @ 2024-10-23 17:36 UTC (permalink / raw)
  To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel

We decided to not set scope for all nodes, this will be very expensive
on big ast trees.  Most of the nodes won't need scope at all.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-10-23 15:37 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-23  2:20 [PATCH olang v1 0/5] Refactors to support semantics Carlos Maniero
2024-10-23  2:20 ` [PATCH olang v1 1/5] type_checker: rename checker to type_checker Carlos Maniero
2024-10-23  2:20 ` [PATCH olang v1 2/5] ast: remove redundancy on ast_node base fields Carlos Maniero
2024-10-23  2:20 ` [PATCH olang v1 3/5] ast: unary: rename ast_unary.expr to ast_unary.operand Carlos Maniero
2024-10-23  2:20 ` [PATCH olang v1 4/5] ast: remove expr reference from return and var assign nodes Carlos Maniero
2024-10-23  2:20 ` [PATCH olang v1 5/5] semantics: add scope to all nodes Carlos Maniero
2024-10-23  2:21   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-23 17:36   ` [PATCH olang v1 5/5] semantics: add scope to all nodes Johnny Richard
2024-10-23 17:34 ` [PATCH olang v1 0/5] Refactors to support semantics Johnny Richard

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