summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-18 23:23:50 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-18 23:23:50 +0000
commit82559fe7ff00f5cce030f4d5231a270267087624 (patch)
treed6571b5376ca153d46947b83ae94508de714b089
parent562e010dd47ec1e238017369ea481700d826b68d (diff)
Fix for #5
-rw-r--r--src/parser/parser_expr.c9
-rw-r--r--src/parser/parser_stmt.c9
-rw-r--r--src/parser/parser_utils.c31
-rw-r--r--tests/features/fstring_lib.h9
-rw-r--r--tests/features/fstring_mod.zc4
-rw-r--r--tests/features/test_fstring.zc23
6 files changed, 80 insertions, 5 deletions
diff --git a/src/parser/parser_expr.c b/src/parser/parser_expr.c
index 6d95f12..ef3980c 100644
--- a/src/parser/parser_expr.c
+++ b/src/parser/parser_expr.c
@@ -800,7 +800,14 @@ static ASTNode *create_fstring_block(ParserContext *ctx, const char *content)
}
if (depth == 1 && *p == ':' && !colon)
{
- colon = p;
+ if ((p + 1) < end_brace && *(p + 1) == ':')
+ {
+ p++;
+ }
+ else
+ {
+ colon = p;
+ }
}
p++;
}
diff --git a/src/parser/parser_stmt.c b/src/parser/parser_stmt.c
index daf3f72..8480716 100644
--- a/src/parser/parser_stmt.c
+++ b/src/parser/parser_stmt.c
@@ -1771,7 +1771,14 @@ char *process_printf_sugar(ParserContext *ctx, const char *content, int newline,
}
if (depth == 1 && *p == ':' && !colon)
{
- colon = p;
+ if (*(p + 1) == ':')
+ {
+ p++;
+ }
+ else
+ {
+ colon = p;
+ }
}
if (depth == 0)
{
diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c
index 0dc7cbf..df55d16 100644
--- a/src/parser/parser_utils.c
+++ b/src/parser/parser_utils.c
@@ -774,7 +774,7 @@ Module *find_module(ParserContext *ctx, const char *alias)
Module *m = ctx->modules;
while (m)
{
- if (strcmp(m->alias, alias) == 0)
+ if (m->alias && strcmp(m->alias, alias) == 0)
{
return m;
}
@@ -786,7 +786,7 @@ Module *find_module(ParserContext *ctx, const char *alias)
void register_module(ParserContext *ctx, const char *alias, const char *path)
{
Module *m = xmalloc(sizeof(Module));
- m->alias = xstrdup(alias);
+ m->alias = alias ? xstrdup(alias) : NULL;
m->path = xstrdup(path);
m->base_name = extract_module_name(path);
m->next = ctx->modules;
@@ -2582,7 +2582,32 @@ char *rewrite_expr_methods(ParserContext *ctx, char *raw)
src++;
char mangled[256];
- snprintf(mangled, sizeof(mangled), "%s_%s", func_name, method);
+
+ // Resolve alias
+ Module *mod = find_module(ctx, func_name);
+ if (mod)
+ {
+ if (mod->is_c_header)
+ {
+ snprintf(mangled, sizeof(mangled), "%s", method);
+ }
+ else
+ {
+ snprintf(mangled, sizeof(mangled), "%s_%s", mod->base_name, method);
+ }
+ }
+ else
+ {
+ ASTNode *sdef = find_struct_def(ctx, func_name);
+ if (sdef)
+ {
+ snprintf(mangled, sizeof(mangled), "%s__%s", func_name, method);
+ }
+ else
+ {
+ snprintf(mangled, sizeof(mangled), "%s_%s", func_name, method);
+ }
+ }
if (*src == ')')
{
diff --git a/tests/features/fstring_lib.h b/tests/features/fstring_lib.h
new file mode 100644
index 0000000..ef292d2
--- /dev/null
+++ b/tests/features/fstring_lib.h
@@ -0,0 +1,9 @@
+
+#ifndef FSTRING_LIB_H
+#define FSTRING_LIB_H
+
+static int lib_val() {
+ return 99;
+}
+
+#endif
diff --git a/tests/features/fstring_mod.zc b/tests/features/fstring_mod.zc
new file mode 100644
index 0000000..78b16ef
--- /dev/null
+++ b/tests/features/fstring_mod.zc
@@ -0,0 +1,4 @@
+
+fn get_val() -> int {
+ return 42;
+}
diff --git a/tests/features/test_fstring.zc b/tests/features/test_fstring.zc
new file mode 100644
index 0000000..3ef64a7
--- /dev/null
+++ b/tests/features/test_fstring.zc
@@ -0,0 +1,23 @@
+import "tests/features/fstring_mod.zc" as Mod;
+
+test "alias namespace fstring" {
+ // Tests that {Mod::get_val()} is parsed correctly (Mod_get_val)
+ var res = f"{Mod::get_val()}";
+ assert(res == "42", "Import namespace in f-string failed");
+
+ // Check local string formatting via println (compile check)
+ println "Val: {Mod::get_val()}";
+}
+
+import "tests/features/fstring_lib.h" as Lib;
+
+test "alias C header fstring" {
+ // Tests that {Lib::lib_val()} resolves to lib_val() (no mangling/prefix)
+ // parser_expr.c strips alias for C headers.
+ // parser_utils.c rewrite_expr_methods needs to do the same.
+ var res = f"{Lib::lib_val()}";
+ assert(res == "99", "C header namespace in f-string failed");
+
+ // Verify println (rewrite_expr_methods path)
+ println "C Val: {Lib::lib_val()}";
+}