Class: Y2R::AST::YCP::CompilerContext
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Y2R::AST::YCP::CompilerContext
- Defined in:
- lib/y2r/ast/ycp.rb
Overview
Compilation context passed to nodes’ |compile| method. It mainly tracks the scope we’re in and contains related helper methods.
Instance Method Summary collapse
- #at_toplevel? ⇒ Boolean
- #disable_elsif ⇒ Object
-
#enable_elsif ⇒ Object
elsif_mode is enabled iff the ‘If` node contain `If` node in its else branch.
- #globals ⇒ Object
- #in?(klass) ⇒ Boolean
- #innermost(*klasses) ⇒ Object
- #inside(block) {|context| ... } ⇒ Object
- #locals ⇒ Object
- #module_name ⇒ Object
- #symbol_for(name) ⇒ Object
- #symbols ⇒ Object
- #with_whitespace(whitespace) ⇒ Object
Instance Method Details
#at_toplevel? ⇒ Boolean
17 18 19 |
# File 'lib/y2r/ast/ycp.rb', line 17 def at_toplevel? !blocks.any?(&:creates_local_scope?) end |
#disable_elsif ⇒ Object
50 51 52 53 54 |
# File 'lib/y2r/ast/ycp.rb', line 50 def disable_elsif context = dup context.elsif_mode = false context end |
#enable_elsif ⇒ Object
elsif_mode is enabled iff the ‘If` node contain `If` node in its else branch. In this mode ifs are translated as `elsif`.
44 45 46 47 48 |
# File 'lib/y2r/ast/ycp.rb', line 44 def enable_elsif context = dup context.elsif_mode = true context end |
#globals ⇒ Object
69 70 71 72 |
# File 'lib/y2r/ast/ycp.rb', line 69 def globals index = blocks.index(&:creates_local_scope?) || blocks.length blocks[0..index].map { |b| b.symbols.map(&:name) }.flatten end |
#in?(klass) ⇒ Boolean
21 22 23 |
# File 'lib/y2r/ast/ycp.rb', line 21 def in?(klass) blocks.find { |b| b.is_a?(klass) } ? true : false end |
#innermost(*klasses) ⇒ Object
25 26 27 |
# File 'lib/y2r/ast/ycp.rb', line 25 def innermost(*klasses) blocks.reverse.find { |b| klasses.any? { |k| b.is_a?(k) } } end |
#inside(block) {|context| ... } ⇒ Object
29 30 31 32 33 34 |
# File 'lib/y2r/ast/ycp.rb', line 29 def inside(block) context = dup context.blocks = blocks + [block] yield context end |
#locals ⇒ Object
64 65 66 67 |
# File 'lib/y2r/ast/ycp.rb', line 64 def locals index = blocks.index(&:creates_local_scope?) || blocks.length blocks[index..-1].map { |b| b.symbols.map(&:name) }.flatten end |
#module_name ⇒ Object
56 57 58 |
# File 'lib/y2r/ast/ycp.rb', line 56 def module_name blocks.first.name end |
#symbol_for(name) ⇒ Object
74 75 76 77 |
# File 'lib/y2r/ast/ycp.rb', line 74 def symbol_for(name) symbols = blocks.map { |b| b.symbols }.flatten symbols.reverse.find { |s| s.name == name } end |
#symbols ⇒ Object
60 61 62 |
# File 'lib/y2r/ast/ycp.rb', line 60 def symbols blocks.map { |b| b.symbols.map(&:name) }.flatten end |
#with_whitespace(whitespace) ⇒ Object
36 37 38 39 40 |
# File 'lib/y2r/ast/ycp.rb', line 36 def with_whitespace(whitespace) context = dup context.whitespace = whitespace context end |