Skip to content

Instantly share code, notes, and snippets.

@nikki93
Created September 8, 2020 23:03
Show Gist options
  • Save nikki93/da8b55a30a2da2d2eba2378f463ef4ee to your computer and use it in GitHub Desktop.
Save nikki93/da8b55a30a2da2d2eba2378f463ef4ee to your computer and use it in GitHub Desktop.
diff --git a/cclox.cc b/cclox.cc
index 0e9d3c5..f736492 100644
--- a/cclox.cc
+++ b/cclox.cc
@@ -59,7 +59,7 @@ struct Value {
}
- auto copy() -> Value {
+ [[nodiscard]] auto copy() const -> Value {
auto result = Value(Nil());
result.data = data;
return result;
@@ -157,12 +157,6 @@ enum class Op : uint8_t {
struct Chunk {
- std::vector<uint8_t> code;
- std::vector<int> lines;
- std::vector<Value> constants;
- std::vector<std::unique_ptr<std::string>> strings;
-
-
auto write(uint8_t byte, int line) -> void {
code.push_back(byte);
lines.push_back(line);
@@ -182,6 +176,34 @@ struct Chunk {
constants.emplace_back(std::forward<Args>(args)...);
return int(constants.size() - 1);
}
+
+
+ [[nodiscard]] auto size() const -> int {
+ return int(code.size());
+ }
+
+ [[nodiscard]] auto start() const -> const uint8_t * {
+ return code.data();
+ }
+
+ [[nodiscard]] auto read(int offset) const -> uint8_t {
+ return code[offset];
+ }
+
+ [[nodiscard]] auto line(int offset) const -> int {
+ return lines[offset];
+ }
+
+ [[nodiscard]] auto constant(int id) const -> const Value & {
+ return constants[id];
+ }
+
+
+private:
+ std::vector<uint8_t> code;
+ std::vector<int> lines;
+ std::vector<Value> constants;
+ std::vector<std::unique_ptr<std::string>> strings;
};
@@ -198,13 +220,13 @@ struct Disassembly {
auto instruction(int offset) -> int {
std::printf("%04d ", offset);
- if (offset > 0 && chunk.lines[offset] == chunk.lines[offset - 1]) {
+ if (offset > 0 && chunk.line(offset) == chunk.line(offset - 1)) {
std::printf(" | ");
} else {
- std::printf("%4d ", chunk.lines[offset]);
+ std::printf("%4d ", chunk.line(offset));
}
- switch (auto opCode = Op(chunk.code[offset])) {
+ switch (auto opCode = Op(chunk.read(offset))) {
case Op::Constant:
return constant("OP_CONSTANT", offset);
case Op::Nil:
@@ -242,7 +264,7 @@ struct Disassembly {
auto all(std::string_view name) {
std::printf("== %.*s ==\n", int(name.size()), name.data());
- for (auto offset = 0; offset < chunk.code.size();) {
+ for (auto offset = 0; offset < chunk.size();) {
offset = instruction(offset);
}
}
@@ -258,9 +280,9 @@ private:
}
auto constant(std::string_view name, int offset) -> int {
- auto constant = chunk.code[offset + 1];
+ auto constant = chunk.read(offset + 1);
std::printf("%-16.*s %4d '", int(name.size()), name.data(), constant);
- chunk.constants[constant].print();
+ chunk.constant(constant).print();
std::printf("'\n");
return offset + 2;
}
@@ -858,14 +880,14 @@ struct VM {
};
- auto ip = chunk.code.data();
+ auto ip = chunk.start();
const auto readByte = [&]() -> uint8_t {
return *ip++;
};
const auto readConstant = [&]() -> Value {
- return chunk.constants[readByte()].copy();
+ return chunk.constant(readByte()).copy();
};
@@ -877,8 +899,8 @@ struct VM {
fprintf(stderr, fmt, args...);
#pragma clang diagnostic pop
- auto instruction = ip - chunk.code.data() - 1;
- auto line = chunk.lines[instruction];
+ auto offset = ip - chunk.start() - 1;
+ auto line = chunk.line(offset);
fprintf(stderr, "\n[line %d] in script\n", line);
clear();
@@ -908,7 +930,7 @@ struct VM {
std::printf(" ]");
}
std::printf("\n");
- Disassembly(chunk).instruction(int(ip - chunk.code.data()));
+ Disassembly(chunk).instruction(int(ip - chunk.start()));
}
switch (auto instruction = Op(readByte())) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment