OperandBase 操作数基类型
文件:OperandBase.js
// JScript source code // ----------- OperandBase --------------- function OperandBase(arg) { this.set_ClassName("OperandBase"); this._type = "undefined"; this._value = null; if (arguments.length > 0) return __operandCopy(arg); }; OperandBase.prototype = { get_Type: function() { return this._type; }, get_Value: function() { return this._value; }, set_Value: function(val) { this._value = val; }, IsValid: function() { return this._value != null; }, IsNumber: function() { return this._type == "number"; }, IsBoolean: function() { return this._type == "boolean"; }, IsString: function() { return this._type == "string"; }, ToString: function() { return this.IsValid() ? this._value.toString() : "null"; } }; // Perform a copying operation. function __operandCopy(arg) { switch ($T(arg)) { case "string": return new OperandString(arg); case "number": return new OperandNumber(arg); case "boolean": return new OperandBoolean(arg); case "OperandNumber": return new OperandNumber(arg.get_Value()); case "OperandBoolean": return new OperandBoolean(arg.get_Value()); case "OperandString": return new OperandString(arg.get_Value()); default: throw new Exception(this, "__operandCopy", "Unsupported type:" + $T(arg)); } // switch };
OperandNumber 数值类型
文件:OperandNumber.js
// JScript source code // ----------- Decimal ------------- function OperandNumber(arg) { this.DeriveFrom(new OperandBase()); this.set_ClassName("OperandNumber"); this._type = "number"; this.set_Value(NaN); if (arguments.length > 0) __initOperandNumber(this, arg); }; OperandNumber.prototype.ToString = function() { return this.get_Value().ToString(); }; OperandNumber.prototype.IsValid = function() { return this.get_Value() != null && !isNaN(this.get_Value()) && isFinite(this.get_Value()); }; function __initOperandNumber(obj, arg) { switch ($T(arg)) { case "OperandNumber": obj.set_Value(arg.get_Value()); break; case "OperandBoolean": obj.set_Value(arg.get_Value() ? 1 : 0); break; case "OperandString": obj.set_Value(parseFloat(arg.get_Value())); break; case "string": { var str = arg.toLowerCase(); if (str == "true") obj.set_Value(1); else if (str == "false") obj.set_Value(0); else try { obj.set_Value(parseFloat(arg)); } catch (e) { } } break; case "boolean": obj.set_Value(arg ? 1 : 0); break; case "number": case "float": { if (isNaN(arg)) throw new Exception(this, "__initOperandNumber", arg + " is not a number"); if (!isFinite(arg)) throw new Exception(this, "__initOperandNumber", arg + " is not finite"); obj.set_Value(arg); } break; default: throw new Exception(this, "Unspported argument:" + arguments[0].ToString()); break; } // switch }; OperandNumber.prototype.Test = function() { $Debug.WriteLine("================ OperandNumber.Test ================"); var values = [ -12345, -1, 0, 1, 12345, "ABCDEFG", "1", "1.1", "true", "false", 1.2, // Boolean true, false, // OperandNumber new OperandNumber(1), new OperandString("1"), new OperandString("1.1") ]; for (var i = 0; i < values.length; i++) { try { var operand = new OperandNumber(values[i]); $Debug.WriteLine(values[i] + "=" + operand.get_Value()); } // try catch (e) { $Debug.WriteLine("Generate operand failed:" + values[i].ToString() + "/nError:" + e.discription); } // try ... catch } // for }; // function Test
OperandBoolean 布尔类型
文件:OperandBoolean.js
function OperandBoolean(arg) { this.DeriveFrom(new OperandBase()); this.set_ClassName("OperandBoolean"); this._type = "boolean"; this.set_Value(false); if (arguments.length > 0) return ___initOperandBoolean(this, arg); }; function ___initOperandBoolean(obj, arg) { switch ($T(arg)) { case "string": { switch (arg.toLowerCase()) { case "true": obj.set_Value(true); break; case "false": obj.set_Value(false); break; default: throw new Exception(this, "__initOperandBoolean", "Invalid argument:" + arg); }// switch } break; case "boolean": obj.set_Value(arg); break; case "number": obj.set_Value(arg != 0 && !isNaN(arg) ? true : false); break; case "OperandBoolean": obj.set_Value(arg.get_Value()); break; case "OperandNumber": obj.set_Value(arg.IsValid() && arg.get_Value() != 0 ? true : false); break; case "OperandString": { switch (arg.get_Value().toLowerCase()) { case "true": obj.set_Value(true); break; case "false": obj.set_Value(false); break; default: throw new Exception(this, "__initOperandBoolean", "Invalid argument:" + arg.ToString()); } // switch } break; default: throw new Exception(this, "__initOperandBoolean", "Unsupported argument:" + arg); } // switch }; OperandBoolean.prototype = { Test: function() { $Debug.WriteLine("================== OperandBoolean.Test =================="); var values = [ // string "true", "false", "tRuE", "fAlSe", "abcdefg", "", // boolean true, false, // number -1, 0, 1, NaN, // OperandBoolean new OperandBoolean(true), new OperandBoolean(false), // OperandNumber new OperandNumber(-1), new OperandNumber(0), new OperandNumber(1), new OperandNumber(100), // OperandString new OperandString("true"), new OperandString("false"), new OperandString("other") ]; for (var i = 0; i < values.length; i++) { try { var operand = new OperandBoolean(values[i]); $Debug.WriteLine("Set " + values[i].ToString() + "=" + operand.get_Value()); } catch (e) { $Debug.WriteLine("Failed to set " + values[i].ToString()); } } } };
OperandString 文本类型
文件:OperandString.js
// JScript source code // ------------- String --------------- function OperandString(arg) { this.DeriveFrom(new OperandBase()); this.set_ClassName("OperandString"); this._type = "string"; if (arguments.length > 0) { switch ($T(arg.get_ClassName)) { case "OperandString": this.set_Value(arg.get_Value()); break; default: this.set_Value(arg); break; } // switch } // if else this.set_Value(""); }; OperandString.prototype = { IsValid: function() { return this.get_Value() != null; }, Concat: function(arg) { if (arguments.length == 0) throw new Exception(this, "Argument(s) needed"); switch ($T(arg)) { case "OperandString": return new OperandString(this.get_Value() + arg.get_Value()); case "OperandNumber": return new OperandString(this.get_Value() + arg.get_Value()); default: return new OperandString(this.get_Value() + arg); } // switch }, // function Concate // Test Test: function() { $Debug.WriteLine("================ OperandString.Test ================"); var values = [ -12345, -1, 0, 1, 12345, "ABCDEFG", "a", "" ]; for (var i = 0; i < values.length; i++) { try { var operand = new OperandString(values[i]); $Debug.WriteLine(values[i] + "=" + operand.get_Value()); } // try catch (e) { $Debug.WriteLine("Generate operand failed:" + values[i] + "/nError:" + e.discription); } // try ... catch } // for var op = [new OperandString("Left"), new OperandString("Right")]; $Debug.WriteLine("Concate strings:" + op[0].get_Value() + " and " + op[1].get_Value() + " get " + op[0].Concat(op[1]).get_Value()); } // function Test };