Class: Y2R::AST::YCP::CompilerContext

Inherits:
OpenStruct
  • Object
show all
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

Instance Method Details

#at_toplevel?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/y2r/ast/ycp.rb', line 17

def at_toplevel?
  !blocks.any?(&:creates_local_scope?)
end

#disable_elsifObject



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_elsifObject

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

#globalsObject



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

Returns:

  • (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

Yields:

  • (context)


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

#localsObject



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_nameObject



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

#symbolsObject



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