* [PATCH olang v1 1/3] ast: return_stmt rename expr field and add it to the factory fn
2024-09-23 10:11 [PATCH olang v1 0/3] Housekeeping: resolve a few FIXMEs related to code style Carlos Maniero
@ 2024-09-23 10:11 ` Carlos Maniero
2024-09-23 11:02 ` Johnny Richard
2024-09-23 10:11 ` [PATCH olang v1 2/3] codegen: move label_index global variable to the codegen scope Carlos Maniero
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: Carlos Maniero @ 2024-09-23 10:11 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero
The name *data* field was not very meaningful. The
*ast_new_node_return_stmt* was not receiving the expr as argument, it
was changed to keep it consistent, once we always expect the factory
functions to return a valid ast_node.
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
src/ast.c | 3 ++-
src/ast.h | 5 ++---
src/checker.c | 2 +-
src/codegen_linux_aarch64.c | 2 +-
src/codegen_linux_x86_64.c | 2 +-
src/parser.c | 6 ++----
src/pretty_print_ast.c | 2 +-
tests/unit/parser_test.c | 2 +-
8 files changed, 11 insertions(+), 13 deletions(-)
diff --git a/src/ast.c b/src/ast.c
index 02b938e..a136182 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -108,12 +108,13 @@ ast_new_node_ref(arena_t *arena, string_view_t identifier)
}
ast_node_t *
-ast_new_node_return_stmt(arena_t *arena)
+ast_new_node_return_stmt(arena_t *arena, ast_node_t *expr)
{
ast_node_t *node_return_stmt = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_return_stmt);
node_return_stmt->kind = AST_NODE_RETURN_STMT;
+ node_return_stmt->as_return_stmt.expr = expr;
return node_return_stmt;
}
diff --git a/src/ast.h b/src/ast.h
index 7cb2ceb..e618314 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -118,8 +118,7 @@ typedef struct ast_binary_op
typedef struct ast_return_stmt
{
- // FIXME: rename to a meaningful name like expr
- ast_node_t *data;
+ ast_node_t *expr;
} ast_return_stmt_t;
typedef struct ast_if_stmt
@@ -165,7 +164,7 @@ ast_node_t *
ast_new_node_ref(arena_t *arena, string_view_t identifier);
ast_node_t *
-ast_new_node_return_stmt(arena_t *arena);
+ast_new_node_return_stmt(arena_t *arena, ast_node_t *expr);
ast_node_t *
ast_new_node_if_stmt(arena_t *arena, ast_node_t *cond, ast_node_t *then, ast_node_t *_else);
diff --git a/src/checker.c b/src/checker.c
index 6ffdc72..def7e86 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -87,7 +87,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.data);
+ populate_scope(checker, scope, return_stmt.expr);
return;
}
diff --git a/src/codegen_linux_aarch64.c b/src/codegen_linux_aarch64.c
index dafdcc4..18173ce 100644
--- a/src/codegen_linux_aarch64.c
+++ b/src/codegen_linux_aarch64.c
@@ -81,7 +81,7 @@ codegen_linux_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.data;
+ ast_node_t *literal_node = return_stmt.expr;
assert(literal_node->kind == AST_NODE_LITERAL);
ast_literal_t literal_u32 = literal_node->as_literal;
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 25cda2d..e604144 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -355,7 +355,7 @@ codegen_linux_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.data;
+ ast_node_t *expr = return_stmt.expr;
codegen_linux_x86_64_emit_expression(codegen, expr);
diff --git a/src/parser.c b/src/parser.c
index e4d0c56..a63a724 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -388,15 +388,13 @@ parser_parse_return_stmt(parser_t *parser)
return NULL;
}
- ast_node_t *node_return_stmt = ast_new_node_return_stmt(parser->arena);
- assert(node_return_stmt);
-
ast_node_t *expr = parser_parse_expr(parser);
if (expr == NULL) {
return NULL;
}
- node_return_stmt->as_return_stmt.data = expr;
+ ast_node_t *node_return_stmt = ast_new_node_return_stmt(parser->arena, expr);
+ assert(node_return_stmt);
if (!skip_expected_token(parser, TOKEN_LF)) {
return NULL;
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index 1fc6dff..a7c75e8 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -156,7 +156,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.data, arena);
+ pretty_print_node_t *child = ast_node_to_pretty_print_node(return_stmt.expr, arena);
list_append(node->children, child);
return node;
diff --git a/tests/unit/parser_test.c b/tests/unit/parser_test.c
index 2256ffd..ccec460 100644
--- a/tests/unit/parser_test.c
+++ b/tests/unit/parser_test.c
@@ -66,7 +66,7 @@ parse_program_test(const MunitParameter params[], void *user_data_or_fixture)
assert_not_null(node);
assert_uint(node->kind, ==, AST_NODE_RETURN_STMT);
- ast_node_t *number_node = node->as_return_stmt.data;
+ ast_node_t *number_node = node->as_return_stmt.expr;
assert_not_null(number_node);
assert_uint(number_node->kind, ==, AST_NODE_LITERAL);
assert_uint(number_node->as_literal.kind, ==, AST_LITERAL_U32);
--
2.34.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH olang v1 3/3] naming: rename all identifier symbols to id
2024-09-23 10:11 [PATCH olang v1 0/3] Housekeeping: resolve a few FIXMEs related to code style Carlos Maniero
2024-09-23 10:11 ` [PATCH olang v1 1/3] ast: return_stmt rename expr field and add it to the factory fn Carlos Maniero
2024-09-23 10:11 ` [PATCH olang v1 2/3] codegen: move label_index global variable to the codegen scope Carlos Maniero
@ 2024-09-23 10:11 ` Carlos Maniero
2024-09-23 10:12 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-23 11:08 ` [PATCH olang v1 0/3] Housekeeping: resolve a few FIXMEs related to code style Johnny Richard
3 siblings, 1 reply; 8+ messages in thread
From: Carlos Maniero @ 2024-09-23 10:11 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero
We were already using this pattern but initially we were callig them the
full name.
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
src/ast.c | 12 ++++++------
src/ast.h | 12 ++++++------
src/checker.c | 2 +-
src/codegen_linux_aarch64.c | 4 ++--
src/codegen_linux_x86_64.c | 8 ++++----
src/lexer.c | 4 ++--
src/lexer.h | 2 +-
src/parser.c | 12 ++++++------
src/pretty_print_ast.c | 6 +++---
tests/unit/parser_test.c | 2 +-
10 files changed, 32 insertions(+), 32 deletions(-)
diff --git a/src/ast.c b/src/ast.c
index a136182..7019316 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -37,7 +37,7 @@ ast_new_program(arena_t *arena, ast_node_t *fn_def)
}
ast_node_t *
-ast_new_node_fn_def(arena_t *arena, string_view_t identifier, string_view_t return_type, ast_node_t *block)
+ast_new_node_fn_def(arena_t *arena, string_view_t id, string_view_t return_type, ast_node_t *block)
{
ast_node_t *node_fn_def = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_fn_def);
@@ -45,7 +45,7 @@ ast_new_node_fn_def(arena_t *arena, string_view_t identifier, string_view_t retu
node_fn_def->kind = AST_NODE_FN_DEF;
ast_fn_definition_t *fn_def = &node_fn_def->as_fn_def;
- fn_def->identifier = identifier;
+ fn_def->id = id;
fn_def->return_type = return_type;
fn_def->block = block;
@@ -53,7 +53,7 @@ ast_new_node_fn_def(arena_t *arena, string_view_t identifier, string_view_t retu
}
ast_node_t *
-ast_new_node_var_def(arena_t *arena, string_view_t identifier, string_view_t type, ast_node_t *value)
+ast_new_node_var_def(arena_t *arena, string_view_t id, string_view_t type, ast_node_t *value)
{
ast_node_t *node_var_def = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_var_def);
@@ -61,7 +61,7 @@ ast_new_node_var_def(arena_t *arena, string_view_t identifier, string_view_t typ
node_var_def->kind = AST_NODE_VAR_DEF;
ast_var_definition_t *var_def = &node_var_def->as_var_def;
- var_def->identifier = identifier;
+ var_def->id = id;
var_def->type = type;
var_def->value = value;
@@ -96,13 +96,13 @@ ast_new_node_literal_u32(arena_t *arena, uint32_t value)
}
ast_node_t *
-ast_new_node_ref(arena_t *arena, string_view_t identifier)
+ast_new_node_ref(arena_t *arena, string_view_t id)
{
ast_node_t *node_ref = (ast_node_t *)arena_alloc(arena, sizeof(ast_node_t));
assert(node_ref);
node_ref->kind = AST_NODE_REF;
- node_ref->as_ref.identifier = identifier;
+ node_ref->as_ref.id = id;
return node_ref;
}
diff --git a/src/ast.h b/src/ast.h
index e618314..df65e59 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -53,7 +53,7 @@ typedef struct ast_program
typedef struct ast_fn_definition
{
- string_view_t identifier;
+ string_view_t id;
string_view_t return_type;
ast_node_t *block;
scope_t *scope;
@@ -61,7 +61,7 @@ typedef struct ast_fn_definition
typedef struct ast_var_definition
{
- string_view_t identifier;
+ string_view_t id;
string_view_t type;
ast_node_t *value;
scope_t *scope;
@@ -83,7 +83,7 @@ typedef struct ast_literal
typedef struct ast_ref
{
- string_view_t identifier;
+ string_view_t id;
scope_t *scope;
} ast_ref_t;
@@ -149,10 +149,10 @@ ast_node_t *
ast_new_program(arena_t *arena, ast_node_t *fn_def);
ast_node_t *
-ast_new_node_fn_def(arena_t *arena, string_view_t identifier, string_view_t return_type, ast_node_t *block);
+ast_new_node_fn_def(arena_t *arena, string_view_t id, string_view_t return_type, ast_node_t *block);
ast_node_t *
-ast_new_node_var_def(arena_t *arena, string_view_t identifier, string_view_t type, ast_node_t *value);
+ast_new_node_var_def(arena_t *arena, string_view_t id, string_view_t type, ast_node_t *value);
ast_node_t *
ast_new_node_bin_op(arena_t *arena, ast_binary_op_kind_t kind, ast_node_t *lhs, ast_node_t *rhs);
@@ -161,7 +161,7 @@ ast_node_t *
ast_new_node_literal_u32(arena_t *arena, uint32_t value);
ast_node_t *
-ast_new_node_ref(arena_t *arena, string_view_t identifier);
+ast_new_node_ref(arena_t *arena, string_view_t id);
ast_node_t *
ast_new_node_return_stmt(arena_t *arena, ast_node_t *expr);
diff --git a/src/checker.c b/src/checker.c
index def7e86..090920c 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -106,7 +106,7 @@ populate_scope(checker_t *checker, scope_t *scope, ast_node_t *ast)
}
case AST_NODE_VAR_DEF: {
- string_view_t id = ast->as_var_def.identifier;
+ string_view_t id = ast->as_var_def.id;
symbol_t *symbol = symbol_new(checker->arena, id, type_from_id(ast->as_var_def.type));
diff --git a/src/codegen_linux_aarch64.c b/src/codegen_linux_aarch64.c
index 18173ce..93dde4f 100644
--- a/src/codegen_linux_aarch64.c
+++ b/src/codegen_linux_aarch64.c
@@ -51,7 +51,7 @@ codegen_linux_aarch64_emit_program(FILE *out, ast_node_t *node)
ast_fn_definition_t fn = program.fn->as_fn_def;
- assert(string_view_eq_to_cstr(fn.identifier, "main"));
+ assert(string_view_eq_to_cstr(fn.id, "main"));
codegen_linux_aarch64_emit_function(out, &fn);
}
@@ -88,7 +88,7 @@ codegen_linux_aarch64_emit_function(FILE *out, ast_fn_definition_t *fn)
assert(literal_u32.kind == AST_LITERAL_U32);
uint32_t exit_code = literal_u32.as_u32;
- fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->identifier));
+ fprintf(out, "" SV_FMT ":\n", SV_ARG(fn->id));
fprintf(out, " mov x0, #%d\n", exit_code);
fprintf(out, " ret\n");
}
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 2e329ca..a0d9d97 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -65,7 +65,7 @@ codegen_linux_x86_64_emit_program(codegen_x86_64_t *codegen, ast_node_t *node)
ast_fn_definition_t fn = program.fn->as_fn_def;
- assert(string_view_eq_to_cstr(fn.identifier, "main"));
+ assert(string_view_eq_to_cstr(fn.id, "main"));
codegen_linux_x86_64_emit_function(codegen, &fn);
}
@@ -103,7 +103,7 @@ codegen_linux_x86_64_emit_expression(codegen_x86_64_t *codegen, ast_node_t *expr
case AST_NODE_REF: {
ast_ref_t ref = expr_node->as_ref;
- symbol_t *symbol = scope_lookup(ref.scope, ref.identifier);
+ symbol_t *symbol = scope_lookup(ref.scope, ref.id);
assert(symbol);
char symbol_ptr[PTR_HEX_CSTR_SIZE];
@@ -366,7 +366,7 @@ codegen_linux_x86_64_emit_block(codegen_x86_64_t *codegen, ast_block_t *block)
ast_var_definition_t var_def = node->as_var_def;
scope_t *scope = var_def.scope;
- symbol_t *symbol = scope_lookup(scope, var_def.identifier);
+ symbol_t *symbol = scope_lookup(scope, var_def.id);
assert(symbol);
char symbol_ptr[PTR_HEX_CSTR_SIZE];
@@ -486,7 +486,7 @@ codegen_linux_x86_64_emit_function(codegen_x86_64_t *codegen, ast_fn_definition_
codegen->base_offset = X86_CALL_EIP_STACK_OFFSET;
ast_node_t *block_node = fn->block;
- fprintf(codegen->out, "" SV_FMT ":\n", SV_ARG(fn->identifier));
+ fprintf(codegen->out, "" SV_FMT ":\n", SV_ARG(fn->id));
fprintf(codegen->out, " mov %%rsp, %%rbp\n");
diff --git a/src/lexer.c b/src/lexer.c
index 12b4719..ebc21b7 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -278,7 +278,7 @@ lexer_next_token(lexer_t *lexer, token_t *token)
static char *token_kind_str_table[] = {
[TOKEN_UNKNOWN] = "unknown",
- [TOKEN_IDENTIFIER] = "identifier",
+ [TOKEN_ID] = "identifier",
[TOKEN_NUMBER] = "number",
[TOKEN_FN] = "fn",
[TOKEN_RETURN] = "return",
@@ -432,7 +432,7 @@ lexer_str_to_token_kind(string_view_t text)
return TOKEN_FN;
}
- return TOKEN_IDENTIFIER;
+ return TOKEN_ID;
}
void
diff --git a/src/lexer.h b/src/lexer.h
index 4a0e2e1..717d21d 100644
--- a/src/lexer.h
+++ b/src/lexer.h
@@ -32,7 +32,7 @@ typedef struct lexer
typedef enum token_kind
{
TOKEN_UNKNOWN,
- TOKEN_IDENTIFIER,
+ TOKEN_ID,
TOKEN_NUMBER,
// Keywords
diff --git a/src/parser.c b/src/parser.c
index a63a724..3cae763 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -236,7 +236,7 @@ parser_parse_factor(parser_t *parser)
case TOKEN_NUMBER:
return ast_new_node_literal_u32(parser->arena, string_view_to_u32(token.value));
- case TOKEN_IDENTIFIER:
+ case TOKEN_ID:
return ast_new_node_ref(parser->arena, token.value);
case TOKEN_OPAREN: {
@@ -269,7 +269,7 @@ parser_parse_fn_definition(parser_t *parser)
token_t fn_name_token;
- if (!expected_next_token(parser, &fn_name_token, TOKEN_IDENTIFIER)) {
+ if (!expected_next_token(parser, &fn_name_token, TOKEN_ID)) {
return NULL;
}
@@ -313,7 +313,7 @@ parser_parse_type(parser_t *parser, string_view_t *type)
token_t token;
- if (!expected_next_token(parser, &token, TOKEN_IDENTIFIER)) {
+ if (!expected_next_token(parser, &token, TOKEN_ID)) {
return false;
}
@@ -459,8 +459,8 @@ parser_parse_var_def(parser_t *parser)
return NULL;
}
- token_t identifier_token;
- if (!expected_next_token(parser, &identifier_token, TOKEN_IDENTIFIER)) {
+ token_t id_token;
+ if (!expected_next_token(parser, &id_token, TOKEN_ID)) {
return NULL;
}
@@ -479,7 +479,7 @@ parser_parse_var_def(parser_t *parser)
return NULL;
}
- ast_node_t *var_node = ast_new_node_var_def(parser->arena, identifier_token.value, var_type, expr);
+ ast_node_t *var_node = ast_new_node_var_def(parser->arena, id_token.value, var_type, expr);
skip_line_feeds(parser->lexer);
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index a7c75e8..1d5576d 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -127,7 +127,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
char name[256];
sprintf(name,
"Function_Definition <name:" SV_FMT "> <return:" SV_FMT ">",
- SV_ARG(fn_def.identifier),
+ SV_ARG(fn_def.id),
SV_ARG(fn_def.return_type));
node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
@@ -203,7 +203,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
ast_var_definition_t var = ast->as_var_def;
char name[256];
- sprintf(name, "Var_Definition <name:" SV_FMT "> <kind:u32>", SV_ARG(var.identifier));
+ sprintf(name, "Var_Definition <name:" SV_FMT "> <kind:u32>", SV_ARG(var.id));
node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
@@ -217,7 +217,7 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
ast_ref_t ref = ast->as_ref;
char name[256];
- sprintf(name, "Reference <name:" SV_FMT ">", SV_ARG(ref.identifier));
+ sprintf(name, "Reference <name:" SV_FMT ">", SV_ARG(ref.id));
node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
diff --git a/tests/unit/parser_test.c b/tests/unit/parser_test.c
index ccec460..8ad16b5 100644
--- a/tests/unit/parser_test.c
+++ b/tests/unit/parser_test.c
@@ -50,7 +50,7 @@ parse_program_test(const MunitParameter params[], void *user_data_or_fixture)
assert_uint(program.fn->kind, ==, AST_NODE_FN_DEF);
ast_fn_definition_t fn = program.fn->as_fn_def;
- assert_memory_equal(fn.identifier.size, fn.identifier.chars, "main");
+ assert_memory_equal(fn.id.size, fn.id.chars, "main");
assert_memory_equal(fn.return_type.size, fn.return_type.chars, "u32");
ast_node_t *block = fn.block;
--
2.34.1
^ permalink raw reply [flat|nested] 8+ messages in thread