Code coverage report for src/ast/bin.js

Statements: 100% (20 / 20)      Branches: 100% (5 / 5)      Functions: 100% (4 / 4)      Lines: 100% (20 / 20)      Ignored: none     

All files » src/ast/ » bin.js
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              1 1     1                                                                         1 147 147 147 147     1 28 28 28   18 18 18         1 1 17 29       1  
/*!
 * Copyright (C) 2017 Glayzzle (BSD3 License)
 * @authors https://github.com/glayzzle/php-parser/graphs/contributors
 * @url http://glayzzle.com
 */
"use strict";
 
var Operation = require('./operation');
var KIND = 'bin';
 
// operators in ascending order of precedence
var binOperatorsPrecedence = [
  ['or'],
  ['xor'],
  ['and'],
  // TODO: assignment / not sure that PHP allows this with expressions
  ['retif'],
  ['??'],
  ['||'],
  ['&&'],
  ['|'],
  ['^'],
  ['&'],
  ['==', '!=', '===', '!==', /* '<>', */ '<=>'],
  ['<', '<=', '>', '>='],
  ['<<', '>>'],
  ['+', '-', '.'],
  ['*', '/', '%'],
  ['!'],
  ['instanceof'],
  // TODO: typecasts
  // TODO: [ (array)
  // TODO: clone, new
];
 
/*
x OP1 (y OP2 z)
z OP1 (x OP2 y)
z OP2 (x OP1 y)
*/
/**
 * Binary operations
 * @constructor Bin
 * @extends {Operation}
 * @property {String} type
 * @property {Expression} left
 * @property {Expression} right
 */
var Bin = Operation.extends(function Bin(type, left, right, location) {
  Operation.apply(this, [KIND, location]);
  this.type = type;
  this.left = left;
  this.right = right;
});
 
Bin.prototype.precedence = function(node) {
  var lLevel = Bin.precedence[node.type];
  var rLevel = Bin.precedence[this.type];
  if (lLevel && rLevel && rLevel < lLevel) {
    // shift precedence
    node.right = this.left;
    this.left = node;
    return this;
  }
};
 
// define nodes shifting
Bin.precedence = {};
binOperatorsPrecedence.forEach(function (list, index) {
  list.forEach(function (operator) {
    Bin.precedence[operator] = index + 1;
  });
});
 
module.exports = Bin;