parent:
820eaee1ebb5edff55abac8b7a41579acbc49137
nmyk <nick@nmyk.io>
2026-11-14T11:36:36-05:00
add environments as an immutable linked list of bindings
diff --git a/eval/env.go b/eval/env.go
new file mode 100644
index 0000000000000000000000000000000000000000..a4faacac8fa8ecc280104fc1a5f3ad470c003da4
--- /dev/null
+++ b/eval/env.go
@@ -0,0 +1,29 @@
+package eval
+
+import "government/sexpr"
+
+// Env is a lexical environment: an immutable linked list of bindings,
+// innermost first. The nil *Env is the empty environment.
+//
+// Extending returns a new head that points at the old list, so a closure
+// can capture an environment just by keeping the pointer: later extensions
+// by anyone else are invisible to it. Lookup walks the chain, so the first
+// match is the innermost binding, which is what makes shadowing work.
+type Env struct {
+ name sexpr.Symbol
+ value sexpr.Value
+ parent *Env
+}
+
+func (e *Env) Extend(name sexpr.Symbol, value sexpr.Value) *Env {
+ return &Env{name: name, value: value, parent: e}
+}
+
+func (e *Env) Lookup(name sexpr.Symbol) (sexpr.Value, bool) {
+ for b := e; b != nil; b = b.parent {
+ if b.name == name {
+ return b.value, true
+ }
+ }
+ return nil, false
+}
diff --git a/eval/env_test.go b/eval/env_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..b0002a57611a5b73fd2d2a90990b869357b42853
--- /dev/null
+++ b/eval/env_test.go
@@ -0,0 +1,58 @@
+package eval
+
+import (
+ "testing"
+
+ "government/sexpr"
+)
+
+func mustLookup(t *testing.T, e *Env, name sexpr.Symbol, want sexpr.Value) {
+ t.Helper()
+ got, ok := e.Lookup(name)
+ if !ok {
+ t.Fatalf("Lookup(%s): unbound, want %s", name, sexpr.Print(want))
+ }
+ if got != want {
+ t.Errorf("Lookup(%s) = %s, want %s", name, sexpr.Print(got), sexpr.Print(want))
+ }
+}
+
+func mustBeUnbound(t *testing.T, e *Env, name sexpr.Symbol) {
+ t.Helper()
+ if got, ok := e.Lookup(name); ok {
+ t.Errorf("Lookup(%s) = %s, want unbound", name, sexpr.Print(got))
+ }
+}
+
+func TestEmptyEnv(t *testing.T) {
+ var empty *Env
+ mustBeUnbound(t, empty, "x")
+}
+
+func TestLookup(t *testing.T) {
+ e := (*Env)(nil).Extend("x", sexpr.Int(1)).Extend("y", sexpr.Int(2))
+ mustLookup(t, e, "x", sexpr.Int(1))
+ mustLookup(t, e, "y", sexpr.Int(2))
+ mustBeUnbound(t, e, "z")
+}
+
+func TestShadowing(t *testing.T) {
+ outer := (*Env)(nil).Extend("x", sexpr.Int(1))
+ inner := outer.Extend("x", sexpr.Int(2))
+ mustLookup(t, inner, "x", sexpr.Int(2))
+ mustLookup(t, outer, "x", sexpr.Int(1))
+}
+
+// What a closure relies on: an environment never changes after it is
+// captured, no matter how it is extended later.
+func TestExtendNeverAffectsParent(t *testing.T) {
+ parent := (*Env)(nil).Extend("x", sexpr.Int(1))
+ a := parent.Extend("y", sexpr.String("a"))
+ b := parent.Extend("y", sexpr.String("b"))
+
+ mustBeUnbound(t, parent, "y")
+ mustLookup(t, a, "y", sexpr.String("a"))
+ mustLookup(t, b, "y", sexpr.String("b"))
+ mustLookup(t, a, "x", sexpr.Int(1))
+ mustLookup(t, b, "x", sexpr.Int(1))
+}
diff --git a/eval/function_test.go b/eval/function_test.go
new file mode 100644
index 0000000000000000000000000000000000000000..983ba5de50b5f992fd1b1158061715741e7981ac
--- /dev/null
+++ b/eval/function_test.go
@@ -0,0 +1,26 @@
+package eval
+
+import (
+ "testing"
+
+ "government/sexpr"
+)
+
+// Closures and builtins live in this package, so it must be able to define
+// sexpr.Function implementations.
+type stubFunc struct {
+ sexpr.FunctionBase
+ name string
+}
+
+func (f stubFunc) Name() string { return f.name }
+
+func TestEvalCanDefineFunctions(t *testing.T) {
+ var v sexpr.Value = stubFunc{name: "+"}
+ if _, ok := v.(sexpr.Function); !ok {
+ t.Fatal("stubFunc is not a sexpr.Function")
+ }
+ if got := sexpr.Print(v); got != "#<builtin +>" {
+ t.Errorf("Print = %s, want #<builtin +>", got)
+ }
+}
diff --git a/sexpr/print_test.go b/sexpr/print_test.go
index 8d503f668b027534a2e10eee07dca695db5f631c..59e409506d9f111a01ee7b27b223d1a04410eb38 100644
--- a/sexpr/print_test.go
+++ b/sexpr/print_test.go
@@ -5,9 +5,11 @@ "math"
"testing"
)
-type testFunc struct{ name string }
+type testFunc struct {
+ FunctionBase
+ name string
+}
-func (testFunc) isValue() {}
func (f testFunc) Name() string { return f.name }
func TestPrint(t *testing.T) {
@@ -53,8 +55,8 @@ {"improper list", ListWithTail(Int(3), Int(1), Int(2)), "(1 2 . 3)"},
{"pair of lists", Cons(List(Int(1)), List(Int(2))), "((1) 2)"},
{"dotted to symbol", Cons(Symbol("a"), Symbol("b")), "(a . b)"},
{"quote form", List(Symbol("quote"), Symbol("x")), "(quote x)"},
- {"builtin", testFunc{"+"}, "#<builtin +>"},
- {"closure", testFunc{""}, "#<lambda>"},
+ {"builtin", testFunc{name: "+"}, "#<builtin +>"},
+ {"closure", testFunc{}, "#<lambda>"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
diff --git a/sexpr/value.go b/sexpr/value.go
index 56a5907da89bea768340526d1c159a1dc1ce4b0d..d5b4d7de114db71643f85a44541c1ffc03c00dde 100644
--- a/sexpr/value.go
+++ b/sexpr/value.go
@@ -1,7 +1,7 @@
package sexpr
-// Value is any GOVERNMENT value. The interface is sealed by isValue, except
-// for Function, which package eval implements.
+// Value is any GOVERNMENT value. The set of value kinds is closed: only
+// this package defines them, except functions (see FunctionBase).
type Value interface {
isValue()
}
@@ -37,12 +37,19 @@
// Nil is the empty list, ().
var Nil = Empty{}
-// Function is implemented by eval's closures and builtins.
+// Function is a callable value: a closure or a builtin, both defined in
+// package eval.
type Function interface {
Value
// Name is the builtin's name, or "" for a closure.
Name() string
}
+
+// FunctionBase is embedded by eval's closures and builtins to make them
+// Values.
+type FunctionBase struct{}
+
+func (FunctionBase) isValue() {}
func (Int) isValue() {}
func (Float) isValue() {}