2009-12-17 22:54:24 -05:00
|
|
|
module CoffeeScript
|
2009-12-17 09:37:42 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Scope objects form a tree corresponding to the shape of the function
|
|
|
|
# definitions present in the script. They provide lexical scope, to determine
|
|
|
|
# whether a variable has been seen before or if it needs to be declared.
|
|
|
|
class Scope
|
2009-12-17 09:37:42 -05:00
|
|
|
|
2010-01-16 15:44:07 -05:00
|
|
|
attr_reader :parent, :expressions, :function, :variables, :temp_variable
|
2009-12-17 09:37:42 -05:00
|
|
|
|
2010-01-03 13:59:17 -05:00
|
|
|
# Initialize a scope with its parent, for lookups up the chain,
|
2010-01-16 15:44:07 -05:00
|
|
|
# as well as the Expressions body where it should declare its variables,
|
|
|
|
# and the function that it wraps.
|
|
|
|
def initialize(parent, expressions, function)
|
|
|
|
@parent, @expressions, @function = parent, expressions, function
|
2009-12-17 22:54:24 -05:00
|
|
|
@variables = {}
|
2010-01-04 00:16:38 -05:00
|
|
|
@temp_variable = @parent ? @parent.temp_variable.dup : '__a'
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-17 09:37:42 -05:00
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Look up a variable in lexical scope, or declare it if not found.
|
|
|
|
def find(name, remote=false)
|
2010-01-04 00:16:38 -05:00
|
|
|
found = check(name)
|
2009-12-17 22:54:24 -05:00
|
|
|
return found if found || remote
|
2009-12-26 01:57:33 -05:00
|
|
|
@variables[name.to_sym] = :var
|
2009-12-17 22:54:24 -05:00
|
|
|
found
|
|
|
|
end
|
|
|
|
|
2009-12-26 01:57:33 -05:00
|
|
|
# Define a local variable as originating from a parameter in current scope
|
|
|
|
# -- no var required.
|
|
|
|
def parameter(name)
|
|
|
|
@variables[name.to_sym] = :param
|
|
|
|
end
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Just check to see if a variable has already been declared.
|
2010-01-04 00:16:38 -05:00
|
|
|
def check(name)
|
2009-12-19 00:33:34 -05:00
|
|
|
return true if @variables[name.to_sym]
|
2010-01-04 00:16:38 -05:00
|
|
|
!!(@parent && @parent.check(name))
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
|
|
|
|
2009-12-19 00:33:34 -05:00
|
|
|
# You can reset a found variable on the immediate scope.
|
|
|
|
def reset(name)
|
|
|
|
@variables[name.to_sym] = false
|
|
|
|
end
|
|
|
|
|
2009-12-17 22:54:24 -05:00
|
|
|
# Find an available, short, name for a compiler-generated variable.
|
|
|
|
def free_variable
|
|
|
|
@temp_variable.succ! while check(@temp_variable)
|
2009-12-26 01:57:33 -05:00
|
|
|
@variables[@temp_variable.to_sym] = :var
|
2010-01-16 12:10:31 -05:00
|
|
|
Value.new(@temp_variable.dup)
|
2009-12-17 22:54:24 -05:00
|
|
|
end
|
2009-12-17 09:37:42 -05:00
|
|
|
|
2010-01-13 20:59:57 -05:00
|
|
|
# Ensure that an assignment is made at the top of scope (or top-level
|
|
|
|
# scope, if requested).
|
|
|
|
def assign(name, value, top=false)
|
|
|
|
return @parent.assign(name, value, top) if top && @parent
|
2010-01-12 17:35:37 -05:00
|
|
|
@variables[name.to_sym] = Value.new(value)
|
|
|
|
end
|
|
|
|
|
2010-02-08 21:10:48 -05:00
|
|
|
# Does this scope reference any variables that need to be declared in the
|
|
|
|
# given function body?
|
2010-01-03 13:59:17 -05:00
|
|
|
def declarations?(body)
|
|
|
|
!declared_variables.empty? && body == @expressions
|
2009-12-26 01:57:33 -05:00
|
|
|
end
|
|
|
|
|
2010-02-08 21:10:48 -05:00
|
|
|
# Does this scope reference any assignments that need to be declared at the
|
|
|
|
# top of the given function body?
|
2010-01-12 17:35:37 -05:00
|
|
|
def assignments?(body)
|
|
|
|
!assigned_variables.empty? && body == @expressions
|
|
|
|
end
|
|
|
|
|
2009-12-26 01:57:33 -05:00
|
|
|
# Return the list of variables first declared in current scope.
|
|
|
|
def declared_variables
|
|
|
|
@variables.select {|k, v| v == :var }.map {|pair| pair[0].to_s }.sort
|
|
|
|
end
|
|
|
|
|
2010-01-12 17:35:37 -05:00
|
|
|
# Return the list of variables that are supposed to be assigned at the top
|
|
|
|
# of scope.
|
|
|
|
def assigned_variables
|
|
|
|
@variables.select {|k, v| v.is_a?(Value) }.sort_by {|pair| pair[0].to_s }
|
|
|
|
end
|
|
|
|
|
|
|
|
def compiled_declarations
|
|
|
|
declared_variables.join(', ')
|
|
|
|
end
|
|
|
|
|
|
|
|
def compiled_assignments
|
|
|
|
assigned_variables.map {|name, val| "#{name} = #{val}"}.join(', ')
|
|
|
|
end
|
|
|
|
|
2010-01-04 09:43:50 -05:00
|
|
|
def inspect
|
|
|
|
"<Scope:#{__id__} #{@variables.inspect}>"
|
|
|
|
end
|
|
|
|
|
2009-12-17 09:37:42 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|