Change stack.push(v) to stack ~= v
This commit is contained in:
parent
72a41e81e6
commit
98e7f950cf
2 changed files with 12 additions and 14 deletions
|
|
@ -9,7 +9,7 @@ struct Stack(T){
|
||||||
data.initialise();
|
data.initialise();
|
||||||
top = data.ptr;
|
top = data.ptr;
|
||||||
}
|
}
|
||||||
void push(T value){
|
void opOpAssign(string op: "~")(T value){
|
||||||
data ~= value;
|
data ~= value;
|
||||||
top = data.ptr + data.count;
|
top = data.ptr + data.count;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -63,9 +63,7 @@ struct VM{
|
||||||
ip += vi.len;
|
ip += vi.len;
|
||||||
return vi.i;
|
return vi.i;
|
||||||
}
|
}
|
||||||
Value readConstant(){
|
Value readConstant() => chunk.constants[readVarUint()];
|
||||||
return chunk.constants[readVarUint()];
|
|
||||||
}
|
|
||||||
Obj.String* readString() => readConstant().asObj.asString;
|
Obj.String* readString() => readConstant().asObj.asString;
|
||||||
Value peek(int distance = 0) => stack.top[-1 - distance];
|
Value peek(int distance = 0) => stack.top[-1 - distance];
|
||||||
bool checkBinaryType(alias type)() => peek(0).isType(type) && peek(1).isType(type);
|
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 b = stack.pop().as!pre;
|
||||||
auto a = 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;
|
return 0;
|
||||||
}
|
}
|
||||||
while(true){
|
while(true){
|
||||||
|
|
@ -95,16 +93,16 @@ struct VM{
|
||||||
opSwitch: final switch(instruction = cast(OpCode)readByte()){
|
opSwitch: final switch(instruction = cast(OpCode)readByte()){
|
||||||
case OpCode.Constant:
|
case OpCode.Constant:
|
||||||
Value constant = readConstant();
|
Value constant = readConstant();
|
||||||
stack.push(constant);
|
stack ~= constant;
|
||||||
break;
|
break;
|
||||||
case OpCode.Nil: stack.push(Value.nil); break;
|
case OpCode.Nil: stack ~= Value.nil; break;
|
||||||
case OpCode.True: stack.push(Value.from(true)); break;
|
case OpCode.True: stack ~= Value.from(true); break;
|
||||||
case OpCode.False: stack.push(Value.from(false)); break;
|
case OpCode.False: stack ~= Value.from(false); break;
|
||||||
case OpCode.Pop: stack.pop(); break;
|
case OpCode.Pop: stack.pop(); break;
|
||||||
|
|
||||||
case OpCode.GetLocal:
|
case OpCode.GetLocal:
|
||||||
long slot = readVarUint();
|
long slot = readVarUint();
|
||||||
stack.push(stack[slot]);
|
stack ~= stack[slot];
|
||||||
break;
|
break;
|
||||||
case OpCode.SetLocal:
|
case OpCode.SetLocal:
|
||||||
long slot = readVarUint();
|
long slot = readVarUint();
|
||||||
|
|
@ -117,7 +115,7 @@ struct VM{
|
||||||
runtimeError("Undefined variable '%s'.", name.chars.ptr);
|
runtimeError("Undefined variable '%s'.", name.chars.ptr);
|
||||||
return InterpretResult.RuntimeError;
|
return InterpretResult.RuntimeError;
|
||||||
}
|
}
|
||||||
stack.push(value);
|
stack ~= value;
|
||||||
break;
|
break;
|
||||||
case OpCode.DefineGlobal:
|
case OpCode.DefineGlobal:
|
||||||
Obj.String* name = readString();
|
Obj.String* name = readString();
|
||||||
|
|
@ -152,7 +150,7 @@ struct VM{
|
||||||
Obj.String* b = stack.pop().asObj.asString;
|
Obj.String* b = stack.pop().asObj.asString;
|
||||||
Obj.String* a = stack.pop().asObj.asString;
|
Obj.String* a = stack.pop().asObj.asString;
|
||||||
Obj.String* result = Obj.String.concat(a, b);
|
Obj.String* result = Obj.String.concat(a, b);
|
||||||
stack.push(Value.from(result));
|
stack ~= Value.from(result);
|
||||||
break opSwitch;
|
break opSwitch;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -162,14 +160,14 @@ struct VM{
|
||||||
}
|
}
|
||||||
|
|
||||||
case OpCode.Not:
|
case OpCode.Not:
|
||||||
stack.push(Value.from(stack.pop().isFalsey));
|
stack ~= Value.from(stack.pop().isFalsey);
|
||||||
break;
|
break;
|
||||||
case OpCode.Negate:
|
case OpCode.Negate:
|
||||||
if(!peek(0).isType(Value.Type.Number)){
|
if(!peek(0).isType(Value.Type.Number)){
|
||||||
runtimeError("Operand must be a number.");
|
runtimeError("Operand must be a number.");
|
||||||
return InterpretResult.RuntimeError;
|
return InterpretResult.RuntimeError;
|
||||||
}
|
}
|
||||||
stack.push(Value.from(-stack.pop().asNumber));
|
stack ~= Value.from(-stack.pop().asNumber);
|
||||||
break;
|
break;
|
||||||
case OpCode.Print:
|
case OpCode.Print:
|
||||||
stack.pop().print();
|
stack.pop().print();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue