Class: NScript::Scope
- Inherits:
-
Object
- Object
- NScript::Scope
- Defined in:
- lib/nscript/scope.rb
Instance Attribute Summary collapse
-
#expressions ⇒ Object
readonly
Returns the value of attribute expressions.
-
#function ⇒ Object
readonly
Returns the value of attribute function.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#temp_variable ⇒ Object
readonly
Returns the value of attribute temp_variable.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
- #assign(name, value, top = false) ⇒ Object
- #assigned_variables ⇒ Object
- #assignments?(body) ⇒ Boolean
- #check(name) ⇒ Object
- #compiled_assignments ⇒ Object
- #compiled_declarations ⇒ Object
- #declarations?(body) ⇒ Boolean
- #declared_variables ⇒ Object
- #find(name, remote = false) ⇒ Object
- #free_variable ⇒ Object
-
#initialize(parent, expressions, function) ⇒ Scope
constructor
A new instance of Scope.
- #inspect ⇒ Object
- #parameter(name) ⇒ Object
- #reset(name) ⇒ Object
Constructor Details
#initialize(parent, expressions, function) ⇒ Scope
Returns a new instance of Scope.
7 8 9 10 11 |
# File 'lib/nscript/scope.rb', line 7 def initialize(parent, expressions, function) @parent, @expressions, @function = parent, expressions, function @variables = {} @temp_variable = @parent ? @parent.temp_variable.dup : '__a' end |
Instance Attribute Details
#expressions ⇒ Object (readonly)
Returns the value of attribute expressions.
5 6 7 |
# File 'lib/nscript/scope.rb', line 5 def expressions @expressions end |
#function ⇒ Object (readonly)
Returns the value of attribute function.
5 6 7 |
# File 'lib/nscript/scope.rb', line 5 def function @function end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
5 6 7 |
# File 'lib/nscript/scope.rb', line 5 def parent @parent end |
#temp_variable ⇒ Object (readonly)
Returns the value of attribute temp_variable.
5 6 7 |
# File 'lib/nscript/scope.rb', line 5 def temp_variable @temp_variable end |
#variables ⇒ Object (readonly)
Returns the value of attribute variables.
5 6 7 |
# File 'lib/nscript/scope.rb', line 5 def variables @variables end |
Instance Method Details
#assign(name, value, top = false) ⇒ Object
39 40 41 42 |
# File 'lib/nscript/scope.rb', line 39 def assign(name, value, top=false) return @parent.assign(name, value, top) if top && @parent @variables[name.to_sym] = Value.new(value) end |
#assigned_variables ⇒ Object
56 57 58 |
# File 'lib/nscript/scope.rb', line 56 def assigned_variables @variables.select {|k, v| v.is_a?(Value) }.sort_by {|pair| pair[0].to_s } end |
#assignments?(body) ⇒ Boolean
48 49 50 |
# File 'lib/nscript/scope.rb', line 48 def assignments?(body) !assigned_variables.empty? && body == @expressions end |
#check(name) ⇒ Object
24 25 26 27 |
# File 'lib/nscript/scope.rb', line 24 def check(name) return true if @variables[name.to_sym] !!(@parent && @parent.check(name)) end |
#compiled_assignments ⇒ Object
64 65 66 |
# File 'lib/nscript/scope.rb', line 64 def compiled_assignments assigned_variables.map {|name, val| "#{name} = #{val}"}.join(', ') end |
#compiled_declarations ⇒ Object
60 61 62 |
# File 'lib/nscript/scope.rb', line 60 def compiled_declarations declared_variables.join(', ') end |
#declarations?(body) ⇒ Boolean
44 45 46 |
# File 'lib/nscript/scope.rb', line 44 def declarations?(body) !declared_variables.empty? && body == @expressions end |
#declared_variables ⇒ Object
52 53 54 |
# File 'lib/nscript/scope.rb', line 52 def declared_variables @variables.select {|k, v| v == :var }.map {|pair| pair[0].to_s }.sort end |
#find(name, remote = false) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/nscript/scope.rb', line 13 def find(name, remote=false) found = check(name) return found if found || remote @variables[name.to_sym] = :var found end |
#free_variable ⇒ Object
33 34 35 36 37 |
# File 'lib/nscript/scope.rb', line 33 def free_variable @temp_variable.succ! while check(@temp_variable) @variables[@temp_variable.to_sym] = :var Value.new(@temp_variable.dup) end |
#inspect ⇒ Object
68 69 70 |
# File 'lib/nscript/scope.rb', line 68 def inspect "<Scope:#{__id__} #{@variables.inspect}>" end |
#parameter(name) ⇒ Object
20 21 22 |
# File 'lib/nscript/scope.rb', line 20 def parameter(name) @variables[name.to_sym] = :param end |
#reset(name) ⇒ Object
29 30 31 |
# File 'lib/nscript/scope.rb', line 29 def reset(name) @variables[name.to_sym] = false end |