diff options
| author | Baitinq <[email protected]> | 2025-05-24 01:28:51 +0200 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-05-24 01:28:51 +0200 |
| commit | 752e5a18f57dd765b47d46a552e5db389ca62b53 (patch) | |
| tree | 032cb4d64ceeb014979677a9cc5d5ec06d002a3c | |
| parent | Codegen: Typecheck binary expressions (diff) | |
| download | pry-lang-752e5a18f57dd765b47d46a552e5db389ca62b53.tar.gz pry-lang-752e5a18f57dd765b47d46a552e5db389ca62b53.tar.bz2 pry-lang-752e5a18f57dd765b47d46a552e5db389ca62b53.zip | |
Codegen: Fix bug with typecheck of return of function params
| -rw-r--r-- | examples/18.src | 20 | ||||
| -rw-r--r-- | examples/19.src | 36 | ||||
| -rw-r--r-- | examples/20.src | 13 | ||||
| -rw-r--r-- | examples/21.src | 22 | ||||
| -rw-r--r-- | examples/9.src | 2 | ||||
| -rw-r--r-- | src/codegen.zig | 23 |
6 files changed, 64 insertions, 52 deletions
diff --git a/examples/18.src b/examples/18.src index 29facd5..17c9fee 100644 --- a/examples/18.src +++ b/examples/18.src @@ -1,18 +1,18 @@ -extern malloc = (i64) => *i64; -extern free = (*i64) => void; +extern malloc = (i64) => *void; +extern free = (*void) => void; import "!stdlib.src"; let main = () => i64 { - let x = malloc(24); - (*(x+0)) = 10; - (*(x+1)) = 20; - (*(x+2)) = 40; + let x = cast(*i8, malloc(24)); + (*(x+cast(*i8, 0))) = 10; + (*(x+cast(*i8, 1))) = 20; + (*(x+cast(*i8, 2))) = 40; println("%p", x); - println("%d", *(x+0)); - println("%d", *(x+1)); - println("%d", *(x+2)); - free(x); + println("%d", *(x+cast(*i8, 0))); + println("%d", *(x+cast(*i8, 1))); + println("%d", *(x+cast(*i8, 2))); + free(cast(*void, x)); return 0; }; diff --git a/examples/19.src b/examples/19.src index 5320ef8..5a3543f 100644 --- a/examples/19.src +++ b/examples/19.src @@ -1,24 +1,24 @@ -extern printf = (*i64, varargs) => void; -extern malloc = (i64) => *i8; -extern free = (*i64) => void; +extern printf = (*i8, varargs) => void; +extern malloc = (i64) => *void; +extern free = (*void) => void; let main = () => i64 { - let buf = malloc(13); - (*(buf+0)) = 'h'; - (*(buf+1)) = 'e'; - (*(buf+2)) = 'l'; - (*(buf+3)) = 'l'; - (*(buf+4)) = 'o'; - (*(buf+5)) = ' '; - (*(buf+6)) = 'w'; - (*(buf+7)) = 'o'; - (*(buf+8)) = 'r'; - (*(buf+9)) = 'l'; - (*(buf+10)) = 'd'; - (*(buf+11)) = '\n'; - (*(buf+12)) = '\0'; + let buf = cast(*i8, malloc(13)); + (*(buf+cast(*i8, 0))) = 'h'; + (*(buf+cast(*i8, 1))) = 'e'; + (*(buf+cast(*i8, 2))) = 'l'; + (*(buf+cast(*i8, 3))) = 'l'; + (*(buf+cast(*i8, 4))) = 'o'; + (*(buf+cast(*i8, 5))) = ' '; + (*(buf+cast(*i8, 6))) = 'w'; + (*(buf+cast(*i8, 7))) = 'o'; + (*(buf+cast(*i8, 8))) = 'r'; + (*(buf+cast(*i8, 9))) = 'l'; + (*(buf+cast(*i8, 10))) = 'd'; + (*(buf+cast(*i8, 11))) = '\n'; + (*(buf+cast(*i8, 12))) = '\0'; printf("%s", buf); - free(buf); + free(cast(*void, buf)); return 0; }; diff --git a/examples/20.src b/examples/20.src index 9e289f1..82356b2 100644 --- a/examples/20.src +++ b/examples/20.src @@ -1,12 +1,13 @@ -extern malloc = (i64) => *i64; -extern free = (*i64) => void; + +extern malloc = (i64) => *void; +extern free = (*void) => void; import "!stdlib.src"; let init_array = (n: i64, arr: *i64) => i64 { let i = 0; while i < n { - (*(arr + i)) = i; + (*(arr + cast(*i64, i))) = i; i = i + 1; }; return 0; @@ -14,16 +15,16 @@ let init_array = (n: i64, arr: *i64) => i64 { let main = () => i64 { let n = 10; - let arr = malloc(n * 8); + let arr = cast(*i64, malloc(n * 8)); init_array(n, arr); let i = 0; while i < n { - println("%d", *(arr + i)); + println("%d", *(arr + cast(*i64, i))); i = i + 1; }; - free(arr); + free(cast(*void, arr)); return 0; }; diff --git a/examples/21.src b/examples/21.src index ca4f24f..5e72c27 100644 --- a/examples/21.src +++ b/examples/21.src @@ -1,13 +1,13 @@ extern rand = () => i64; -extern malloc = (i64) => *i64; -extern free = (*i64) => void; +extern malloc = (i64) => *void; +extern free = (*void) => void; import "!stdlib.src"; let init_array = (n: i64, arr: *i64) => void { let i = 0; while i < n { - (*(arr + i)) = rand(); + (*(arr + cast(*i64, i))) = rand(); i = i + 1; }; return; @@ -16,7 +16,7 @@ let init_array = (n: i64, arr: *i64) => void { let print_array = (n: i64, arr: *i64) => void { let i = 0; while i < n { - println("%d", *(arr + i)); + println("%d", *(arr + cast(*i64, i))); i = i + 1; }; return; @@ -29,13 +29,13 @@ let sort_array = (n: i64, arr: *i64) => void { while i < n { j = i + 1; while j < n { - let x = *((arr + i)); - let y = *((arr + j)); + let x = *(arr + cast(*i64, i)); + let y = *(arr + cast(*i64, j)); if x > y { - let tmp = (*(arr + i)); - (*(arr + i)) = (*(arr + j)); - (*(arr + j)) = tmp; + let tmp = *(arr + cast(*i64, i)); + *(arr + cast(*i64, i)) = *(arr + cast(*i64, j)); + *(arr + cast(*i64, j)) = tmp; }; j = j + 1; @@ -48,7 +48,7 @@ let sort_array = (n: i64, arr: *i64) => void { let main = () => i64 { let n = 10; - let arr = malloc(n * 8); + let arr = cast(*i64, malloc(n * 8)); init_array(n, arr); println("Pre-sorted:"); @@ -59,7 +59,7 @@ let main = () => i64 { println("Sorted:"); print_array(n, arr); - free(arr); + free(cast(*void, arr)); return 0; }; diff --git a/examples/9.src b/examples/9.src index 6704c1d..9087c09 100644 --- a/examples/9.src +++ b/examples/9.src @@ -1,7 +1,7 @@ import "!stdlib.src"; let print_boolean = (b: bool) => i64 { - println("%d\n", b); + println("%d", b); return 0; }; diff --git a/src/codegen.zig b/src/codegen.zig index 7d5308b..e9eec9b 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -232,12 +232,23 @@ pub const CodeGen = struct { const res = llvm.LLVMBuildCall2(self.builder, try self.get_llvm_type(function.node_type), function.value, @ptrCast(arguments.items), @intCast(arguments.items.len), "") orelse return CodeGenError.CompilationError; - const function_return_type = switch (function.node.*) { - .FUNCTION_DEFINITION => |x| x.return_type, - .PRIMARY_EXPRESSION => |x| x.IDENTIFIER.type.?, - .TYPE => |x| x.FUNCTION_TYPE.return_type, + var function_return_type: *parser.Node = undefined; + switch (function.node.*) { + .FUNCTION_DEFINITION => |x| { + function_return_type = x.return_type; + }, + .PRIMARY_EXPRESSION => |x| { + const f = self.environment.get_variable(x.IDENTIFIER.name).?.node_type; + std.debug.assert(f.TYPE == .FUNCTION_TYPE); + function_return_type = f.TYPE.FUNCTION_TYPE.return_type; + }, + .TYPE => |x| { + function_return_type = x.FUNCTION_TYPE.return_type; + }, else => unreachable, - }; + } + + std.debug.print("FN: {s} -> ret: {any}\n", .{ function_call_statement.expression.PRIMARY_EXPRESSION.IDENTIFIER.name, function_return_type }); return self.create_variable(.{ .value = res, @@ -260,7 +271,7 @@ pub const CodeGen = struct { const val = try self.generate_expression_value(expression.?, null); - std.debug.print("3TYP {any}: {any} vs {any}\n", .{ expression.?, self.current_function_return_type.?, val.node_type }); + std.debug.print("3TYP : {any} vs {any}\n", .{ self.current_function_return_type.?, val.node_type }); std.debug.assert(self.compare_types(self.current_function_return_type.?, val.node_type, false)); _ = llvm.LLVMBuildRet(self.builder, val.value); |