基於JavaScript的公式解譯器 – 3 【運算元類型的實現】

來源:互聯網
上載者:User

OperandBase 運算元基底類型

檔案:OperandBase.js

// JScript source code<br />// ----------- OperandBase ---------------<br />function OperandBase(arg) {<br /> this.set_ClassName("OperandBase");<br /> this._type = "undefined";<br /> this._value = null;</p><p> if (arguments.length > 0)<br /> return __operandCopy(arg);<br />};</p><p>OperandBase.prototype =<br />{<br /> get_Type: function() { return this._type; },<br /> get_Value: function() { return this._value; },<br /> set_Value: function(val) { this._value = val; },<br /> IsValid: function() { return this._value != null; },<br /> IsNumber: function() { return this._type == "number"; },<br /> IsBoolean: function() { return this._type == "boolean"; },<br /> IsString: function() { return this._type == "string"; },<br /> ToString: function() { return this.IsValid() ? this._value.toString() : "null"; }<br />};</p><p>// Perform a copying operation.<br />function __operandCopy(arg) {<br /> switch ($T(arg)) {<br /> case "string": return new OperandString(arg);<br /> case "number": return new OperandNumber(arg);<br /> case "boolean": return new OperandBoolean(arg);<br /> case "OperandNumber": return new OperandNumber(arg.get_Value());<br /> case "OperandBoolean": return new OperandBoolean(arg.get_Value());<br /> case "OperandString": return new OperandString(arg.get_Value());<br /> default: throw new Exception(this, "__operandCopy", "Unsupported type:" + $T(arg));<br /> } // switch<br />};<br />

 

OperandNumber 數實值型別

檔案:OperandNumber.js

// JScript source code<br />// ----------- Decimal -------------<br />function OperandNumber(arg) {<br /> this.DeriveFrom(new OperandBase());<br /> this.set_ClassName("OperandNumber");<br /> this._type = "number";<br /> this.set_Value(NaN);</p><p> if (arguments.length > 0)<br /> __initOperandNumber(this, arg);<br />};</p><p>OperandNumber.prototype.ToString = function() {<br /> return this.get_Value().ToString();<br />};</p><p>OperandNumber.prototype.IsValid = function() {<br /> return this.get_Value() != null && !isNaN(this.get_Value()) && isFinite(this.get_Value());<br />};</p><p>function __initOperandNumber(obj, arg) {<br /> switch ($T(arg)) {<br /> case "OperandNumber": obj.set_Value(arg.get_Value()); break;<br /> case "OperandBoolean": obj.set_Value(arg.get_Value() ? 1 : 0); break;<br /> case "OperandString": obj.set_Value(parseFloat(arg.get_Value())); break;<br /> case "string":<br /> {<br /> var str = arg.toLowerCase();</p><p> if (str == "true")<br /> obj.set_Value(1);<br /> else<br /> if (str == "false")<br /> obj.set_Value(0);<br /> else<br /> try {<br /> obj.set_Value(parseFloat(arg));<br /> }<br /> catch (e) {<br /> }<br /> }<br /> break;<br /> case "boolean": obj.set_Value(arg ? 1 : 0); break;<br /> case "number":<br /> case "float":<br /> {<br /> if (isNaN(arg))<br /> throw new Exception(this, "__initOperandNumber", arg + " is not a number");</p><p> if (!isFinite(arg))<br /> throw new Exception(this, "__initOperandNumber", arg + " is not finite");</p><p> obj.set_Value(arg);<br /> }<br /> break;<br /> default:<br /> throw new Exception(this, "Unspported argument:" + arguments[0].ToString()); break;<br /> } // switch<br />};</p><p>OperandNumber.prototype.Test = function() {<br /> $Debug.WriteLine("================ OperandNumber.Test ================");</p><p> var values = [<br /> -12345,<br /> -1,<br /> 0,<br /> 1,<br /> 12345,<br /> "ABCDEFG",<br /> "1",<br /> "1.1",<br /> "true",<br /> "false",<br /> 1.2,<br /> // Boolean<br /> true,<br /> false,<br /> // OperandNumber<br /> new OperandNumber(1),<br /> new OperandString("1"),<br /> new OperandString("1.1")<br /> ];</p><p> for (var i = 0; i < values.length; i++) {<br /> try {<br /> var operand = new OperandNumber(values[i]);</p><p> $Debug.WriteLine(values[i] + "=" + operand.get_Value());<br /> } // try<br /> catch (e) {<br /> $Debug.WriteLine("Generate operand failed:" + values[i].ToString() + "/nError:" + e.discription);<br /> } // try ... catch<br /> } // for<br />}; // function Test<br />

 

OperandBoolean 布爾類型

檔案:OperandBoolean.js

function OperandBoolean(arg) {<br /> this.DeriveFrom(new OperandBase());<br /> this.set_ClassName("OperandBoolean");<br /> this._type = "boolean";<br /> this.set_Value(false);</p><p> if (arguments.length > 0)<br /> return ___initOperandBoolean(this, arg);<br />};</p><p>function ___initOperandBoolean(obj, arg) {<br /> switch ($T(arg)) {<br /> case "string":<br /> {<br /> switch (arg.toLowerCase()) {<br /> case "true": obj.set_Value(true); break;<br /> case "false": obj.set_Value(false); break;<br /> default: throw new Exception(this, "__initOperandBoolean", "Invalid argument:" + arg);<br /> }// switch<br /> }<br /> break;<br /> case "boolean": obj.set_Value(arg); break;<br /> case "number": obj.set_Value(arg != 0 && !isNaN(arg) ? true : false); break;<br /> case "OperandBoolean": obj.set_Value(arg.get_Value()); break;<br /> case "OperandNumber": obj.set_Value(arg.IsValid() && arg.get_Value() != 0 ? true : false); break;<br /> case "OperandString":<br /> {<br /> switch (arg.get_Value().toLowerCase()) {<br /> case "true": obj.set_Value(true); break;<br /> case "false": obj.set_Value(false); break;<br /> default: throw new Exception(this, "__initOperandBoolean", "Invalid argument:" + arg.ToString());<br /> } // switch<br /> }<br /> break;<br /> default: throw new Exception(this, "__initOperandBoolean", "Unsupported argument:" + arg);<br /> } // switch<br />};</p><p>OperandBoolean.prototype = {<br /> Test: function() {<br /> $Debug.WriteLine("================== OperandBoolean.Test ==================");</p><p> var values = [<br /> // string<br /> "true",<br /> "false",<br /> "tRuE",<br /> "fAlSe",<br /> "abcdefg",<br /> "",<br /> // boolean<br /> true,<br /> false,<br /> // number<br /> -1,<br /> 0,<br /> 1,<br /> NaN,<br /> // OperandBoolean<br /> new OperandBoolean(true),<br /> new OperandBoolean(false),<br /> // OperandNumber<br /> new OperandNumber(-1),<br /> new OperandNumber(0),<br /> new OperandNumber(1),<br /> new OperandNumber(100),<br /> // OperandString<br /> new OperandString("true"),<br /> new OperandString("false"),<br /> new OperandString("other")<br /> ];</p><p> for (var i = 0; i < values.length; i++) {<br /> try {<br /> var operand = new OperandBoolean(values[i]);</p><p> $Debug.WriteLine("Set " + values[i].ToString() + "=" + operand.get_Value());<br /> }<br /> catch (e) {<br /> $Debug.WriteLine("Failed to set " + values[i].ToString());<br /> }<br /> }<br /> }<br />};

 

OperandString  文本類型

檔案:OperandString.js

// JScript source code<br />// ------------- String ---------------<br />function OperandString(arg) {<br /> this.DeriveFrom(new OperandBase());<br /> this.set_ClassName("OperandString");<br /> this._type = "string";</p><p> if (arguments.length > 0) {<br /> switch ($T(arg.get_ClassName)) {<br /> case "OperandString": this.set_Value(arg.get_Value()); break;<br /> default: this.set_Value(arg); break;<br /> } // switch<br /> } // if<br /> else<br /> this.set_Value("");<br />};</p><p>OperandString.prototype =<br />{<br /> IsValid: function() {<br /> return this.get_Value() != null;<br /> },<br /> Concat: function(arg) {<br /> if (arguments.length == 0)<br /> throw new Exception(this, "Argument(s) needed");</p><p> switch ($T(arg)) {<br /> case "OperandString": return new OperandString(this.get_Value() + arg.get_Value());<br /> case "OperandNumber": return new OperandString(this.get_Value() + arg.get_Value());<br /> default: return new OperandString(this.get_Value() + arg);<br /> } // switch<br /> }, // function Concate<br /> // Test<br /> Test: function() {<br /> $Debug.WriteLine("================ OperandString.Test ================");</p><p> var values = [<br /> -12345,<br /> -1,<br /> 0,<br /> 1,<br /> 12345,<br /> "ABCDEFG",<br /> "a",<br /> ""<br /> ];</p><p> for (var i = 0; i < values.length; i++) {<br /> try {<br /> var operand = new OperandString(values[i]);</p><p> $Debug.WriteLine(values[i] + "=" + operand.get_Value());<br /> } // try<br /> catch (e) {<br /> $Debug.WriteLine("Generate operand failed:" + values[i] + "/nError:" + e.discription);<br /> } // try ... catch<br /> } // for</p><p> var op = [new OperandString("Left"), new OperandString("Right")];</p><p> $Debug.WriteLine("Concate strings:" +<br /> op[0].get_Value() + " and " + op[1].get_Value() +<br /> " get " + op[0].Concat(op[1]).get_Value());<br /> } // function Test<br />};<br />

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.