summaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-02-01 14:01:51 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-02-01 14:01:51 +0000
commitfbfce63744882d48ea2fc514ab1594000254db80 (patch)
treecc7949dab2149d829d3c04e3d542553ed883593f /src/codegen
parenteafd8c67012ea253436b79f703dc0702046703f8 (diff)
Related to #138
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/codegen.c92
-rw-r--r--src/codegen/codegen_decl.c19
2 files changed, 105 insertions, 6 deletions
diff --git a/src/codegen/codegen.c b/src/codegen/codegen.c
index 0496a46..384820b 100644
--- a/src/codegen/codegen.c
+++ b/src/codegen/codegen.c
@@ -1150,14 +1150,102 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out)
break;
}
case NODE_EXPR_CAST:
- fprintf(out, "((%s)(", node->cast.target_type);
+ {
+ const char *t = node->cast.target_type;
+ const char *mapped = t;
+ if (strcmp(t, "c_int") == 0)
+ {
+ mapped = "int";
+ }
+ else if (strcmp(t, "c_uint") == 0)
+ {
+ mapped = "unsigned int";
+ }
+ else if (strcmp(t, "c_long") == 0)
+ {
+ mapped = "long";
+ }
+ else if (strcmp(t, "c_ulong") == 0)
+ {
+ mapped = "unsigned long";
+ }
+ else if (strcmp(t, "c_short") == 0)
+ {
+ mapped = "short";
+ }
+ else if (strcmp(t, "c_ushort") == 0)
+ {
+ mapped = "unsigned short";
+ }
+ else if (strcmp(t, "c_char") == 0)
+ {
+ mapped = "char";
+ }
+ else if (strcmp(t, "c_uchar") == 0)
+ {
+ mapped = "unsigned char";
+ }
+ else if (strcmp(t, "int") == 0)
+ {
+ mapped = "int32_t";
+ }
+ else if (strcmp(t, "uint") == 0)
+ {
+ mapped = "unsigned int";
+ }
+
+ fprintf(out, "((%s)(", mapped);
codegen_expression(ctx, node->cast.expr, out);
fprintf(out, "))");
break;
+ }
case NODE_EXPR_SIZEOF:
if (node->size_of.target_type)
{
- fprintf(out, "sizeof(%s)", node->size_of.target_type);
+ const char *t = node->size_of.target_type;
+ const char *mapped = t;
+ if (strcmp(t, "c_int") == 0)
+ {
+ mapped = "int";
+ }
+ else if (strcmp(t, "c_uint") == 0)
+ {
+ mapped = "unsigned int";
+ }
+ else if (strcmp(t, "c_long") == 0)
+ {
+ mapped = "long";
+ }
+ else if (strcmp(t, "c_ulong") == 0)
+ {
+ mapped = "unsigned long";
+ }
+ else if (strcmp(t, "c_short") == 0)
+ {
+ mapped = "short";
+ }
+ else if (strcmp(t, "c_ushort") == 0)
+ {
+ mapped = "unsigned short";
+ }
+ else if (strcmp(t, "c_char") == 0)
+ {
+ mapped = "char";
+ }
+ else if (strcmp(t, "c_uchar") == 0)
+ {
+ mapped = "unsigned char";
+ }
+ else if (strcmp(t, "int") == 0)
+ {
+ mapped = "int32_t"; // Strict mapping
+ }
+ else if (strcmp(t, "uint") == 0)
+ {
+ mapped = "unsigned int"; // uint alias
+ }
+
+ fprintf(out, "sizeof(%s)", mapped);
}
else
{
diff --git a/src/codegen/codegen_decl.c b/src/codegen/codegen_decl.c
index 31bd2ee..1623ffc 100644
--- a/src/codegen/codegen_decl.c
+++ b/src/codegen/codegen_decl.c
@@ -1130,12 +1130,23 @@ void print_type_defs(ParserContext *ctx, FILE *out, ASTNode *nodes)
fprintf(out, "typedef struct Tuple_%s Tuple_%s;\nstruct Tuple_%s { ", t->sig, t->sig,
t->sig);
char *s = xstrdup(t->sig);
- char *p = strtok(s, "_");
+ char *current = s;
+ char *next_sep = strstr(current, "__");
int i = 0;
- while (p)
+ while (current)
{
- fprintf(out, "%s v%d; ", p, i++);
- p = strtok(NULL, "_");
+ if (next_sep)
+ {
+ *next_sep = 0;
+ fprintf(out, "%s v%d; ", current, i++);
+ current = next_sep + 2;
+ next_sep = strstr(current, "__");
+ }
+ else
+ {
+ fprintf(out, "%s v%d; ", current, i++);
+ break;
+ }
}
free(s);
fprintf(out, "};\n");