From 98e7f950cf4cbb09f360237b0f2edaa4592bbdd8 Mon Sep 17 00:00:00 2001 From: nazrin Date: Sat, 7 Jun 2025 23:57:45 +0000 Subject: [PATCH] Change `stack.push(v)` to `stack ~= v` --- src/clox/container/stack.d | 2 +- src/clox/vm.d | 24 +++++++++++------------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/clox/container/stack.d b/src/clox/container/stack.d index 7fb88aa..6ac7551 100644 --- a/src/clox/container/stack.d +++ b/src/clox/container/stack.d @@ -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; } diff --git a/src/clox/vm.d b/src/clox/vm.d index 6290e2d..bb08094 100644 --- a/src/clox/vm.d +++ b/src/clox/vm.d @@ -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();