Skip to content

Commit

Permalink
Adds support for parsing vectors at comptime
Browse files Browse the repository at this point in the history
  • Loading branch information
MasonRemaley committed Feb 1, 2025
1 parent fafd323 commit e61152a
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/Sema/LowerZon.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn lowerExpr(self: *LowerZon, node: Zoir.Node.Index, res_ty: Type) CompileError!
.@"struct" => return self.lowerStructOrTuple(node, res_ty),
.@"union" => return self.lowerUnion(node, res_ty),
.pointer => return self.lowerPointer(node, res_ty),
.vector => return self.lowerVector(node, res_ty),

.type,
.noreturn,
Expand All @@ -98,7 +99,6 @@ fn lowerExpr(self: *LowerZon, node: Zoir.Node.Index, res_ty: Type) CompileError!
.@"opaque",
.frame,
.@"anyframe",
.vector,
.void,
=> return self.fail(
node,
Expand Down Expand Up @@ -669,3 +669,34 @@ fn lowerUnion(self: *LowerZon, node: Zoir.Node.Index, res_ty: Type) !InternPool.
.val = val,
});
}

fn lowerVector(self: *LowerZon, node: Zoir.Node.Index, res_ty: Type) !InternPool.Index {
const ip = &self.sema.pt.zcu.intern_pool;

const vector_info = ip.indexToKey(res_ty.toIntern()).vector_type;

const elem_nodes: Zoir.Node.Index.Range = switch (node.get(self.file.zoir.?)) {
.array_literal => |nodes| nodes,
.empty_literal => .{ .start = node, .len = 0 },
else => return self.fail(node, "expected type '{}'", .{res_ty.fmt(self.sema.pt)}),
};

const elems = try self.sema.arena.alloc(InternPool.Index, vector_info.len);

if (elem_nodes.len != vector_info.len) {
return self.fail(
node,
"expected {} vector elements; found {}",
.{ vector_info.len, elem_nodes.len },
);
}

for (elems, 0..) |*elem, i| {
elem.* = try self.lowerExpr(elem_nodes.at(@intCast(i)), .fromInterned(vector_info.child));
}

return self.sema.pt.intern(.{ .aggregate = .{
.ty = res_ty.toIntern(),
.storage = .{ .elems = elems },
} });
}
35 changes: 35 additions & 0 deletions test/behavior/zon.zig
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,38 @@ test "inf and nan" {
try expect(std.math.isNegativeInf(actual[2]));
}
}

test "vector" {
{
const actual: @Vector(0, bool) = @import("zon/vec0.zon");
const expected: @Vector(0, bool) = .{};
try expectEqual(expected, actual);
}
{
const actual: @Vector(3, bool) = @import("zon/vec3_bool.zon");
const expected: @Vector(3, bool) = .{ false, false, true };
try expectEqual(expected, actual);
}

{
const actual: @Vector(0, f32) = @import("zon/vec0.zon");
const expected: @Vector(0, f32) = .{};
try expectEqual(expected, actual);
}
{
const actual: @Vector(3, f32) = @import("zon/vec3_float.zon");
const expected: @Vector(3, f32) = .{ 1.5, 2.5, 3.5 };
try expectEqual(expected, actual);
}

{
const actual: @Vector(0, u8) = @import("zon/vec0.zon");
const expected: @Vector(0, u8) = .{};
try expectEqual(expected, actual);
}
{
const actual: @Vector(3, u8) = @import("zon/vec3_int.zon");
const expected: @Vector(3, u8) = .{ 2, 4, 6 };
try expectEqual(expected, actual);
}
}
1 change: 1 addition & 0 deletions test/behavior/zon/vec3_bool.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.{ false, false, true }
1 change: 1 addition & 0 deletions test/behavior/zon/vec3_float.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.{ 1.5, 2.5, 3.5 }
1 change: 1 addition & 0 deletions test/behavior/zon/vec3_int.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.{ 2, 4, 6 }
10 changes: 10 additions & 0 deletions test/cases/compile_errors/@import_zon_vec_too_few.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export fn entry() void {
const f: @Vector(3, f32) = @import("zon/tuple.zon");
_ = f;
}

// error
// imports=zon/tuple.zon
//
// zon/tuple.zon:1:2: error: expected 3 vector elements; found 2
// tmp.zig:2:40: note: imported here
10 changes: 10 additions & 0 deletions test/cases/compile_errors/@import_zon_vec_too_many.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export fn entry() void {
const f: @Vector(1, f32) = @import("zon/tuple.zon");
_ = f;
}

// error
// imports=zon/tuple.zon
//
// zon/tuple.zon:1:2: error: expected 1 vector elements; found 2
// tmp.zig:2:40: note: imported here
10 changes: 10 additions & 0 deletions test/cases/compile_errors/@import_zon_vec_wrong_type.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export fn entry() void {
const f: @Vector(2, bool) = @import("zon/tuple.zon");
_ = f;
}

// error
// imports=zon/tuple.zon
//
// zon/tuple.zon:1:4: error: expected type 'bool'
// tmp.zig:2:41: note: imported here

0 comments on commit e61152a

Please sign in to comment.