From 2d8538557dfdd8b86879e4a7ae35749f659bcb34 Mon Sep 17 00:00:00 2001 From: Baitinq Date: Mon, 24 Mar 2025 23:33:26 +0100 Subject: Feature: Add "proper" pointer types --- src/codegen.zig | 5 ++++- src/parser.zig | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/codegen.zig b/src/codegen.zig index 552dd4b..fc956a5 100644 --- a/src/codegen.zig +++ b/src/codegen.zig @@ -518,7 +518,6 @@ pub const CodeGen = struct { .SIMPLE_TYPE => |t| { if (std.mem.eql(u8, t.name, "i64")) return llvm.LLVMInt64Type(); if (std.mem.eql(u8, t.name, "bool")) return llvm.LLVMInt1Type(); - if (std.mem.eql(u8, t.name, "ptr")) return llvm.LLVMPointerType(llvm.LLVMInt8Type(), 0); //TODO: id like *i64 unreachable; }, // TODO: Properly handle this vv @@ -531,6 +530,10 @@ pub const CodeGen = struct { const function_type = llvm.LLVMFunctionType(return_type, paramtypes.items.ptr, @intCast(paramtypes.items.len), 0) orelse unreachable; return function_type; }, + .POINTER_TYPE => |t| { + const inner_type = try self.get_llvm_type(t.type); + return llvm.LLVMPointerType(inner_type, 0); + }, } } diff --git a/src/parser.zig b/src/parser.zig index d7c54f8..a66817c 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -73,6 +73,9 @@ pub const Node = union(enum) { parameters: []*Node, return_type: *Node, }, + POINTER_TYPE: struct { + type: *Node, + }, }, RETURN_STATEMENT: struct { expression: *Node, @@ -534,6 +537,15 @@ pub const Parser = struct { errdefer if (!self.try_context) std.debug.print("Error parsing type annotation {any}\n", .{self.peek_token()}); return self.accept_parse(parse_function_type) orelse switch (self.consume_token().?.type) { + .MUL => { + return self.create_node(.{ + .TYPE = .{ + .POINTER_TYPE = .{ + .type = try self.parse_type(), + }, + }, + }); + }, .IDENTIFIER => |ident| { //TODO: we should only accept specific type identifiers return try self.create_node(.{ -- cgit 1.4.1