Documentation
¶
Index ¶
- Constants
- Variables
- func ObjectToNativeBoolean(o Object) bool
- type Array
- type Boolean
- type Builtin
- type BuiltinFunction
- type Closure
- type CompiledFunction
- type Environment
- func (e *Environment) DefineConstant(name string, val Object) Object
- func (e *Environment) DefineVariable(name string, val Object) Object
- func (e *Environment) Exists(name string, inherit bool) bool
- func (e *Environment) Get(name string) (Object, bool)
- func (e *Environment) Set(name string, val Object) (Object, error)
- type Error
- type Function
- type Hash
- type HashKey
- type HashPair
- type Hashable
- type Integer
- type Null
- type Object
- type ObjectType
- type ReturnValue
- type String
Constants ¶
View Source
const ( INTEGER_OBJ = "INTEGER" BOOLEAN_OBJ = "BOOLEAN" NULL_OBJ = "NULL" STRING_OBJ = "STRING" ARRAY_OBJ = "ARRAY" HASH_OBJ = "HASH" RETURN_VALUE_OBJ = "RETURN_VALUE" FUNCTION_OBJ = "FUNCTION" COMPILED_FUNCTION_OBJ = "COMPILED_FUNCTION_OBJ" BUILTIN_OBJ = "BUILTIN" CLOSURE_OBJ = "CLOSURE" ERROR_OBJ = "ERROR" )
Variables ¶
View Source
var Builtins = []struct { Name string Builtin *Builtin }{ { "len", &Builtin{Fn: func(args ...Object) Object { if len(args) != 1 { return newError("wrong number of arguments. got=%d, want=1", len(args)) } switch arg := args[0].(type) { case *Array: return &Integer{Value: int64(len(arg.Elements))} case *String: return &Integer{Value: int64(len(arg.Value))} default: return newError("argument to `len` not supported, got %s", args[0].Type()) } }}, }, { "first", &Builtin{Fn: func(args ...Object) Object { if len(args) != 1 { return newError("wrong number of arguments. got=%d, want=1", len(args)) } if args[0].Type() != ARRAY_OBJ { return newError("argument to `first` must be ARRAY, got %s", args[0].Type()) } arr := args[0].(*Array) if len(arr.Elements) > 0 { return arr.Elements[0] } return nil }, }, }, { "last", &Builtin{Fn: func(args ...Object) Object { if len(args) != 1 { return newError("wrong number of arguments. got=%d, want=1", len(args)) } if args[0].Type() != ARRAY_OBJ { return newError("argument to `last` must be ARRAY, got %s", args[0].Type()) } arr := args[0].(*Array) length := len(arr.Elements) if length > 0 { return arr.Elements[length-1] } return nil }, }, }, { "rest", &Builtin{Fn: func(args ...Object) Object { if len(args) != 1 { return newError("wrong number of arguments. got=%d, want=1", len(args)) } if args[0].Type() != ARRAY_OBJ { return newError("argument to `rest` must be ARRAY, got %s", args[0].Type()) } arr := args[0].(*Array) length := len(arr.Elements) if length > 0 { newElements := make([]Object, length-1, length-1) copy(newElements, arr.Elements[1:length]) return &Array{Elements: newElements} } return nil }, }, }, { "push", &Builtin{Fn: func(args ...Object) Object { if len(args) != 2 { return newError("wrong number of arguments. got=%d, want=2", len(args)) } if args[0].Type() != ARRAY_OBJ { return newError("argument to `push` must be ARRAY, got %s", args[0].Type()) } arr := args[0].(*Array) length := len(arr.Elements) newElements := make([]Object, length+1, length+1) copy(newElements, arr.Elements) newElements[length] = args[1] return &Array{Elements: newElements} }, }, }, { "read", &Builtin{Fn: func(args ...Object) Object { reader := bufio.NewReader(os.Stdin) text, _ := reader.ReadString('\n') text = strings.Replace(text, "\n", "", -1) return &String{Value: text} }, }, }, { "print", &Builtin{Fn: func(args ...Object) Object { for _, arg := range args { fmt.Print(arg.Inspect()) } return nil }, }, }, { "println", &Builtin{Fn: func(args ...Object) Object { if len(args) == 0 { fmt.Println("") return nil } for _, arg := range args { fmt.Println(arg.Inspect()) } return nil }, }, }, { "env", &Builtin{Fn: func(args ...Object) Object { if len(args) > 1 { return newError("wrong number of arguments. got=%d, want=1", len(args)) } if len(args) == 1 && args[0].Type() == STRING_OBJ { stringArg, _ := args[0].(*String) env := os.Getenv(stringArg.Value) return &String{Value: env} } hash := &Hash{} hash.Pairs = make(map[HashKey]HashPair) for _, e := range os.Environ() { pair := strings.SplitN(e, "=", 2) key := String{Value: pair[0]} value := HashPair{ Key: &key, Value: &String{Value: pair[1]}, } hash.Pairs[key.HashKey()] = value } return hash }, }, }, }
Functions ¶
Types ¶
type Array ¶
type Array struct {
Elements []Object
}
** Array
func (*Array) Type ¶
func (ao *Array) Type() ObjectType
type Boolean ¶
type Boolean struct {
Value bool
}
** Boolean
func (*Boolean) Type ¶
func (b *Boolean) Type() ObjectType
type Builtin ¶
type Builtin struct {
Fn BuiltinFunction
}
func GetBuiltinByName ¶
func (*Builtin) Type ¶
func (b *Builtin) Type() ObjectType
type Closure ¶
type Closure struct { Fn *CompiledFunction Free []Object }
** Closure
func (*Closure) Type ¶
func (c *Closure) Type() ObjectType
type CompiledFunction ¶
type CompiledFunction struct { Instructions code.Instructions NumLocals int NumParameters int NumDefaults int }
** Compiled function
func (*CompiledFunction) Inspect ¶
func (cf *CompiledFunction) Inspect() string
func (*CompiledFunction) Type ¶
func (cf *CompiledFunction) Type() ObjectType
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
func NewEnclosedEnvironment ¶
func NewEnclosedEnvironment(outer *Environment) *Environment
func NewEnvironment ¶
func NewEnvironment() *Environment
func (*Environment) DefineConstant ¶
func (e *Environment) DefineConstant(name string, val Object) Object
func (*Environment) DefineVariable ¶
func (e *Environment) DefineVariable(name string, val Object) Object
type Error ¶
type Error struct {
Message string
}
** Error
func (*Error) Type ¶
func (e *Error) Type() ObjectType
type Function ¶
type Function struct { Parameters []*ast.Identifier Body *ast.BlockStatement Defaults map[string]ast.Expression Env *Environment }
** Function
func (*Function) Type ¶
func (f *Function) Type() ObjectType
type Integer ¶
type Integer struct {
Value int64
}
** Integer
func (*Integer) Type ¶
func (i *Integer) Type() ObjectType
type Object ¶
type Object interface { Type() ObjectType Inspect() string }
type ObjectType ¶
type ObjectType string
type ReturnValue ¶
type ReturnValue struct {
Value Object
}
** ReturnValue
func (*ReturnValue) Inspect ¶
func (rv *ReturnValue) Inspect() string
func (*ReturnValue) Type ¶
func (rv *ReturnValue) Type() ObjectType
Click to show internal directories.
Click to hide internal directories.