From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang 1/5] parser: replace type enum to an struction string id representation
Date: Sat, 21 Sep 2024 08:24:53 +0000 (UTC) [thread overview]
Message-ID: <20240921082437.396691-2-carlos@maniero.me> (raw)
In-Reply-To: <20240921082437.396691-1-carlos@maniero.me>
Ensuring the type is correct is a semantics/checker role. Instead of
trying to converting it to an enum on the parses step it just store the
identifier of the given type.
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
src/ast.h | 4 ++--
src/parser.c | 7 ++-----
src/pretty_print_ast.c | 4 ++--
tests/integration/tests/0001_main_exit.ol | 2 +-
tests/integration/tests/0020_if_statement.ol | 2 +-
tests/integration/tests/0023_else_statement.ol | 2 +-
tests/integration/tests/0024_var_definition.ol | 2 +-
tests/unit/parser_test.c | 2 +-
8 files changed, 11 insertions(+), 14 deletions(-)
diff --git a/src/ast.h b/src/ast.h
index a45a271..72fcc3e 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -40,9 +40,9 @@ typedef enum
AST_NODE_UNKNOWN
} ast_node_kind_t;
-typedef enum
+typedef struct type
{
- TYPE_U32
+ string_view_t id;
} type_t;
typedef struct ast_block
diff --git a/src/parser.c b/src/parser.c
index d71500f..6076a64 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -317,12 +317,9 @@ parser_parse_type(parser_t *parser, type_t *type)
return false;
}
- if (string_view_eq_to_cstr(token.value, "u32")) {
- *type = TYPE_U32;
- return true;
- }
+ type->id = token.value;
- return false;
+ return true;
}
static ast_node_t *
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index d6eef67..8a02095 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -126,9 +126,9 @@ ast_node_to_pretty_print_node(ast_node_t *ast, arena_t *arena)
char name[256];
sprintf(name,
- "Function_Definition <name:" SV_FMT "> <return:%d>",
+ "Function_Definition <name:" SV_FMT "> <return:" SV_FMT ">",
SV_ARG(fn_def.identifier),
- fn_def.return_type);
+ SV_ARG(fn_def.return_type.id));
node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
diff --git a/tests/integration/tests/0001_main_exit.ol b/tests/integration/tests/0001_main_exit.ol
index f446eb9..9068434 100644
--- a/tests/integration/tests/0001_main_exit.ol
+++ b/tests/integration/tests/0001_main_exit.ol
@@ -24,7 +24,7 @@ fn main(): u32 {
#
# TEST test_ast WITH
# Translation_Unit
-# `-Function_Definition <name:main> <return:0>
+# `-Function_Definition <name:main> <return:u32>
# `-Block
# `-Return_Statement
# `-Literal <kind:u32> <value:0>
diff --git a/tests/integration/tests/0020_if_statement.ol b/tests/integration/tests/0020_if_statement.ol
index d48122f..460a05e 100644
--- a/tests/integration/tests/0020_if_statement.ol
+++ b/tests/integration/tests/0020_if_statement.ol
@@ -29,7 +29,7 @@ fn main(): u32 {
# TEST test_ast WITH
# Translation_Unit
-# `-Function_Definition <name:main> <return:0>
+# `-Function_Definition <name:main> <return:u32>
# `-Block
# |-If_Statement
# | |-Binary_Operation (==)
diff --git a/tests/integration/tests/0023_else_statement.ol b/tests/integration/tests/0023_else_statement.ol
index fafb22e..f3ddc64 100644
--- a/tests/integration/tests/0023_else_statement.ol
+++ b/tests/integration/tests/0023_else_statement.ol
@@ -31,7 +31,7 @@ fn main(): u32 {
# TEST test_ast WITH
# Translation_Unit
-# `-Function_Definition <name:main> <return:0>
+# `-Function_Definition <name:main> <return:u32>
# `-Block
# `-If_Statement
# |-Binary_Operation (!=)
diff --git a/tests/integration/tests/0024_var_definition.ol b/tests/integration/tests/0024_var_definition.ol
index 5c23449..ebbcda4 100644
--- a/tests/integration/tests/0024_var_definition.ol
+++ b/tests/integration/tests/0024_var_definition.ol
@@ -28,7 +28,7 @@ fn main(): u32 {
# TEST test_ast WITH
# Translation_Unit
-# `-Function_Definition <name:main> <return:0>
+# `-Function_Definition <name:main> <return:u32>
# `-Block
# |-Var_Definition <name:code> <kind:u32>
# | `-Literal <kind:u32> <value:0>
diff --git a/tests/unit/parser_test.c b/tests/unit/parser_test.c
index 3cdac41..33657df 100644
--- a/tests/unit/parser_test.c
+++ b/tests/unit/parser_test.c
@@ -51,7 +51,7 @@ parse_program_test(const MunitParameter params[], void *user_data_or_fixture)
ast_fn_definition_t fn = program.fn->as_fn_def;
assert_memory_equal(fn.identifier.size, fn.identifier.chars, "main");
- assert_uint(fn.return_type, ==, TYPE_U32);
+ assert_memory_equal(fn.return_type.id.size, fn.return_type.id.chars, "u32");
ast_node_t *block = fn.block;
assert_not_null(block);
--
2.34.1
next prev parent reply other threads:[~2024-09-21 8:24 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-21 8:24 [PATCH olang 0/5] extend unsined integers types (u8, u16, u64) Carlos Maniero
2024-09-21 8:24 ` Carlos Maniero [this message]
2024-09-21 17:59 ` [PATCH olang 1/5] parser: replace type enum to an struction string id representation Johnny Richard
2024-09-21 8:24 ` [PATCH olang 2/5] checker: scope: populate symbol's type Carlos Maniero
2024-09-21 18:47 ` Johnny Richard
2024-09-21 21:23 ` Carlos Maniero
2024-09-22 13:46 ` Johnny Richard
2024-09-21 8:24 ` [PATCH olang 3/5] codegen: fix map simbol list type Carlos Maniero
2024-09-21 18:50 ` Johnny Richard
2024-09-21 8:25 ` [PATCH olang 4/5] codegen: calculate the variable offset based on symbol type Carlos Maniero
2024-09-21 18:56 ` Johnny Richard
2024-09-21 8:25 ` [PATCH olang 5/5] codegen: perform mov instructions based on variable type Carlos Maniero
2024-09-21 8:26 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-21 19:17 ` [PATCH olang 5/5] codegen: perform mov instructions based on variable type Johnny Richard
2024-09-21 21:30 ` Carlos Maniero
2024-09-22 14:16 ` [PATCH olang 0/5] extend unsined integers types (u8, u16, u64) 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=20240921082437.396691-2-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