1 # Copyright (C) 2002-2004 Kouichirou Eto, All rights reserved.
4 class Node < Array # a branch of Tree structure
5 def initialize(nodeleaf=nil, nodenum=nil)
10 original_add(@nodeleaf)
20 @nodenum -= 1 if @nodenum
27 ar += n.nodes if n.is_a? Node
38 @depth = 1 # the deepest stack size. If there is no tree, depth is 1.
41 def depth() @depth - 1; end
43 def add_node(nodeleaf=nil, nodenum=nil) # add a node
44 new_node = Node.new(nodeleaf, nodenum)
45 @stack.last << new_node
47 if @depth < @stack.length
48 @depth = @stack.length
53 def end_node # terminate this node.
58 def add_leaf(a) # add a leaf.
65 n = @stack.last.nodenum
68 end_check # recursive.
73 n = @stack.last.nodenum
74 return nil if @root.length == 0 # no tree is good tree
75 raise "unmatch leaves" if n && n != 0
76 raise "extra nodes" if @root.first.is_a?(Node) && @root.length != 1
77 raise "extra leaves" if @root.length != 1
93 def to_s() @root.to_s end
95 def inspect() @root.inspect end
106 @str.each_character {|char|
108 add_node(char, idc_argument_number(char))
116 return true if obj.is_idc?
117 return true if "+*".include?(obj.to_s) # only for test
121 def idc_argument_number(obj)
122 n = obj.idc_argument_number
124 return 2 # only for test
129 raise "contains ques" if /\?/ =~ @str #?が含まれている?