diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-17 12:14:26 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-17 12:14:26 +0000 |
| commit | d15dbaf025df9265dce417faaa5ccf33ae04d4b5 (patch) | |
| tree | 57201674789a305c0bc7b82632fa906534dbf37d | |
| parent | 91e763a600afc86e9630e4db592d8108f65def16 (diff) | |
Fix for #31
| -rw-r--r-- | examples/features/comptime_fib.zc (renamed from examples/comptime_fib.zc) | 0 | ||||
| -rw-r--r-- | examples/features/showcase.zc (renamed from examples/showcase.zc) | 0 | ||||
| -rw-r--r-- | examples/graphics/mandelbrot.zc | 78 | ||||
| -rw-r--r-- | examples/graphics/raylib_demo.zc (renamed from examples/raylib_demo.zc) | 0 | ||||
| -rw-r--r-- | src/codegen/codegen.c | 33 | ||||
| -rw-r--r-- | src/parser/parser_type.c | 7 |
6 files changed, 117 insertions, 1 deletions
diff --git a/examples/comptime_fib.zc b/examples/features/comptime_fib.zc index 1ad2898..1ad2898 100644 --- a/examples/comptime_fib.zc +++ b/examples/features/comptime_fib.zc diff --git a/examples/showcase.zc b/examples/features/showcase.zc index eca5480..eca5480 100644 --- a/examples/showcase.zc +++ b/examples/features/showcase.zc diff --git a/examples/graphics/mandelbrot.zc b/examples/graphics/mandelbrot.zc new file mode 100644 index 0000000..f3fa4bd --- /dev/null +++ b/examples/graphics/mandelbrot.zc @@ -0,0 +1,78 @@ + +struct Complex { + re: float, + im: float, +} + +fn complex_make(re: float, im: float) -> Complex { + return Complex { re: re, im: im }; +} + +fn complex_add(a: Complex, b: Complex) -> Complex { + return Complex { re: a.re + b.re, im: a.im + b.im }; +} + +fn complex_mul(a: Complex, b: Complex) -> Complex { + return Complex { + re: a.re * b.re - a.im * b.im, + im: a.re * b.im + a.im * b.re + }; +} + +fn complex_abs2(z: Complex) -> float { + return z.re * z.re + z.im * z.im; +} + +fn complex_print(z: Complex) { + println "{z.re}{z.im}i"; +} + +fn pick_char(iter: int, max_iter: int, edge_chars: char[], edge_count: int) -> char { + if (iter >= max_iter) { return ' '; } + if (iter <= 0) { return edge_chars[0]; } + + var t: float = ((float)iter) / ((float)max_iter); + var idx: int = (int)(t * ((float)(edge_count - 1))); + + if (idx < 0) { idx = 0; } + if (idx >= edge_count) { idx = edge_count - 1; } + + return edge_chars[idx]; +} + +fn main() { + var width: int = 120; + var height: int = 40; + var max_iter: int = 200; + + var edge_chars: char[12] = [ '#', '@', '%', '8', '&', '*', '+', '=', '-', ':', '.', ',' ]; + var edge_count: int = 12; + + var min_re: float = -2.2; + var max_re: float = 1.0; + var min_im: float = -1.2; + var max_im: float = 1.2; + + for y in 0..height { + var im: float = + max_im - (max_im - min_im) * ( ((float)y) / ((float)(height - 1)) ); + + for x in 0..width { + var re: float = + min_re + (max_re - min_re) * ( ((float)x) / ((float)(width - 1)) ); + + var c: Complex = complex_make(re, im); + var z: Complex = complex_make(0.0, 0.0); + + var iter: int = 0; + while (iter < max_iter and complex_abs2(z) <= 4.0) { + z = complex_add(complex_mul(z, z), c); + iter += 1; + } + + var pixel = pick_char(iter, max_iter, edge_chars, edge_count); + print "{pixel}"; + } + print "\n"; + } +} diff --git a/examples/raylib_demo.zc b/examples/graphics/raylib_demo.zc index 77b661e..77b661e 100644 --- a/examples/raylib_demo.zc +++ b/examples/graphics/raylib_demo.zc diff --git a/src/codegen/codegen.c b/src/codegen/codegen.c index 370bcec..f019a4b 100644 --- a/src/codegen/codegen.c +++ b/src/codegen/codegen.c @@ -668,15 +668,46 @@ void codegen_expression(ParserContext *ctx, ASTNode *node, FILE *out) } else { + FuncSig *sig = NULL; + if (node->call.callee->type == NODE_EXPR_VAR) + { + sig = find_func(ctx, node->call.callee->var_ref.name); + } + ASTNode *arg = node->call.args; + int arg_idx = 0; while (arg) { - codegen_expression(ctx, arg, out); + int handled = 0; + if (sig && arg_idx < sig->total_args) + { + Type *param_t = sig->arg_types[arg_idx]; + Type *arg_t = arg->type_info; + + if (param_t && param_t->kind == TYPE_ARRAY && param_t->array_size == 0 && + arg_t && arg_t->kind == TYPE_ARRAY && arg_t->array_size > 0) + { + char *inner = type_to_string(param_t->inner); + fprintf(out, "(Slice_%s){.data = ", inner); + codegen_expression(ctx, arg, out); + fprintf(out, ", .len = %d, .cap = %d}", arg_t->array_size, + arg_t->array_size); + free(inner); + handled = 1; + } + } + + if (!handled) + { + codegen_expression(ctx, arg, out); + } + if (arg->next) { fprintf(out, ", "); } arg = arg->next; + arg_idx++; } } fprintf(out, ")"); diff --git a/src/parser/parser_type.c b/src/parser/parser_type.c index 1b5e8b6..27328fe 100644 --- a/src/parser/parser_type.c +++ b/src/parser/parser_type.c @@ -707,6 +707,13 @@ Type *parse_type_formal(ParserContext *ctx, Lexer *l) expect(l, TOK_RBRACKET, "Expected ']' in array type"); + if (size == 0) + { + char *inner_str = type_to_string(t); + register_slice(ctx, inner_str); + free(inner_str); + } + Type *arr = type_new(TYPE_ARRAY); arr->inner = t; arr->array_size = size; |
