Strings 19

This commit is contained in:
nazrin 2025-06-05 20:12:29 +00:00
parent 10c44eab2e
commit eec0a94aac
15 changed files with 358 additions and 55 deletions

View file

@ -2,9 +2,10 @@ module clox.chunk;
import std.container.array;
import std.stdio;
import std.algorithm.searching;
import std.algorithm;
import clox.value;
import clox.object;
import clox.container.rle;
import clox.container.int24;
@ -49,8 +50,21 @@ struct Chunk{
Array!ubyte code;
Rle!(Uint24, ubyte) lines;
Array!Value constants;
string name;
~this(){
stderr.writeln("Deallocing chunk ", name);
foreach(value; constants[].filter!isObj)
value.getObj.freeObject();
}
uint addConstant(in Value value) @nogc nothrow {
long index = constants[].countUntil(value);
long index;
switch(value.type){
case Value.Type.Str:
index = constants[].countUntil!(v => v.isStr && v.getStr.data == value.getStr.data);
break;
default:
index = constants[].countUntil(value);
}
if(index >= 0)
return cast(uint)index;
constants ~= value;
@ -58,7 +72,7 @@ struct Chunk{
}
void write(ubyte b, uint line = 0) @nogc nothrow {
ubyte[1] data = [ b ];
write(data, line);
this.write(data, line);
}
void write(ubyte[] b, uint line = 0) @nogc nothrow {
code ~= b;