summaryrefslogtreecommitdiff
path: root/src/codegen/codegen_decl.c
diff options
context:
space:
mode:
authorZuhaitz <zuhaitz.zechhub@gmail.com>2026-01-25 22:36:29 +0000
committerGitHub <noreply@github.com>2026-01-25 22:36:29 +0000
commitdc69e7a19e86373ee117d56b3747ff56d1631e96 (patch)
tree1b94f1f43b98a26a9c153585568505bed95e60ec /src/codegen/codegen_decl.c
parentfa858115f6b9625065b2488aeee028fe2c7d60b2 (diff)
parent74a4469dfba9a63a57caabc65c4faa65b2a59308 (diff)
Merge pull request #127 from sureshkrishnan-ai/JsonType
fixed buffer overflow in vector. Added serialize and deserialize in j…
Diffstat (limited to 'src/codegen/codegen_decl.c')
-rw-r--r--src/codegen/codegen_decl.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/codegen/codegen_decl.c b/src/codegen/codegen_decl.c
index d525963..e5d73a0 100644
--- a/src/codegen/codegen_decl.c
+++ b/src/codegen/codegen_decl.c
@@ -22,13 +22,15 @@ static void emit_freestanding_preamble(FILE *out)
"uint64_t\n",
out);
fputs("#define F32 float\n#define F64 double\n", out);
- fputs("#define _z_str(x) _Generic((x), _Bool: \"%d\", char: \"%c\", "
+ fputs("static inline const char* _z_bool_str(_Bool b) { return b ? \"true\" : \"false\"; }\n", out);
+ fputs("#define _z_str(x) _Generic((x), _Bool: \"%s\", char: \"%c\", "
"signed char: \"%c\", unsigned char: \"%u\", short: \"%d\", "
"unsigned short: \"%u\", int: \"%d\", unsigned int: \"%u\", "
"long: \"%ld\", unsigned long: \"%lu\", long long: \"%lld\", "
"unsigned long long: \"%llu\", float: \"%f\", double: \"%f\", "
"char*: \"%s\", void*: \"%p\")\n",
out);
+ fputs("#define _z_arg(x) _Generic((x), _Bool: _z_bool_str(x), default: (x))\n", out);
fputs("typedef struct { void *func; void *ctx; } z_closure_T;\n", out);
fputs("__attribute__((weak)) void* z_malloc(usize sz) { return NULL; }\n", out);
@@ -62,7 +64,10 @@ void emit_preamble(ParserContext *ctx, FILE *out)
fputs("#define ZC_AUTO auto\n", out);
fputs("#define ZC_CAST(T, x) static_cast<T>(x)\n", out);
// C++ _z_str via overloads
- fputs("inline const char* _z_str(bool) { return \"%d\"; }\n", out);
+ fputs("inline const char* _z_bool_str(bool b) { return b ? \"true\" : \"false\"; }\n", out);
+ fputs("inline const char* _z_str(bool) { return \"%s\"; }\n", out);
+ fputs("inline const char* _z_arg(bool b) { return _z_bool_str(b); }\n", out);
+ fputs("template<typename T> inline T _z_arg(T x) { return x; }\n", out);
fputs("inline const char* _z_str(char) { return \"%c\"; }\n", out);
fputs("inline const char* _z_str(int) { return \"%d\"; }\n", out);
fputs("inline const char* _z_str(unsigned int) { return \"%u\"; }\n", out);
@@ -82,13 +87,15 @@ void emit_preamble(ParserContext *ctx, FILE *out)
fputs("#define ZC_AUTO __auto_type\n", out);
fputs("#define ZC_CAST(T, x) ((T)(x))\n", out);
fputs("#ifdef __TINYC__\n#define __auto_type __typeof__\n#endif\n", out);
- fputs("#define _z_str(x) _Generic((x), _Bool: \"%d\", char: \"%c\", "
+ fputs("static inline const char* _z_bool_str(_Bool b) { return b ? \"true\" : \"false\"; }\n", out);
+ fputs("#define _z_str(x) _Generic((x), _Bool: \"%s\", char: \"%c\", "
"signed char: \"%c\", unsigned char: \"%u\", short: \"%d\", "
"unsigned short: \"%u\", int: \"%d\", unsigned int: \"%u\", "
"long: \"%ld\", unsigned long: \"%lu\", long long: \"%lld\", "
"unsigned long long: \"%llu\", float: \"%f\", double: \"%f\", "
"char*: \"%s\", void*: \"%p\")\n",
out);
+ fputs("#define _z_arg(x) _Generic((x), _Bool: _z_bool_str(x), default: (x))\n", out);
}
fputs("typedef size_t usize;\ntypedef char* string;\n", out);