summaryrefslogtreecommitdiff
path: root/src/parser/parser_decl.c
diff options
context:
space:
mode:
authorSAJJA EASWAR <eshwarsajja20@gmail.com>2026-01-25 22:59:36 +0530
committerSAJJA EASWAR <eshwarsajja20@gmail.com>2026-01-25 22:59:36 +0530
commitebc8b94baa6bc694cb4829e2eb2934a1f17fa6a1 (patch)
tree71b952ad455bf17d5bdea01472f0e2297f25eabe /src/parser/parser_decl.c
parent863118c95caac0d69a35f6ae4d2e83844734a8a1 (diff)
parent489336b2101bf16edeec7bfc4379408eb19b936e (diff)
Merge branch 'main' into pr-109
Diffstat (limited to 'src/parser/parser_decl.c')
-rw-r--r--src/parser/parser_decl.c38
1 files changed, 32 insertions, 6 deletions
diff --git a/src/parser/parser_decl.c b/src/parser/parser_decl.c
index 7e0cc5b..5d5af1e 100644
--- a/src/parser/parser_decl.c
+++ b/src/parser/parser_decl.c
@@ -207,17 +207,43 @@ char *patch_self_args(const char *args, const char *struct_name)
{
return NULL;
}
- char *new_args = xmalloc(strlen(args) + strlen(struct_name) + 10);
+
+ // Sanitize struct name for C usage (Vec<T> -> Vec_T)
+ char *safe_name = xmalloc(strlen(struct_name) + 1);
+ int j = 0;
+ for (int i = 0; struct_name[i]; i++)
+ {
+ if (struct_name[i] == '<')
+ {
+ safe_name[j++] = '_';
+ }
+ else if (struct_name[i] == '>')
+ {
+ // skip
+ }
+ else if (struct_name[i] == ' ')
+ {
+ // skip
+ }
+ else
+ {
+ safe_name[j++] = struct_name[i];
+ }
+ }
+ safe_name[j] = 0;
+
+ char *new_args = xmalloc(strlen(args) + strlen(safe_name) + 10);
// Check if it starts with "void* self"
if (strncmp(args, "void* self", 10) == 0)
{
- sprintf(new_args, "%s* self%s", struct_name, args + 10);
+ sprintf(new_args, "%s* self%s", safe_name, args + 10);
}
else
{
strcpy(new_args, args);
}
+ free(safe_name);
return new_args;
}
// Helper for Value-Returning Defer
@@ -562,17 +588,17 @@ ASTNode *parse_var_decl(ParserContext *ctx, Lexer *l)
// Fallbacks for literals
else if (init->type == NODE_EXPR_LITERAL)
{
- if (init->literal.type_kind == 0)
+ if (init->literal.type_kind == LITERAL_INT)
{
type = xstrdup("int");
type_obj = type_new(TYPE_INT);
}
- else if (init->literal.type_kind == 1)
+ else if (init->literal.type_kind == LITERAL_FLOAT)
{
type = xstrdup("float");
type_obj = type_new(TYPE_FLOAT);
}
- else if (init->literal.type_kind == 2)
+ else if (init->literal.type_kind == LITERAL_STRING)
{
type = xstrdup("string");
type_obj = type_new(TYPE_STRING);
@@ -596,7 +622,7 @@ ASTNode *parse_var_decl(ParserContext *ctx, Lexer *l)
add_symbol_with_token(ctx, name, type, type_obj, name_tok);
// NEW: Capture Const Integer Values
- if (init && init->type == NODE_EXPR_LITERAL && init->literal.type_kind == 0)
+ if (init && init->type == NODE_EXPR_LITERAL && init->literal.type_kind == LITERAL_INT)
{
Symbol *s = find_symbol_entry(ctx, name); // Helper to find the struct
if (s)