Documentation ¶
Index ¶
Constants ¶
View Source
const ( BuiltinFuncNameLen = "len" BuiltinFuncNameFirst = "first" BuiltinFuncNameLast = "last" BuiltinFuncNameRest = "rest" BuiltinFuncNamePush = "push" BuiltinFuncNamePuts = "puts" )
View Source
const ( IntegerObj = "Integer" BooleanObj = "Boolean" NullObj = "Null" ReturnValueObj = "ReturnValue" ErrorObj = "Error" FunctionObj = "Function" StringObj = "String" BuiltinObj = "Builtin" ArrayObj = "Array" HashObj = "Hash" CompiledFunctionObj = "CompiledFunction" ClosureObj = "Closure" )
Variables ¶
View Source
var Builtins = []struct { Name string Builtin *Builtin }{ { BuiltinFuncNameLen, &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 %q not supported, got %s", BuiltinFuncNameLen, args[0].Type()) } }, }, }, { BuiltinFuncNamePuts, &Builtin{Fn: func(args ...Object) Object { for _, arg := range args { fmt.Println(arg.Inspect()) } return nil }, }, }, { BuiltinFuncNameFirst, &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() != ArrayObj { return newError("argument to %q must be %s, got %s", BuiltinFuncNameFirst, ArrayObj, args[0].Type()) } arr := args[0].(*Array) if len(arr.Elements) > 0 { return arr.Elements[0] } return nil }, }, }, { BuiltinFuncNameLast, &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() != ArrayObj { return newError("argument to %q must be %s, got %s", BuiltinFuncNameLast, ArrayObj, args[0].Type()) } arr := args[0].(*Array) length := len(arr.Elements) if length > 0 { return arr.Elements[length-1] } return nil }, }, }, { BuiltinFuncNameRest, &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() != ArrayObj { return newError("argument to %q must be %s, got %s", BuiltinFuncNameRest, ArrayObj, 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 }, }, }, { BuiltinFuncNamePush, &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() != ArrayObj { return newError("argument to %q must be %s, got %s", BuiltinFuncNamePush, ArrayObj, 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} }, }, }, }
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array struct {
Elements []Object
}
func (*Array) Type ¶
func (ao *Array) Type() ObjectType
type Boolean ¶
type Boolean struct {
Value bool
}
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 BuiltinFunction ¶
type Closure ¶
type Closure struct { Fn *CompiledFunction Free []Object }
func (*Closure) Type ¶
func (c *Closure) Type() ObjectType
type CompiledFunction ¶
type CompiledFunction struct { Instructions code.Instructions NumLocals int NumParameters int }
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
type Error ¶
type Error struct {
Message string
}
func (*Error) Type ¶
func (e *Error) Type() ObjectType
type Function ¶
type Function struct { Parameters []*ast.Identifier Body *ast.BlockStatement Env *Environment }
func (*Function) Type ¶
func (f *Function) Type() ObjectType
type HashKey ¶
type HashKey struct { Type ObjectType Value uint64 }
type Integer ¶
type Integer struct {
Value int64
}
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
}
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.