A Virtual Machine 15

This commit is contained in:
nazrin 2025-06-03 19:04:25 +00:00
parent 7fa01b4fb9
commit aba643a88e
7 changed files with 139 additions and 28 deletions

25
src/clox/util.d Normal file
View file

@ -0,0 +1,25 @@
module clox.util;
struct Stack(T, size_t N){
T* top;
T[N] data;
invariant{ assert(top <= data.ptr + N); assert(top >= data.ptr); }
this(int _) @nogc nothrow{
top = data.ptr;
}
void push(T value) @nogc nothrow{
assert(top < data.ptr + N);
debug assert(*top is T.init);
*(top++) = value;
}
T pop() @nogc nothrow{
assert(top > data.ptr);
T t = *(--top);
debug *(top) = T.init;
return t;
}
const(T)[] live() @nogc nothrow const{
return data[0 .. (top - data.ptr)];
}
}