2019-06-01 04:46:01 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
const Allocator = std.mem.Allocator;
|
|
|
|
|
|
|
|
// NOTE: right now, only numbers.
|
2019-06-02 02:33:53 +00:00
|
|
|
|
|
|
|
pub const ValueType = enum(u8) {
|
|
|
|
Bool,
|
|
|
|
Nil,
|
|
|
|
Number,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const ValueValue = union(ValueType) {
|
|
|
|
Bool: bool,
|
|
|
|
Nil: void,
|
|
|
|
Number: f64,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const Value = struct {
|
|
|
|
vtype: ValueType,
|
|
|
|
as: ValueValue,
|
|
|
|
};
|
|
|
|
|
|
|
|
// helper functions
|
|
|
|
pub fn BoolVal(val: bool) Value {
|
|
|
|
return Value{ .vtype = .Bool, .as = ValueValue{ .Bool = val } };
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn NilVal() Value {
|
|
|
|
return Value{ .vtype = .Nil, .as = ValueValue{ .Nil = {} } };
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn NumberVal(val: f64) Value {
|
|
|
|
return Value{ .vtype = .Number, .as = ValueValue{ .Number = val } };
|
|
|
|
}
|
2019-06-01 04:46:01 +00:00
|
|
|
|
|
|
|
pub fn printValue(stdout: var, value: Value) !void {
|
2019-06-02 02:33:53 +00:00
|
|
|
switch (value.as) {
|
2019-06-02 02:44:59 +00:00
|
|
|
.Nil => try stdout.print("nil"),
|
|
|
|
.Bool => try stdout.print("{}", value.as.Bool),
|
2019-06-02 02:33:53 +00:00
|
|
|
.Number => try stdout.print("{}", value.as.Number),
|
|
|
|
else => unreachable,
|
|
|
|
}
|
2019-06-01 04:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub const ValueList = struct {
|
2019-06-01 05:06:23 +00:00
|
|
|
count: usize,
|
2019-06-01 04:46:01 +00:00
|
|
|
values: []Value,
|
|
|
|
allocator: *Allocator,
|
|
|
|
|
|
|
|
pub fn init(allocator: *Allocator) !ValueList {
|
|
|
|
return ValueList{
|
|
|
|
.count = 0,
|
|
|
|
.allocator = allocator,
|
|
|
|
.values = try allocator.alloc(Value, 0),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write(self: *ValueList, value: Value) !void {
|
|
|
|
if (self.values.len < self.count + 1) {
|
|
|
|
self.values = try self.allocator.realloc(
|
|
|
|
self.values,
|
|
|
|
self.count + 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.values[self.count] = value;
|
|
|
|
self.count += 1;
|
|
|
|
}
|
|
|
|
};
|