op: relax Push/Pop to Save/Restore semantics

A previous change implemented save/restore semantics where a saved
state can be restored anywhere, not just in a stack-like manner.

This change similarly relaxes the exported Push/Pop operations; the next
change will rename them accordingly.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-01-12 20:39:23 +01:00
parent f7902f299b
commit ae2c74ec13
+3 -5
View File
@@ -88,7 +88,6 @@ type Ops struct {
// StackOp. // StackOp.
nextStateID int nextStateID int
stackStack stack
macroStack stack macroStack stack
} }
@@ -96,7 +95,6 @@ type Ops struct {
// in a stack-like manner. // in a stack-like manner.
type StackOp struct { type StackOp struct {
id int id int
stackID stackID
macroID int macroID int
ops *Ops ops *Ops
} }
@@ -150,7 +148,6 @@ func Push(o *Ops) StackOp {
s := StackOp{ s := StackOp{
ops: o, ops: o,
id: o.nextStateID, id: o.nextStateID,
stackID: o.stackStack.push(),
macroID: o.macroStack.currentID, macroID: o.macroStack.currentID,
} }
bo := binary.LittleEndian bo := binary.LittleEndian
@@ -162,10 +159,12 @@ func Push(o *Ops) StackOp {
// Pop (restore) a previously Pushed operations state. // Pop (restore) a previously Pushed operations state.
func (s StackOp) Pop() { func (s StackOp) Pop() {
if s.id == 0 {
panic("zero-value op")
}
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.stackID)
bo := binary.LittleEndian bo := binary.LittleEndian
data := s.ops.Write(opconst.TypeLoadLen) data := s.ops.Write(opconst.TypeLoadLen)
data[0] = byte(opconst.TypeLoad) data[0] = byte(opconst.TypeLoad)
@@ -175,7 +174,6 @@ func (s StackOp) Pop() {
// Reset the Ops, preparing it for re-use. Reset invalidates // Reset the Ops, preparing it for re-use. Reset invalidates
// any recorded macros. // any recorded macros.
func (o *Ops) Reset() { func (o *Ops) Reset() {
o.stackStack = stack{}
o.macroStack = stack{} o.macroStack = stack{}
// Leave references to the GC. // Leave references to the GC.
for i := range o.refs { for i := range o.refs {