private ScriptObject ResolveOperand(CodeObject value) { m_script.SetStackInfo(value.StackInfo); ScriptObject ret = ResolveOperand_impl(value); if (value.Not) { ScriptBoolean b = ret as ScriptBoolean; if (b == null) { throw new ExecutionException("Script Object Type [" + ret.Type + "] is cannot use [!] sign"); } ret = b.Inverse(); } else if (value.Negative) { ScriptNumber b = ret as ScriptNumber; if (b == null) { throw new ExecutionException("Script Object Type [" + ret.Type + "] is cannot use [-] sign"); } ret = b.Negative(); } return(ret); }
ScriptObject ParseTernary(CodeTernary ternary) { ScriptBoolean b = ResolveOperand(ternary.Allow) as ScriptBoolean; if (b == null) { throw new ExecutionException("三目运算符 条件必须是一个bool型"); } return(b.Value ? ResolveOperand(ternary.True) : ResolveOperand(ternary.False)); }
bool ProcessCondition(TempCondition con, Executable_Block block) { if (con == null) { return(false); } if (con.Allow != null) { ScriptBoolean b = ResolveOperand(con.Allow) as ScriptBoolean; if (b == null) { throw new ExecutionException("if 条件必须是一个bool型"); } if (b.Value == false) { return(false); } } con.Context.Initialize(this); con.Context.Execute(); return(true); }
public void SetThermostatHold(ScriptNumber thermostatID, ScriptBoolean hold) { Logger.Error("Not supported."); }
ScriptObject ParseOperate(CodeOperator operate) { TokenType type = operate.Operator; ScriptObject left = ResolveOperand(operate.Left); if (type == TokenType.Plus) { ScriptObject right = ResolveOperand(operate.Right); if (left is ScriptString || right is ScriptString) { return(m_script.CreateString(left.ToString() + right.ToString())); } else if (left is ScriptNumber && right is ScriptNumber) { return((left as ScriptNumber).Compute(TokenType.Plus, right as ScriptNumber)); } else { throw new ExecutionException("operate [+] left right is not same type"); } } else if (type == TokenType.Minus || type == TokenType.Multiply || type == TokenType.Divide || type == TokenType.Modulo || type == TokenType.InclusiveOr || type == TokenType.Combine || type == TokenType.XOR || type == TokenType.Shr || type == TokenType.Shi) { ScriptNumber leftNumber = left as ScriptNumber; if (leftNumber == null) { throw new ExecutionException("运算符[左边]必须是number类型"); } ScriptNumber rightNumber = ResolveOperand(operate.Right) as ScriptNumber; if (rightNumber == null) { throw new ExecutionException("运算符[右边]必须是number类型"); } return(leftNumber.Compute(type, rightNumber)); } else { if (left is ScriptBoolean) { bool b1 = ((ScriptBoolean)left).Value; if (type == TokenType.And) { if (b1 == false) { return(ScriptBoolean.False); } ScriptBoolean right = ResolveOperand(operate.Right) as ScriptBoolean; if (right == null) { throw new ExecutionException("operate [&&] right is not a bool"); } return(right.Value ? ScriptBoolean.True : ScriptBoolean.False); } else if (type == TokenType.Or) { if (b1 == true) { return(ScriptBoolean.True); } ScriptBoolean right = ResolveOperand(operate.Right) as ScriptBoolean; if (right == null) { throw new ExecutionException("operate [||] right is not a bool"); } return(right.Value ? ScriptBoolean.True : ScriptBoolean.False); } else { ScriptBoolean right = ResolveOperand(operate.Right) as ScriptBoolean; if (right == null) { throw new ExecutionException("operate [==] [!=] right is not a bool"); } bool b2 = right.Value; if (type == TokenType.Equal) { return(b1 == b2 ? ScriptBoolean.True : ScriptBoolean.False); } else if (type == TokenType.NotEqual) { return(b1 != b2 ? ScriptBoolean.True : ScriptBoolean.False); } else { throw new ExecutionException("nonsupport operate [" + type + "] with bool"); } } } else { ScriptObject right = ResolveOperand(operate.Right); if (left is ScriptNull || right is ScriptNull) { bool ret = false; if (type == TokenType.Equal) { ret = (left == right); } else if (type == TokenType.NotEqual) { ret = (left != right); } else { throw new ExecutionException("nonsupport operate [" + type + "] with null"); } return(ret ? ScriptBoolean.True : ScriptBoolean.False); } if (type == TokenType.Equal) { return(left.ObjectValue.Equals(right.ObjectValue) ? ScriptBoolean.True : ScriptBoolean.False); } else if (type == TokenType.NotEqual) { return(!left.ObjectValue.Equals(right.ObjectValue) ? ScriptBoolean.True : ScriptBoolean.False); } if (left.Type != right.Type) { throw new ExecutionException("[operate] left right is not same type"); } if (left is ScriptString) { return(((ScriptString)left).Compare(type, (ScriptString)right) ? ScriptBoolean.True : ScriptBoolean.False); } else if (left is ScriptNumber) { return(((ScriptNumber)left).Compare(type, (ScriptNumber)right) ? ScriptBoolean.True : ScriptBoolean.False); } else { throw new ExecutionException("nonsupport operate [" + type + "] with " + left.Type); } } } throw new ExecutionException("错误的操作符号 " + operate.Operator); }
/// <summary> /// Not implemented in this driver. /// </summary> /// <param name="thermostatId"></param> /// <param name="hold"></param> public void SetThermostatHold(ScriptNumber thermostatId, ScriptBoolean hold) { // Not implemented in this driver, see implementation using ScriptNumber instead }
ScriptObject ParseOperate(CodeOperator operate) { TokenType type = operate.Operator; ScriptObject left = ResolveOperand(operate.Left); if (type == TokenType.Plus) { ScriptObject right = ResolveOperand(operate.Right); if (left is ScriptString || right is ScriptString) { return(m_script.CreateString(left.ToString() + right.ToString())); } else if (left is ScriptNumber && right is ScriptNumber) { return((left as ScriptNumber).ComputePlus(right as ScriptNumber)); } else { throw new ExecutionException("operate [+] left right is not same type"); } } else if (type == TokenType.Minus || type == TokenType.Multiply || type == TokenType.Divide || type == TokenType.Modulo) { ScriptNumber leftNumber = left as ScriptNumber; if (leftNumber == null) { throw new ExecutionException("operate [+ - * /] left is not number"); } ScriptNumber rightNumber = ResolveOperand(operate.Right) as ScriptNumber; if (rightNumber == null) { throw new ExecutionException("operate [+ - * /] right is not number"); } if (operate.Operator == TokenType.Minus) { return(leftNumber.ComputeMinus(rightNumber)); } else if (operate.Operator == TokenType.Multiply) { return(leftNumber.ComputeMultiply(rightNumber)); } else if (operate.Operator == TokenType.Divide) { return(leftNumber.ComputeDivide(rightNumber)); } else if (operate.Operator == TokenType.Modulo) { return(leftNumber.ComputeModulo(rightNumber)); } } else { if (left is ScriptBoolean) { if (type == TokenType.And) { bool b1 = ((ScriptBoolean)left).Value; if (b1 == false) { return(ScriptBoolean.False); } ScriptBoolean right = ResolveOperand(operate.Right) as ScriptBoolean; if (right == null) { throw new ExecutionException("operate [&&] right is not a bool"); } return(right.Value ? ScriptBoolean.True : ScriptBoolean.False); } else if (type == TokenType.Or) { bool b1 = ((ScriptBoolean)left).Value; if (b1 == true) { return(ScriptBoolean.True); } ScriptBoolean right = ResolveOperand(operate.Right) as ScriptBoolean; if (right == null) { throw new ExecutionException("operate [||] right is not a bool"); } return(right.Value ? ScriptBoolean.True : ScriptBoolean.False); } else { bool b1 = ((ScriptBoolean)left).Value; ScriptBoolean right = ResolveOperand(operate.Right) as ScriptBoolean; if (right == null) { throw new ExecutionException("operate [==] [!=] right is not a bool"); } bool b2 = right.Value; if (type == TokenType.Equal) { return(b1 == b2 ? ScriptBoolean.True : ScriptBoolean.False); } else if (type == TokenType.NotEqual) { return(b1 != b2 ? ScriptBoolean.True : ScriptBoolean.False); } else { throw new ExecutionException("nonsupport operate [" + type + "] with bool"); } } } else { ScriptObject right = ResolveOperand(operate.Right); if (left is ScriptNull || right is ScriptNull) { bool ret = false; if (type == TokenType.Equal) { ret = (left == right); } else if (type == TokenType.NotEqual) { ret = (left != right); } else { throw new ExecutionException("nonsupport operate [" + type + "] with null"); } return(ret ? ScriptBoolean.True : ScriptBoolean.False); } if (type == TokenType.Equal) { return(left.ObjectValue.Equals(right.ObjectValue) ? ScriptBoolean.True : ScriptBoolean.False); } else if (type == TokenType.NotEqual) { return(!left.ObjectValue.Equals(right.ObjectValue) ? ScriptBoolean.True : ScriptBoolean.False); } if (left.Type != right.Type) { throw new ExecutionException("[operate] left right is not same type"); } if (left is ScriptString) { string str1 = ((ScriptString)left).Value; string str2 = ((ScriptString)right).Value; bool ret = false; if (type == TokenType.Greater) { ret = string.Compare(str1, str2) < 0; } else if (type == TokenType.GreaterOrEqual) { ret = string.Compare(str1, str2) <= 0; } else if (type == TokenType.Less) { ret = string.Compare(str1, str2) > 0; } else if (type == TokenType.LessOrEqual) { ret = string.Compare(str1, str2) >= 0; } else { throw new ExecutionException("nonsupport operate [" + type + "] with string"); } return(ret ? ScriptBoolean.True : ScriptBoolean.False); } else if (left is ScriptNumber) { return(((ScriptNumber)left).Compare(type, operate, (ScriptNumber)right) ? ScriptBoolean.True : ScriptBoolean.False); } else { throw new ExecutionException("nonsupport operate [" + type + "] with " + left.Type); } } } throw new ExecutionException("错误的操作符号 " + operate.Operator); }