Change stack.push(v) to stack ~= v

This commit is contained in:
nazrin 2025-06-07 23:57:45 +00:00
parent 72a41e81e6
commit 98e7f950cf
2 changed files with 12 additions and 14 deletions

View file

@ -9,7 +9,7 @@ struct Stack(T){
data.initialise();
top = data.ptr;
}
void push(T value){
void opOpAssign(string op: "~")(T value){
data ~= value;
top = data.ptr + data.count;
}

View file

@ -63,9 +63,7 @@ struct VM{
ip += vi.len;
return vi.i;
}
Value readConstant(){
return chunk.constants[readVarUint()];
}
Value readConstant() => chunk.constants[readVarUint()];
Obj.String* readString() => readConstant().asObj.asString;
Value peek(int distance = 0) => stack.top[-1 - distance];
bool checkBinaryType(alias type)() => peek(0).isType(type) && peek(1).isType(type);
@ -77,7 +75,7 @@ struct VM{
}
auto b = stack.pop().as!pre;
auto a = stack.pop().as!pre;
stack.push(Value.from(mixin("a", op, "b")));
stack ~= Value.from(mixin("a", op, "b"));
return 0;
}
while(true){
@ -95,16 +93,16 @@ struct VM{
opSwitch: final switch(instruction = cast(OpCode)readByte()){
case OpCode.Constant:
Value constant = readConstant();
stack.push(constant);
stack ~= constant;
break;
case OpCode.Nil: stack.push(Value.nil); break;
case OpCode.True: stack.push(Value.from(true)); break;
case OpCode.False: stack.push(Value.from(false)); break;
case OpCode.Nil: stack ~= Value.nil; break;
case OpCode.True: stack ~= Value.from(true); break;
case OpCode.False: stack ~= Value.from(false); break;
case OpCode.Pop: stack.pop(); break;
case OpCode.GetLocal:
long slot = readVarUint();
stack.push(stack[slot]);
stack ~= stack[slot];
break;
case OpCode.SetLocal:
long slot = readVarUint();
@ -117,7 +115,7 @@ struct VM{
runtimeError("Undefined variable '%s'.", name.chars.ptr);
return InterpretResult.RuntimeError;
}
stack.push(value);
stack ~= value;
break;
case OpCode.DefineGlobal:
Obj.String* name = readString();
@ -152,7 +150,7 @@ struct VM{
Obj.String* b = stack.pop().asObj.asString;
Obj.String* a = stack.pop().asObj.asString;
Obj.String* result = Obj.String.concat(a, b);
stack.push(Value.from(result));
stack ~= Value.from(result);
break opSwitch;
}
}
@ -162,14 +160,14 @@ struct VM{
}
case OpCode.Not:
stack.push(Value.from(stack.pop().isFalsey));
stack ~= Value.from(stack.pop().isFalsey);
break;
case OpCode.Negate:
if(!peek(0).isType(Value.Type.Number)){
runtimeError("Operand must be a number.");
return InterpretResult.RuntimeError;
}
stack.push(Value.from(-stack.pop().asNumber));
stack ~= Value.from(-stack.pop().asNumber);
break;
case OpCode.Print:
stack.pop().print();