summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-17 18:09:31 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-17 18:09:31 +0000
commit7ac5be7ba8f700f69009c5e980ee7b12b0653586 (patch)
tree85c44ed59d768d0bae8849b4590655b82a2a8c57
parent661a71defc66cfeea1681dddd944ba017087f78a (diff)
Fix for #60
-rw-r--r--src/codegen/codegen.h1
-rw-r--r--src/codegen/codegen_decl.c11
-rw-r--r--src/codegen/codegen_main.c3
-rw-r--r--src/parser/parser_utils.c12
-rw-r--r--src/utils/utils.c1
-rw-r--r--tests/features/test_alias.zc30
6 files changed, 54 insertions, 4 deletions
diff --git a/src/codegen/codegen.h b/src/codegen/codegen.h
index 45a00ed..c6e2836 100644
--- a/src/codegen/codegen.h
+++ b/src/codegen/codegen.h
@@ -28,6 +28,7 @@ void emit_func_signature(FILE *out, ASTNode *func, const char *name_override);
// Declaration emission (codegen_decl.c).
void emit_preamble(ParserContext *ctx, FILE *out);
void emit_includes_and_aliases(ASTNode *node, FILE *out);
+void emit_type_aliases(ASTNode *node, FILE *out);
void emit_struct_defs(ParserContext *ctx, ASTNode *node, FILE *out);
void emit_trait_defs(ASTNode *node, FILE *out);
void emit_enum_protos(ASTNode *node, FILE *out);
diff --git a/src/codegen/codegen_decl.c b/src/codegen/codegen_decl.c
index e42d83b..6f9a1cb 100644
--- a/src/codegen/codegen_decl.c
+++ b/src/codegen/codegen_decl.c
@@ -207,7 +207,16 @@ void emit_includes_and_aliases(ASTNode *node, FILE *out)
fprintf(out, "#include \"%s\"\n", node->include.path);
}
}
- else if (node->type == NODE_TYPE_ALIAS)
+ node = node->next;
+ }
+}
+
+// Emit type aliases (after struct defs so the aliased types exist)
+void emit_type_aliases(ASTNode *node, FILE *out)
+{
+ while (node)
+ {
+ if (node->type == NODE_TYPE_ALIAS)
{
fprintf(out, "typedef %s %s;\n", node->type_alias.original_type,
node->type_alias.alias);
diff --git a/src/codegen/codegen_main.c b/src/codegen/codegen_main.c
index ea90131..c86b6c9 100644
--- a/src/codegen/codegen_main.c
+++ b/src/codegen/codegen_main.c
@@ -341,6 +341,9 @@ void codegen_node(ParserContext *ctx, ASTNode *node, FILE *out)
emit_struct_defs(ctx, sorted, out);
}
+ // Emit type aliases after struct defs (so aliased generic types exist)
+ emit_type_aliases(kids, out);
+
ASTNode *raw_iter = kids;
while (raw_iter)
{
diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c
index 87cf24e..0dc7cbf 100644
--- a/src/parser/parser_utils.c
+++ b/src/parser/parser_utils.c
@@ -2248,6 +2248,14 @@ char *rewrite_expr_methods(ParserContext *ctx, char *raw)
*pc = 0;
}
+ // Resolve type alias if exists (for example: Vec2f -> Vec2_float)
+ const char *resolved_type = find_type_alias(ctx, base_t);
+ if (resolved_type)
+ {
+ free(base_t);
+ base_t = xstrdup(resolved_type);
+ }
+
ASTNode *def = find_struct_def(ctx, base_t);
int is_field = 0;
if (def && (def->type == NODE_STRUCT))
@@ -2299,7 +2307,7 @@ char *rewrite_expr_methods(ParserContext *ctx, char *raw)
}
// Mixin Lookup Logic
- char target_func[128];
+ char target_func[256];
sprintf(target_func, "%s__%s", ptr_check, method);
char *final_cast = NULL;
@@ -2405,7 +2413,7 @@ char *rewrite_expr_methods(ParserContext *ctx, char *raw)
}
}
// Mixin Lookup Logic (No Parens)
- char target_func[128];
+ char target_func[256];
sprintf(target_func, "%s__%s", ptr_check, method);
char *final_cast = NULL;
diff --git a/src/utils/utils.c b/src/utils/utils.c
index 775e385..df7576f 100644
--- a/src/utils/utils.c
+++ b/src/utils/utils.c
@@ -535,6 +535,7 @@ CompilerConfig g_config = {0};
void scan_build_directives(ParserContext *ctx, const char *src)
{
+ (void)ctx; // Currently unused, reserved for future use
const char *p = src;
while (*p)
{
diff --git a/tests/features/test_alias.zc b/tests/features/test_alias.zc
index f7bc42a..ec84947 100644
--- a/tests/features/test_alias.zc
+++ b/tests/features/test_alias.zc
@@ -17,6 +17,15 @@ fn add(a: int, b: int) -> int {
return a + b;
}
+// Generic struct for alias testing
+struct Vec2<T> {
+ x: T;
+ y: T;
+}
+
+alias Vec2f = Vec2<f32>;
+alias Vec2i = Vec2<int>;
+
test "alias basic" {
var x: MyInt = 10;
var ptr: MyIntPtr = &x;
@@ -28,14 +37,33 @@ test "alias basic" {
assert(res == 11, "Function with alias arg failed");
}
+test "alias struct" {
+ var p: Point = Point{x: 0, y: 0};
p.x = 100;
p.y = 200;
assert(p.x == 100, "Struct alias access failed");
}
+test "alias generic struct" {
+ var v = Vec2f{x: 1.5, y: 2.5};
+ assert(v.x > 1.0, "Generic alias field access failed");
+ assert(v.y > 2.0, "Generic alias field access failed");
+
+ var vi = Vec2i{x: 10, y: 20};
+ assert(vi.x == 10, "Generic int alias failed");
+ assert(vi.y == 20, "Generic int alias failed");
+}
+
+test "alias fstring" {
+ var v = Vec2f{x: 3.14, y: 6.28};
+ // This tests that f-string interpolation correctly resolves the alias
+ println "v.x = {v.x}, v.y = {v.y}";
+}
+
test "alias function pointer" {
// var op: BinOp;
- // Assignment currently not supported for function types without casting op = add;
+ // Assignment currently not supported for function types without casting
+ // op = add;
// assert(op(1, 2) == 3, "Function alias");
}