|
@@ -2,49 +2,42 @@ package value
|
|
|
|
|
|
import "fmt"
|
|
import "fmt"
|
|
|
|
|
|
|
|
+type Members map[string]Value
|
|
|
|
+
|
|
type Value interface {
|
|
type Value interface {
|
|
- ID() int
|
|
|
|
Name() string
|
|
Name() string
|
|
// Proto/type of the value.
|
|
// Proto/type of the value.
|
|
Type() Type
|
|
Type() Type
|
|
String() string
|
|
String() string
|
|
- Parent() Value
|
|
|
|
- Members() []Value
|
|
|
|
|
|
+ Members() Members
|
|
}
|
|
}
|
|
|
|
|
|
// A type is also a value
|
|
// A type is also a value
|
|
type Type Value
|
|
type Type Value
|
|
|
|
|
|
-var TypeType Type = NewObject(-1, "Type", nil, nil)
|
|
|
|
-var ObjectType Type = NewObject(-2, "Object", TypeType, nil)
|
|
|
|
-var StringType Type = NewObject(-3, "String", ObjectType, nil)
|
|
|
|
-var IntType Type = NewObject(-4, "Int", ObjectType, nil)
|
|
|
|
|
|
+var TypeType Type = NewObject("Type", nil, nil)
|
|
|
|
+var ObjectType Type = NewObject("Object", TypeType, nil)
|
|
|
|
+var ErrorType Type = NewObject("Error", ObjectType, nil)
|
|
|
|
+var StringType Type = NewObject("String", ObjectType, nil)
|
|
|
|
+var IntType Type = NewObject("Int", ObjectType, nil)
|
|
|
|
+var ListType Type = NewObject("List", ObjectType, nil)
|
|
|
|
+var MethodType Type = NewObject("Method", ObjectType, nil)
|
|
|
|
|
|
type Object struct {
|
|
type Object struct {
|
|
- id int
|
|
|
|
name string
|
|
name string
|
|
typ Type
|
|
typ Type
|
|
- parent Value
|
|
|
|
- members []Value
|
|
|
|
|
|
+ members Members
|
|
}
|
|
}
|
|
|
|
|
|
func (o Object) String() string {
|
|
func (o Object) String() string {
|
|
return fmt.Sprintf("%s: %d", o.typ, o.name)
|
|
return fmt.Sprintf("%s: %d", o.typ, o.name)
|
|
}
|
|
}
|
|
|
|
|
|
-func (o Object) ID() int {
|
|
|
|
- return o.id
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
func (o Object) Name() string {
|
|
func (o Object) Name() string {
|
|
return o.name
|
|
return o.name
|
|
}
|
|
}
|
|
|
|
|
|
-func (o Object) Parent() Value {
|
|
|
|
- return o.parent
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-func (o Object) Members() []Value {
|
|
|
|
|
|
+func (o Object) Members() Members {
|
|
return o.members
|
|
return o.members
|
|
}
|
|
}
|
|
|
|
|
|
@@ -52,11 +45,22 @@ func (o Object) Type() Type {
|
|
return o.typ
|
|
return o.typ
|
|
}
|
|
}
|
|
|
|
|
|
-func NewObject(id int, name string, typ Type,
|
|
|
|
- parent Value, members ...Value) Object {
|
|
|
|
- return Object{
|
|
|
|
- id: id, name: name, typ: typ, parent: parent, members: members,
|
|
|
|
- }
|
|
|
|
|
|
+func NewObject(name string, typ Type, members Members) Object {
|
|
|
|
+ return Object{name: name, typ: typ, members: members}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type Error struct {
|
|
|
|
+ Object
|
|
|
|
+ Value error
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (s Error) String() string {
|
|
|
|
+ return fmt.Sprintf("%w", s.Value)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func NewError(name string, members Members, value error) Error {
|
|
|
|
+ return Error{Object: NewObject(name, ErrorType, members),
|
|
|
|
+ Value: value}
|
|
}
|
|
}
|
|
|
|
|
|
type String struct {
|
|
type String struct {
|
|
@@ -68,9 +72,8 @@ func (s String) String() string {
|
|
return s.Value
|
|
return s.Value
|
|
}
|
|
}
|
|
|
|
|
|
-func NewString(value string, id int, name string,
|
|
|
|
- parent Value, members ...Value) String {
|
|
|
|
- return String{Object: NewObject(id, name, StringType, parent, members...),
|
|
|
|
|
|
+func NewString(name string, members Members, value string) String {
|
|
|
|
+ return String{Object: NewObject(name, StringType, members),
|
|
Value: value}
|
|
Value: value}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -79,9 +82,8 @@ type Int struct {
|
|
Value int64
|
|
Value int64
|
|
}
|
|
}
|
|
|
|
|
|
-func NewInt(value int64, id int, name string,
|
|
|
|
- parent Value, members ...Value) Int {
|
|
|
|
- return Int{Object: NewObject(id, name, IntType, parent, members...),
|
|
|
|
|
|
+func NewInt(name string, members Members, value int64) Int {
|
|
|
|
+ return Int{Object: NewObject(name, IntType, members),
|
|
Value: value}
|
|
Value: value}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -91,15 +93,31 @@ func (i Int) String() string {
|
|
|
|
|
|
type List struct {
|
|
type List struct {
|
|
Object
|
|
Object
|
|
|
|
+ Value []Value
|
|
}
|
|
}
|
|
|
|
|
|
-func NewList(value int64, id int, name string,
|
|
|
|
- parent Value, members ...Value) List {
|
|
|
|
- return List{Object: NewObject(id, name, IntType, parent, members...)}
|
|
|
|
|
|
+func NewList(name string, members Members, value ...Value) List {
|
|
|
|
+ return List{Object: NewObject(name, ListType, members),
|
|
|
|
+ Value: value}
|
|
}
|
|
}
|
|
|
|
|
|
func (l List) String() string {
|
|
func (l List) String() string {
|
|
- return fmt.Sprintf("[%v]", l.members)
|
|
|
|
|
|
+ return fmt.Sprintf("[%v]", l.Value)
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type Method struct {
|
|
|
|
+ Object
|
|
|
|
+ Value func(env Value, self Value, args ...Value) List
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func NewMethod(name string, members Members,
|
|
|
|
+ value func(env Value, self Value, args ...Value) List) Method {
|
|
|
|
+ return Method{Object: NewObject(name, MethodType, members),
|
|
|
|
+ Value: value}
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func (m Method) String() string {
|
|
|
|
+ return fmt.Sprintf("Method: %s", m.Name)
|
|
}
|
|
}
|
|
|
|
|
|
var _ Value = String{}
|
|
var _ Value = String{}
|