OperatorUnaryBase 一元操作符基底類型
檔案: OperatorUnaryBase.js
function OperatorUnaryBase() {<br /> this.DeriveFrom(new OperatorBase());<br /> this.set_ClassName("OperatorUnaryBase");<br /> this._type = new UnaryType("undefined");<br />};</p><p>OperatorUnaryBase.prototype.GetOneNumberOperand = function(operands) {<br /> var operand = operands.pop();</p><p> if (!(operand.IsNumber() || operand.IsBoolean()))<br /> throw new Exception(this, "GetOneNumberOperand", "Unsupported type:" + $T(operand));</p><p> return operand;<br />}; // prototype</p><p>// --------------- TEST ------------------------<br />OperatorUnaryBase.prototype.Test = function() {<br /> $Debug.WriteLine("=================== " + this.get_ClassName() + " =====================");</p><p> var values = [<br /> -1,<br /> 0,<br /> 1,<br /> -99999,<br /> 99999,<br /> true,<br /> false,<br /> "ABCDEFG"<br /> ];</p><p> for (var i = 0; i < values.length; i++) {<br /> try {<br /> var operand = new OperandBase(values[i]),<br /> res = this.Evaluate([operand]);</p><p> $Debug.WriteLine(<br /> this.get_Sign() + operand.ToString() +<br /> "[" + $T(values[i]) + "] =" + res.ToString());<br /> }<br /> catch (e) {<br /> $Debug.WriteLine(<br /> "Failed:" +<br /> this.get_Sign() + values[i].ToString() + " [" + $T(values[i]) + "]" +<br /> "/nError:" + e.description);<br /> }<br /> } // for<br />}; // function Test<br />
一元操作的實現
主要為'+', '-',其他的像'!','~'在後續檔案中。
檔案:OperatorUnary.js
function OperatorPositive() {<br /> this.DeriveFrom(new OperatorUnaryBase());<br /> this.set_ClassName("OperatorPositive");<br /> this.set_Sign("+");<br />};</p><p>OperatorPositive.prototype.Evaluate = function(operands) {<br /> var val = this.GetOneNumberOperand(operands);</p><p> return new OperandNumber(val.get_Value());<br />};</p><p>function OperatorNegative() {<br /> this.DeriveFrom(new OperatorUnaryBase());<br /> this.set_ClassName("OperatorNegative");<br /> this.set_Sign("-");<br />};</p><p>OperatorNegative.prototype.Evaluate = function(operands) {<br /> var val = this.GetOneNumberOperand(operands);</p><p> return new OperandNumber(-val.get_Value());<br />};