Class: Rbsiev::Environment

Inherits:
Object
  • Object
show all
Includes:
Primitives
Defined in:
lib/rbsiev/environment.rb

Defined Under Namespace

Classes: Frame

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Primitives

#append, #car, #cdr, #cons, #display, #list, #list?, #number?, #pair?, #write

Methods included from Primitives::Arithmetic

#add, #div, #mod, #mul, #subtract, #zero?

Methods included from Primitives::Comparison

#ge?, #gt?, #le?, #lt?, #same_value?

Methods included from Primitives::EmptyList

#null?

Constructor Details

#initialize(base_env) ⇒ Environment

Returns a new instance of Environment.



42
43
44
45
# File 'lib/rbsiev/environment.rb', line 42

def initialize(base_env)
  @frame = nil
  @enclosing_environment = base_env
end

Instance Attribute Details

#enclosing_environmentObject (readonly)

Returns the value of attribute enclosing_environment.



47
48
49
# File 'lib/rbsiev/environment.rb', line 47

def enclosing_environment
  @enclosing_environment
end

Class Method Details

.make_frame(variables, values) ⇒ Object



12
13
14
# File 'lib/rbsiev/environment.rb', line 12

def self.make_frame(variables, values)
  Frame.new(variables, values)
end

.the_empty_environmentObject



8
9
10
# File 'lib/rbsiev/environment.rb', line 8

def self.the_empty_environment
  Environment.new(nil)
end

Instance Method Details

#define_variable(var, val) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/rbsiev/environment.rb', line 77

def define_variable(var, val)
  if first_frame.nil?
    set_frame!(Environment.make_frame([var], [val]))
  else
    first_frame.add_binding(var, val)
  end
  var
end

#extend(vars, vals) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/rbsiev/environment.rb', line 53

def extend(vars, vals)
  if vars.size == vals.size
    new_env = Environment.new(self)
    new_env.set_frame!(Environment.make_frame(vars, vals))
    new_env
  elsif var.size < vals.size
    raise Error, "Too many arguments supplied: %s => %s" % [vars, vals]
  else
    raise Error, "Too few arguments supplied: %s => %s" % [vars, vals]
  end
end

#first_frameObject



49
50
51
# File 'lib/rbsiev/environment.rb', line 49

def first_frame
  @frame
end

#lookup_variable_value(var) ⇒ Object

Raises:



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rbsiev/environment.rb', line 65

def lookup_variable_value(var)
  val = nil
  env = self
  while env
    val = env.first_frame && env.first_frame.lookup(var)
    break if val
    env = env.enclosing_environment
  end
  raise Error, "Unbound variable: got=%s" % var if val.nil?
  val
end

#set_variable_value(var, val) ⇒ Object

Raises:



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rbsiev/environment.rb', line 86

def set_variable_value(var, val)
  current = nil
  env = self
  while env
    current = env.first_frame && env.first_frame.lookup(var)
    if current
      env.first_frame.set(var, val)
      break
    else
      env = env.enclosing_environment
    end
  end
  raise Error, "Unbound variable: got=%s" % var if current.nil?
  val
end