-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.zig
More file actions
95 lines (81 loc) · 2.95 KB
/
value.zig
File metadata and controls
95 lines (81 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const std = @import("std");
const Object = @import("./object.zig").Object;
const ObjectType = @import("./object.zig").ObjectType;
pub const ValueType = enum { number, boolean, nil, obj };
pub const Value = union(ValueType) {
const Self = @This();
number: f64,
boolean: bool,
nil,
obj: *Object,
pub inline fn NumberValue(val: f64) Self {
return Value{ .number = val };
}
pub inline fn BooleanValue(val: bool) Self {
return Value{ .boolean = val };
}
pub inline fn NilValue() Self {
return Value.nil;
}
pub inline fn ObjectValue(val: *Object) Self {
return Value{ .obj = val };
}
pub inline fn isBool(self: Self) bool {
return @as(ValueType, self) == ValueType.boolean;
}
pub inline fn isObj(self: Self) bool {
return @as(ValueType, self) == ValueType.obj;
}
pub inline fn asNumber(self: Self) f64 {
return self.number;
}
pub inline fn isObjType(self: Self, objType: ObjectType) bool {
if (!self.isObj()) return false;
switch (self.obj.objType) {
.STRING => return objType == .STRING,
.FUNCTION => return objType == .FUNCTION,
.NATIVE_FUNC => return objType == .NATIVE_FUNC,
.CLOSURE => return objType == .CLOSURE,
.UPVALUE => return objType == .UPVALUE,
.CLASS => return objType == .CLASS,
.INSTANCE => return objType == .INSTANCE,
//else => return false,
}
}
pub inline fn isNil(self: Self) bool {
return @as(ValueType, self) == ValueType.nil;
}
pub inline fn equals(self: Self, second: Value) bool {
switch (self) {
.nil => return second == .nil,
.boolean => return (second == .boolean and self.boolean == second.boolean),
.number => return (second == .number and self.number == second.number),
.obj => return second == .obj and self.obj == second.obj,
//else => return false,
}
}
pub inline fn isNaN(self: Self) bool {
return @as(ValueType, self) != ValueType.number;
}
};
pub fn printValue(value: Value) void {
switch (value) {
.number => std.debug.print("{d}\n", .{value.number}),
.boolean => std.debug.print("{}\n", .{value.boolean}),
.obj => |objVal| {
switch (objVal.objType) {
.STRING => std.debug.print("{s}\n", .{objVal.asString().chars}),
.FUNCTION => {
const name = if (objVal.asFunction().name) |name| name.chars else "script";
std.debug.print("<fn {s}>\n", .{name});
},
.NATIVE_FUNC => objVal.printObj(),
.CLOSURE => objVal.printObj(),
.UPVALUE => objVal.printObj(),
.CLASS => objVal.printObj(),
.INSTANCE => objVal.printObj(),
}
},
.nil => std.debug.print("nil\n", .{}),
}
}