summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-13 12:58:55 +0000
committerZuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian>2026-01-13 12:58:55 +0000
commit8d7e628878aac07d0380463196f823502d1816ce (patch)
tree276e19725a5c89e65b6fea30b358f64644212e6a /src
parentd561a63d8b5f6beadca30ab7b7846b387d67929a (diff)
Fix for #10
Diffstat (limited to 'src')
-rw-r--r--src/codegen/codegen.h2
-rw-r--r--src/codegen/codegen_decl.c9
-rw-r--r--src/codegen/codegen_main.c2
-rw-r--r--src/main.c4
-rw-r--r--src/parser/parser.h1
-rw-r--r--src/parser/parser_stmt.c7
6 files changed, 17 insertions, 8 deletions
diff --git a/src/codegen/codegen.h b/src/codegen/codegen.h
index d489fb3..4b66f99 100644
--- a/src/codegen/codegen.h
+++ b/src/codegen/codegen.h
@@ -24,7 +24,7 @@ const char *parse_original_method_name(const char *mangled);
void emit_auto_type(ParserContext *ctx, ASTNode *init_expr, Token t, FILE *out);
// Declaration emission (codegen_decl.c).
-void emit_preamble(FILE *out);
+void emit_preamble(ParserContext *ctx, FILE *out);
void emit_includes_and_aliases(ASTNode *node, FILE *out);
void emit_struct_defs(ParserContext *ctx, ASTNode *node, FILE *out);
void emit_trait_defs(ASTNode *node, FILE *out);
diff --git a/src/codegen/codegen_decl.c b/src/codegen/codegen_decl.c
index ac4ae14..4979236 100644
--- a/src/codegen/codegen_decl.c
+++ b/src/codegen/codegen_decl.c
@@ -8,7 +8,7 @@
#include <string.h>
// Emit C preamble with standard includes and type definitions.
-void emit_preamble(FILE *out)
+void emit_preamble(ParserContext *ctx, FILE *out)
{
if (g_config.is_freestanding)
{
@@ -54,8 +54,11 @@ void emit_preamble(FILE *out)
fputs("#include <unistd.h>\n#include <fcntl.h>\n", out); // POSIX functions
fputs("#ifdef __TINYC__\n#define __auto_type __typeof__\n#endif\n", out);
fputs("typedef size_t usize;\ntypedef char* string;\n", out);
- fputs("#include <pthread.h>\n", out);
- fputs("typedef struct { pthread_t thread; void *result; } Async;\n", out);
+ if (ctx->has_async)
+ {
+ fputs("#include <pthread.h>\n", out);
+ fputs("typedef struct { pthread_t thread; void *result; } Async;\n", out);
+ }
fputs("typedef struct { void *func; void *ctx; } z_closure_T;\n", out);
fputs("#define U0 void\n#define I8 int8_t\n#define U8 uint8_t\n#define I16 "
"int16_t\n#define U16 uint16_t\n",
diff --git a/src/codegen/codegen_main.c b/src/codegen/codegen_main.c
index c9c69f6..3634af3 100644
--- a/src/codegen/codegen_main.c
+++ b/src/codegen/codegen_main.c
@@ -202,7 +202,7 @@ void codegen_node(ParserContext *ctx, ASTNode *node, FILE *out)
if (!ctx->skip_preamble)
{
- emit_preamble(out);
+ emit_preamble(ctx, out);
}
emit_includes_and_aliases(kids, out);
diff --git a/src/main.c b/src/main.c
index 583f735..eb1df7e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -305,9 +305,9 @@ int main(int argc, char **argv)
// TCC-specific adjustments?
// Already handled by user passing --cc tcc
- snprintf(cmd, sizeof(cmd), "%s %s %s %s %s -o %s out.c -lm -lpthread -I./src %s", g_config.cc,
+ snprintf(cmd, sizeof(cmd), "%s %s %s %s %s -o %s out.c -lm %s -I./src %s", g_config.cc,
g_config.gcc_flags, g_cflags, g_config.is_freestanding ? "-ffreestanding" : "", "",
- outfile, g_link_flags);
+ outfile, g_parser_ctx->has_async ? "-lpthread" : "", g_link_flags);
if (g_config.verbose)
{
diff --git a/src/parser/parser.h b/src/parser/parser.h
index 4aeb2c8..e3a5e6b 100644
--- a/src/parser/parser.h
+++ b/src/parser/parser.h
@@ -265,6 +265,7 @@ struct ParserContext
FILE *hoist_out; // For plugins to hoist code to file scope
int skip_preamble; // If 1, codegen_node(NODE_ROOT) won't emit preamble
int is_repl; // REPL mode flag
+ int has_async; // Track if async features are used
};
// Token helpers
diff --git a/src/parser/parser_stmt.c b/src/parser/parser_stmt.c
index 8a89cc7..4478d22 100644
--- a/src/parser/parser_stmt.c
+++ b/src/parser/parser_stmt.c
@@ -37,6 +37,11 @@ ASTNode *parse_function(ParserContext *ctx, Lexer *l, int is_async)
Token name_tok = lexer_next(l);
char *name = token_strdup(name_tok);
+ if (is_async)
+ {
+ ctx->has_async = 1;
+ }
+
// Check for C reserved word conflict
if (is_c_reserved_word(name))
{
@@ -3876,7 +3881,7 @@ char *run_comptime_block(ParserContext *ctx, Lexer *l)
zpanic("Could not create temp file %s", filename);
}
- emit_preamble(f);
+ emit_preamble(ctx, f);
fprintf(
f,
"size_t _z_check_bounds(size_t index, size_t size) { if (index >= size) { fprintf(stderr, "