Skip to content

Commit 3069378

Browse files
committed
Begin refacting "Item" into a Data struct
Data struct contains an embeded Type tag. Eventually all types will be contained within a Data. The first type to be contained with Data is the Symbol type, which was previously (and incorrectly) called Atom.
1 parent c7fb3bf commit 3069378

File tree

5 files changed

+327
-186
lines changed

5 files changed

+327
-186
lines changed

env.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type Binding map[*Data]Item
6+
type Env struct {
7+
vars Binding
8+
outer *Env
9+
}
10+
11+
func NewEnv(outer *Env) *Env {
12+
return &Env{
13+
vars: make(Binding),
14+
outer: outer,
15+
}
16+
17+
}
18+
19+
func (e *Env) BindName(name string, i Item) {
20+
sym := internSymbol(name)
21+
e.vars[sym] = i
22+
}
23+
24+
func Symbolp(sym *Data) bool {
25+
return sym.Type == SymbolType
26+
}
27+
28+
func (e *Env) Bind(sym *Data, i Item) {
29+
if !Symbolp(sym) {
30+
panic(fmt.Errorf("sym is not a Symbol: %v", sym))
31+
}
32+
e.vars[sym] = i
33+
}
34+
35+
func (e *Env) Var(sym *Data) (Item, error) {
36+
if !Symbolp(sym) {
37+
panic(fmt.Errorf("sym is not a Symbol: %v", sym))
38+
}
39+
v, ok := e.vars[sym]
40+
if !ok {
41+
return nil, fmt.Errorf("Undefined symbol: %v", sym)
42+
}
43+
return v, nil
44+
}
45+
46+
func (env *Env) Find(sym *Data) *Env {
47+
if !Symbolp(sym) {
48+
panic(fmt.Errorf("sym is not a Symbol: %v", sym))
49+
}
50+
if _, ok := env.vars[sym]; ok {
51+
return env
52+
} else if env.outer != nil {
53+
return env.outer.Find(sym)
54+
} else {
55+
return env
56+
}
57+
}
58+
59+
func (env *Env) FindVar(sym *Data) (Item, error) {
60+
return env.Find(sym).Var(sym)
61+
}

0 commit comments

Comments
 (0)