about summary refs log tree commit diff
path: root/pkg/lang/vm/value/cells.go
blob: 19ecfde29f9e09782454bc375646d20f23f072b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package value

import (
	"jinx/pkg/lang/vm/code"
	"jinx/pkg/lang/vm/mem"
)

type ArrayCell []Value

func (a ArrayCell) DropCell(m mem.Mem) {
	for _, v := range a {
		v.Drop(m)
	}
}

func (a ArrayCell) MatchingCellKind() mem.CellKind {
	return mem.CellKindArray
}

func (a ArrayCell) Get() []Value {
	return a
}

type StringCell string

func (s StringCell) DropCell(m mem.Mem) {
}

func (s StringCell) MatchingCellKind() mem.CellKind {
	return mem.CellKindString
}

func (s StringCell) Get() string {
	return string(s)
}

type ObjectCell struct {
	Members map[string]Value
}

func (o ObjectCell) DropCell(m mem.Mem) {
	for _, v := range o.Members {
		v.Drop(m)
	}
}

func (o ObjectCell) MatchingCellKind() mem.CellKind {
	return mem.CellKindObject
}

func (o ObjectCell) Get() map[string]Value {
	return o.Members
}

type TypeCell Type

func (t TypeCell) DropCell(m mem.Mem) {
	typ := t.Get()
	for _, f := range typ.Methods {
		// Wrap data in a Value to drop it.
		val := NewFunction(code.Pos{}, 0).WithData(f)
		val.Drop(m)
	}

	for _, v := range typ.Statics {
		v.Drop(m)
	}
}

func (t TypeCell) MatchingCellKind() mem.CellKind {
	return mem.CellKindType
}

func (t TypeCell) Get() Type {
	return Type(t)
}

type OutletCell Value

func (o OutletCell) DropCell(m mem.Mem) {
	Value(o).Drop(m)
}

func (o OutletCell) MatchingCellKind() mem.CellKind {
	return mem.CellKindOutlet
}

func (o OutletCell) Get() Value {
	return Value(o)
}

type EnvCell Env

func (e EnvCell) DropCell(m mem.Mem) {
	for _, v := range e.references {
		m.Release(v.outlet)
	}
}

func (e EnvCell) MatchingCellKind() mem.CellKind {
	return mem.CellKindEnv
}

func (e EnvCell) Get() Env {
	return Env(e)
}

type GlobalCell Value

func (g GlobalCell) DropCell(m mem.Mem) {
	panic("global cell cannot be dropped")
}

func (g GlobalCell) MatchingCellKind() mem.CellKind {
	return mem.CellKindGlobal
}

func (g GlobalCell) Get() Value {
	return Value(g)
}