authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-12 13:13:14+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-30 15:58:12+02:00
log2c12f4a993343abb65ebb881f095c38ba0310306
treeeeb47808ba79d012f6e2d8c5e1ef4647f3ec80e7
parent7e2774367e5420a8f388b57f80bede8f0f680ca7
signaturelock-open Commit is signed but in an unrecognized format.

stage2: implement Value.eql for void, null and types


1 files changed, 97 insertions(+), 5 deletions(-)

src/value.zig+97-5
......@@ -1233,12 +1233,23 @@ pub const Value = extern union {
12331233 }
12341234
12351235 pub fn eql(a: Value, b: Value) bool {
1236 if (a.tag() == b.tag() and a.tag() == .enum_literal) {
1237 const a_name = @fieldParentPtr(Payload.Bytes, "base", a.ptr_otherwise).data;
1238 const b_name = @fieldParentPtr(Payload.Bytes, "base", b.ptr_otherwise).data;
1239 return std.mem.eql(u8, a_name, b_name);
1236 if (a.tag() == b.tag()) {
1237 if (a.tag() == .void_value or a.tag() == .null_value) {
1238 return true;
1239 } else if (a.tag() == .enum_literal) {
1240 const a_name = @fieldParentPtr(Payload.Bytes, "base", a.ptr_otherwise).data;
1241 const b_name = @fieldParentPtr(Payload.Bytes, "base", b.ptr_otherwise).data;
1242 return std.mem.eql(u8, a_name, b_name);
1243 }
1244 }
1245 if (a.isType() and b.isType()) {
1246 // 128 bytes should be enough to hold both types
1247 var buf: [128]u8 = undefined;
1248 var fib = std.heap.FixedBufferAllocator.init(&buf);
1249 const a_type = a.toType(&fib.allocator) catch unreachable;
1250 const b_type = b.toType(&fib.allocator) catch unreachable;
1251 return a_type.eql(b_type);
12401252 }
1241 // TODO non numerical comparisons
12421253 return compare(a, .eq, b);
12431254 }
12441255
......@@ -1656,6 +1667,87 @@ pub const Value = extern union {
16561667 };
16571668 }
16581669
1670 /// Valid for all types. Asserts the value is not undefined.
1671 pub fn isType(self: Value) bool {
1672 return switch (self.tag()) {
1673 .ty,
1674 .int_type,
1675 .u8_type,
1676 .i8_type,
1677 .u16_type,
1678 .i16_type,
1679 .u32_type,
1680 .i32_type,
1681 .u64_type,
1682 .i64_type,
1683 .usize_type,
1684 .isize_type,
1685 .c_short_type,
1686 .c_ushort_type,
1687 .c_int_type,
1688 .c_uint_type,
1689 .c_long_type,
1690 .c_ulong_type,
1691 .c_longlong_type,
1692 .c_ulonglong_type,
1693 .c_longdouble_type,
1694 .f16_type,
1695 .f32_type,
1696 .f64_type,
1697 .f128_type,
1698 .c_void_type,
1699 .bool_type,
1700 .void_type,
1701 .type_type,
1702 .anyerror_type,
1703 .comptime_int_type,
1704 .comptime_float_type,
1705 .noreturn_type,
1706 .null_type,
1707 .undefined_type,
1708 .fn_noreturn_no_args_type,
1709 .fn_void_no_args_type,
1710 .fn_naked_noreturn_no_args_type,
1711 .fn_ccc_void_no_args_type,
1712 .single_const_pointer_to_comptime_int_type,
1713 .const_slice_u8_type,
1714 .enum_literal_type,
1715 .anyframe_type,
1716 .error_set,
1717 => true,
1718
1719 .zero,
1720 .one,
1721 .empty_array,
1722 .bool_true,
1723 .bool_false,
1724 .function,
1725 .variable,
1726 .int_u64,
1727 .int_i64,
1728 .int_big_positive,
1729 .int_big_negative,
1730 .ref_val,
1731 .decl_ref,
1732 .elem_ptr,
1733 .bytes,
1734 .repeated,
1735 .float_16,
1736 .float_32,
1737 .float_64,
1738 .float_128,
1739 .void_value,
1740 .enum_literal,
1741 .@"error",
1742 .empty_struct_value,
1743 .null_value,
1744 => false,
1745
1746 .undef => unreachable,
1747 .unreachable_value => unreachable,
1748 };
1749 }
1750
16591751 /// This type is not copyable since it may contain pointers to its inner data.
16601752 pub const Payload = struct {
16611753 tag: Tag,