diff options
| author | Baitinq <[email protected]> | 2025-02-04 19:06:52 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-02-04 19:06:52 +0100 |
| commit | 65cd24c6842b7e525500767794f46b3c97d70ab3 (patch) | |
| tree | b3b74c5a6634331c91349f6b3ba1f5ac67c54dd0 | |
| parent | Codegen: Fix verifier errors and ensure verifier runs (diff) | |
| download | interpreter-65cd24c6842b7e525500767794f46b3c97d70ab3.tar.gz interpreter-65cd24c6842b7e525500767794f46b3c97d70ab3.tar.bz2 interpreter-65cd24c6842b7e525500767794f46b3c97d70ab3.zip | |
Codegen: Support anonymous functions
| -rw-r--r-- | src/codegen.zig | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/codegen.zig b/src/codegen.zig index 593239b..adee266 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -154,12 +154,18 @@ pub const CodeGen = struct { std.debug.assert(statement.* == parser.Node.FUNCTION_CALL_STATEMENT); const function_call_statement = statement.FUNCTION_CALL_STATEMENT; - std.debug.assert(function_call_statement.expression.* == parser.Node.PRIMARY_EXPRESSION); - const primary_expression = function_call_statement.expression.PRIMARY_EXPRESSION; - - std.debug.assert(primary_expression == .IDENTIFIER); - const ident = primary_expression.IDENTIFIER; - const xd = self.environment.get_variable(ident.name) orelse return CodeGenError.CompilationError; + var function: *Variable = undefined; + switch (function_call_statement.expression.*) { + .PRIMARY_EXPRESSION => |primary_expression| { + std.debug.assert(primary_expression == .IDENTIFIER); + const ident = primary_expression.IDENTIFIER; + function = self.environment.get_variable(ident.name) orelse return CodeGenError.CompilationError; + }, + .FUNCTION_DEFINITION => |*function_definition| { + function = try self.generate_expression_value(@ptrCast(function_definition), null); + }, + else => unreachable, + } var arguments = std.ArrayList(types.LLVMValueRef).init(self.arena); @@ -168,7 +174,7 @@ pub const CodeGen = struct { try arguments.append(arg.value); } - return core.LLVMBuildCall2(self.builder, xd.type, xd.value, @ptrCast(arguments.items), @intCast(arguments.items.len), "") orelse return CodeGenError.CompilationError; + return core.LLVMBuildCall2(self.builder, function.type, function.value, @ptrCast(arguments.items), @intCast(arguments.items.len), "") orelse return CodeGenError.CompilationError; } fn generate_return_statement(self: *CodeGen, statement: *parser.Node) !void { @@ -223,7 +229,7 @@ pub const CodeGen = struct { try paramtypes.append(core.LLVMInt64Type()); } const function_type = core.LLVMFunctionType(core.LLVMInt64Type(), paramtypes.items.ptr, @intCast(paramtypes.items.len), 0) orelse return CodeGenError.CompilationError; - const function = core.LLVMAddFunction(self.llvm_module, try std.fmt.allocPrintZ(self.arena, "{s}", .{name orelse ""}), function_type) orelse return CodeGenError.CompilationError; + const function = core.LLVMAddFunction(self.llvm_module, try std.fmt.allocPrintZ(self.arena, "{s}", .{name orelse "unnamed_func"}), function_type) orelse return CodeGenError.CompilationError; const function_entry = core.LLVMAppendBasicBlock(function, "entrypoint") orelse return CodeGenError.CompilationError; core.LLVMPositionBuilderAtEnd(self.builder, function_entry); |