-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchunk.zig
More file actions
152 lines (137 loc) · 4.32 KB
/
chunk.zig
File metadata and controls
152 lines (137 loc) · 4.32 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const std = @import("std");
const memanage = @import("utils.mem.zig"); // Ensure this path is correct
const List = @import("list.zig").List;
const Value = @import("value.zig").Value;
const ValuesList = @import("valueslist.zig").ValuesList;
pub const OpCode = enum(u8) {
const Self = @This();
op_return,
op_print,
op_constant,
op_nil,
op_true,
op_false,
op_pop,
op_duplicate,
op_define_global,
op_get_global,
op_set_global,
op_get_local,
op_set_local,
op_set_upvalue,
op_get_upvalue,
op_class,
op_jump_if_false,
op_jump,
op_loop,
op_call,
op_set_property,
op_get_property,
op_closure,
op_close_upvalue,
op_not,
op_equal,
op_greater,
op_less,
op_negate,
op_add,
op_subtract,
op_mult,
op_divide,
op_constant_long,
pub fn toU8(self: Self) u8 {
return @intFromEnum(self);
}
pub fn fromU8(n: u8) Self {
return @enumFromInt(n);
}
pub fn toString(self: Self) []const u8 {
return switch (self) {
.op_return => "OP_RETURN",
.op_print => "OP_PRINT",
.op_constant => "OP_CONSTANT",
.op_add => "OP_ADD",
.op_subtract => "OP_SUBTRACT",
.op_mult => "OP_MULTIPLY",
.op_divide => "OP_DIVIDE",
.op_negate => "OP_NEGATE",
.op_equal => "OP_EQUAL",
.op_nil => "OP_NIL",
.op_true => "OP_TRUE",
.op_false => "OP_FALSE",
.op_pop => "OP_POP",
.op_duplicate => "OP_DUPLICTE",
.op_define_global => "OP_DEFINE_GLOBAL",
.op_get_global => "OP_GET_GLOBAL",
.op_set_global => "OP_SET_GLOBAL",
.op_get_local => "OP_GET_LOCAL",
.op_set_local => "OP_SET_LOCAL",
.op_set_upvalue => "OP_SET_UPVALUE",
.op_get_upvalue => "OP_GET_UPVALUE",
.op_class => "OP_CLASS",
.op_jump_if_false => "OP_JUMP_IF_FALSE",
.op_closure => "OP_CLOSURE",
.op_close_upvalue => "OP_CLOSE_UPVALUE",
.op_jump => "OP_JUMP",
.op_loop => "OP_LOOP",
.op_call => "OP_CALL",
.op_set_property => "OP_SET_PROPERTY",
.op_get_property => "OP_GET_PROPERTY",
.op_not => "OP_NOT",
.op_greater => "OP_GREATER",
.op_less => "OP_LESS",
.op_constant_long => "OP_CONSTANT_LONG",
};
}
};
pub const Chunk = struct {
const Self = @This();
const BytesList = List(u8);
const ValueList = ValuesList(Value);
const LinesList = List(usize);
code: BytesList,
lines: LinesList,
constants: ValueList,
pub fn init(allocator: *std.mem.Allocator) Self {
return Self{
.code = BytesList.init(allocator),
.lines = LinesList.init(allocator),
.constants = ValueList.init(allocator),
};
}
pub fn writeChunk(self: *Self, byte: u8, line: usize) !void {
self.code.appendItem(byte);
self.lines.appendItem(line);
}
pub fn addConstant(self: *Self, value: Value) usize {
const idx: usize = self.constants.count;
self.constants.appendValue(value);
return idx;
}
pub fn writeConstant(self: *Self, value: Value, line: usize) void {
// Add the constant to the chunk and get the index.
const idx: usize = addConstant(self, value);
if (idx < 256) {
// If the index fits in one byte, use OP_CONSTANT.
try writeChunk(self, OpCode.op_constant.toU8(), line);
try writeChunk(self, @intCast(idx), line);
} else {
// If the index doesn't fit in one byte, use OP_CONSTANT_LONG.
try writeChunk(self, OpCode.op_constant_long.toU8(), line);
// Write the index as three separate bytes.
try writeChunk(self, @intCast((idx & 0xff)), line);
try writeChunk(self, @intCast((idx >> 8) & 0xff), line);
try writeChunk(self, @intCast((idx >> 16) & 0xff), line);
}
}
pub fn freeChunk(self: *Chunk) void {
self.code.deinit();
self.constants.deinit();
self.lines.deinit();
}
pub fn deinit(self: *Chunk) void {
self.code.deinit();
self.constants.deinit();
self.lines.deinit();
}
};