diff options
| author | Baitinq <[email protected]> | 2025-01-29 00:19:03 +0100 |
|---|---|---|
| committer | Baitinq <[email protected]> | 2025-01-29 00:19:56 +0100 |
| commit | 87160741fa6d81318a492708fd83984c8807fc77 (patch) | |
| tree | 8156ee9cc8e32da6ea7facf951ce9ac784bd1232 /src | |
| parent | Codegen: Get printf working (diff) | |
| download | interpreter-87160741fa6d81318a492708fd83984c8807fc77.tar.gz interpreter-87160741fa6d81318a492708fd83984c8807fc77.tar.bz2 interpreter-87160741fa6d81318a492708fd83984c8807fc77.zip | |
Codegen: Get variable declarations kind of working
Diffstat (limited to 'src')
| -rw-r--r-- | src/codegen.zig | 55 |
1 files changed, 37 insertions, 18 deletions
diff --git a/src/codegen.zig b/src/codegen.zig index a6a086d..5016e26 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -14,6 +14,7 @@ pub const CodeGenError = error{ pub const CodeGen = struct { llvm_module: types.LLVMModuleRef, builder: types.LLVMBuilderRef, + symbol_table: std.StringHashMap(types.LLVMValueRef), arena: std.mem.Allocator, @@ -30,6 +31,7 @@ pub const CodeGen = struct { self.* = .{ .llvm_module = module, .builder = builder, + .symbol_table = std.StringHashMap(types.LLVMValueRef).init(arena), .arena = arena, }; @@ -112,21 +114,34 @@ pub const CodeGen = struct { //tmp std.debug.assert(assignment_statement.is_declaration == true); - std.debug.assert(assignment_statement.expression.* == parser.Node.FUNCTION_DEFINITION); - - const function_name = try std.fmt.allocPrintZ(self.arena, "{s}", .{assignment_statement.name}); - - const function_type = core.LLVMFunctionType(core.LLVMInt64Type(), &[_]types.LLVMTypeRef{}, 0, 0) orelse return CodeGenError.CompilationError; - const function = core.LLVMAddFunction(self.llvm_module, function_name, function_type) orelse return CodeGenError.CompilationError; - const function_entry = core.LLVMAppendBasicBlock(function, "entrypoint") orelse return CodeGenError.CompilationError; - core.LLVMPositionBuilderAtEnd(self.builder, function_entry); - - //tmp - std.debug.assert(assignment_statement.expression.* == parser.Node.FUNCTION_DEFINITION); - const function_defintion = assignment_statement.expression.FUNCTION_DEFINITION; - - for (function_defintion.statements) |stmt| { - try self.generate_statement(stmt); + const variable_name = try std.fmt.allocPrintZ(self.arena, "{s}", .{assignment_statement.name}); + + switch (assignment_statement.expression.*) { + .FUNCTION_DEFINITION => { + const function_type = core.LLVMFunctionType(core.LLVMInt64Type(), &[_]types.LLVMTypeRef{}, 0, 0) orelse return CodeGenError.CompilationError; + const function = core.LLVMAddFunction(self.llvm_module, variable_name, function_type) orelse return CodeGenError.CompilationError; + const function_entry = core.LLVMAppendBasicBlock(function, "entrypoint") orelse return CodeGenError.CompilationError; + core.LLVMPositionBuilderAtEnd(self.builder, function_entry); + + //tmp + std.debug.assert(assignment_statement.expression.* == parser.Node.FUNCTION_DEFINITION); + const function_defintion = assignment_statement.expression.FUNCTION_DEFINITION; + + for (function_defintion.statements) |stmt| { + try self.generate_statement(stmt); + } + }, + .PRIMARY_EXPRESSION => |exp| { + switch (exp) { + .NUMBER => { + const variable = core.LLVMBuildAlloca(self.builder, core.LLVMInt64Type(), variable_name) orelse return CodeGenError.CompilationError; + _ = core.LLVMBuildStore(self.builder, core.LLVMConstInt(core.LLVMInt64Type(), @intCast(exp.NUMBER.value), 0), variable) orelse return CodeGenError.CompilationError; + try self.symbol_table.put(variable_name, variable); + }, + else => unreachable, + } + }, + else => unreachable, } } @@ -143,8 +158,12 @@ pub const CodeGen = struct { std.debug.assert(function_call_statement.arguments.len == 1); const argument = function_call_statement.arguments[0]; std.debug.assert(argument.* == .PRIMARY_EXPRESSION); - std.debug.assert(argument.PRIMARY_EXPRESSION == .NUMBER); - const num_argument = argument.PRIMARY_EXPRESSION.NUMBER; + + const num_argument: types.LLVMValueRef = switch (argument.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).?, "").?, + else => unreachable, + }; const function_name = try std.fmt.allocPrintZ(self.arena, "{s}", .{ident.name}); const function = core.LLVMGetNamedFunction(self.llvm_module, function_name) orelse return CodeGenError.CompilationError; @@ -158,7 +177,7 @@ pub const CodeGen = struct { core.LLVMInt64Type(), }), 2, 0); - const arguments = @constCast(&[_]types.LLVMValueRef{ format_str_ptr, core.LLVMConstInt(core.LLVMInt64Type(), @intCast(num_argument.value), 0) }); + const arguments = @constCast(&[_]types.LLVMValueRef{ format_str_ptr, num_argument }); _ = core.LLVMBuildCall2(self.builder, fucntion_type, function, arguments, 2, "function_call") orelse return CodeGenError.CompilationError; } |