123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- 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)
- }
- type List struct {
- Object
- }
- func NewList(value int64, id int, name string,
- parent Value, members ...Value) List {
- return List{Object: NewObject(id, name, IntType, parent, members...)}
- }
- func (l List) String() string {
- return fmt.Sprintf("[%v]", l.members)
- }
- var _ Value = String{}
- var _ Value = Int{}
- var _ Value = List{}
|