Compiling Expressions 17
This commit is contained in:
parent
8fb449825d
commit
41404633da
18 changed files with 546 additions and 64 deletions
51
src/clox/emitter.d
Normal file
51
src/clox/emitter.d
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
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;
|
||||
Chunk* currentChunk(){
|
||||
return chunk;
|
||||
}
|
||||
void emit(Args...)(Args args){
|
||||
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){
|
||||
emit(OpCode.Constant, makeConstant(value));
|
||||
}
|
||||
void emitReturn(){
|
||||
emit(OpCode.Return);
|
||||
}
|
||||
void endCompiler(){
|
||||
emitReturn();
|
||||
debug(printCode){
|
||||
if(!compiler.parser.hadError)
|
||||
disassembleChunk(currentChunk());
|
||||
}
|
||||
}
|
||||
uint makeConstant(Value value){
|
||||
uint constant = chunk.addConstant(value);
|
||||
return constant;
|
||||
}
|
||||
void setLine(uint l){
|
||||
this.line = l;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue