class CodeTools::AST::State

In a perfect world, each AST node would fully encapsulate its state. But in the real world, some state exists across the AST rather than just in a node. For example, some nodes need to emit different bytecode when in a rescue.

This class maintains state needed as the AST is walked to generate bytecode. An instance of State is pushed onto a stack in the Generator instance as each ClosedScope or Iter is entered, and popped when left.

Attributes

flip_flops[RW]
check_for_locals[RW]
eval[R]
eval?[R]
scope[R]
super[R]
super?[R]

Public Class Methods

new(scope) click to toggle source
# File lib/rubinius/code/ast/node.rb, line 277
def initialize(scope)
  @scope = scope
  @ensure = 0
  @ensure_level = nil
  @block = 0
  @masgn = 0
  @loop = 0
  @op_asgn = 0
  @rescue = []
  @name = []
  @check_for_locals = true
end

Public Instance Methods

block?() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 346
def block?
  @block > 0
end
ensure?() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 322
def ensure?
  @ensure > 0
end
flip_flops() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 350
def flip_flops
  State.flip_flops
end
loop?() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 402
def loop?
  @loop > 0
end
masgn?() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 366
def masgn?
  @masgn > 0
end
name() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 298
def name
  @name.last
end
op_asgn?() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 378
def op_asgn?
  @op_asgn > 0
end
pop_block() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 342
def pop_block
  @block -= 1 if block?
end
pop_ensure() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 318
def pop_ensure
  @ensure -= 1 if ensure?
end
pop_inside_ensure() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 330
def pop_inside_ensure
  @ensure_level = nil
end
pop_loop() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 398
def pop_loop
  @loop -= 1 if loop?
end
pop_masgn() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 362
def pop_masgn
  @masgn -= 1 if masgn?
end
pop_name() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 294
def pop_name
  @name.pop
end
pop_op_asgn() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 374
def pop_op_asgn
  @op_asgn -= 1 if op_asgn?
end
pop_rescue() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 306
def pop_rescue
  @rescue.pop if rescue?
end
push_block() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 338
def push_block
  @block += 1
end
push_ensure() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 314
def push_ensure
  @ensure += 1
end
push_eval(scope) click to toggle source
# File lib/rubinius/code/ast/node.rb, line 388
def push_eval(scope)
  @eval = scope
end
push_flip_flop() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 354
def push_flip_flop
  State.flip_flops += 1
end
push_inside_ensure() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 326
def push_inside_ensure
  @ensure_level = @loop
end
push_loop() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 394
def push_loop
  @loop += 1
end
push_masgn() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 358
def push_masgn
  @masgn += 1
end
push_name(name) click to toggle source
# File lib/rubinius/code/ast/node.rb, line 290
def push_name(name)
  @name.push name
end
push_op_asgn() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 370
def push_op_asgn
  @op_asgn += 1
end
push_rescue(val) click to toggle source
# File lib/rubinius/code/ast/node.rb, line 302
def push_rescue(val)
  @rescue.push(val)
end
push_super(scope) click to toggle source
# File lib/rubinius/code/ast/node.rb, line 382
def push_super(scope)
  @super = scope
end
rescue?() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 310
def rescue?
  @rescue.last
end
top_level_ensure?() click to toggle source
# File lib/rubinius/code/ast/node.rb, line 334
def top_level_ensure?
  @ensure_level && @ensure_level == @loop
end