lib/minjs/ecma262/st.rb in minjs-0.2.1 vs lib/minjs/ecma262/st.rb in minjs-0.2.2

- old
+ new

@@ -35,10 +35,16 @@ def deep_dup self.class.new(@statement_list.deep_dup) end + def replace(from, to) + if from == @statement_list + @statement_list = to + end + end + def traverse(parent, &block) @statement_list.traverse(self, &block) yield self, parent end @@ -82,10 +88,18 @@ else StEmpty.new end end + def to_return? + to_statement? and to_statement.to_return? + end + + def to_return + to_statement.to_return + end + def empty? @statement_list.statement_list.select{|s| s.class != StEmpty }.length == 0 end @@ -179,10 +193,11 @@ @vars.each do |x| if x[1] and x[1].kind_of? ExpParen and x[1].val.priority <= PRIORITY_ASSIGNMENT x[1] = x[1].val end end + self end def add_paren @vars.each do |x| if x[1] and x[1].priority > PRIORITY_ASSIGNMENT @@ -260,10 +275,11 @@ def remove_paren if @exp.kind_of? ExpParen @exp = @exp.val if @exp.remove_paren? end + self end def add_paren self end @@ -325,28 +341,26 @@ end end def to_return then_exp = then_st.exp; - else_exp = else_st.exp; + if @else_st + else_exp = else_st.exp; + end if then_exp.nil? then_exp = ExpVoid.new(ECMA262Numeric.new(0)) end - if else_exp.nil? + if @else_st and else_exp.nil? else_exp = ExpVoid.new(ECMA262Numeric.new(0)) end if @else_st - ret = StReturn.new(ExpCond.new(@cond, then_exp, else_exp).add_paren) + ret = add_remove_paren StReturn.new(ExpCond.new(@cond, then_exp, else_exp)) else - ret = StReturn.new(ExpLogicalAnd.new(@cond, then_exp).add_paren) + ret = add_remove_paren StReturn.new(ExpLogicalAnd.new(@cond, then_exp)) end - if ret.to_js.length <= to_js.length - ret - else - self - end + ret end def to_exp? if !@else_st return false if @then_st.to_exp? == false @@ -359,24 +373,32 @@ def to_exp(options = {}) cond = @cond.deep_dup if !@else_st then_exp = @then_st.to_exp(options) - if cond.kind_of? ExpLogicalNot - return ExpParen.new(ExpLogicalOr.new(ExpParen.new(cond.val), ExpParen.new(then_exp))) + if(options[:cond]) + if cond.kind_of? ExpLogicalNot + add_remove_paren ExpCond.new(cond.val, ECMA262Numeric.new(0), then_exp) + else + add_remove_paren ExpCond.new(cond, then_exp, ECMA262Numeric.new(0)) + end else - return ExpParen.new(ExpLogicalAnd.new(ExpParen.new(cond), ExpParen.new(then_exp))) + if cond.kind_of? ExpLogicalNot + add_remove_paren ExpLogicalOr.new(cond.val, then_exp) + else + add_remove_paren ExpLogicalAnd.new(cond, then_exp) + end end else - then_exp = ExpParen.new(@then_st.to_exp(options)) - else_exp = ExpParen.new(@else_st.to_exp(options)) - end + then_exp = @then_st.to_exp(options) + else_exp = @else_st.to_exp(options) - if cond.kind_of? ExpLogicalNot - ExpCond.new(ExpParen.new(cond.val), else_exp, then_exp) - else - ExpCond.new(ExpParen.new(cond), then_exp, else_exp) + if cond.kind_of? ExpLogicalNot + add_remove_paren ExpCond.new(cond.val, else_exp, then_exp) + else + add_remove_paren ExpCond.new(cond, then_exp, else_exp) + end end end def remove_paren if @cond.kind_of? ExpParen @@ -840,11 +862,11 @@ def initialize(exp = nil) @exp = exp end def deep_dup - self.class.new(@exp) + self.class.new(@exp ? @exp.deep_dup : nil) end def traverse(parent, &block) @exp.traverse(self, &block) if @exp yield self, parent @@ -871,11 +893,11 @@ def initialize(exp = nil) @exp = exp end def deep_dup - self.class.new(@exp) + self.class.new(@exp ? @exp.deep_dup : nil) end def traverse(parent, &block) @exp.traverse(self, &block) if @exp yield self, parent @@ -902,14 +924,10 @@ def initialize(exp = nil) @exp = exp end def deep_dup - self.class.new(@exp) - end - - def deep_dup self.class.new(exp ? exp.deep_dup : nil) end def replace(from, to) if from .eql? @exp @@ -953,19 +971,20 @@ self end end #12.10 class StWith < St - attr_reader :exp, :statement + attr_reader :exp, :statement, :context - def initialize(exp, statement) + def initialize(context, exp, statement) + @context = context @exp = exp @statement = statement end def deep_dup - self.class.new(@exp, @statement) + self.class.new(@context, @exp.deep_dup, @statement.deep_dup) end def replace(from, to) if @exp .eql? from @exp = to @@ -1012,11 +1031,11 @@ @exp = exp @blocks = blocks end def deep_dup - self.class.new(@exp, + self.class.new(@exp.deep_dup, @blocks.collect{|x, y| [ x ? x.deep_dup : nil, y ? y.deep_dup : nil ] @@ -1083,11 +1102,11 @@ @label = label @statement = statement end def deep_dup - self.class.new(@label, @statement) + self.class.new(@label.deep_dup, @statement.deep_dup) end def replace(from, to) if from .eql? @label @label = to @@ -1119,11 +1138,11 @@ def initialize(exp) @exp = exp end def deep_dup - self.class.new(@exp) + self.class.new(@exp.deep_dup) end def traverse(parent, &block) @exp.traverse(self, &block) yield self, parent @@ -1150,10 +1169,13 @@ @catch = catch @finally = finally end def deep_dup - self.class.new(@context, @try, @catch, @finally) + self.class.new(@context, + @try.deep_dup, + @catch ? [@catch[0].deep_dup, @catch[1].deep_dup] : nil, + @finally ? @finally.deep_dup : nil) end def replace(from, to) if from .eql? @try @try = to