lox-d/src/clox/chunk.d
2025-06-07 23:52:16 +00:00

75 lines
1.8 KiB
D

module clox.chunk;
import std.algorithm.searching;
import clox.memory;
import clox.value;
import clox.container.dynarray;
struct OpColour{
string r, g, b;
}
enum OpCode : ubyte{
@(OpColour("200", "200", "100")) Constant,
@(OpColour("255", "200", "100")) Nil,
@(OpColour("255", "200", "100")) True,
@(OpColour("255", "200", "100")) False,
@(OpColour("000", "200", "100")) Pop,
@(OpColour("060", "200", "150")) GetLocal,
@(OpColour("000", "200", "150")) GetGlobal,
@(OpColour("000", "200", "150")) DefineGlobal,
@(OpColour("060", "200", "150")) SetLocal,
@(OpColour("000", "200", "150")) SetGlobal,
@(OpColour("255", "100", "100")) Equal,
@(OpColour("255", "100", "100")) Greater,
@(OpColour("255", "100", "100")) Less,
@(OpColour("255", "100", "100")) NotEqual,
@(OpColour("255", "100", "100")) GreaterEqual,
@(OpColour("255", "100", "100")) LessEqual,
@(OpColour("100", "100", "255")) Add,
@(OpColour("100", "100", "255")) Subtract,
@(OpColour("100", "100", "255")) Multiply,
@(OpColour("100", "100", "255")) Divide,
@(OpColour("200", "100", "100")) Not,
@(OpColour("100", "100", "200")) Negate,
@(OpColour("000", "200", "100")) Print,
@(OpColour("000", "200", "100")) Return,
}
struct Chunk{
DynArray!ubyte code;
DynArray!uint lines;
DynArray!Value constants;
void initialise(){
code.initialise();
lines.initialise();
constants.initialise();
}
void write(in ubyte[] bytes, uint line = 0){
foreach(b; bytes)
write(b, line);
}
void write(ubyte b, uint line = 0){
code ~= b;
lines ~= line;
}
size_t addConstant(Value value){
long index = constants[].countUntil(value);
if(index >= 0)
return index;
constants ~= value;
return constants.count - 1;
}
void free(){
code.free();
lines.free();
constants.free();
initialise();
}
}