about summary refs log tree commit diff
path: root/src/codegen.zig
diff options
context:
space:
mode:
authorBaitinq <[email protected]>2025-02-15 10:56:08 +0100
committerBaitinq <[email protected]>2025-02-15 11:00:04 +0100
commitfa6d9cdf57db4244a331035d35d1b962630e3ae1 (patch)
treed3926279eb046bbc11d4102ba8b336d037596da1 /src/codegen.zig
parentExamples: Update initial example with types (diff)
downloadinterpreter-fa6d9cdf57db4244a331035d35d1b962630e3ae1.tar.gz
interpreter-fa6d9cdf57db4244a331035d35d1b962630e3ae1.tar.bz2
interpreter-fa6d9cdf57db4244a331035d35d1b962630e3ae1.zip
Feature: Introduce initial support for function return types
Diffstat (limited to '')
-rw-r--r--src/codegen.zig8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index 3884923..ed1890d 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -247,7 +247,8 @@ pub const CodeGen = struct {
                     std.debug.assert(param.PRIMARY_EXPRESSION == .IDENTIFIER);
                     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 return_type = get_llvm_type(function_definition.return_type);
+                const function_type = core.LLVMFunctionType(return_type, 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 "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);
@@ -455,6 +456,11 @@ pub const CodeGen = struct {
         });
     }
 
+    fn get_llvm_type(type_name: []const u8) types.LLVMTypeRef {
+        if (std.mem.eql(u8, type_name, "i64")) return core.LLVMInt64Type();
+        unreachable;
+    }
+
     fn create_print_function(self: *CodeGen) !void {
         const print_function_type = core.LLVMFunctionType(core.LLVMVoidType(), @constCast(&[_]types.LLVMTypeRef{core.LLVMInt64Type()}), 1, 0);
         const print_function = core.LLVMAddFunction(self.llvm_module, "print", print_function_type);