module clox.emitter; import clox.compiler; import clox.chunk; import clox.value; import clox.util; import clox.dbg; import clox.container.varint; struct Emitter{ Compiler* compiler; Chunk* chunk; private uint line = 1; Chunk* currentChunk() @nogc nothrow { return chunk; } void emit(Args...)(Args args) @nogc nothrow { static foreach(v; args){{ static if(is(typeof(v) == OpCode)){ auto bytes = v; } else static if(is(typeof(v) == uint)){ auto bytes = VarUint(v).bytes; } else { static assert(0); } currentChunk.write(bytes, line); }} } void emitConstant(Value value) @nogc nothrow { emit(OpCode.Constant, makeConstant(value)); } void emitReturn() @nogc nothrow { emit(OpCode.Return); } void endCompiler(){ emitReturn(); debug(printCode){ if(!compiler.parser.hadError) disassembleChunk(currentChunk()); } } uint makeConstant(Value value) @nogc nothrow { uint constant = chunk.addConstant(value); return constant; } void setLine(uint l) @nogc nothrow { this.line = l; } }