import std.stdio; import std.string; import std.conv; import parser; enum ObjType { FUNCTION, SCRIPT, } abstract class Obj { ObjType type; } class Function : Obj { Chunk chunk; int arity; string name; ObjType type; this(ObjType type, string name = "") { this.type = type; this.chunk = new Chunk(); this.arity = 0; this.name = name; } override string toString() { if (type == ObjType.SCRIPT) { return ""; } else { return format("", name); } } } enum OpCode { OP_ADD, OP_LESS, OP_NEGATE, OP_RETURN, OP_CONSTANT, OP_DEF_GLOBAL, OP_GET_GLOBAL, OP_SET_GLOBAL, OP_DEF_LOCAL, OP_GET_LOCAL, OP_SET_LOCAL, OP_POP, OP_POPB, OP_POP_SCOPE, OP_SUBTRACT, OP_NIL, OP_JUMP, OP_JUMP_IF_FALSE, OP_JUMP_IF_TRUE, OP_CALL, } 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); } }