From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v1 5/5] semantics: add scope to all nodes
Date: Wed, 23 Oct 2024 02:20:46 +0000 (UTC) [thread overview]
Message-ID: <20241023022022.38379-6-carlos@maniero.me> (raw)
In-Reply-To: <20241023022022.38379-1-carlos@maniero.me>
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
next prev parent reply other threads:[~2024-10-23 2:20 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Carlos Maniero [this message]
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
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=20241023022022.38379-6-carlos@maniero.me \
--to=carlos@maniero.me \
--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