add basic records
This commit is contained in:
parent
ca0031431d
commit
19011e67da
2 changed files with 113 additions and 3 deletions
|
|
@ -22,7 +22,7 @@ const (
|
|||
ObjectValueType
|
||||
FunctionValueType
|
||||
BuiltinFunctionValueType
|
||||
VariableValueType
|
||||
RecordValueType
|
||||
)
|
||||
|
||||
func (v ValueType) String() string {
|
||||
|
|
@ -47,8 +47,8 @@ func (v ValueType) String() string {
|
|||
return "function"
|
||||
case BuiltinFunctionValueType:
|
||||
return "builtin function"
|
||||
case VariableValueType:
|
||||
return "variable"
|
||||
case RecordValueType:
|
||||
return "record"
|
||||
}
|
||||
|
||||
return "undefined"
|
||||
|
|
@ -767,3 +767,59 @@ func (v *BuiltinFunctionValue) Clone() Value {
|
|||
v.Constant,
|
||||
}
|
||||
}
|
||||
|
||||
type RecordValue struct {
|
||||
Entries map[string]Value
|
||||
}
|
||||
|
||||
func (v *RecordValue) Type() ValueType {
|
||||
return RecordValueType
|
||||
}
|
||||
|
||||
func (v *RecordValue) String() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (v *RecordValue) DebugString() string {
|
||||
return v.String()
|
||||
}
|
||||
|
||||
func (v *RecordValue) Clone() Value {
|
||||
return &RecordValue{
|
||||
v.Entries,
|
||||
}
|
||||
}
|
||||
|
||||
func (v *RecordValue) Equals(other Value) bool {
|
||||
if other.Type() != RecordValueType {
|
||||
return false
|
||||
}
|
||||
|
||||
r := other.(*RecordValue)
|
||||
|
||||
if len(r.Entries) != len(v.Entries) {
|
||||
return false
|
||||
}
|
||||
|
||||
for key, val := range v.Entries {
|
||||
oth, ok := r.Entries[key]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if !val.Equals(oth) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (v *RecordValue) Get(key string) (Value, error) {
|
||||
val, ok := v.Entries[key]
|
||||
if !ok {
|
||||
return nil, errors.New(fmt.Sprintf("record has no property \"%s\"", key))
|
||||
}
|
||||
|
||||
return val, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue