Class: CodeTools::AST::State

Inherits:
Object
  • Object
show all
Defined in:
lib/rubinius/ast/node.rb

Overview

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.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope) ⇒ State

Returns a new instance of State.



276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/rubinius/ast/node.rb', line 276

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

Class Attribute Details

.flip_flopsObject

Returns the value of attribute flip_flops.



271
272
273
# File 'lib/rubinius/ast/node.rb', line 271

def flip_flops
  @flip_flops
end

Instance Attribute Details

#check_for_localsObject

Returns the value of attribute check_for_locals.



268
269
270
# File 'lib/rubinius/ast/node.rb', line 268

def check_for_locals
  @check_for_locals
end

#evalObject (readonly) Also known as: eval?

Returns the value of attribute eval.



267
268
269
# File 'lib/rubinius/ast/node.rb', line 267

def eval
  @eval
end

#scopeObject (readonly)

Returns the value of attribute scope.



267
268
269
# File 'lib/rubinius/ast/node.rb', line 267

def scope
  @scope
end

#superObject (readonly) Also known as: super?

Returns the value of attribute super.



267
268
269
# File 'lib/rubinius/ast/node.rb', line 267

def super
  @super
end

Instance Method Details

#block?Boolean

Returns:

  • (Boolean)


345
346
347
# File 'lib/rubinius/ast/node.rb', line 345

def block?
  @block > 0
end

#ensure?Boolean

Returns:

  • (Boolean)


321
322
323
# File 'lib/rubinius/ast/node.rb', line 321

def ensure?
  @ensure > 0
end

#flip_flopsObject



349
350
351
# File 'lib/rubinius/ast/node.rb', line 349

def flip_flops
  State.flip_flops
end

#loop?Boolean

Returns:

  • (Boolean)


401
402
403
# File 'lib/rubinius/ast/node.rb', line 401

def loop?
  @loop > 0
end

#masgn?Boolean

Returns:

  • (Boolean)


365
366
367
# File 'lib/rubinius/ast/node.rb', line 365

def masgn?
  @masgn > 0
end

#nameObject



297
298
299
# File 'lib/rubinius/ast/node.rb', line 297

def name
  @name.last
end

#op_asgn?Boolean

Returns:

  • (Boolean)


377
378
379
# File 'lib/rubinius/ast/node.rb', line 377

def op_asgn?
  @op_asgn > 0
end

#pop_blockObject



341
342
343
# File 'lib/rubinius/ast/node.rb', line 341

def pop_block
  @block -= 1 if block?
end

#pop_ensureObject



317
318
319
# File 'lib/rubinius/ast/node.rb', line 317

def pop_ensure
  @ensure -= 1 if ensure?
end

#pop_inside_ensureObject



329
330
331
# File 'lib/rubinius/ast/node.rb', line 329

def pop_inside_ensure
  @ensure_level = nil
end

#pop_loopObject



397
398
399
# File 'lib/rubinius/ast/node.rb', line 397

def pop_loop
  @loop -= 1 if loop?
end

#pop_masgnObject



361
362
363
# File 'lib/rubinius/ast/node.rb', line 361

def pop_masgn
  @masgn -= 1 if masgn?
end

#pop_nameObject



293
294
295
# File 'lib/rubinius/ast/node.rb', line 293

def pop_name
  @name.pop
end

#pop_op_asgnObject



373
374
375
# File 'lib/rubinius/ast/node.rb', line 373

def pop_op_asgn
  @op_asgn -= 1 if op_asgn?
end

#pop_rescueObject



305
306
307
# File 'lib/rubinius/ast/node.rb', line 305

def pop_rescue
  @rescue.pop if rescue?
end

#push_blockObject



337
338
339
# File 'lib/rubinius/ast/node.rb', line 337

def push_block
  @block += 1
end

#push_ensureObject



313
314
315
# File 'lib/rubinius/ast/node.rb', line 313

def push_ensure
  @ensure += 1
end

#push_eval(scope) ⇒ Object



387
388
389
# File 'lib/rubinius/ast/node.rb', line 387

def push_eval(scope)
  @eval = scope
end

#push_flip_flopObject



353
354
355
# File 'lib/rubinius/ast/node.rb', line 353

def push_flip_flop
  State.flip_flops += 1
end

#push_inside_ensureObject



325
326
327
# File 'lib/rubinius/ast/node.rb', line 325

def push_inside_ensure
  @ensure_level = @loop
end

#push_loopObject



393
394
395
# File 'lib/rubinius/ast/node.rb', line 393

def push_loop
  @loop += 1
end

#push_masgnObject



357
358
359
# File 'lib/rubinius/ast/node.rb', line 357

def push_masgn
  @masgn += 1
end

#push_name(name) ⇒ Object



289
290
291
# File 'lib/rubinius/ast/node.rb', line 289

def push_name(name)
  @name.push name
end

#push_op_asgnObject



369
370
371
# File 'lib/rubinius/ast/node.rb', line 369

def push_op_asgn
  @op_asgn += 1
end

#push_rescue(val) ⇒ Object



301
302
303
# File 'lib/rubinius/ast/node.rb', line 301

def push_rescue(val)
  @rescue.push(val)
end

#push_super(scope) ⇒ Object



381
382
383
# File 'lib/rubinius/ast/node.rb', line 381

def push_super(scope)
  @super = scope
end

#rescue?Boolean

Returns:

  • (Boolean)


309
310
311
# File 'lib/rubinius/ast/node.rb', line 309

def rescue?
  @rescue.last
end

#top_level_ensure?Boolean

Returns:

  • (Boolean)


333
334
335
# File 'lib/rubinius/ast/node.rb', line 333

def top_level_ensure?
  @ensure_level && @ensure_level == @loop
end