A Virtual Machine 15
This commit is contained in:
parent
7fa01b4fb9
commit
aba643a88e
7 changed files with 139 additions and 28 deletions
25
src/clox/util.d
Normal file
25
src/clox/util.d
Normal 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)];
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue