Class: CodeTools::AST::State

Inherits:
Object
  • Object
show all
Defined in:
lib/rubinius/code/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.



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

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.



273
274
275
# File 'lib/rubinius/code/ast/node.rb', line 273

def flip_flops
  @flip_flops
end

Instance Attribute Details

#check_for_localsObject

Returns the value of attribute check_for_locals.



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

def check_for_locals
  @check_for_locals
end

#evalObject (readonly) Also known as: eval?

Returns the value of attribute eval.



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

def eval
  @eval
end

#scopeObject (readonly)

Returns the value of attribute scope.



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

def scope
  @scope
end

#superObject (readonly) Also known as: super?

Returns the value of attribute super.



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

def super
  @super
end

Instance Method Details

#block?Boolean

Returns:

  • (Boolean)


347
348
349
# File 'lib/rubinius/code/ast/node.rb', line 347

def block?
  @block > 0
end

#ensure?Boolean

Returns:

  • (Boolean)


323
324
325
# File 'lib/rubinius/code/ast/node.rb', line 323

def ensure?
  @ensure > 0
end

#flip_flopsObject



351
352
353
# File 'lib/rubinius/code/ast/node.rb', line 351

def flip_flops
  State.flip_flops
end

#loop?Boolean

Returns:

  • (Boolean)


403
404
405
# File 'lib/rubinius/code/ast/node.rb', line 403

def loop?
  @loop > 0
end

#masgn?Boolean

Returns:

  • (Boolean)


367
368
369
# File 'lib/rubinius/code/ast/node.rb', line 367

def masgn?
  @masgn > 0
end

#nameObject



299
300
301
# File 'lib/rubinius/code/ast/node.rb', line 299

def name
  @name.last
end

#op_asgn?Boolean

Returns:

  • (Boolean)


379
380
381
# File 'lib/rubinius/code/ast/node.rb', line 379

def op_asgn?
  @op_asgn > 0
end

#pop_blockObject



343
344
345
# File 'lib/rubinius/code/ast/node.rb', line 343

def pop_block
  @block -= 1 if block?
end

#pop_ensureObject



319
320
321
# File 'lib/rubinius/code/ast/node.rb', line 319

def pop_ensure
  @ensure -= 1 if ensure?
end

#pop_inside_ensureObject



331
332
333
# File 'lib/rubinius/code/ast/node.rb', line 331

def pop_inside_ensure
  @ensure_level = nil
end

#pop_loopObject



399
400
401
# File 'lib/rubinius/code/ast/node.rb', line 399

def pop_loop
  @loop -= 1 if loop?
end

#pop_masgnObject



363
364
365
# File 'lib/rubinius/code/ast/node.rb', line 363

def pop_masgn
  @masgn -= 1 if masgn?
end

#pop_nameObject



295
296
297
# File 'lib/rubinius/code/ast/node.rb', line 295

def pop_name
  @name.pop
end

#pop_op_asgnObject



375
376
377
# File 'lib/rubinius/code/ast/node.rb', line 375

def pop_op_asgn
  @op_asgn -= 1 if op_asgn?
end

#pop_rescueObject



307
308
309
# File 'lib/rubinius/code/ast/node.rb', line 307

def pop_rescue
  @rescue.pop if rescue?
end

#push_blockObject



339
340
341
# File 'lib/rubinius/code/ast/node.rb', line 339

def push_block
  @block += 1
end

#push_ensureObject



315
316
317
# File 'lib/rubinius/code/ast/node.rb', line 315

def push_ensure
  @ensure += 1
end

#push_eval(scope) ⇒ Object



389
390
391
# File 'lib/rubinius/code/ast/node.rb', line 389

def push_eval(scope)
  @eval = scope
end

#push_flip_flopObject



355
356
357
# File 'lib/rubinius/code/ast/node.rb', line 355

def push_flip_flop
  State.flip_flops += 1
end

#push_inside_ensureObject



327
328
329
# File 'lib/rubinius/code/ast/node.rb', line 327

def push_inside_ensure
  @ensure_level = @loop
end

#push_loopObject



395
396
397
# File 'lib/rubinius/code/ast/node.rb', line 395

def push_loop
  @loop += 1
end

#push_masgnObject



359
360
361
# File 'lib/rubinius/code/ast/node.rb', line 359

def push_masgn
  @masgn += 1
end

#push_name(name) ⇒ Object



291
292
293
# File 'lib/rubinius/code/ast/node.rb', line 291

def push_name(name)
  @name.push name
end

#push_op_asgnObject



371
372
373
# File 'lib/rubinius/code/ast/node.rb', line 371

def push_op_asgn
  @op_asgn += 1
end

#push_rescue(val) ⇒ Object



303
304
305
# File 'lib/rubinius/code/ast/node.rb', line 303

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

#push_super(scope) ⇒ Object



383
384
385
# File 'lib/rubinius/code/ast/node.rb', line 383

def push_super(scope)
  @super = scope
end

#rescue?Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/rubinius/code/ast/node.rb', line 311

def rescue?
  @rescue.last
end

#top_level_ensure?Boolean

Returns:

  • (Boolean)


335
336
337
# File 'lib/rubinius/code/ast/node.rb', line 335

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