Class: Fusion::Interpreter::Env
- Inherits:
-
Object
- Object
- Fusion::Interpreter::Env
- Defined in:
- lib/fusion/interpreter/env.rb
Defined Under Namespace
Classes: DuplicateBinding
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#bind(name, value, checked: true) ⇒ Object
Pattern bindings: - Shadowing a binding from a parent Env is always allowed.
- #child ⇒ Object
- #context(key) ⇒ Object
-
#initialize(parent = nil) ⇒ Env
constructor
A new instance of Env.
- #lookup(name) ⇒ Object
-
#root ⇒ Object
The topmost ancestor — the binding-free root a run is built on.
-
#set_context(key, value) ⇒ Object
Hidden interpreter context: -
:dir: the directory @-references resolve against (a path String).
Constructor Details
#initialize(parent = nil) ⇒ Env
Returns a new instance of Env.
24 25 26 27 28 |
# File 'lib/fusion/interpreter/env.rb', line 24 def initialize(parent = nil) @vars = {} # pattern bindings, keyed by identifier @context = {} # hidden interpreter context, keyed by symbol @parent = parent end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
22 23 24 |
# File 'lib/fusion/interpreter/env.rb', line 22 def parent @parent end |
Instance Method Details
#bind(name, value, checked: true) ⇒ Object
Pattern bindings:
- Shadowing a binding from a parent Env is always allowed.
- A duplicate identifier in the same Env is usually an error, but allowed on the REPL.
43 44 45 46 47 48 49 |
# File 'lib/fusion/interpreter/env.rb', line 43 def bind(name, value, checked: true) if checked && @vars.key?(name) raise DuplicateBinding, name end @vars[name] = value end |
#child ⇒ Object
30 31 32 |
# File 'lib/fusion/interpreter/env.rb', line 30 def child Env.new(self) end |
#context(key) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/fusion/interpreter/env.rb', line 78 def context(key) if @context.key?(key) @context[key] elsif @parent @parent.context(key) else :__unbound__ end end |
#lookup(name) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/fusion/interpreter/env.rb', line 51 def lookup(name) if @vars.key?(name) @vars[name] elsif @parent @parent.lookup(name) else :__unbound__ end end |
#root ⇒ Object
The topmost ancestor — the binding-free root a run is built on. The interpreter loads files relative to it, so they stay isolated.
36 37 38 |
# File 'lib/fusion/interpreter/env.rb', line 36 def root @parent ? @parent.root : self end |
#set_context(key, value) ⇒ Object
Hidden interpreter context:
:dir: the directory @-references resolve against (a path String).:file: the current file's absolute path, used for error origins (a String; absent for inline/REPL code, which reports as origin "code" with file ""). :self: the current top-level unit's own Thunk, used for recursion via a bare@.:jail: the run's jail root confining @-resolution (an absolute path String, or nil for unconfined). Set once on the root and, unlike the others, never overridden by a descendant.:call_site: the innermost user-codefilea stdlib body borrows for its errors (a String). Set on a stdlib function's clause env in Interpreter#apply; user/inline code derives its own and omits it.
73 74 75 76 |
# File 'lib/fusion/interpreter/env.rb', line 73 def set_context(key, value) @context[key] = value self end |