class NScript::Scope
Attributes
expressions[R]
function[R]
parent[R]
temp_variable[R]
variables[R]
Public Class Methods
new(parent, expressions, function)
click to toggle source
# 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
Public Instance Methods
assign(name, value, top=false)
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# File lib/nscript/scope.rb, line 48 def assignments?(body) !assigned_variables.empty? && body == @expressions end
check(name)
click to toggle source
# File lib/nscript/scope.rb, line 24 def check(name) return true if @variables[name.to_sym] !!(@parent && @parent.check(name)) end
compiled_assignments()
click to toggle source
# File lib/nscript/scope.rb, line 64 def compiled_assignments assigned_variables.map {|name, val| "#{name} = #{val}"}.join(', ') end
compiled_declarations()
click to toggle source
# File lib/nscript/scope.rb, line 60 def compiled_declarations declared_variables.join(', ') end
declarations?(body)
click to toggle source
# File lib/nscript/scope.rb, line 44 def declarations?(body) !declared_variables.empty? && body == @expressions end
declared_variables()
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# File lib/nscript/scope.rb, line 68 def inspect "<Scope:#{__id__} #{@variables.inspect}>" end
parameter(name)
click to toggle source
# File lib/nscript/scope.rb, line 20 def parameter(name) @variables[name.to_sym] = :param end
reset(name)
click to toggle source
# File lib/nscript/scope.rb, line 29 def reset(name) @variables[name.to_sym] = false end