op: return value StackOp from Push and make Pop use a value receiver

To match Record, we'd like Push to return a value. To do that and
support the one-line

	defer op.Push(ops).Pop()

Pop needs to use a value receiver as well. Drop the active field
and make it so. The field was only a sanity check, a check which is
already done by Ops.stackStack, albeit with a less specific panic.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-06-02 10:52:46 +02:00
parent ae8a377cda
commit 3e8c502550
+7 -19
View File
@@ -100,7 +100,6 @@ type Ops struct {
type StackOp struct { type StackOp struct {
id stackID id stackID
macroID int macroID int
active bool
ops *Ops ops *Ops
} }
@@ -158,34 +157,23 @@ func (c CallOp) Add(o *Ops) {
} }
// Push (save) the current operations state. // Push (save) the current operations state.
func Push(o *Ops) *StackOp { func Push(o *Ops) StackOp {
var s StackOp s := StackOp{
s.push(o) ops: o,
return &s id: o.stackStack.push(),
} macroID: o.macroStack.currentID,
func (s *StackOp) push(o *Ops) {
if s.active {
panic("unbalanced push")
} }
s.active = true
s.ops = o
s.id = o.stackStack.push()
s.macroID = o.macroStack.currentID
data := o.Write(opconst.TypePushLen) data := o.Write(opconst.TypePushLen)
data[0] = byte(opconst.TypePush) data[0] = byte(opconst.TypePush)
return s
} }
// Pop (restore) a previously Pushed operations state. // Pop (restore) a previously Pushed operations state.
func (s *StackOp) Pop() { func (s StackOp) Pop() {
if !s.active {
panic("unbalanced pop")
}
if s.ops.macroStack.currentID != s.macroID { if s.ops.macroStack.currentID != s.macroID {
panic("pop in a different macro than push") panic("pop in a different macro than push")
} }
s.ops.stackStack.pop(s.id) s.ops.stackStack.pop(s.id)
s.active = false
data := s.ops.Write(opconst.TypePopLen) data := s.ops.Write(opconst.TypePopLen)
data[0] = byte(opconst.TypePop) data[0] = byte(opconst.TypePop)
} }