diff options
| author | Baitinq <[email protected]> | 2025-01-30 00:44:40 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-01-30 00:44:40 +0100 |
| commit | 43bd11bd44fedb6056609a3b95bd17ecf0ad21a4 (patch) | |
| tree | 633f065a23594f63e8d6ba06c68a409729034f87 /src | |
| parent | Codegen: Get function declaration/calling kind of working (diff) | |
| download | interpreter-43bd11bd44fedb6056609a3b95bd17ecf0ad21a4.tar.gz interpreter-43bd11bd44fedb6056609a3b95bd17ecf0ad21a4.tar.bz2 interpreter-43bd11bd44fedb6056609a3b95bd17ecf0ad21a4.zip | |
Codegen: Support function calls in return statement
Diffstat (limited to 'src')
| -rw-r--r-- | src/codegen.zig | 15 | ||||
| -rw-r--r-- | src/evaluator.zig | 1 | ||||
| -rw-r--r-- | src/parser.zig | 3 |
3 files changed, 9 insertions, 10 deletions
diff --git a/src/codegen.zig b/src/codegen.zig index 3404314..cb94f3e 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -183,7 +183,7 @@ pub const CodeGen = struct { } } - fn generate_function_call_statement(self: *CodeGen, statement: *parser.Node) !void { + fn generate_function_call_statement(self: *CodeGen, statement: *parser.Node) !types.LLVMValueRef { std.debug.assert(statement.* == parser.Node.FUNCTION_CALL_STATEMENT); const function_call_statement = statement.FUNCTION_CALL_STATEMENT; @@ -206,18 +206,21 @@ pub const CodeGen = struct { const xd = self.symbol_table.get(ident.name) orelse return CodeGenError.CompilationError; - _ = core.LLVMBuildCall2(self.builder, xd.type, xd.value, @ptrCast(arguments.items), @intCast(arguments.items.len), "function_call") orelse return CodeGenError.CompilationError; + return core.LLVMBuildCall2(self.builder, xd.type, xd.value, @ptrCast(arguments.items), @intCast(arguments.items.len), "function_call") orelse return CodeGenError.CompilationError; } fn generate_return_statement(self: *CodeGen, statement: *parser.Node) !void { std.debug.assert(statement.* == parser.Node.RETURN_STATEMENT); const expression = statement.RETURN_STATEMENT.expression; - std.debug.assert(expression.* == parser.Node.PRIMARY_EXPRESSION); - const num_argument: types.LLVMValueRef = switch (expression.PRIMARY_EXPRESSION) { - .NUMBER => |n| core.LLVMConstInt(core.LLVMInt64Type(), @intCast(n.value), 0), - .IDENTIFIER => |i| core.LLVMBuildLoad2(self.builder, core.LLVMInt64Type(), self.symbol_table.get(i.name).?.value, "").?, + const num_argument: types.LLVMValueRef = switch (expression.*) { + .PRIMARY_EXPRESSION => |primary_expression| switch (primary_expression) { + .NUMBER => |n| core.LLVMConstInt(core.LLVMInt64Type(), @intCast(n.value), 0), + .IDENTIFIER => |i| core.LLVMBuildLoad2(self.builder, core.LLVMInt64Type(), self.symbol_table.get(i.name).?.value, "").?, + else => unreachable, + }, + .FUNCTION_CALL_STATEMENT => |*fn_call| try self.generate_function_call_statement(@ptrCast(fn_call)), else => unreachable, }; diff --git a/src/evaluator.zig b/src/evaluator.zig index 3a145f8..ad489f1 100644 --- a/src/evaluator.zig +++ b/src/evaluator.zig @@ -207,7 +207,6 @@ pub const Evaluator = struct { return val; }, - else => unreachable, } }, // I don't like having 2 places where we evaluate functions diff --git a/src/parser.zig b/src/parser.zig index 3f77b19..c5eb623 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -58,9 +58,6 @@ pub const Node = union(enum) { IDENTIFIER: struct { name: []const u8, }, - FUNCTION_CALL: struct { - name: []const u8, - }, }, FUNCTION_DEFINITION: struct { statements: []*Node, |