Mako/types/value.go

107 lines
1.8 KiB
Go

package types
import "fmt"
// ValueType represents the type of a runtime value
type ValueType int
const (
NIL_TYPE ValueType = iota
BOOL_TYPE
NUMBER_TYPE
STRING_TYPE
FUNCTION_TYPE
TABLE_TYPE
)
// Value interface represents any value in the language at runtime
type Value interface {
Type() ValueType
String() string
}
// NilValue represents a nil value
type NilValue struct{}
func (n NilValue) Type() ValueType {
return NIL_TYPE
}
func (n NilValue) String() string {
return "nil"
}
// BoolValue represents a boolean value
type BoolValue struct {
Value bool
}
func (b BoolValue) Type() ValueType {
return BOOL_TYPE
}
func (b BoolValue) String() string {
if b.Value {
return "true"
}
return "false"
}
// NumberValue represents a numeric value
type NumberValue struct {
Value float64
}
func (n NumberValue) Type() ValueType {
return NUMBER_TYPE
}
func (n NumberValue) String() string {
return fmt.Sprintf("%g", n.Value)
}
// StringValue represents a string value
type StringValue struct {
Value string
}
func (s StringValue) Type() ValueType {
return STRING_TYPE
}
func (s StringValue) String() string {
return s.Value
}
// FunctionValue represents a function value
type FunctionValue struct {
Name string
Arity int
// We'll add bytecode and other function data later
}
func (f FunctionValue) Type() ValueType {
return FUNCTION_TYPE
}
func (f FunctionValue) String() string {
if f.Name == "" {
return "<anonymous function>"
}
return fmt.Sprintf("<function %s>", f.Name)
}
// TableValue represents a table (map/dictionary/array)
type TableValue struct {
Data map[Value]Value
MetaTable *TableValue
}
func (t TableValue) Type() ValueType {
return TABLE_TYPE
}
func (t TableValue) String() string {
return fmt.Sprintf("<table: %p>", &t)
}