您的位置:首页 > Web前端 > JavaScript

基于JavaScript的公式解释器 - 4 【操作符基类型的实现】

2010-10-18 22:20 399 查看
操作符优先级定义

文件:OperatorBase.js

// JScript source code
var $OperatorPriority = {
    // Logic
    "||": 0,
    "&&": 0,
    // Compare
    ">": 1,
    ">=": 1,
    "<": 1,
    "<=": 1,
    // Logic compute
    "|": 2,
    "&": 2,
    "^": 2,
    // Mathematic
    "+": 3,
    "-": 3,
    "*": 4,
    "/": 4,
    "%": 4,
    // Func
    "function": 5,
    "unary": 6
};




操作符类型定义

文件:OperatorBase.js

/*****************************
        Operator Type
*****************************/
function UnknownType(sign, argsNum) {
    this.DeriveFrom(new Object());
    this.set_ClassName("UnknownType");
    this._sign = sign;
    this._requiredArgsNum = argsNum;
};

UnknownType.prototype = {
    get_RequiredArgumentsNum: function() { return this._requiredArgsNum; },
    get_Sign: function() { return this._sign; }
};

function UnaryType(sign) {
    this.DeriveFrom(new UnknownType(sign, 1));
    this.set_ClassName("UnaryType");
};

function BinaryType(sign) {
    this.DeriveFrom(new UnknownType(sign, 2));
    this.set_ClassName("BinaryType");
};

function ParenthesesType(sign) {
    this.DeriveFrom(new UnknownType(sign, 1));
    this.set_ClassName("ParentthesesType");
};

function FunctionType(name) {
    this.DeriveFrom(new UnknownType(name, -1));
    this.set_ClassName("FunctionType");
};




OperatorBase 操作符基类型定义

文件:OperatorBase.js

// ----------- OperatorBase --------------
function OperatorBase() {
    this.set_ClassName("OperatorBase");
    this._type = new UnknownType("undefined", -1);
};

OperatorBase.prototype = {
    // Properties
    get_Type: function() { return this._type; },
    get_Sign: function() { return this._type.get_Sign(); },
    set_Sign: function(sign) { this._type._sign = sign; },
    get_RequiredArgumentsNum: function() { return this._type.get_RequiredArgumentsNum(); },
    get_Priority: function() {
        if (!(this.IsFunction() || this.IsUnary()))
            return $OperatorPriority[this.get_Sign()];
        else {
            if (this.IsFunction())
                return $OperatorPriority["function"];
            else
                return $OperatorPriority["unary"];
        }
    },
    IsPreceding: function(anotherOperator) {
        if (this.IsUnary()) {
            if (anotherOperator.IsUnary())
                return false;
            else
                return true;
        }
        else {
            if (anotherOperator.IsUnary())
                return false;
            else
                return this.get_Priority() > anotherOperator.get_Priority();
        }
    }, // IsPreceding
    IsUnary: function() { return $T(this._type) == "UnaryType"; },
    IsBinary: function() { return $T(this._type) == "BinaryType"; },
    IsFunction: function() { return $T(this._type) == "FunctionType"; },
    IsParentheses: function() { return $T(this._type) == "ParenthesesType"; },
    Evaluate: function(operandArray) { 
        throw new Exception(this, "Evaluate", "Not implemented"); 
    } // Evaluate
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: