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 {
id stackID
macroID int
active bool
ops *Ops
}
@@ -158,34 +157,23 @@ func (c CallOp) Add(o *Ops) {
}
// Push (save) the current operations state.
func Push(o *Ops) *StackOp {
var s StackOp
s.push(o)
return &s
}
func (s *StackOp) push(o *Ops) {
if s.active {
panic("unbalanced push")
func Push(o *Ops) StackOp {
s := StackOp{
ops: o,
id: o.stackStack.push(),
macroID: o.macroStack.currentID,
}
s.active = true
s.ops = o
s.id = o.stackStack.push()
s.macroID = o.macroStack.currentID
data := o.Write(opconst.TypePushLen)
data[0] = byte(opconst.TypePush)
return s
}
// Pop (restore) a previously Pushed operations state.
func (s *StackOp) Pop() {
if !s.active {
panic("unbalanced pop")
}
func (s StackOp) Pop() {
if s.ops.macroStack.currentID != s.macroID {
panic("pop in a different macro than push")
}
s.ops.stackStack.pop(s.id)
s.active = false
data := s.ops.Write(opconst.TypePopLen)
data[0] = byte(opconst.TypePop)
}