164 lines
2.8 KiB
Go
164 lines
2.8 KiB
Go
package object
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"code.jmug.me/jmug/compiler-in-go/pkg/ast"
|
|
)
|
|
|
|
type ObjectType string
|
|
|
|
const (
|
|
INTEGER_OBJ = "INTEGER"
|
|
BOOLEAN_OBJ = "BOOLEAN"
|
|
NULL_OBJ = "NULL"
|
|
RETURN_VALUE_OBJ = "RETURN"
|
|
ERROR_OBJ = "ERROR"
|
|
FUNCTION_OBJ = "FUNCTION"
|
|
STRING_OBJ = "STRING"
|
|
BUILTIN_OBJ = "BUILTIN"
|
|
ARRAY_OBJ = "ARRAY"
|
|
HASH_OBJ = "HASH"
|
|
)
|
|
|
|
type Object interface {
|
|
Type() ObjectType
|
|
Inspect() string
|
|
}
|
|
|
|
type Integer struct {
|
|
Value int64
|
|
}
|
|
|
|
func (i *Integer) Type() ObjectType {
|
|
return INTEGER_OBJ
|
|
}
|
|
func (i *Integer) Inspect() string {
|
|
return fmt.Sprintf("%d", i.Value)
|
|
}
|
|
|
|
type Boolean struct {
|
|
Value bool
|
|
}
|
|
|
|
func (b *Boolean) Type() ObjectType {
|
|
return BOOLEAN_OBJ
|
|
}
|
|
func (b *Boolean) Inspect() string {
|
|
return fmt.Sprintf("%t", b.Value)
|
|
}
|
|
|
|
type Null struct{}
|
|
|
|
func (n *Null) Type() ObjectType {
|
|
return NULL_OBJ
|
|
}
|
|
func (n *Null) Inspect() string {
|
|
return "null"
|
|
}
|
|
|
|
type ReturnValue struct {
|
|
Value Object
|
|
}
|
|
|
|
func (rv *ReturnValue) Type() ObjectType {
|
|
return RETURN_VALUE_OBJ
|
|
}
|
|
func (rv *ReturnValue) Inspect() string {
|
|
return rv.Value.Inspect()
|
|
}
|
|
|
|
type Error struct {
|
|
Message string
|
|
}
|
|
|
|
func (e *Error) Type() ObjectType {
|
|
return ERROR_OBJ
|
|
}
|
|
func (e *Error) Inspect() string {
|
|
return "ERROR: " + e.Message
|
|
}
|
|
|
|
type Function struct {
|
|
Parameters []*ast.Identifier
|
|
Body *ast.BlockStatement
|
|
Env *Environment
|
|
}
|
|
|
|
func (f *Function) Type() ObjectType {
|
|
return FUNCTION_OBJ
|
|
}
|
|
func (f *Function) Inspect() string {
|
|
var out bytes.Buffer
|
|
params := []string{}
|
|
for _, p := range f.Parameters {
|
|
params = append(params, p.Value)
|
|
}
|
|
out.WriteString("fn")
|
|
out.WriteString("(" + strings.Join(params, ", ") + ")")
|
|
out.WriteString(" {\n" + f.Body.String() + "\n}")
|
|
return out.String()
|
|
}
|
|
|
|
type String struct {
|
|
Value string
|
|
}
|
|
|
|
func (s *String) Type() ObjectType {
|
|
return STRING_OBJ
|
|
}
|
|
func (s *String) Inspect() string {
|
|
return s.Value
|
|
}
|
|
|
|
type BuiltinFunction func(args ...Object) Object
|
|
type Builtin struct {
|
|
Fn BuiltinFunction
|
|
}
|
|
|
|
func (b *Builtin) Type() ObjectType {
|
|
return BUILTIN_OBJ
|
|
}
|
|
func (b *Builtin) Inspect() string {
|
|
return "builtin function"
|
|
}
|
|
|
|
type Array struct {
|
|
Elements []Object
|
|
}
|
|
|
|
func (a *Array) Type() ObjectType {
|
|
return ARRAY_OBJ
|
|
}
|
|
func (a *Array) Inspect() string {
|
|
elements := []string{}
|
|
for _, el := range a.Elements {
|
|
elements = append(elements, el.Inspect())
|
|
}
|
|
return fmt.Sprintf("[%s]", strings.Join(elements, ", "))
|
|
}
|
|
|
|
type HashPair struct {
|
|
Key Object
|
|
Value Object
|
|
}
|
|
|
|
type Hash struct {
|
|
Pairs map[HashKey]HashPair
|
|
}
|
|
|
|
func (h *Hash) Type() ObjectType {
|
|
return HASH_OBJ
|
|
}
|
|
func (h *Hash) Inspect() string {
|
|
pairs := []string{}
|
|
for _, p := range h.Pairs {
|
|
pairs = append(
|
|
pairs,
|
|
fmt.Sprintf("%s: %s", p.Key.Inspect(), p.Value.Inspect()),
|
|
)
|
|
}
|
|
return "{" + strings.Join(pairs, ", ") + "}"
|
|
}
|