This commit is contained in:
agra
2026-02-10 22:47:43 +02:00
parent ef14144d49
commit 70435d3c85
11 changed files with 443 additions and 5 deletions

View File

@@ -206,7 +206,14 @@ pub const Parser = struct {
fn parseTypeExpr(self: *Parser) anyerror!*Node {
const start = self.current.loc.start;
// Array type: [N]T or Slice type: []T
// Pointer type: *T
if (self.current.tag == .star) {
self.advance(); // skip '*'
const pointee_type = try self.parseTypeExpr();
return try self.createNode(start, .{ .pointer_type_expr = .{ .pointee_type = pointee_type } });
}
// Array type: [N]T, Slice type: []T, Many-pointer type: [*]T
if (self.current.tag == .l_bracket) {
self.advance(); // skip '['
if (self.current.tag == .r_bracket) {
@@ -215,6 +222,13 @@ pub const Parser = struct {
const elem_type = try self.parseTypeExpr();
return try self.createNode(start, .{ .slice_type_expr = .{ .element_type = elem_type } });
}
if (self.current.tag == .star) {
// Many-pointer type: [*]T
self.advance(); // skip '*'
try self.expect(.r_bracket); // expect ']'
const elem_type = try self.parseTypeExpr();
return try self.createNode(start, .{ .many_pointer_type_expr = .{ .element_type = elem_type } });
}
const len_node = try self.parseExpr();
try self.expect(.r_bracket);
const elem_type = try self.parseTypeExpr();
@@ -819,6 +833,12 @@ pub const Parser = struct {
const operand = try self.parseUnary();
return try self.createNode(start, .{ .unary_op = .{ .op = .xx, .operand = operand } });
}
if (self.current.tag == .ampersand) {
const start = self.current.loc.start;
self.advance();
const operand = try self.parseUnary();
return try self.createNode(start, .{ .unary_op = .{ .op = .address_of, .operand = operand } });
}
// cast(Type) expr — prefix operator with type parameter
if (self.current.tag == .identifier and std.mem.eql(u8, self.tokenSlice(self.current), "cast")) {
const saved_lexer = self.lexer;
@@ -896,6 +916,10 @@ pub const Parser = struct {
.elements = try elements.toOwnedSlice(self.allocator),
.type_expr = expr,
} });
} else if (self.current.tag == .star) {
// Dereference: expr.*
self.advance();
expr = try self.createNode(expr.span.start, .{ .deref_expr = .{ .operand = expr } });
} else {
// Field access
if (self.current.tag != .identifier) {
@@ -985,6 +1009,10 @@ pub const Parser = struct {
self.advance();
return try self.createNode(start, .{ .bool_literal = .{ .value = false } });
},
.kw_null => {
self.advance();
return try self.createNode(start, .{ .null_literal = {} });
},
.identifier => {
const name = self.tokenSlice(self.current);
// Check if this identifier is a type name (e.g. s32, u8, s128)