* [PATCH olang] parser: parse pointer types
@ 2024-10-09 12:19 Carlos Maniero
2024-10-09 12:19 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-09 14:38 ` [PATCH olang] parser: parse pointer types Johnny Richard
0 siblings, 2 replies; 3+ messages in thread
From: Carlos Maniero @ 2024-10-09 12:19 UTC (permalink / raw)
To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero
Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
src/checker.c | 2 ++
src/codegen_linux_x86_64.c | 3 ++
src/parser.c | 59 +++++++++++++++++++++++---------------
src/pretty_print_ast.c | 2 +-
src/type.c | 14 ++++++++-
src/type.h | 19 ++++++++++--
6 files changed, 72 insertions(+), 27 deletions(-)
diff --git a/src/checker.c b/src/checker.c
index c688b7a..7a9a7b6 100644
--- a/src/checker.c
+++ b/src/checker.c
@@ -81,6 +81,8 @@ type_resolve(type_t *type)
case TYPE_UNKNOWN:
*type = type_from_id(type->as_unknown.id);
break;
+ case TYPE_PTR:
+ type_resolve(type->as_ptr.type);
case TYPE_PRIMITIVE:
break;
}
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 703f3b5..ac268ad 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -682,6 +682,9 @@ type_to_bytes(type_t *type)
case TYPE_PRIMITIVE: {
return type->as_primitive.size;
}
+ case TYPE_PTR: {
+ return 8;
+ }
case TYPE_UNKNOWN: {
assert(0 && "cannot calculate size of an unknown type: probably a parser issue.");
}
diff --git a/src/parser.c b/src/parser.c
index 6875b42..0bfa282 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -34,8 +34,8 @@ expected_next_token(parser_t *parser, token_t *token, token_kind_t kind);
static bool
expected_token(token_t *token, token_kind_t kind);
-static bool
-parser_parse_type(parser_t *parser, string_view_t *type);
+static type_t *
+parser_parse_type(parser_t *parser);
static ast_node_t *
parser_parse_block(parser_t *parser);
@@ -368,10 +368,13 @@ parser_parse_fn_params(parser_t *parser)
return NULL;
}
- string_view_t type_id;
- parser_parse_type(parser, &type_id);
+ type_t *type = parser_parse_type(parser);
- ast_fn_param_t *param = ast_new_fn_param(parser->arena, token.value, type_new_unknown(parser->arena, type_id));
+ if (type == NULL) {
+ return NULL;
+ }
+
+ ast_fn_param_t *param = ast_new_fn_param(parser->arena, token.value, type);
list_append(params, param);
skip_line_feeds(parser->lexer);
@@ -408,8 +411,9 @@ parser_parse_fn_definition(parser_t *parser)
return NULL;
}
- string_view_t fn_return_type;
- if (!parser_parse_type(parser, &fn_return_type)) {
+ type_t *ret_type = parser_parse_type(parser);
+
+ if (ret_type == NULL) {
return NULL;
}
@@ -420,21 +424,16 @@ parser_parse_fn_definition(parser_t *parser)
return NULL;
}
- return ast_new_node_fn_def(parser->arena,
- fn_name_token.loc,
- fn_name_token.value,
- params,
- type_new_unknown(parser->arena, fn_return_type),
- block);
+ return ast_new_node_fn_def(parser->arena, fn_name_token.loc, fn_name_token.value, params, ret_type, block);
}
-static bool
-parser_parse_type(parser_t *parser, string_view_t *type)
+static type_t *
+parser_parse_type(parser_t *parser)
{
skip_line_feeds(parser->lexer);
if (!skip_expected_token(parser, TOKEN_COLON)) {
- return false;
+ return NULL;
}
skip_line_feeds(parser->lexer);
@@ -442,12 +441,27 @@ parser_parse_type(parser_t *parser, string_view_t *type)
token_t token;
if (!expected_next_token(parser, &token, TOKEN_ID)) {
- return false;
+ return NULL;
}
- *type = token.value;
+ token_t ptr_token;
- return true;
+ lexer_peek_next(parser->lexer, &ptr_token);
+
+ type_t *type = type_new_unknown(parser->arena, token.value);
+
+ if (ptr_token.kind == TOKEN_STAR) {
+ if (!skip_expected_token(parser, TOKEN_STAR)) {
+ return NULL;
+ }
+ string_view_t ptr_id = token.value;
+
+ ptr_id.size = ptr_token.value.chars - token.value.chars + ptr_token.value.size;
+
+ return type_new_ptr(parser->arena, ptr_id, type);
+ }
+
+ return type;
}
static ast_node_t *
@@ -642,8 +656,8 @@ parser_parse_var_def(parser_t *parser)
return NULL;
}
- string_view_t var_type;
- if (!parser_parse_type(parser, &var_type)) {
+ type_t *type = parser_parse_type(parser);
+ if (type == NULL) {
return NULL;
}
@@ -657,8 +671,7 @@ parser_parse_var_def(parser_t *parser)
return NULL;
}
- ast_node_t *var_node = ast_new_node_var_def(
- parser->arena, token_id.loc, token_id.value, type_new_unknown(parser->arena, var_type), expr);
+ ast_node_t *var_node = ast_new_node_var_def(parser->arena, token_id.loc, token_id.value, type, expr);
return var_node;
}
diff --git a/src/pretty_print_ast.c b/src/pretty_print_ast.c
index 5feb270..edc6320 100644
--- a/src/pretty_print_ast.c
+++ b/src/pretty_print_ast.c
@@ -274,7 +274,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.id));
+ sprintf(name, "Var_Definition <name:" SV_FMT "> <kind:" SV_FMT ">", SV_ARG(var.id), SV_ARG(var.type->id));
node->name = (char *)arena_alloc(arena, sizeof(char) * (strlen(name) + 1));
strcpy(node->name, name);
diff --git a/src/type.c b/src/type.c
index 9de5650..4a8d6f4 100644
--- a/src/type.c
+++ b/src/type.c
@@ -24,6 +24,18 @@ type_new_unknown(arena_t *arena, string_view_t id)
assert(type);
type->kind = TYPE_UNKNOWN;
- type->as_unknown.id = id;
+ type->id = id;
+ return type;
+}
+
+type_t *
+type_new_ptr(arena_t *arena, string_view_t id, type_t *ref_type)
+{
+ type_t *type = arena_alloc(arena, sizeof(type_t));
+ assert(type);
+
+ type->kind = TYPE_PTR;
+ type->id = id;
+ type->as_ptr.type = ref_type;
return type;
}
diff --git a/src/type.h b/src/type.h
index 97a2a41..d930a88 100644
--- a/src/type.h
+++ b/src/type.h
@@ -18,10 +18,14 @@
#define TYPE_H
#include "arena.h"
#include "string_view.h"
+
+typedef union type type_t;
+
typedef enum
{
TYPE_UNKNOWN,
- TYPE_PRIMITIVE
+ TYPE_PRIMITIVE,
+ TYPE_PTR
} type_kind_t;
typedef enum
@@ -46,7 +50,14 @@ typedef struct type_unknown
string_view_t id;
} type_unknown_t;
-typedef union
+typedef struct type_ptr
+{
+ type_kind_t _type_kind;
+ string_view_t id;
+ type_t *type;
+} type_ptr_t;
+
+typedef union type
{
struct
{
@@ -55,8 +66,12 @@ typedef union
};
type_unknown_t as_unknown;
type_primitive_t as_primitive;
+ type_ptr_t as_ptr;
} type_t;
type_t *
type_new_unknown(arena_t *arena, string_view_t id);
+
+type_t *
+type_new_ptr(arena_t *arena, string_view_t id, type_t *type);
#endif
base-commit: 7f8968454f0aece10c70340f17422d13d14cca79
--
2.46.0
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-10-09 12:38 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-09 12:19 [PATCH olang] parser: parse pointer types Carlos Maniero
2024-10-09 12:19 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-09 14:38 ` [PATCH olang] parser: parse pointer types 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