Documentation ¶
Index ¶
Constants ¶
const ( IntegerObj = "INTEGER" BooleanObj = "BOOLEAN" NullObj = "NULL" ReturnValueObj = "RETURN_VALUE" ErrorObj = "ERROR" FunctionObj = "FUNCTION" StringObj = "STRING" BuiltinObj = "BUILTIN" ArrayObj = "ARRAY" HashObj = "HASH" CompiledFunctionObj = "COMPILED_FUNCTION_OBJ" ClosureObj = "CLOSURE" )
Define object types
Variables ¶
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, Expected: 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()) } }, }, }, { "print", &Builtin{ Fn: func(args ...Object) Object { for _, arg := range args { fmt.Println(arg.Inspect()) } return nil }, }, }, { "first", &Builtin{ Fn: func(args ...Object) Object { if len(args) != 1 { return newError("Wrong number of arguments. Got: %d, Expected: 1", len(args)) } if args[0].Type() != ArrayObj { return newError("Argument to `first` must be an Array. Got: %s", args[0].Type()) } array := args[0].(*Array) if len(array.Elements) > 0 { return array.Elements[0] } return nil }, }, }, { "last", &Builtin{ Fn: func(args ...Object) Object { if len(args) != 1 { return newError("Wrong number of arguments. Got: %d, Expected: 1", len(args)) } if args[0].Type() != ArrayObj { return newError("Argument to `last` must be an Array. Got: %s", args[0].Type()) } array := args[0].(*Array) length := len(array.Elements) if length > 0 { return array.Elements[length-1] } return nil }, }, }, { "rest", &Builtin{ Fn: func(args ...Object) Object { if len(args) != 1 { return newError("Wrong number of arguments. Got: %d, Expected: 1", len(args)) } if args[0].Type() != ArrayObj { return newError("Argument to `rest` must be an Array. Got: %s", args[0].Type()) } array := args[0].(*Array) length := len(array.Elements) if length > 0 { newElements := make([]Object, length-1, length-1) copy(newElements, array.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, Expected: 2", len(args)) } if args[0].Type() != ArrayObj { return newError("Argument to `push` must be an Array. Got: %s", args[0].Type()) } array := args[0].(*Array) length := len(array.Elements) newElements := make([]Object, length+1, length+1) copy(newElements, array.Elements) newElements[length] = args[1] return &Array{Elements: newElements} }, }, }, { "pop", &Builtin{ Fn: func(args ...Object) Object { if len(args) != 1 { return newError("Wrong number of arguments. Got: %d, Expected: 1", len(args)) } if args[0].Type() != ArrayObj { return newError("Argument to `pop` must be an Array. Got: %s", args[0].Type()) } array := args[0].(*Array) length := len(array.Elements) if length == 0 { return nil } newElements := make([]Object, length-1, length-1) copy(newElements, array.Elements[0:length-1]) return &Array{Elements: newElements} }, }, }, }
Builtins defines all of Monkey's built in functions
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array struct {
Elements []Object
}
Array type wraps the array's elements in a slice of Objects
func (*Array) Type ¶
func (a *Array) Type() ObjectType
Type returns our Array's ObjectType (ArrayObj)
type Boolean ¶
type Boolean struct {
Value bool
}
Boolean type holds the value of the boolean as a bool
func (*Boolean) HashKey ¶
HashKey returns a HashKey with a Value of 1 or 0 (true or false) and a Type of BooleanObj
func (*Boolean) Type ¶
func (b *Boolean) Type() ObjectType
Type returns our Boolean's ObjectType (BooleanObj)
type Builtin ¶
type Builtin struct {
Fn BuiltinFunction
}
Builtin is our object wrapper holding a builtin function
func GetBuiltinByName ¶
GetBuiltinByName takes a name, iterates over our builtins slice and returns the appropriate builtin
type BuiltinFunction ¶
BuiltinFunction is a type representing functions we write in Go and expose to our users inside monkey-lang
type Closure ¶
type Closure struct { Fn *CompiledFunction Free []Object }
Closure holds a pointer to its compiled function and a slice of its free objects (variables it has access to that are not in either global or local scope)
func (*Closure) Type ¶
func (c *Closure) Type() ObjectType
Type returns our Closure's ObjectType (ClosureObj)
type CompiledFunction ¶
type CompiledFunction struct { Instructions code.Instructions NumLocals int NumParameters int }
CompiledFunction holds the instructions we get from the compilation of a function literal and is an object.Object, which means we can add it as a constant to our compiler.Bytecode and load it in the VM. It also holds the NumLocals which we pass to the VM to allocate the correct amount of stack space ("hole") to save the local bindings
func (*CompiledFunction) Inspect ¶
func (cf *CompiledFunction) Inspect() string
Inspect returns the string "CompiledFunction[address]" - Address of 0th element in base 16 notation, with leading 0x
func (*CompiledFunction) Type ¶
func (cf *CompiledFunction) Type() ObjectType
Type returns our CompiledFunction's ObjectType (CompiledFunctionObj)
type Environment ¶
type Environment struct {
// contains filtered or unexported fields
}
Environment holds a store of key value pairs and a pointer to an "outer", enclosing environment
func NewEnclosedEnvironment ¶
func NewEnclosedEnvironment(outer *Environment) *Environment
NewEnclosedEnvironment creates a new Environment and attaches the outer environment that's passed in, to the new environment, as it's enclosing environment
func NewEnvironment ¶
func NewEnvironment() *Environment
NewEnvironment creates and returns a pointer to an Environment
type Error ¶
type Error struct {
Message string
}
Error type holds an error Message
func (*Error) Type ¶
func (e *Error) Type() ObjectType
Type returns our Error's ObjectType (ErrorObj)
type Function ¶
type Function struct { Parameters []*ast.Identifier Body *ast.BlockStatement Env *Environment }
Function holds Parameters as a slice of *Identifier, a Body which is a *ast.BlockStatement and a pointer to it's environment
func (*Function) Type ¶
func (f *Function) Type() ObjectType
Type returns our Function's ObjectType (FunctionObj)
type Hash ¶
Hash hold Pairs which are a map of HashKey -> HashPair. We map the keys to HashPairs instead of Objects for a better ability to Inspect() and see the key and value
type HashKey ¶
type HashKey struct { Type ObjectType Value uint64 }
HashKey type wraps the key's type and holds its value
type Hashable ¶
type Hashable interface {
HashKey() HashKey
}
Hashable is one method called HashKey. Any object that that can be used as a HashKey must implement this interface (*object.String, *object.boolean, *object.integer)
type Integer ¶
type Integer struct {
Value int64
}
Integer type holds the value of the integer as an int64
func (*Integer) HashKey ¶
HashKey returns a HashKey with a Value of the Integer and a Type of IntegerObj
type Null ¶
type Null struct{}
Null type is an empty struct
type Object ¶
type Object interface { Type() ObjectType Inspect() string }
Object represents monkey's object system. Every value in monkey-lang must implement this interface
type ReturnValue ¶
type ReturnValue struct {
Value Object
}
ReturnValue type holds a return value
func (*ReturnValue) Inspect ¶
func (rv *ReturnValue) Inspect() string
Inspect returns a string representation of the ReturnValue's Value
func (*ReturnValue) Type ¶
func (rv *ReturnValue) Type() ObjectType
Type returns our ReturnValue's ObjectType (ReturnValueObj)
type String ¶
type String struct {
Value string
}
String type holds the value of the string
func (*String) HashKey ¶
HashKey returns a HashKey with a Value of a 64-bit FNV-1a hash of the String and a Type of StringObj