Code coverage report for src/compile/expression.js

Statements: 97.56% (40 / 41)      Branches: 95.65% (22 / 23)      Functions: 100% (2 / 2)      Lines: 97.56% (40 / 41)      Ignored: none     

All files » src/compile/ » expression.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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 941       1               175 175 175   65   6 6     5 5     2 2     1 1     5 5     2 2     4 4     16 16     5 5     6 6     1 1     3 3     1 1     1 1     1 1     6 6             65   110          
module.exports = function(Velocity, utils){
  /**
   * expression运算
   */
  utils.mixin(Velocity.prototype, {
    /**
     * 表达式求值,表达式主要是数学表达式,逻辑运算和比较运算,到最底层数据结构,
     * 基本数据类型,使用 getLiteral求值,getLiteral遇到是引用的时候,使用
     * getReferences求值
     */
    getExpression: function(ast){
 
      var exp = ast.expression;
      var ret;
      if (ast.type === 'math') {
 
        switch(ast.operator) {
          case '+':
          ret = this.getExpression(exp[0]) + this.getExpression(exp[1]);
          break;
 
          case '-':
          ret = this.getExpression(exp[0]) - this.getExpression(exp[1]);
          break;
 
          case '/':
          ret = this.getExpression(exp[0]) / this.getExpression(exp[1]);
          break;
 
          case '%':
          ret = this.getExpression(exp[0]) % this.getExpression(exp[1]);
          break;
 
          case '*':
          ret = this.getExpression(exp[0]) * this.getExpression(exp[1]);
          break;
 
          case '||':
          ret = this.getExpression(exp[0]) || this.getExpression(exp[1]);
          break;
 
          case '&&':
          ret = this.getExpression(exp[0]) && this.getExpression(exp[1]);
          break;
 
          case '>':
          ret = this.getExpression(exp[0]) > this.getExpression(exp[1]);
          break;
 
          case '<':
          ret = this.getExpression(exp[0]) < this.getExpression(exp[1]);
          break;
 
          case '==':
          ret = this.getExpression(exp[0]) == this.getExpression(exp[1]);
          break;
 
          case '>=':
          ret = this.getExpression(exp[0]) >= this.getExpression(exp[1]);
          break;
 
          case '<=':
          ret = this.getExpression(exp[0]) <= this.getExpression(exp[1]);
          break;
 
          case '!=':
          ret = this.getExpression(exp[0]) != this.getExpression(exp[1]);
          break;
 
          case 'minus':
          ret = - this.getExpression(exp[0]);
          break;
 
          case 'not':
          ret = !this.getExpression(exp[0]);
          break;
 
          case 'parenthesis':
          ret = this.getExpression(exp[0]);
          break;
 
          default:
          return;
          // code
        }
 
        return ret;
      } else {
        return this.getLiteral(ast);
      }
    }
  });
};