Compiling Expressions 17

This commit is contained in:
nazrin 2025-06-04 19:49:53 +00:00
parent 8fb449825d
commit 41404633da
18 changed files with 546 additions and 64 deletions

View file

@ -2,8 +2,11 @@ module clox.chunk;
import std.container.array;
import std.stdio;
import std.algorithm.searching;
import clox.value;
import clox.container.rle;
import clox.container.int24;
enum OpCode : ubyte{
Constant,
@ -14,15 +17,23 @@ enum OpCode : ubyte{
struct Chunk{
Array!ubyte code;
Array!uint lines;
Rle!(Uint24, ubyte) lines;
Array!Value constants;
ubyte addConstant(in Value value) @nogc nothrow {
uint addConstant(in Value value) @nogc nothrow {
long index = constants[].countUntil(value);
if(index >= 0)
return cast(uint)index;
constants ~= value;
return cast(ubyte)((constants.length) - 1);
return cast(uint)((constants.length) - 1);
}
void write(ubyte b, uint line = 0) @nogc nothrow {
ubyte[1] data = [ b ];
write(data, line);
}
void write(ubyte[] b, uint line = 0) @nogc nothrow {
code ~= b;
lines ~= line;
foreach(i; 0 .. b.length)
lines ~= Uint24(line); // TODO could be done without a loop
}
}