Documentation
¶
Index ¶
- Constants
- Variables
- type Array
- type Boolean
- type Builtin
- type BuiltinFunction
- type Closure
- type CompiledFunction
- type Error
- type Float
- type Hash
- type HashKey
- type HashPair
- type Hashable
- type Integer
- type MemberFn
- type MemberFunction
- type Null
- type Object
- type ObjectType
- type ReturnValue
- type Scope
- type String
Constants ¶
View Source
const ( BuiltinFuncNameLen = "len" BuiltinFuncNamePrint = "print" // Type conversions BuiltinFuncNameInt = "int" BuiltinFuncNameFloat = "float" BuiltinFuncNameString = "string" )
View Source
const ( IntegerObj = "Integer" FloatObj = "Float" BooleanObj = "Boolean" NullObj = "Null" ReturnValueObj = "ReturnValue" ErrorObj = "Error" FunctionObj = "Function" StringObj = "String" BuiltinObj = "Builtin" ArrayObj = "Array" HashObj = "Hash" CompiledFunctionObj = "CompiledFunction" ClosureObj = "Closure" ScopeObj = "Scope" )
Variables ¶
View Source
var ( NullValue = NullObject True = &Boolean{Value: true} False = &Boolean{Value: false} )
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()) } }, }, }, { BuiltinFuncNamePrint, &Builtin{Fn: func(args ...Object) Object { for _, arg := range args { fmt.Println(arg.Inspect()) } return nil }, }, }, { BuiltinFuncNameInt, &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 *Integer: return arg case *Float: return &Integer{Value: int64(arg.Value)} case *String: val, err := strconv.ParseInt(arg.Value, 10, 64) if err != nil { return newError("Conversion to int failed!") } return &Integer{Value: val} default: return newError("argument to %q not supported, got %s", BuiltinFuncNameLen, args[0].Type()) } }, }, }, { BuiltinFuncNameFloat, &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 *Integer: return &Float{Value: float64(arg.Value)} case *Float: return arg case *String: val, err := strconv.ParseFloat(arg.Value, 64) if err != nil { return newError("Conversion to float failed!") } return &Float{Value: val} default: return newError("argument to %q not supported, got %s", BuiltinFuncNameLen, args[0].Type()) } }, }, }, { BuiltinFuncNameString, &Builtin{Fn: func(args ...Object) Object { if len(args) != 1 { return newError("wrong number of arguments. got=%d, want=1", len(args)) } return &String{Value: args[0].String()} }, }, }, }
View Source
var NullObject = &Null{}
Functions ¶
This section is empty.
Types ¶
type Array ¶
type Array struct {
Elements []Object
}
func (*Array) InfixOperation ¶
func (*Array) Type ¶
func (ao *Array) Type() ObjectType
type Boolean ¶
type Boolean struct {
Value bool
}
func (*Boolean) InfixOperation ¶
func (*Boolean) Type ¶
func (b *Boolean) Type() ObjectType
type Builtin ¶
type Builtin struct {
Fn BuiltinFunction
}
func (*Builtin) InfixOperation ¶
func (*Builtin) Type ¶
func (b *Builtin) Type() ObjectType
type BuiltinFunction ¶
type Closure ¶
type Closure struct {
Fn *CompiledFunction
Free []Object
Exports map[string]Object
}
func (*Closure) InfixOperation ¶
func (*Closure) Type ¶
func (c *Closure) Type() ObjectType
type CompiledFunction ¶
type CompiledFunction struct {
Instructions code.Instructions
NumLocals int
NumParameters int
}
func (*CompiledFunction) GetMember ¶
func (obj *CompiledFunction) GetMember(name string) Object
func (*CompiledFunction) InfixOperation ¶
func (obj *CompiledFunction) InfixOperation(operator string, other Object) Object
func (*CompiledFunction) Inspect ¶
func (cf *CompiledFunction) Inspect() string
func (*CompiledFunction) String ¶
func (cf *CompiledFunction) String() string
func (*CompiledFunction) Type ¶
func (cf *CompiledFunction) Type() ObjectType
type Error ¶
type Error struct {
Message string
}
func (*Error) InfixOperation ¶
func (*Error) Type ¶
func (e *Error) Type() ObjectType
type Float ¶
type Float struct {
Value float64
}
func (*Float) InfixOperation ¶
func (*Float) Type ¶
func (i *Float) Type() ObjectType
type HashKey ¶
type HashKey struct {
Type ObjectType
Value string
}
type Integer ¶
type Integer struct {
Value int64
}
func (*Integer) InfixOperation ¶
func (*Integer) Type ¶
func (i *Integer) Type() ObjectType
type MemberFn ¶
type MemberFn struct {
Obj Object
Fn MemberFunction
}
func (*MemberFn) InfixOperation ¶
func (*MemberFn) Type ¶
func (b *MemberFn) Type() ObjectType
type MemberFunction ¶
type ObjectType ¶
type ObjectType string
type ReturnValue ¶
type ReturnValue struct {
Value Object
}
func (*ReturnValue) GetMember ¶
func (obj *ReturnValue) GetMember(name string) Object
func (*ReturnValue) InfixOperation ¶
func (obj *ReturnValue) InfixOperation(operator string, other Object) Object
func (*ReturnValue) Inspect ¶
func (rv *ReturnValue) Inspect() string
func (*ReturnValue) String ¶
func (rv *ReturnValue) String() string
func (*ReturnValue) Type ¶
func (rv *ReturnValue) Type() ObjectType
type Scope ¶
type Scope struct {
Name string
Constants []Object
Instructions code.Instructions
NumLocals int
Exports map[string]Object
}
func (*Scope) InfixOperation ¶
func (*Scope) Type ¶
func (cf *Scope) Type() ObjectType
type String ¶
type String struct {
Value string
}
func (*String) InfixOperation ¶
func (*String) Type ¶
func (s *String) Type() ObjectType
Source Files
¶
Click to show internal directories.
Click to hide internal directories.