forked from luna/jorts
35 lines
703 B
Zig
35 lines
703 B
Zig
|
const std = @import("std");
|
||
|
|
||
|
const Allocator = std.mem.Allocator;
|
||
|
|
||
|
pub const ObjType = enum {
|
||
|
String,
|
||
|
};
|
||
|
|
||
|
pub const ObjValue = struct {
|
||
|
String: []u8,
|
||
|
};
|
||
|
|
||
|
pub const Object = struct {
|
||
|
otype: ObjType,
|
||
|
value: ObjValue,
|
||
|
};
|
||
|
|
||
|
pub fn copyString(allocator: *Allocator, data: []const u8) !*Object {
|
||
|
var str = try allocator.alloc(u8, data.len);
|
||
|
std.mem.copy(u8, str, data);
|
||
|
|
||
|
var obj = try allocator.create(Object);
|
||
|
obj.otype = ObjType.String;
|
||
|
obj.value = ObjValue{ .String = str };
|
||
|
|
||
|
return obj;
|
||
|
}
|
||
|
|
||
|
pub fn printObject(stdout: var, obj: Object) !void {
|
||
|
switch (obj.otype) {
|
||
|
.String => try stdout.print("{}", obj.value.String),
|
||
|
else => unreachable,
|
||
|
}
|
||
|
}
|