2010-02-08 21:10:48 -05:00
|
|
|
# Scope objects form a tree corresponding to the shape of the function
|
2010-02-07 15:45:05 -05:00
|
|
|
# definitions present in the script. They provide lexical scope, to determine
|
2010-02-04 13:36:33 -05:00
|
|
|
# whether a variable has been seen before or if it needs to be declared.
|
2010-02-08 21:10:48 -05:00
|
|
|
#
|
|
|
|
# Initialize a scope with its parent, for lookups up the chain,
|
|
|
|
# as well as the Expressions body where it should declare its variables,
|
|
|
|
# and the function that it wraps.
|
|
|
|
Scope: exports.Scope: (parent, expressions, method) ->
|
|
|
|
@parent: parent
|
|
|
|
@expressions: expressions
|
|
|
|
@method: method
|
|
|
|
@variables: {}
|
|
|
|
@temp_variable: if @parent then @parent.temp_variable else '__a'
|
|
|
|
this
|
2010-02-04 13:36:33 -05:00
|
|
|
|
|
|
|
# Look up a variable in lexical scope, or declare it if not found.
|
2010-02-08 21:10:48 -05:00
|
|
|
Scope::find: (name, remote) ->
|
|
|
|
found: @check name
|
|
|
|
return found if found or remote
|
|
|
|
@variables[name]: 'var'
|
2010-02-04 13:36:33 -05:00
|
|
|
found
|
|
|
|
|
|
|
|
# Define a local variable as originating from a parameter in current scope
|
|
|
|
# -- no var required.
|
2010-02-08 21:10:48 -05:00
|
|
|
Scope::parameter: (name) ->
|
|
|
|
@variables[name]: 'param'
|
2010-02-04 13:36:33 -05:00
|
|
|
|
|
|
|
# Just check to see if a variable has already been declared.
|
2010-02-08 21:10:48 -05:00
|
|
|
Scope::check: (name) ->
|
|
|
|
return true if @variables[name]
|
|
|
|
!!(@parent and @parent.check(name))
|
2010-02-04 13:36:33 -05:00
|
|
|
|
|
|
|
# You can reset a found variable on the immediate scope.
|
2010-02-08 21:10:48 -05:00
|
|
|
Scope::reset: (name) ->
|
|
|
|
delete @variables[name]
|
|
|
|
|
|
|
|
# Find an available, short, name for a compiler-generated variable.
|
|
|
|
Scope::free_variable: ->
|
2010-02-08 23:16:29 -05:00
|
|
|
(@temp_variable: succ(@temp_variable)) while @check @temp_variable
|
2010-02-08 21:10:48 -05:00
|
|
|
@variables[@temp_variable]: 'var'
|
|
|
|
@temp_variable
|
|
|
|
|
|
|
|
# Ensure that an assignment is made at the top of scope (or top-level
|
|
|
|
# scope, if requested).
|
|
|
|
Scope::assign: (name, value, top_level) ->
|
|
|
|
return @parent.assign(name, value, top_level) if top_level and @parent
|
|
|
|
@variables[name]: {value: value, assigned: true}
|
|
|
|
|
|
|
|
# Does this scope reference any variables that need to be declared in the
|
|
|
|
# given function body?
|
|
|
|
Scope::has_declarations: (body) ->
|
|
|
|
body is @expressions and @declared_variables().length
|
|
|
|
|
|
|
|
# Does this scope reference any assignments that need to be declared at the
|
|
|
|
# top of the given function body?
|
|
|
|
Scope::has_assignments: (body) ->
|
|
|
|
body is @expressions and @assigned_variables().length
|
|
|
|
|
|
|
|
# Return the list of variables first declared in current scope.
|
|
|
|
Scope::declared_variables: ->
|
|
|
|
(key for key, val of @variables when val is 'var').sort()
|
|
|
|
|
|
|
|
# Return the list of variables that are supposed to be assigned at the top
|
|
|
|
# of scope.
|
|
|
|
Scope::assigned_variables: ->
|
|
|
|
([key, val.value] for key, val of @variables when val.assigned).sort()
|
|
|
|
|
|
|
|
Scope::compiled_declarations: ->
|
|
|
|
@declared_variables().join(', ')
|
|
|
|
|
|
|
|
Scope::compiled_assignments: ->
|
|
|
|
(t[0] + ' = ' + t[1] for t in @assigned_variables()).join(', ')
|
|
|
|
|
|
|
|
|
|
|
|
# Helper functions:
|
|
|
|
|
|
|
|
# The next character alphabetically, to produce the following string.
|
|
|
|
succ: (str) ->
|
|
|
|
str.slice(0, str.length - 1) +
|
|
|
|
String.fromCharCode(str.charCodeAt(str.length - 1) + 1)
|