Class: Fusion::Interpreter::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/fusion/interpreter/env.rb

Defined Under Namespace

Classes: DuplicateBinding

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Env

Returns a new instance of Env.



24
25
26
27
# File 'lib/fusion/interpreter/env.rb', line 24

def initialize(parent = nil)
  @vars = {}
  @parent = parent
end

Instance Attribute Details

#parentObject (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) ⇒ Object

Insert a pattern binding, rejecting a duplicate binder. Only this env’s own scope is checked: a binder may shadow a name from a parent env, but must be unique within one pattern/clause.

Raises:



38
39
40
41
42
# File 'lib/fusion/interpreter/env.rb', line 38

def bind(name, value)
  raise DuplicateBinding, name if @vars.key?(name)

  @vars[name] = value
end

#childObject



54
55
56
# File 'lib/fusion/interpreter/env.rb', line 54

def child
  Env.new(self)
end

#define(name, value) ⇒ Object

Unchecked insert, for interpreter-internal names (__dir__, built-ins, …).



30
31
32
33
# File 'lib/fusion/interpreter/env.rb', line 30

def define(name, value)
  @vars[name] = value
  self
end

#lookup(name) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/fusion/interpreter/env.rb', line 44

def lookup(name)
  if @vars.key?(name)
    @vars[name]
  elsif @parent
    @parent.lookup(name)
  else
    :__unbound__
  end
end