| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- package object
- import (
- "bytes"
- "fmt"
- "github/runnignwater/monkey/ast"
- "strings"
- )
- // ObjType type of object
- type ObjType string
- const (
- IntegerObj = "INTEGER"
- BooleanObj = "BOOLEAN"
- NullObj = "NULL"
- ReturnValueObj = "RETURN_VALUE"
- ErrorObj = "ERROR"
- FunctionObj = "FUNCTION"
- )
- // Object source code as an Object
- type Object interface {
- Type() ObjType
- Inspect() string
- }
- type Integer struct {
- Value int64
- }
- func (i *Integer) Type() ObjType { return IntegerObj }
- func (i *Integer) Inspect() string { return fmt.Sprintf("%d", i.Value) }
- type Boolean struct {
- Value bool
- }
- func (b *Boolean) Type() ObjType { return BooleanObj }
- func (b *Boolean) Inspect() string { return fmt.Sprintf("%t", b.Value) }
- type Null struct{}
- func (n *Null) Type() ObjType { return NullObj }
- func (n *Null) Inspect() string { return "null" }
- type ReturnValue struct {
- Value Object
- }
- func (rv *ReturnValue) Type() ObjType { return ReturnValueObj }
- func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() }
- type Error struct {
- Msg string
- }
- func (e *Error) Type() ObjType { return ErrorObj }
- func (e *Error) Inspect() string { return "ERROR: " + e.Msg }
- type Function struct {
- Parameters []*ast.Identifier
- Body *ast.BlockStatement
- Env *Environment
- }
- func (f *Function) Type() ObjType { return FunctionObj }
- func (f *Function) Inspect() string {
- var out bytes.Buffer
- params := []string{}
- for _, p := range f.Parameters {
- params = append(params, p.String())
- }
- out.WriteString("fn")
- out.WriteString("(")
- out.WriteString(strings.Join(params, ", "))
- out.WriteString(") {\n")
- out.WriteString(f.Body.String())
- out.WriteString("\n}")
- return out.String()
- }
|