File: org.jruby.ast.rb

package info (click to toggle)
jruby 1.5.1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: non-free
  • in suites: squeeze-lts
  • size: 47,024 kB
  • ctags: 74,144
  • sloc: ruby: 398,155; java: 169,506; yacc: 3,782; xml: 2,469; ansic: 415; sh: 279; makefile: 78; tcl: 40
file content (97 lines) | stat: -rw-r--r-- 2,546 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class org::jruby::ast::Node
  POS = org.jruby.lexer.yacc.SimpleSourcePosition.new("-",-1)
  def [](ix)
    self.child_nodes[ix]
  end

  def first
    self.child_nodes[0] if self.child_nodes.size > 0
  end

  def last
    self.child_nodes[self.child_nodes.size-1] if self.child_nodes.size > 0
  end

  def +(other)
    blk = org.jruby.ast.BlockNode.new POS
    blk.add self
    blk.add other
    blk
  end

  attr_accessor :locals

  def run
    unless defined?(JRuby)
      require 'jruby'
    end
    root = self
    unless org.jruby.ast.RootNode === root
      pos = POS
      scope1 = org.jruby.parser.LocalStaticScope.new(nil)
      scope1.setVariables(java.lang.String[self.locals || 40].new)
      scope = org.jruby.runtime.DynamicScope.newDynamicScope(scope1, nil)
      root = org.jruby.ast.RootNode.new(pos, scope, self)
    end

    JRuby::runtime.runInterpreter root
  end

  def inspect(indent = 0)
    s = ' '*indent + self.class.name.split('::').last

    if self.respond_to?(:name)
      s << " |#{self.name}|"
    end
    if self.respond_to?(:value)
      s << " ==#{self.value.inspect}"
    end

    if self.respond_to?(:index)
      s << " &#{self.index.inspect}"
    end

    if self.respond_to?(:depth)
      s << " >#{self.depth.inspect}"
    end

    [:receiver_node, :args_node, :var_node, :head_node, :value_node, :iter_node, :body_node, :next_node, :condition, :then_body, :else_body].each do |mm|
      if self.respond_to?(mm)
        begin
          s << "\n#{self.send(mm).inspect(indent+2)}" if self.send(mm)
        rescue
          s << "\n#{' '*(indent+2)}#{self.send(mm).inspect}" if self.send(mm)
        end
      end
    end

    if org::jruby::ast::ListNode === self
      (0...self.size).each do |n|
        begin
          s << "\n#{self.get(n).inspect(indent+2)}" if self.get(n)
        rescue
          s << "\n#{' '*(indent+2)}#{self.get(n).inspect}" if self.get(n)
        end
      end
    end
    s
  end

  def to_yaml_node(out)
    content = []
    content << {'name' => self.name} if self.respond_to?(:name)
    content << {'value' => self.value} if self.respond_to?(:value)
    [:receiver_node, :args_node, :value_node, :iter_node, :body_node, :next_node].each do |mm|
      if self.respond_to?(mm)
        content << self.send(mm) if self.send(mm)
      end
    end
    if org::jruby::ast::ListNode === self
      (0...self.size).each do |n|
        content << self.get(n) if self.get(n)
      end
    end
    out.map({ }.taguri, { self.class.name.split('::').last => content }, nil)
  end
end