crosslang/golang-lean/GolangLean/Env.lean
Maximus Gorog fd3d42ae33 Add 'golang-lean/' from commit 'f5f17019224c6a6c319387214ceb8e29d09251c6'
git-subtree-dir: golang-lean
git-subtree-mainline: 6487c7046f
git-subtree-split: f5f1701922
2026-05-12 02:59:14 -06:00

37 lines
1.1 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import GolangLean.Value
namespace GolangLean
/-! # Environment / heap.
Distinct from octive-lean's flat `Env`: Go has lexically-nested scopes,
addressable variables (so a `&x` produces a stable heap index), and a
package-level namespace. This is a placeholder shape; flesh out as needed. -/
abbrev Frame := List (String × Value)
structure Env where
frames : List Frame -- top of list = innermost scope
package : List (String × Value) -- package-level decls
heap : Array Value -- addressable cells (`&x` returns an index)
builtins : List (String × (Array Value → IO (Except String Value)))
deriving Inhabited
namespace Env
def empty : Env :=
{ frames := [[]], package := [], heap := #[], builtins := [] }
/-- Look up a name: innermost frame first, then package, then builtins (later). -/
def lookup (env : Env) (name : String) : Option Value :=
let rec go : List Frame → Option Value
| [] => env.package.lookup name
| f :: fs =>
match f.lookup name with
| some v => some v
| none => go fs
go env.frames
end Env
end GolangLean