aboutsummaryrefslogtreecommitdiff
path: root/object
diff options
context:
space:
mode:
Diffstat (limited to 'object')
-rw-r--r--object/environment.go14
-rw-r--r--object/object.go43
2 files changed, 46 insertions, 11 deletions
diff --git a/object/environment.go b/object/environment.go
index 4b37749..bd89056 100644
--- a/object/environment.go
+++ b/object/environment.go
@@ -1,20 +1,20 @@
package object
func NewEnvironment() *Environment {
- s := make(map[string]Object)
- return &Environment{store: s}
+ s := make(map[string]Object)
+ return &Environment{store: s}
}
type Environment struct {
- store map[string]Object
+ store map[string]Object
}
func (e *Environment) Get(name string) (Object, bool) {
- obj, ok := e.store[name]
- return obj, ok
+ obj, ok := e.store[name]
+ return obj, ok
}
func (e *Environment) Set(name string, val Object) Object {
- e.store[name] = val
- return val
+ e.store[name] = val
+ return val
}
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 }