about summary refs log tree commit diff
path: root/pkg/lang/vm/mem
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang/vm/mem')
-rw-r--r--pkg/lang/vm/mem/cell.go6
-rw-r--r--pkg/lang/vm/mem/mem.go17
-rw-r--r--pkg/lang/vm/mem/ptr.go8
3 files changed, 28 insertions, 3 deletions
diff --git a/pkg/lang/vm/mem/cell.go b/pkg/lang/vm/mem/cell.go
index 044a45c..3f052d5 100644
--- a/pkg/lang/vm/mem/cell.go
+++ b/pkg/lang/vm/mem/cell.go
@@ -6,7 +6,11 @@ const (
 	CellKindEmpty CellKind = iota
 	CellKindString
 	CellKindArray
-	CellKindEscaped
+
+	CellKindEnv
+	CellKindOutlet
+
+	CellKindForbidden
 )
 
 type cell struct {
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")
diff --git a/pkg/lang/vm/mem/ptr.go b/pkg/lang/vm/mem/ptr.go
index 5e3bf3d..f6e3e64 100644
--- a/pkg/lang/vm/mem/ptr.go
+++ b/pkg/lang/vm/mem/ptr.go
@@ -4,7 +4,15 @@ import "strconv"
 
 type Ptr uint64
 
+const (
+	NullPtr Ptr = 0
+)
+
 func (p Ptr) String() string {
 	val := strconv.FormatUint(uint64(p), 10)
 	return "@" + val
 }
+
+func (p Ptr) IsNull() bool {
+	return p == NullPtr
+}