Class: StyleScript::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/style_script/scope.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, expressions, function) ⇒ Scope

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.



13
14
15
16
17
# File 'lib/style_script/scope.rb', line 13

def initialize(parent, expressions, function)
  @parent, @expressions, @function = parent, expressions, function
  @variables = {}
  @temp_variable = @parent ? @parent.temp_variable.dup : '__a'
end

Instance Attribute Details

#expressionsObject (readonly)

Returns the value of attribute expressions.



8
9
10
# File 'lib/style_script/scope.rb', line 8

def expressions
  @expressions
end

#functionObject (readonly)

Returns the value of attribute function.



8
9
10
# File 'lib/style_script/scope.rb', line 8

def function
  @function
end

#parentObject (readonly)

Returns the value of attribute parent.



8
9
10
# File 'lib/style_script/scope.rb', line 8

def parent
  @parent
end

#temp_variableObject (readonly)

Returns the value of attribute temp_variable.



8
9
10
# File 'lib/style_script/scope.rb', line 8

def temp_variable
  @temp_variable
end

#variablesObject (readonly)

Returns the value of attribute variables.



8
9
10
# File 'lib/style_script/scope.rb', line 8

def variables
  @variables
end

Instance Method Details

#assign(name, value, top = false) ⇒ Object

Ensure that an assignment is made at the top of scope (or top-level scope, if requested).



53
54
55
56
# File 'lib/style_script/scope.rb', line 53

def assign(name, value, top=false)
  return @parent.assign(name, value, top) if top && @parent
  @variables[name.to_sym] = Value.new(value)
end

#assigned_variablesObject

Return the list of variables that are supposed to be assigned at the top of scope.



77
78
79
# File 'lib/style_script/scope.rb', line 77

def assigned_variables
  @variables.select {|k, v| v.is_a?(Value) }.sort_by {|pair| pair[0].to_s }
end

#assignments?(body) ⇒ Boolean

Does this scope reference any assignments that need to be declared at the top of the given function body?

Returns:

  • (Boolean)


66
67
68
# File 'lib/style_script/scope.rb', line 66

def assignments?(body)
  !assigned_variables.empty? && body == @expressions
end

#check(name) ⇒ Object

Just check to see if a variable has already been declared.



34
35
36
37
# File 'lib/style_script/scope.rb', line 34

def check(name)
  return true if @variables[name.to_sym]
  !!(@parent && @parent.check(name))
end

#compiled_assignmentsObject



85
86
87
# File 'lib/style_script/scope.rb', line 85

def compiled_assignments
  assigned_variables.map {|name, val| "#{name} = #{val}"}.join(', ')
end

#compiled_declarationsObject



81
82
83
# File 'lib/style_script/scope.rb', line 81

def compiled_declarations
  declared_variables.join(', ')
end

#declarations?(body) ⇒ Boolean

Does this scope reference any variables that need to be declared in the given function body?

Returns:

  • (Boolean)


60
61
62
# File 'lib/style_script/scope.rb', line 60

def declarations?(body)
  !declared_variables.empty? && body == @expressions
end

#declared_variablesObject

Return the list of variables first declared in current scope.



71
72
73
# File 'lib/style_script/scope.rb', line 71

def declared_variables
  @variables.select {|k, v| v == :var }.map {|pair| pair[0].to_s }.sort
end

#find(name, remote = false) ⇒ Object

Look up a variable in lexical scope, or declare it if not found.



20
21
22
23
24
25
# File 'lib/style_script/scope.rb', line 20

def find(name, remote=false)
  found = check(name)
  return found if found || remote
  @variables[name.to_sym] = :var
  found
end

#free_variableObject

Find an available, short, name for a compiler-generated variable.



45
46
47
48
49
# File 'lib/style_script/scope.rb', line 45

def free_variable
  @temp_variable.succ! while check(@temp_variable)
  @variables[@temp_variable.to_sym] = :var
  Value.new(@temp_variable.dup)
end

#inspectObject



89
90
91
# File 'lib/style_script/scope.rb', line 89

def inspect
  "<Scope:#{__id__} #{@variables.inspect}>"
end

#parameter(name) ⇒ Object

Define a local variable as originating from a parameter in current scope – no var required.



29
30
31
# File 'lib/style_script/scope.rb', line 29

def parameter(name)
  @variables[name.to_sym] = :param
end

#reset(name) ⇒ Object

You can reset a found variable on the immediate scope.



40
41
42
# File 'lib/style_script/scope.rb', line 40

def reset(name)
  @variables[name.to_sym] = false
end