lang: types: Export the base value for others

This lets others implement this more easily.
This commit is contained in:
James Shubin
2023-09-25 16:40:58 -04:00
parent 5440ef7eb2
commit dbba49540e

View File

@@ -459,54 +459,54 @@ func (vs ValueSlice) Len() int { return len(vs) }
func (vs ValueSlice) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] } func (vs ValueSlice) Swap(i, j int) { vs[i], vs[j] = vs[j], vs[i] }
func (vs ValueSlice) Less(i, j int) bool { return vs[i].Less(vs[j]) } func (vs ValueSlice) Less(i, j int) bool { return vs[i].Less(vs[j]) }
// base implements the missing methods that all types need. // Base implements the missing methods that all types need.
type base struct{} type Base struct{}
// Bool represents the value of this type as a bool if it is one. If this is not // Bool represents the value of this type as a bool if it is one. If this is not
// a bool, then this panics. // a bool, then this panics.
func (obj *base) Bool() bool { func (obj *Base) Bool() bool {
panic("not a bool") panic("not a bool")
} }
// Str represents the value of this type as a string if it is one. If this is // Str represents the value of this type as a string if it is one. If this is
// not a string, then this panics. // not a string, then this panics.
func (obj *base) Str() string { func (obj *Base) Str() string {
panic("not an str") // yes, i think this is the correct grammar panic("not an str") // yes, i think this is the correct grammar
} }
// Int represents the value of this type as an integer if it is one. If this is // Int represents the value of this type as an integer if it is one. If this is
// not an integer, then this panics. // not an integer, then this panics.
func (obj *base) Int() int64 { func (obj *Base) Int() int64 {
panic("not an int") panic("not an int")
} }
// Float represents the value of this type as a float if it is one. If this is // Float represents the value of this type as a float if it is one. If this is
// not a float, then this panics. // not a float, then this panics.
func (obj *base) Float() float64 { func (obj *Base) Float() float64 {
panic("not a float") panic("not a float")
} }
// List represents the value of this type as a list if it is one. If this is not // List represents the value of this type as a list if it is one. If this is not
// a list, then this panics. // a list, then this panics.
func (obj *base) List() []Value { func (obj *Base) List() []Value {
panic("not a list") panic("not a list")
} }
// Map represents the value of this type as a dictionary if it is one. If this // Map represents the value of this type as a dictionary if it is one. If this
// is not a map, then this panics. // is not a map, then this panics.
func (obj *base) Map() map[Value]Value { func (obj *Base) Map() map[Value]Value {
panic("not a list") panic("not a list")
} }
// Struct represents the value of this type as a struct if it is one. If this is // Struct represents the value of this type as a struct if it is one. If this is
// not a struct, then this panics. // not a struct, then this panics.
func (obj *base) Struct() map[string]Value { func (obj *Base) Struct() map[string]Value {
panic("not a struct") panic("not a struct")
} }
// Func represents the value of this type as a function if it is one. If this is // Func represents the value of this type as a function if it is one. If this is
// not a function, then this panics. // not a function, then this panics.
func (obj *base) Func() interface{} { func (obj *Base) Func() interface{} {
panic("not a func") panic("not a func")
} }
@@ -514,7 +514,7 @@ func (obj *base) Func() interface{} {
// that this base implementation of the method be replaced in the specific type. // that this base implementation of the method be replaced in the specific type.
// This *may* panic if the two types aren't the same. // This *may* panic if the two types aren't the same.
// NOTE: this can be used as an example template to write your own function. // NOTE: this can be used as an example template to write your own function.
//func (obj *base) Less(v Value) bool { //func (obj *Base) Less(v Value) bool {
// // TODO: cheap less, be smarter in each type eg: int's should cmp as int // // TODO: cheap less, be smarter in each type eg: int's should cmp as int
// return obj.String() < v.String() // return obj.String() < v.String()
//} //}
@@ -523,7 +523,7 @@ func (obj *base) Func() interface{} {
// implementation uses the base Less implementation and should be replaced. It // implementation uses the base Less implementation and should be replaced. It
// is always nice to implement this properly so that we get better error output. // is always nice to implement this properly so that we get better error output.
// NOTE: this can be used as an example template to write your own function. // NOTE: this can be used as an example template to write your own function.
//func (obj *base) Cmp(v Value) error { //func (obj *Base) Cmp(v Value) error {
// // if they're both true or both false, then they must be the same, // // if they're both true or both false, then they must be the same,
// // because we expect that if x < & && y < x then x == y // // because we expect that if x < & && y < x then x == y
// if obj.Less(v) != v.Less(obj) { // if obj.Less(v) != v.Less(obj) {
@@ -534,7 +534,7 @@ func (obj *base) Func() interface{} {
// BoolValue represents a boolean value. // BoolValue represents a boolean value.
type BoolValue struct { type BoolValue struct {
base Base
V bool V bool
} }
@@ -597,7 +597,7 @@ func (obj *BoolValue) Bool() bool {
// StrValue represents a string value. // StrValue represents a string value.
type StrValue struct { type StrValue struct {
base Base
V string V string
} }
@@ -652,7 +652,7 @@ func (obj *StrValue) Str() string {
// IntValue represents an integer value. // IntValue represents an integer value.
type IntValue struct { type IntValue struct {
base Base
V int64 V int64
} }
@@ -706,7 +706,7 @@ func (obj *IntValue) Int() int64 {
// FloatValue represents an integer value. // FloatValue represents an integer value.
type FloatValue struct { type FloatValue struct {
base Base
V float64 V float64
} }
@@ -763,7 +763,7 @@ func (obj *FloatValue) Float() float64 {
// ListValue represents a list value. // ListValue represents a list value.
type ListValue struct { type ListValue struct {
base Base
V []Value // all elements must have type T.Val V []Value // all elements must have type T.Val
T *Type T *Type
} }
@@ -891,7 +891,7 @@ func (obj *ListValue) Contains(v Value) (index int, exists bool) {
// MapValue represents a dictionary value. // MapValue represents a dictionary value.
type MapValue struct { type MapValue struct {
base Base
// the types of all keys and values are represented inside of T // the types of all keys and values are represented inside of T
V map[Value]Value V map[Value]Value
T *Type T *Type
@@ -1024,7 +1024,7 @@ func (obj *MapValue) Lookup(key Value) (value Value, exists bool) {
// StructValue represents a struct value. The keys are ordered. // StructValue represents a struct value. The keys are ordered.
// TODO: if all functions require arg names to call, we don't need to order! // TODO: if all functions require arg names to call, we don't need to order!
type StructValue struct { type StructValue struct {
base Base
V map[string]Value // each field can have a different type V map[string]Value // each field can have a different type
T *Type // contains ordered field types T *Type // contains ordered field types
} }
@@ -1146,7 +1146,7 @@ func (obj *StructValue) Lookup(k string) (value Value, exists bool) {
// FuncValue is not a Value, but it is a useful building block for implementing // FuncValue is not a Value, but it is a useful building block for implementing
// Func nodes. // Func nodes.
type FuncValue struct { type FuncValue struct {
base Base
V func([]Value) (Value, error) V func([]Value) (Value, error)
T *Type // contains ordered field types, arg names are a bonus part T *Type // contains ordered field types, arg names are a bonus part
} }
@@ -1269,7 +1269,7 @@ func (obj *FuncValue) Call(args []Value) (Value, error) {
// VariantValue represents a variant value. // VariantValue represents a variant value.
type VariantValue struct { type VariantValue struct {
base Base
V Value // formerly I experimented with using interface{} instead V Value // formerly I experimented with using interface{} instead
T *Type T *Type
} }