Browse Source

Value representation.

Beoran 2 years ago
parent
commit
1e2d110a8f
1 changed files with 93 additions and 0 deletions
  1. 93 0
      selsl/value/value.go

+ 93 - 0
selsl/value/value.go

@@ -0,0 +1,93 @@
+package value
+
+import "fmt"
+
+type Value interface {
+	ID() int
+	Name() string
+	// Proto/type of the value.
+	Type() Type
+	String() string
+	Parent() Value
+	Members() []Value
+}
+
+// A type is also a 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)
+
+type Object struct {
+	id      int
+	name    string
+	typ     Type
+	parent  Value
+	members []Value
+}
+
+func (o Object) String() string {
+	return fmt.Sprintf("%s: %d", o.typ, o.name)
+}
+
+func (o Object) ID() int {
+	return o.id
+}
+
+func (o Object) Name() string {
+	return o.name
+}
+
+func (o Object) Parent() Value {
+	return o.parent
+}
+
+func (o Object) Members() []Value {
+	return o.members
+}
+
+func (o Object) Type() Type {
+	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,
+	}
+}
+
+type String struct {
+	Object
+	Value string
+}
+
+func (s String) String() string {
+	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...),
+		Value: value}
+}
+
+type Int struct {
+	Object
+	Value int64
+}
+
+func NewInt(value int64, id int, name string,
+	parent Value, members ...Value) Int {
+	return Int{Object: NewObject(id, name, IntType, parent, members...),
+		Value: value}
+}
+
+func (i Int) String() string {
+	return fmt.Sprintf("%d", i.Value)
+}
+
+var _ Value = String{}
+var _ Value = Int{}