Class: CodeTools::AST::EvalExpression

Inherits:
Container show all
Defined in:
lib/rubinius/code/ast/definitions.rb

Instance Attribute Summary

Attributes inherited from Container

#file, #name, #pre_exe, #variable_scope

Attributes inherited from ClosedScope

#body

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Container

#container_bytecode, #pop_state, #to_sexp

Methods inherited from ClosedScope

#attach_and_call, #module?, #nest_scope, #new_nested_local, #to_sexp

Methods inherited from Node

#ascii_graph, #attributes, #children, #defined, match_arguments?, match_send?, #new_block_generator, #new_generator, #node_name, #or_bytecode, #pos, #set_child, #to_sexp, #transform, transform, transform_comment, transform_kind, transform_kind=, transform_name, #value_defined, #visit, #walk

Constructor Details

#initialize(body) ⇒ EvalExpression

Returns a new instance of EvalExpression.



1151
1152
1153
1154
# File 'lib/rubinius/code/ast/definitions.rb', line 1151

def initialize(body)
  super body
  @name = :__eval_script__
end

Instance Method Details

#assign_local_reference(var) ⇒ Object



1194
1195
1196
1197
1198
1199
1200
1201
# File 'lib/rubinius/code/ast/definitions.rb', line 1194

def assign_local_reference(var)
  unless reference = search_local(var.name)
    variable = new_local var.name
    reference = variable.reference
  end

  var.variable = reference
end

#bytecode(g) ⇒ Object



1208
1209
1210
1211
1212
1213
1214
1215
# File 'lib/rubinius/code/ast/definitions.rb', line 1208

def bytecode(g)
  super(g)

  container_bytecode(g) do
    @body.bytecode(g)
    g.ret
  end
end

#new_local(name) ⇒ Object



1190
1191
1192
# File 'lib/rubinius/code/ast/definitions.rb', line 1190

def new_local(name)
  variables[name] ||= Compiler::EvalLocalVariable.new name
end

#push_state(g) ⇒ Object



1203
1204
1205
1206
# File 'lib/rubinius/code/ast/definitions.rb', line 1203

def push_state(g)
  g.push_state self
  g.state.push_eval self
end

#search_local(name) ⇒ Object

Returns a cached reference to a variable or searches all surrounding scopes for a variable. If no variable is found, it returns nil and a nested scope will create the variable in itself.



1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
# File 'lib/rubinius/code/ast/definitions.rb', line 1179

def search_local(name)
  if variable = variables[name]
    return variable.nested_reference
  end

  if variable = search_scopes(name)
    variables[name] = variable
    return variable.nested_reference
  end
end

#search_scopes(name) ⇒ Object



1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
# File 'lib/rubinius/code/ast/definitions.rb', line 1160

def search_scopes(name)
  depth = 1
  scope = @variable_scope
  while scope
    if !scope.method.for_eval? and slot = scope.method.local_slot(name)
      return Compiler::NestedLocalVariable.new(depth, slot)
    elsif scope.eval_local_defined?(name, false)
      return Compiler::EvalLocalVariable.new(name)
    end

    depth += 1
    scope = scope.parent
  end
end

#sexp_nameObject



1217
1218
1219
# File 'lib/rubinius/code/ast/definitions.rb', line 1217

def sexp_name
  :eval
end

#should_cache?Boolean

Returns:

  • (Boolean)


1156
1157
1158
# File 'lib/rubinius/code/ast/definitions.rb', line 1156

def should_cache?
  !@body.kind_of?(AST::ClosedScope)
end