From 10c75f60c1f5fa27268ea9a850b63b777b087cbc Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Fri, 19 May 2023 17:55:36 -0400 Subject: initial commit --- chunk.d | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 chunk.d (limited to 'chunk.d') 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 ""; + } 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); + } + +} + + -- cgit v1.2.3