99 lines
2.5 KiB
D
99 lines
2.5 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 OpConst;
|
|
enum OpStack;
|
|
enum OpJump;
|
|
enum OpCall;
|
|
|
|
enum OpCode : ubyte{
|
|
@OpConst @(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,
|
|
@OpStack @(OpColour("060", "210", "150")) GetLocal,
|
|
@OpStack @(OpColour("060", "200", "150")) SetLocal,
|
|
@OpStack @(OpColour("080", "210", "150")) GetUpvalue,
|
|
@OpStack @(OpColour("080", "200", "150")) SetUpvalue,
|
|
|
|
@OpConst @(OpColour("000", "200", "150")) GetGlobal,
|
|
@OpConst @(OpColour("000", "200", "150")) DefineGlobal,
|
|
@OpConst @(OpColour("000", "200", "150")) SetGlobal,
|
|
|
|
@OpJump @(OpColour("000", "255", "000")) Jump,
|
|
@OpJump @(OpColour("000", "200", "000")) JumpIfFalse,
|
|
@OpJump @(OpColour("010", "255", "000")) Loop,
|
|
|
|
@OpConst @(OpColour("060", "010", "150")) GetProp,
|
|
@OpConst @(OpColour("060", "000", "150")) SetProp,
|
|
|
|
@(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,
|
|
|
|
@OpCall @(OpColour("250", "200", "250")) Call,
|
|
@(OpColour("250", "200", "250")) Invoke,
|
|
@(OpColour("250", "200", "250")) Closure,
|
|
@(OpColour("250", "200", "050")) CloseUpvalue,
|
|
@(OpColour("250", "190", "200")) Return,
|
|
|
|
@OpConst @(OpColour("050", "190", "200")) Class,
|
|
@OpConst @(OpColour("100", "190", "200")) Method,
|
|
}
|
|
|
|
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;
|
|
}
|
|
int addConstant(Value value){
|
|
int index = cast(int)constants[].countUntil(value);
|
|
if(index >= 0)
|
|
return index;
|
|
constants ~= value;
|
|
return cast(int)constants.count - 1;
|
|
}
|
|
void free(){
|
|
code.free();
|
|
lines.free();
|
|
constants.free();
|
|
initialise();
|
|
}
|
|
}
|
|
|