Evaluate function literals and function calls.
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
parent
500a058ff8
commit
a76f47a7a3
4 changed files with 137 additions and 1 deletions
|
|
@ -1,6 +1,12 @@
|
|||
package object
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"code.jmug.me/jmug/interpreter-in-go/pkg/ast"
|
||||
)
|
||||
|
||||
type ObjectType string
|
||||
|
||||
|
|
@ -10,6 +16,7 @@ const (
|
|||
NULL_OBJ = "NULL"
|
||||
RETURN_VALUE_OBJ = "RETURN"
|
||||
ERROR_OBJ = "ERROR"
|
||||
FUNCTION_OBJ = "FUNCTION"
|
||||
)
|
||||
|
||||
type Object interface {
|
||||
|
|
@ -69,3 +76,24 @@ func (e *Error) Type() ObjectType {
|
|||
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()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue