summaryrefslogtreecommitdiff
path: root/vm1
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2023-08-29 18:22:13 +0200
committerMarc Vertes <mvertes@free.fr>2023-08-29 18:22:13 +0200
commit0f4bfe6e70263fbeb580014b62632f403b29b414 (patch)
treede750b60a6c0027b36accf9f0596c3e651309cda /vm1
parenta3ab9ef5be74cb54a87674aa48abb0c46f9c58f6 (diff)
gint: add an interactive REPL
Diffstat (limited to 'vm1')
-rw-r--r--vm1/vm.go10
-rw-r--r--vm1/vm_test.go4
2 files changed, 10 insertions, 4 deletions
diff --git a/vm1/vm.go b/vm1/vm.go
index f7113f2..c2f4882 100644
--- a/vm1/vm.go
+++ b/vm1/vm.go
@@ -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 {