Documentation ¶
Index ¶
- Constants
- Variables
- type Array
- type Boolean
- type Builtin
- type BuiltinFunction
- type Closure
- type CompiledFunction
- type Environment
- type Error
- type Float
- type Function
- type Hash
- type HashKey
- type HashPair
- type Hashable
- type Integer
- type Macro
- type Nil
- type Object
- type Quote
- type ReturnValue
- type String
- type Type
Constants ¶
const ( // IntegerType represents a type of integers. IntegerType Type = "Integer" // FloatType represents a type of floating point numbers. FloatType = "Float" // BooleanType represents a type of booleans. BooleanType = "Boolean" // NilType represents a type of nil. NilType = "Nil" // ReturnValueType represents a type of return values. ReturnValueType = "ReturnValue" // ErrorType represents a type of errors. ErrorType = "Error" // FunctionType represents a type of functions. FunctionType = "Function" // StringType represents a type of strings. StringType = "String" // BuiltinType represents a type of builtin functions. BuiltinType = "Builtin" // ArrayType represents a type of arrays. ArrayType = "Array" // HashType represents a type of hashes. HashType = "Hash" // QuoteType represents a type of quotes used for macros. QuoteType = "Quote" // MacroType represents a type of macros. MacroType = "Macro" // CompiledFunctionType represents a type of compiled functions. CompiledFunctionType = "CompiledFunction" // ClosureType represents a type of closures. ClosureType = "Closure" )
Variables ¶
var Builtins = []struct { Name string Builtin *Builtin }{ { Name: "len", Builtin: &Builtin{ Fn: func(args ...Object) Object { if l := len(args); l != 1 { return newError("wrong number of arguments. want=1, got=%d", l) } switch arg := args[0].(type) { case *String: return &Integer{Value: int64(len(arg.Value))} case *Array: return &Integer{Value: int64(len(arg.Elements))} default: return newError("argument to `len` not supported, got %s", arg.Type()) } }, }, }, { Name: "puts", Builtin: &Builtin{ Fn: func(args ...Object) Object { for _, arg := range args { fmt.Println(arg.Inspect()) } return nil }, }, }, { Name: "first", Builtin: &Builtin{ Fn: func(args ...Object) Object { if l := len(args); l != 1 { return newError("wrong number of arguments. want=1, got=%d", l) } if typ := args[0].Type(); typ != ArrayType { return newError("argument to `first` must be Array, got %s", typ) } arr := args[0].(*Array) if len(arr.Elements) > 0 { return arr.Elements[0] } return nil }, }, }, { Name: "last", Builtin: &Builtin{ Fn: func(args ...Object) Object { if l := len(args); l != 1 { return newError("wrong number of arguments. want=1, got=%d", l) } if typ := args[0].Type(); typ != ArrayType { return newError("argument to `last` must be Array, got %s", typ) } arr := args[0].(*Array) if l := len(arr.Elements); l > 0 { return arr.Elements[l-1] } return nil }, }, }, { Name: "rest", Builtin: &Builtin{ Fn: func(args ...Object) Object { if l := len(args); l != 1 { return newError("wrong number of arguments. want=1, got=%d", l) } if typ := args[0].Type(); typ != ArrayType { return newError("argument to `last` must be Array, got %s", typ) } arr := args[0].(*Array) l := len(arr.Elements) if l == 0 { return nil } newElems := make([]Object, l-1) copy(newElems, arr.Elements[1:l]) return &Array{Elements: newElems} }, }, }, { Name: "push", Builtin: &Builtin{ Fn: func(args ...Object) Object { if l := len(args); l != 2 { return newError("wrong number of arguments. want=%d, got=%d", 2, l) } if typ := args[0].Type(); typ != ArrayType { return newError("first argument to `push` must be Array, got %s", typ) } arr := args[0].(*Array) l := len(arr.Elements) newElems := make([]Object, l+1) copy(newElems, arr.Elements) newElems[l] = args[1] return &Array{Elements: newElems} }, }, }, }
Builtins is a list of built-in functions.
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array struct {
Elements []Object
}
Array represents an array.
type Boolean ¶
type Boolean struct {
Value bool
}
Boolean represents a boolean.
type Builtin ¶
type Builtin struct {
Fn BuiltinFunction
}
Builtin represents a builtin function.
func GetBuiltinByName ¶
GetBuiltinByName returns a built-in function matching a given name. If no function is found with the name, it returns nil.
type BuiltinFunction ¶
BuiltinFunction represents a function signature of builtin functions.
type Closure ¶
type Closure struct { Fn *CompiledFunction Free []Object }
Closure represents a closure. It has a pointer to the function it wraps, `Fn`, and a place to keep the free variables it carries around, `Free`.
type CompiledFunction ¶
type CompiledFunction struct { Instructions code.Instructions // NumLocals is used for reserving slots to store local bindings on the stack NumLocals int NumParameters int }
CompiledFunction represents a function compiled to bytecode instructions.
func (*CompiledFunction) Inspect ¶
func (cf *CompiledFunction) Inspect() string
Inspect returns a string representation of `cf`.
func (*CompiledFunction) Type ¶
func (cf *CompiledFunction) Type() Type
Type returns the type of `cf`.
type Environment ¶
type Environment interface { // Get retrieves the value of a variable named by the `name`. // If the variable is present in the environment the value is returned and the boolean is true. // Otherwise the returned value will be nil and the boolean will be false. Get(name string) (Object, bool) // Set sets the `val` of a variable named by the `name` and returns the `val` itself. Set(name string, val Object) Object }
Environment associates values with variable names.
func NewEnclosedEnvironment ¶
func NewEnclosedEnvironment(outer Environment) Environment
NewEnclosedEnvironment creates a new Environment which holds the given outer Environment.
type Error ¶
type Error struct {
Message string
}
Error represents an error.
type Float ¶
type Float struct {
Value float64
}
Float represents an integer.
type Function ¶
type Function struct { Parameters []*ast.Ident Body *ast.BlockStatement Env Environment }
Function represents a function.
type Hash ¶
Hash represents a hash.
type Hashable ¶
type Hashable interface {
HashKey() HashKey
}
Hashable is the interface that is able to become a hash key.
type Integer ¶
type Integer struct {
Value int64
}
Integer represents an integer.
type Macro ¶
type Macro struct { Parameters []*ast.Ident Body *ast.BlockStatement Env Environment }
Macro represents a macro.
type Nil ¶
type Nil struct{}
Nil represents the absence of any value.
type Quote ¶
Quote represents a quote, i.e. an unevaluated expression.
type ReturnValue ¶
type ReturnValue struct {
Value Object
}
ReturnValue represents a return value.
func (*ReturnValue) Inspect ¶
func (rv *ReturnValue) Inspect() string
Inspect returns a string representation of the ReturnValue.
func (*ReturnValue) Type ¶
func (rv *ReturnValue) Type() Type
Type returns the type of the ReturnValue.