about summary refs log tree commit diff
path: root/pkg/lang
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang')
-rw-r--r--pkg/lang/vm/exec.go2
-rw-r--r--pkg/lang/vm/utils.go2
-rw-r--r--pkg/lang/vm/value/cells.go3
-rw-r--r--pkg/lang/vm/value/data.go37
-rw-r--r--pkg/lang/vm/value/value.go4
5 files changed, 44 insertions, 4 deletions
diff --git a/pkg/lang/vm/exec.go b/pkg/lang/vm/exec.go
index bca4e03..9464e7a 100644
--- a/pkg/lang/vm/exec.go
+++ b/pkg/lang/vm/exec.go
@@ -262,7 +262,7 @@ func (vm *VM) execGetMember(name string) error {
 				return err
 			}
 
-			method := value.NewFunction(code.Pos{}, 0).WithData(methodData.WithEnv(envPtr))
+			method := value.FromData(methodData.WithEnv(envPtr))
 			// method = method.Clone(vm.memory) will only be necessary when we support methods with environments.
 
 			vm.stack.Push(method)
diff --git a/pkg/lang/vm/utils.go b/pkg/lang/vm/utils.go
index 166d404..26703b6 100644
--- a/pkg/lang/vm/utils.go
+++ b/pkg/lang/vm/utils.go
@@ -31,7 +31,7 @@ func (vm *VM) popCallAndDrop() (code.Pos, error) {
 		return code.Pos{}, err
 	}
 
-	if err := value.NewFunction(code.Pos{}, 0).WithData(fn).Drop(vm.memory); err != nil {
+	if err := value.FromData(fn).Drop(vm.memory); err != nil {
 		return code.Pos{}, err
 	}
 
diff --git a/pkg/lang/vm/value/cells.go b/pkg/lang/vm/value/cells.go
index 646f3c1..e08f53f 100644
--- a/pkg/lang/vm/value/cells.go
+++ b/pkg/lang/vm/value/cells.go
@@ -2,7 +2,6 @@ package value
 
 import (
 	"fmt"
-	"jinx/pkg/lang/vm/code"
 	"jinx/pkg/lang/vm/mem"
 )
 
@@ -68,7 +67,7 @@ func (t TypeCell) DropCell(m mem.Mem) error {
 	typ := t.Get()
 	for _, f := range typ.Methods {
 		// Wrap data in a Value to drop it.
-		val := NewFunction(code.Pos{}, 0).WithData(f)
+		val := FromData(f)
 		if err := val.Drop(m); err != nil {
 			return err
 		}
diff --git a/pkg/lang/vm/value/data.go b/pkg/lang/vm/value/data.go
index 59e4742..fa18b6d 100644
--- a/pkg/lang/vm/value/data.go
+++ b/pkg/lang/vm/value/data.go
@@ -10,6 +10,7 @@ import (
 
 type Data interface {
 	String(mem.Mem) (string, error)
+	DataType() TypeKind
 }
 
 type IntData int64
@@ -22,6 +23,10 @@ func (i IntData) String(_ mem.Mem) (string, error) {
 	return strconv.FormatInt(int64(i), 10), nil
 }
 
+func (i IntData) DataType() TypeKind {
+	return IntType
+}
+
 type FloatData float64
 
 func (f FloatData) Get() float64 {
@@ -32,6 +37,10 @@ func (f FloatData) String(_ mem.Mem) (string, error) {
 	return strconv.FormatFloat(float64(f), 'f', -1, 64), nil
 }
 
+func (f FloatData) DataType() TypeKind {
+	return FloatType
+}
+
 type StringData struct {
 	data mem.Ptr
 }
@@ -52,6 +61,10 @@ func (s StringData) RawString(m mem.Mem) (string, error) {
 	}
 }
 
+func (s StringData) DataType() TypeKind {
+	return StringType
+}
+
 type BoolData bool
 
 func (b BoolData) Get() bool {
@@ -62,6 +75,10 @@ func (b BoolData) String(_ mem.Mem) (string, error) {
 	return strconv.FormatBool(bool(b)), nil
 }
 
+func (b BoolData) DataType() TypeKind {
+	return BoolType
+}
+
 type ArrayData struct {
 	data mem.Ptr
 }
@@ -155,12 +172,20 @@ func (a ArrayData) Pop(m mem.Mem) (Value, error) {
 	return popped, nil
 }
 
+func (a ArrayData) DataType() TypeKind {
+	return ArrayType
+}
+
 type NullData struct{}
 
 func (n NullData) String(_ mem.Mem) (string, error) {
 	return "null", nil
 }
 
+func (n NullData) DataType() TypeKind {
+	return NullType
+}
+
 type FunctionData struct {
 	pos    code.Pos
 	args   uint
@@ -202,6 +227,10 @@ func (f FunctionData) String(_ mem.Mem) (string, error) {
 	}
 }
 
+func (f FunctionData) DataType() TypeKind {
+	return FunctionType
+}
+
 type TypeRefData struct {
 	typeRef mem.Ptr
 }
@@ -240,6 +269,10 @@ func (t TypeRefData) AddMethod(m mem.Mem, name string, method FunctionData) erro
 	return nil
 }
 
+func (t TypeRefData) DataType() TypeKind {
+	return TypeRefType
+}
+
 type ObjectData struct {
 	t   mem.Ptr
 	obj mem.Ptr
@@ -260,3 +293,7 @@ func (o ObjectData) Type() mem.Ptr {
 func (o ObjectData) WithType(t mem.Ptr) ObjectData {
 	return ObjectData{t: t, obj: o.obj}
 }
+
+func (o ObjectData) DataType() TypeKind {
+	return ObjectType
+}
diff --git a/pkg/lang/vm/value/value.go b/pkg/lang/vm/value/value.go
index 21cf962..785703f 100644
--- a/pkg/lang/vm/value/value.go
+++ b/pkg/lang/vm/value/value.go
@@ -89,6 +89,10 @@ func NewType(m mem.Mem, name string) (Value, error) {
 	return Value{t: TypeRefType, d: TypeRefData{typeRef: ptr}}, nil
 }
 
+func FromData(d Data) Value {
+	return Value{t: d.DataType(), d: d}
+}
+
 func (v Value) Type() TypeKind {
 	return v.t
 }