Class: Verneuil::Scope
- Inherits:
-
Object
- Object
- Verneuil::Scope
- Defined in:
- lib/verneuil/scope.rb
Overview
The execution scope for verneuil code. This maintains a link to the external context given when starting the process to be able to delegate method calls to user code.
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
In Ruby, we also call this the ‘self’.
Instance Method Summary collapse
-
#child(local_vars = {}) ⇒ Object
Returns a nested scope that has access to the current scope in a Ruby 1.9 fashion.
- #defined?(name) ⇒ Boolean
-
#initialize(context, local_vars = {}, parent = nil) ⇒ Scope
constructor
A new instance of Scope.
- #inspect ⇒ Object
- #lvar_exist?(name) ⇒ Boolean
- #lvar_exist_locally?(name) ⇒ Boolean
- #lvar_get(name) ⇒ Object
- #lvar_set(name, value) ⇒ Object
- #method_call(name, *args) ⇒ Object
Constructor Details
#initialize(context, local_vars = {}, parent = nil) ⇒ Scope
Returns a new instance of Scope.
10 11 12 13 14 |
# File 'lib/verneuil/scope.rb', line 10 def initialize(context, local_vars={}, parent=nil) @local_vars = local_vars @context = context @parent = parent end |
Instance Attribute Details
#context ⇒ Object (readonly)
In Ruby, we also call this the ‘self’.
8 9 10 |
# File 'lib/verneuil/scope.rb', line 8 def context @context end |
Instance Method Details
#child(local_vars = {}) ⇒ Object
Returns a nested scope that has access to the current scope in a Ruby 1.9 fashion.
47 48 49 |
# File 'lib/verneuil/scope.rb', line 47 def child(local_vars={}) self.class.new(context, local_vars, self) end |
#defined?(name) ⇒ Boolean
38 39 40 41 42 |
# File 'lib/verneuil/scope.rb', line 38 def defined?(name) return 'local-variable' if lvar_exist?(name) return 'method' if context.respond_to?(name) nil end |
#inspect ⇒ Object
55 56 57 58 |
# File 'lib/verneuil/scope.rb', line 55 def inspect "scope(#{@local_vars.inspect[2..-2]})" + (@parent ? "-> #{@parent.inspect}" : '') end |
#lvar_exist?(name) ⇒ Boolean
16 17 18 |
# File 'lib/verneuil/scope.rb', line 16 def lvar_exist?(name) lvar_exist_locally?(name) || @parent && @parent.lvar_exist?(name) end |
#lvar_exist_locally?(name) ⇒ Boolean
19 20 21 |
# File 'lib/verneuil/scope.rb', line 19 def lvar_exist_locally?(name) @local_vars.has_key?(name) end |
#lvar_get(name) ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/verneuil/scope.rb', line 22 def lvar_get(name) unless lvar_exist_locally? name return @parent.lvar_get(name) if @parent raise Verneuil::NameError, "No such local variable #{name.inspect}." end @local_vars[name] end |
#lvar_set(name, value) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/verneuil/scope.rb', line 31 def lvar_set(name, value) unless lvar_exist_locally? name return @parent.lvar_set(name, value) if @parent && @parent.lvar_exist?(name) end @local_vars[name] = value end |
#method_call(name, *args) ⇒ Object
51 52 53 |
# File 'lib/verneuil/scope.rb', line 51 def method_call(name, *args) context.send(name, *args) end |