Class: Minjs::ECMA262::ExpAdd

Inherits:
Expression show all
Includes:
BinaryOperation
Defined in:
lib/minjs/ecma262/expression.rb

Overview

Class of the Additionr operator expression element.

See Also:

Instance Attribute Summary

Attributes included from BinaryOperation

#val, #val2

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods included from BinaryOperation

#==, #add_paren, #deep_dup, #remove_paren, #replace, #to_js, #traverse

Methods inherited from Expression

#left_hand_side_exp?, #side_effect?

Methods inherited from Base

#==, #add_remove_paren, #concat, #deep_dup, #replace, #to_js, #traverse

Constructor Details

#initialize(val, val2) ⇒ ExpAdd

Returns a new instance of ExpAdd.



1244
1245
1246
1247
# File 'lib/minjs/ecma262/expression.rb', line 1244

def initialize(val, val2)
  @val = val
  @val2 = val2
end

Instance Method Details

#priorityFixnum

Returns expression priority.

Returns:

  • (Fixnum)

    expression priority



1255
1256
1257
# File 'lib/minjs/ecma262/expression.rb', line 1255

def priority
  PRIORITY_ADDITIVE
end

#reduce(parent) ⇒ Object

reduce expression if available

Parameters:

  • parent (Base)

    parent element



1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
# File 'lib/minjs/ecma262/expression.rb', line 1261

def reduce(parent)
  #
  # String + String/
  # a + b = a.concat(b)
  if @val.kind_of? ECMA262String or @val2.kind_of? ECMA262String
    if @val.respond_to? :to_ecma262_string and @val2.respond_to? :to_ecma262_string
      v = @val.to_ecma262_string
      v2 = @val2.to_ecma262_string
      if !v.nil? and !v2.nil?
        new_str = ECMA262String.new(v + v2)
        parent.replace(self, new_str)
      end
    end
  #
  # Numeric + Numeric
  #
  elsif @val.respond_to? :to_ecma262_number and @val2.respond_to? :to_ecma262_number
    #
    #11.6.3 Applying the Additive Operators to Numbers(TODO)
    #
    # N + M => (N + M)
    v = @val.to_ecma262_number
    v2 = @val2.to_ecma262_number
    if !v.nil? and !v2.nil?
      parent.replace(self, ECMA262Numeric.new(v + v2))
    end
  end
end

#symObject

symbol of expression



1250
1251
1252
# File 'lib/minjs/ecma262/expression.rb', line 1250

def sym
  "+"
end