aboutsummaryrefslogtreecommitdiff
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.go16
-rw-r--r--pkg/lang/vm/mem/mem.go68
-rw-r--r--pkg/lang/vm/mem/ptr.go10
3 files changed, 94 insertions, 0 deletions
diff --git a/pkg/lang/vm/mem/cell.go b/pkg/lang/vm/mem/cell.go
new file mode 100644
index 0000000..044a45c
--- /dev/null
+++ b/pkg/lang/vm/mem/cell.go
@@ -0,0 +1,16 @@
+package mem
+
+type CellKind int
+
+const (
+ CellKindEmpty CellKind = iota
+ CellKindString
+ CellKindArray
+ CellKindEscaped
+)
+
+type cell struct {
+ kind CellKind
+ refs int
+ data any
+}
diff --git a/pkg/lang/vm/mem/mem.go b/pkg/lang/vm/mem/mem.go
new file mode 100644
index 0000000..4335347
--- /dev/null
+++ b/pkg/lang/vm/mem/mem.go
@@ -0,0 +1,68 @@
+package mem
+
+type Mem struct {
+ cells []cell
+ free []int
+}
+
+func New() Mem {
+ return Mem{
+ cells: make([]cell, 0),
+ free: make([]int, 0),
+ }
+}
+
+func (m *Mem) Allocate(kind CellKind) Ptr {
+ if len(m.free) > 0 {
+ idx := m.free[len(m.free)-1]
+ m.free = m.free[:len(m.free)-1]
+
+ if m.cells[idx].kind != CellKindEmpty {
+ panic("invalid free cell")
+ }
+
+ m.cells[idx].kind = kind
+ return Ptr(idx)
+ } else {
+ idx := len(m.cells)
+ m.cells = append(m.cells, cell{kind: kind})
+ return Ptr(idx)
+ }
+
+}
+
+func (m *Mem) Set(ptr Ptr, v any) {
+ if ptr >= Ptr(len(m.cells)) {
+ panic("out of bounds")
+ }
+
+ m.cells[ptr].data = v
+}
+
+func (m *Mem) Get(ptr Ptr) any {
+ if ptr >= Ptr(len(m.cells)) {
+ panic("out of bounds")
+ }
+
+ return m.cells[ptr].data
+}
+
+func (m *Mem) Retain(ptr Ptr) {
+ if ptr >= Ptr(len(m.cells)) {
+ panic("out of bounds")
+ }
+
+ m.cells[ptr].refs++
+}
+
+func (m *Mem) Release(ptr Ptr) {
+ if ptr >= Ptr(len(m.cells)) {
+ panic("out of bounds")
+ }
+
+ m.cells[ptr].refs--
+ if m.cells[ptr].refs == 0 {
+ m.cells[ptr].kind = CellKindEmpty
+ m.free = append(m.free, int(ptr))
+ }
+}
diff --git a/pkg/lang/vm/mem/ptr.go b/pkg/lang/vm/mem/ptr.go
new file mode 100644
index 0000000..5e3bf3d
--- /dev/null
+++ b/pkg/lang/vm/mem/ptr.go
@@ -0,0 +1,10 @@
+package mem
+
+import "strconv"
+
+type Ptr uint64
+
+func (p Ptr) String() string {
+ val := strconv.FormatUint(uint64(p), 10)
+ return "@" + val
+}