diff options
Diffstat (limited to 'vm1')
| -rw-r--r-- | vm1/vm.go | 10 | ||||
| -rw-r--r-- | vm1/vm_test.go | 4 |
2 files changed, 10 insertions, 4 deletions
@@ -6,7 +6,7 @@ import ( "strconv" // for tracing only ) -const debug = false +const debug = true // Byte-code instruction set. const ( @@ -155,7 +155,7 @@ func (m *Machine) Run() (err error) { } } -func (m *Machine) PushCode(code [][]int64) (p int) { +func (m *Machine) PushCode(code ...[]int64) (p int) { p = len(m.code) m.code = append(m.code, code...) return p @@ -165,6 +165,12 @@ func (m *Machine) SetIP(ip int) { m.ip = ip } func (m *Machine) Push(v ...any) (l int) { l = len(m.mem); m.mem = append(m.mem, v...); return } func (m *Machine) Pop() (v any) { l := len(m.mem) - 1; v = m.mem[l]; m.mem = m.mem[:l]; return } +func (m *Machine) PopExit() { + if l := len(m.code); l > 0 && m.code[l-1][1] == Exit { + m.code = m.code[:l-1] + } +} + // Disassemble returns the code as a readable string. func Disassemble(code [][]int64) (asm string) { for _, op := range code { diff --git a/vm1/vm_test.go b/vm1/vm_test.go index 257ac44..272a321 100644 --- a/vm1/vm_test.go +++ b/vm1/vm_test.go @@ -13,7 +13,7 @@ func TestVM(t *testing.T) { for _, v := range test.sym { m.Push(v) } - m.PushCode(test.code) + m.PushCode(test.code...) if err := m.Run(); err != nil { t.Errorf("run error: %v", err) } @@ -33,7 +33,7 @@ func BenchmarkVM(b *testing.B) { for i := 0; i < b.N; i++ { b.StopTimer() m := &Machine{} - m.PushCode(test.code) + m.PushCode(test.code...) b.StartTimer() if err := m.Run(); err != nil { |
