Hash Tables 20

This commit is contained in:
nazrin 2025-06-07 04:09:48 +00:00
parent 3b234814fa
commit a7b7348f61
20 changed files with 868 additions and 837 deletions

View file

@ -1,51 +1,31 @@
module clox.emitter;
import clox.compiler;
import clox.chunk;
import clox.value;
import clox.util;
import clox.dbg;
import core.stdc.stdio;
import clox.container.varint;
import clox.chunk;
import clox.compiler;
import clox.value;
struct Emitter{
Compiler* compiler;
Chunk* chunk;
private uint line = 1;
Chunk* currentChunk() @nogc nothrow {
return chunk;
uint setLine(uint line) => this.line = line;
void initialise(Compiler* compiler){
this.compiler = compiler;
}
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 emit(ubyte[] bytes...){
foreach(b; bytes)
compiler.currentChunk.write(b, compiler.parser.previous.line);
}
void emitConstant(Value value) @nogc nothrow {
emit(OpCode.Constant, makeConstant(value));
}
void emitReturn() @nogc nothrow {
void emitReturn(){
emit(OpCode.Return);
}
void endCompiler(){
emitReturn();
debug(printCode){
if(!compiler.parser.hadError)
disassembleChunk(currentChunk());
}
void emitConstant(Value value){
emit(OpCode.Constant, cast(ubyte)makeConstant(value));
}
uint makeConstant(Value value) @nogc nothrow {
uint constant = chunk.addConstant(value);
uint makeConstant(Value value){
uint constant = compiler.currentChunk.addConstant(value);
return constant;
}
void setLine(uint l) @nogc nothrow {
this.line = l;
}
}