summary refs log tree commit diff
path: root/src/codegen.zig
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-30 23:39:16 +0100
committerBaitinq <manuelpalenzuelamerino@gmail.com>2025-01-30 23:39:32 +0100
commit8d5f212c9875818c75e3007ecadadfd112a3438d (patch)
treefc5057a9b83c758a2bc16540b356a5f96afaf736 /src/codegen.zig
parentCodegen: Get addition kind of working (diff)
downloadinterpreter-8d5f212c9875818c75e3007ecadadfd112a3438d.tar.gz
interpreter-8d5f212c9875818c75e3007ecadadfd112a3438d.tar.bz2
interpreter-8d5f212c9875818c75e3007ecadadfd112a3438d.zip
Codegen: Fix bug calling printf
Diffstat (limited to '')
-rw-r--r--src/codegen.zig13
1 files changed, 6 insertions, 7 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index 9278650..46c2dd1 100644
--- a/src/codegen.zig
+++ b/src/codegen.zig
@@ -44,7 +44,7 @@ pub const CodeGen = struct {
         const printf_function_type = core.LLVMFunctionType(core.LLVMVoidType(), @constCast(&[_]types.LLVMTypeRef{
             core.LLVMPointerType(core.LLVMInt8Type(), 0),
             core.LLVMInt64Type(),
-        }), 2, 0);
+        }), 2, 1);
         const printf_function = core.LLVMAddFunction(self.llvm_module, "printf", printf_function_type) orelse return CodeGenError.CompilationError;
 
         const print_function_type = core.LLVMFunctionType(core.LLVMVoidType(), @constCast(&[_]types.LLVMTypeRef{core.LLVMInt64Type()}), 1, 0);
@@ -60,7 +60,7 @@ pub const CodeGen = struct {
             core.LLVMGetParam(print_function, 0),
         });
 
-        _ = core.LLVMBuildCall2(self.builder, printf_function_type, printf_function, arguments, 2, "printf_call") orelse return CodeGenError.CompilationError;
+        _ = core.LLVMBuildCall2(self.builder, printf_function_type, printf_function, arguments, 2, "") orelse return CodeGenError.CompilationError;
         _ = core.LLVMBuildRetVoid(self.builder);
 
         try self.symbol_table.put("print", try self.create_variable(.{
@@ -155,6 +155,7 @@ pub const CodeGen = struct {
 
         std.debug.assert(primary_expression == .IDENTIFIER);
         const ident = primary_expression.IDENTIFIER;
+        const xd = self.symbol_table.get(ident.name) orelse return CodeGenError.CompilationError;
 
         var arguments = std.ArrayList(types.LLVMValueRef).init(self.arena);
 
@@ -163,8 +164,6 @@ pub const CodeGen = struct {
             try arguments.append(arg.value);
         }
 
-        const xd = self.symbol_table.get(ident.name) orelse return CodeGenError.CompilationError;
-
         return core.LLVMBuildCall2(self.builder, xd.type, xd.value, @ptrCast(arguments.items), @intCast(arguments.items.len), "") orelse return CodeGenError.CompilationError;
     }
 
@@ -236,10 +235,10 @@ pub const CodeGen = struct {
         const main_function = self.symbol_table.get("main") orelse return CodeGenError.CompilationError;
         const main_function_return = core.LLVMBuildCall2(self.builder, main_function.type, main_function.value, &[_]types.LLVMTypeRef{}, 0, "main_call") orelse return CodeGenError.CompilationError;
 
-        const exit_func_type = core.LLVMFunctionType(core.LLVMInt8Type(), @constCast(&[_]types.LLVMTypeRef{core.LLVMInt8Type()}), 1, 0);
+        const exit_func_type = core.LLVMFunctionType(core.LLVMVoidType(), @constCast(&[_]types.LLVMTypeRef{core.LLVMInt8Type()}), 1, 0);
         const exit_func = core.LLVMAddFunction(self.llvm_module, "exit", exit_func_type);
-        const exit_func_return = core.LLVMBuildCall2(self.builder, exit_func_type, exit_func, @constCast(&[_]types.LLVMValueRef{main_function_return}), 1, "exit_call");
-        _ = core.LLVMBuildRet(self.builder, exit_func_return);
+        _ = core.LLVMBuildCall2(self.builder, exit_func_type, exit_func, @constCast(&[_]types.LLVMValueRef{main_function_return}), 1, "exit_call");
+        _ = core.LLVMBuildRetVoid(self.builder);
     }
 
     fn create_variable(self: *CodeGen, variable_value: Variable) !*Variable {