31 lines
660 B
D
31 lines
660 B
D
module clox.emitter;
|
|
|
|
import core.stdc.stdio;
|
|
|
|
import clox.chunk;
|
|
import clox.compiler;
|
|
import clox.value;
|
|
|
|
struct Emitter{
|
|
Compiler* compiler;
|
|
private uint line = 1;
|
|
uint setLine(uint line) => this.line = line;
|
|
void initialise(Compiler* compiler){
|
|
this.compiler = compiler;
|
|
}
|
|
void emit(ubyte[] bytes...){
|
|
foreach(b; bytes)
|
|
compiler.currentChunk.write(b, compiler.parser.previous.line);
|
|
}
|
|
void emitReturn(){
|
|
emit(OpCode.Return);
|
|
}
|
|
void emitConstant(Value value){
|
|
emit(OpCode.Constant, cast(ubyte)makeConstant(value));
|
|
}
|
|
uint makeConstant(Value value){
|
|
uint constant = compiler.currentChunk.addConstant(value);
|
|
return constant;
|
|
}
|
|
}
|
|
|