about summary refs log tree commit diff
path: root/pkg/lang/vm/mem/mem.go
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang/vm/mem/mem.go')
-rw-r--r--pkg/lang/vm/mem/mem.go17
1 files changed, 15 insertions, 2 deletions
diff --git a/pkg/lang/vm/mem/mem.go b/pkg/lang/vm/mem/mem.go
index 4335347..4cb6fc4 100644
--- a/pkg/lang/vm/mem/mem.go
+++ b/pkg/lang/vm/mem/mem.go
@@ -6,8 +6,13 @@ type Mem struct {
 }
 
 func New() Mem {
+	cells := make([]cell, 1)
+
+	// Reserve NullPtr
+	cells[NullPtr].kind = CellKindForbidden
+
 	return Mem{
-		cells: make([]cell, 0),
+		cells: cells,
 		free:  make([]int, 0),
 	}
 }
@@ -25,7 +30,7 @@ func (m *Mem) Allocate(kind CellKind) Ptr {
 		return Ptr(idx)
 	} else {
 		idx := len(m.cells)
-		m.cells = append(m.cells, cell{kind: kind})
+		m.cells = append(m.cells, cell{kind: kind, refs: 1})
 		return Ptr(idx)
 	}
 
@@ -47,6 +52,14 @@ func (m *Mem) Get(ptr Ptr) any {
 	return m.cells[ptr].data
 }
 
+func (m *Mem) Is(ptr Ptr, kind CellKind) bool {
+	if ptr >= Ptr(len(m.cells)) {
+		panic("out of bounds")
+	}
+
+	return m.cells[ptr].kind == kind
+}
+
 func (m *Mem) Retain(ptr Ptr) {
 	if ptr >= Ptr(len(m.cells)) {
 		panic("out of bounds")