diff options
Diffstat (limited to 'object/object.go')
| -rw-r--r-- | object/object.go | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/object/object.go b/object/object.go index 20deb0c..793ff6b 100644 --- a/object/object.go +++ b/object/object.go @@ -1,6 +1,11 @@ package object -import "fmt" +import ( + "bytes" + "fmt" + "mana/ast" + "strings" +) type ObjectType string @@ -9,7 +14,8 @@ const ( BOOLEAN_OBJ = "BOOLEAN" NULL_OBJ = "NULL" RETURN_VALUE_OBJ = "RETURN_VALUE" - ERROR_OBJ = "ERROR" + FUNCTION_OBJ = "FUNCTION" + ERROR_OBJ = "ERROR" ) type Object interface { @@ -32,7 +38,13 @@ type ReturnValue struct { } type Error struct { - Message string + Message string +} + +type Function struct { + Parameters []*ast.Identifier + Body *ast.BlockStatement + Env *Environment } func (i *Integer) Type() ObjectType { @@ -67,5 +79,28 @@ func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() } +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.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 (e *Error) Type() ObjectType { return ERROR_OBJ } -func (e *Error) Inspect() string { return "ERROR:" + e.Message } +func (e *Error) Inspect() string { return "ERROR:" + e.Message } |
