module support
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package object
|
||||
|
||||
import "unicode"
|
||||
|
||||
func NewEnclosedEnvironment(outer *Environment) *Environment {
|
||||
env := NewEnvironment()
|
||||
env.outer = outer
|
||||
@@ -28,3 +30,18 @@ func (e *Environment) Set(name string, val Object) Object {
|
||||
e.store[name] = val
|
||||
return val
|
||||
}
|
||||
|
||||
// ExportedHash returns a new Hash with the names and values of every publicly
|
||||
// exported binding in the environment. That is every binding that starts with a
|
||||
// capital letter. This is used by the module import system to wrap up the
|
||||
// evaluated module into an object.
|
||||
func (e *Environment) ExportedHash() *Hash {
|
||||
pairs := make(map[HashKey]HashPair)
|
||||
for k, v := range e.store {
|
||||
if unicode.IsUpper(rune(k[0])) {
|
||||
s := &String{Value: k}
|
||||
pairs[s.HashKey()] = HashPair{Key: s, Value: v}
|
||||
}
|
||||
}
|
||||
return &Hash{Pairs: pairs}
|
||||
}
|
||||
|
||||
29
object/module.go
Normal file
29
object/module.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package object
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Module is the module type used to represent a collection of variabels.
|
||||
type Module struct {
|
||||
Name string
|
||||
Attrs Object
|
||||
}
|
||||
|
||||
func (m Module) String() string {
|
||||
return m.Inspect()
|
||||
}
|
||||
|
||||
func (m Module) Type() ObjectType {
|
||||
return MODULE_OBJ
|
||||
}
|
||||
|
||||
func (m Module) Bool() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (m Module) Inspect() string {
|
||||
return fmt.Sprintf("<module '%s'>", m.Name)
|
||||
}
|
||||
|
||||
func (m Module) Compare(other Object) int {
|
||||
return 1
|
||||
}
|
||||
@@ -18,6 +18,7 @@ const (
|
||||
HASH_OBJ = "hash"
|
||||
COMPILED_FUNCTION_OBJ = "COMPILED_FUNCTION"
|
||||
CLOSURE_OBJ = "closure"
|
||||
MODULE_OBJ = "module"
|
||||
)
|
||||
|
||||
// Comparable is the interface for comparing two Object and their underlying
|
||||
|
||||
Reference in New Issue
Block a user