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_LESS, OP_NEGATE, OP_RETURN, OP_CONSTANT, OP_DEFINE_GLOBAL, OP_GET_GLOBAL, OP_POP, OP_SUBTRACT, OP_NIL, } 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); } }