aboutsummaryrefslogtreecommitdiff
path: root/chunk.d
diff options
context:
space:
mode:
Diffstat (limited to 'chunk.d')
-rw-r--r--chunk.d64
1 files changed, 64 insertions, 0 deletions
diff --git a/chunk.d b/chunk.d
new file mode 100644
index 0000000..5233a87
--- /dev/null
+++ b/chunk.d
@@ -0,0 +1,64 @@
+import std.stdio;
+import std.string;
+import std.conv;
+
+import parser;
+
+enum ObjType {
+ FUNCTION,
+}
+
+abstract class Obj {
+ ObjType type;
+}
+
+class Function : Obj {
+ Chunk chunk;
+ string name;
+
+ this() {
+ this.type = ObjType.FUNCTION;
+ this.chunk = new Chunk();
+ this.name = "";
+ }
+
+ override string toString() {
+ if (name == "") {
+ return "<neb>";
+ } else {
+ return name;
+ }
+ }
+}
+
+enum OpCode {
+ OP_ADD,
+ OP_RETURN,
+ OP_CONSTANT,
+ OP_POP,
+}
+
+class Chunk {
+ int count = 0;
+ ubyte[] code;
+ int[] lines;
+ Value[] constants;
+
+ //int writeOp(OpCode opCode, int line) {
+ int writeOp(ubyte opCode, int line) {
+ this.code ~= opCode;
+ this.lines ~= line;
+ this.count++;
+ assert(this.code.length == count);
+ assert(this.lines.length == count);
+ return count;
+ }
+
+ int addConstant(Value value) {
+ this.constants ~= value;
+ return to!int(this.constants.length - 1);
+ }
+
+}
+
+