Class: NScript::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/nscript/parser/nodes.rb

Constant Summary collapse

TAB =
'  '

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.children(*attributes) ⇒ Object



19
20
21
22
23
# File 'lib/nscript/parser/nodes.rb', line 19

def self.children(*attributes)
  attr_reader(*attributes)
  attrs = attributes.map {|a| "[@#{a}]" }.join(', ')
  class_eval "def children; [#{attrs}].flatten.compact; end"
end

.statementObject



6
7
8
# File 'lib/nscript/parser/nodes.rb', line 6

def self.statement
  class_eval "def statement?; true; end"
end

.statement_onlyObject



10
11
12
13
# File 'lib/nscript/parser/nodes.rb', line 10

def self.statement_only
  statement
  class_eval "def statement_only?; true; end"
end

.top_sensitiveObject



15
16
17
# File 'lib/nscript/parser/nodes.rb', line 15

def self.top_sensitive
  class_eval "def top_sensitive?; true; end"
end

Instance Method Details

#childrenObject



57
# File 'lib/nscript/parser/nodes.rb', line 57

def children;         [];     end

#compile(o = {}) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/nscript/parser/nodes.rb', line 30

def compile(o={})
  @options = o.dup
  @indent  = o[:indent]
  top      = self.top_sensitive? ? @options[:top] : @options.delete(:top)
  closure  = statement? && !statement_only? && !top && !@options[:return] && !self.is_a?(CommentNode)
  closure  &&= !contains? {|n| n.statement_only? }
  closure  ? compile_closure(@options) : compile_node(@options)
end

#compile_closure(o = {}) ⇒ Object



39
40
41
42
# File 'lib/nscript/parser/nodes.rb', line 39

def compile_closure(o={})
  @indent = o[:indent]
  ClosureNode.wrap(self).compile(o.merge(:shared_scope => o[:scope]))
end

#contains?(&block) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'lib/nscript/parser/nodes.rb', line 48

def contains?(&block)
  children.each do |node|
    return true if yield(node)
    return true if node.is_a?(Node) && node.contains?(&block)
  end
  false
end

#idt(tabs = 0) ⇒ Object



44
45
46
# File 'lib/nscript/parser/nodes.rb', line 44

def idt(tabs=0)
  @indent + (TAB * tabs)
end

#statement?Boolean

Returns:

  • (Boolean)


58
# File 'lib/nscript/parser/nodes.rb', line 58

def statement?;       false;  end

#statement_only?Boolean

Returns:

  • (Boolean)


59
# File 'lib/nscript/parser/nodes.rb', line 59

def statement_only?;  false;  end

#top_sensitive?Boolean

Returns:

  • (Boolean)


60
# File 'lib/nscript/parser/nodes.rb', line 60

def top_sensitive?;   false;  end

#unwrapObject



56
# File 'lib/nscript/parser/nodes.rb', line 56

def unwrap;           self;   end

#write(code) ⇒ Object



25
26
27
28
# File 'lib/nscript/parser/nodes.rb', line 25

def write(code)
  puts "#{self.class.to_s}:\n#{@options.inspect}\n#{code}\n\n" if ENV['VERBOSE']
  code
end