public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v2 1/4] parser: replace type enum to a string view
Date: Sun, 22 Sep 2024 00:46:35 +0000 (UTC)	[thread overview]
Message-ID: <20240922004619.518310-2-carlos@maniero.me> (raw)
In-Reply-To: <20240922004619.518310-1-carlos@maniero.me>

Ensuring the type is correct is a semantics/checker role. Instead of
trying to convert it to an enum on the parser, it just stores the
identifier of the given type.

Once our spec always expects a TOKEN_COLON (:) before the type
identifier, this constraint was moved to the type parser function.

The string_view is enough until we decide how we gonna handle pointers
and arrays.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 src/ast.c                                     |  4 +--
 src/ast.h                                     | 13 +++----
 src/parser.c                                  | 35 ++++++++-----------
 src/pretty_print_ast.c                        |  4 +--
 tests/integration/tests/0001_main_exit.ol     |  2 +-
 tests/integration/tests/0020_if_statement.ol  |  2 +-
 .../integration/tests/0023_else_statement.ol  |  2 +-
 .../integration/tests/0024_var_definition.ol  |  2 +-
 tests/unit/parser_test.c                      |  2 +-
 9 files changed, 27 insertions(+), 39 deletions(-)

diff --git a/src/ast.c b/src/ast.c
index d2fd2fa..02b938e 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, type_t return_type, ast_node_t *block)
+ast_new_node_fn_def(arena_t *arena, string_view_t identifier, 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);
@@ -53,7 +53,7 @@ ast_new_node_fn_def(arena_t *arena, string_view_t identifier, type_t return_type
 }
 
 ast_node_t *
-ast_new_node_var_def(arena_t *arena, string_view_t identifier, type_t type, ast_node_t *value)
+ast_new_node_var_def(arena_t *arena, string_view_t identifier, 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);
diff --git a/src/ast.h b/src/ast.h
index a45a271..ff26ed8 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -40,11 +40,6 @@ typedef enum
     AST_NODE_UNKNOWN
 } ast_node_kind_t;
 
-typedef enum
-{
-    TYPE_U32
-} type_t;
-
 typedef struct ast_block
 {
     list_t *nodes;
@@ -58,7 +53,7 @@ typedef struct ast_program
 typedef struct ast_fn_definition
 {
     string_view_t identifier;
-    type_t return_type;
+    string_view_t return_type;
     ast_node_t *block;
     scope_t *scope;
 } ast_fn_definition_t;
@@ -66,7 +61,7 @@ typedef struct ast_fn_definition
 typedef struct ast_var_definition
 {
     string_view_t identifier;
-    type_t type;
+    string_view_t type;
     ast_node_t *value;
     scope_t *scope;
 } ast_var_definition_t;
@@ -154,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, type_t return_type, ast_node_t *block);
+ast_new_node_fn_def(arena_t *arena, string_view_t identifier, string_view_t return_type, ast_node_t *block);
 
 ast_node_t *
-ast_new_node_var_def(arena_t *arena, string_view_t identifier, type_t type, ast_node_t *value);
+ast_new_node_var_def(arena_t *arena, string_view_t identifier, 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);
diff --git a/src/parser.c b/src/parser.c
index d71500f..e4d0c56 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -34,7 +34,7 @@ static bool
 expected_token(parser_t *parser, token_t *token, token_kind_t kind);
 
 static bool
-parser_parse_type(parser_t *parser, type_t *type);
+parser_parse_type(parser_t *parser, string_view_t *type);
 
 static ast_node_t *
 parser_parse_block(parser_t *parser);
@@ -285,15 +285,7 @@ parser_parse_fn_definition(parser_t *parser)
         return NULL;
     }
 
-    skip_line_feeds(parser->lexer);
-
-    if (!skip_expected_token(parser, TOKEN_COLON)) {
-        return NULL;
-    }
-
-    skip_line_feeds(parser->lexer);
-
-    type_t fn_return_type;
+    string_view_t fn_return_type;
     if (!parser_parse_type(parser, &fn_return_type)) {
         return NULL;
     }
@@ -309,20 +301,25 @@ parser_parse_fn_definition(parser_t *parser)
 }
 
 static bool
-parser_parse_type(parser_t *parser, type_t *type)
+parser_parse_type(parser_t *parser, string_view_t *type)
 {
+    skip_line_feeds(parser->lexer);
+
+    if (!skip_expected_token(parser, TOKEN_COLON)) {
+        return false;
+    }
+
+    skip_line_feeds(parser->lexer);
+
     token_t token;
 
     if (!expected_next_token(parser, &token, TOKEN_IDENTIFIER)) {
         return false;
     }
 
-    if (string_view_eq_to_cstr(token.value, "u32")) {
-        *type = TYPE_U32;
-        return true;
-    }
+    *type = token.value;
 
-    return false;
+    return true;
 }
 
 static ast_node_t *
@@ -469,11 +466,7 @@ parser_parse_var_def(parser_t *parser)
         return NULL;
     }
 
-    if (!skip_expected_token(parser, TOKEN_COLON)) {
-        return NULL;
-    }
-
-    type_t var_type;
+    string_view_t var_type;
     if (!parser_parse_type(parser, &var_type)) {
         return NULL;
     }
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index d6eef67..1fc6dff 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));
             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..2256ffd 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.size, fn.return_type.chars, "u32");
 
     ast_node_t *block = fn.block;
     assert_not_null(block);
-- 
2.34.1


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

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-22  0:46 [PATCH olang v2 0/4] extend unsined integers types (u8, u16, u64) Carlos Maniero
2024-09-22  0:46 ` Carlos Maniero [this message]
2024-09-22  0:46 ` [PATCH olang v2 2/4] checker: scope: populate symbol's type Carlos Maniero
2024-09-22  0:46 ` [PATCH olang v2 3/4] codegen: fix map symbol list type Carlos Maniero
2024-09-22  0:46 ` [PATCH olang v2 4/4] codegen: operate mov instructions based on the symbol's type Carlos Maniero
2024-09-22  0:47   ` [olang/patches/.build.yml] build success builds.sr.ht
2024-09-22 14:15   ` [PATCH olang v2 4/4] codegen: operate mov instructions based on the symbol's type Johnny Richard
2024-09-22 14:19 ` [PATCH olang v2 0/4] 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=20240922004619.518310-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