Files
monkey/internal/object/function.go
Chuck Smith 07fd82b261
Some checks failed
Build / build (push) Successful in 10m29s
Publish Image / publish (push) Failing after 31s
Test / build (push) Failing after 6m34s
optimizations
2024-04-02 14:08:08 -04:00

67 lines
1017 B
Go

package object
import (
"bytes"
"monkey/internal/ast"
"strings"
)
type Function struct {
BaseObject
Parameters []*ast.Identifier
Body *ast.BlockStatement
Env *Environment
}
func (f Function) Bool() bool {
return false
}
func (f Function) Type() Type {
return FunctionType
}
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()
}
func (f Function) String() string {
return f.Inspect()
}
type ReturnValue struct {
BaseObject
Value Object
}
func (rv ReturnValue) Bool() bool {
return true
}
func (rv ReturnValue) Type() Type {
return ReturnType
}
func (rv ReturnValue) Inspect() string {
return rv.Value.Inspect()
}
func (rv ReturnValue) String() string {
return rv.Inspect()
}