diff options
Diffstat (limited to 'src/GCodes/GCodeBuffer/ExpressionParser.cpp')
-rw-r--r-- | src/GCodes/GCodeBuffer/ExpressionParser.cpp | 284 |
1 files changed, 136 insertions, 148 deletions
diff --git a/src/GCodes/GCodeBuffer/ExpressionParser.cpp b/src/GCodes/GCodeBuffer/ExpressionParser.cpp index ce5e2fc5..f6b882b6 100644 --- a/src/GCodes/GCodeBuffer/ExpressionParser.cpp +++ b/src/GCodes/GCodeBuffer/ExpressionParser.cpp @@ -113,8 +113,7 @@ void ExpressionParser::ParseInternal(ExpressionValue& val, bool evaluate, uint8_ { case TypeCode::Uint32: // Convert enumeration to integer - val.iVal = (int32_t)val.uVal; - val.SetType(TypeCode::Int32); + val.SetInt((int32_t)val.uVal); break; case TypeCode::Int32: @@ -122,8 +121,7 @@ void ExpressionParser::ParseInternal(ExpressionValue& val, bool evaluate, uint8_ break; case TypeCode::DateTime_tc: // unary + converts a DateTime to a seconds count - val.iVal = (uint32_t)val.Get56BitValue(); - val.SetType(TypeCode::Int32); + val.SetInt((uint32_t)val.Get56BitValue()); break; default: @@ -146,11 +144,11 @@ void ExpressionParser::ParseInternal(ExpressionValue& val, bool evaluate, uint8_ ParseInternal(val, evaluate, UnaryPriority); if (val.GetType() == TypeCode::CString) { - val.Set((int32_t)strlen(val.sVal)); + val.SetInt((int32_t)strlen(val.sVal)); } else if (val.GetType() == TypeCode::HeapString) { - val.Set((int32_t)val.shVal.GetLength()); + val.SetInt((int32_t)val.shVal.GetLength()); } else { @@ -336,8 +334,7 @@ void ExpressionParser::ParseInternal(ExpressionValue& val, bool evaluate, uint8_ if (val2.GetType() == TypeCode::DateTime_tc) { // Difference of two data/times - val.SetType(TypeCode::Int32); - val.iVal = (int32_t)(val.Get56BitValue() - val2.Get56BitValue()); + val.SetInt((int32_t)(val.Get56BitValue() - val2.Get56BitValue())); } else if (val2.GetType() == TypeCode::Uint32) { @@ -389,133 +386,130 @@ void ExpressionParser::ParseInternal(ExpressionValue& val, bool evaluate, uint8_ case '>': BalanceTypes(val, val2, evaluate); - switch (val.GetType()) { - case TypeCode::Int32: - val.bVal = (val.iVal > val2.iVal); - break; + bool bResult; + switch (val.GetType()) + { + case TypeCode::Int32: + bResult = (val.iVal > val2.iVal); + break; - case TypeCode::Float: - val.bVal = (val.fVal > val2.fVal); - break; + case TypeCode::Float: + bResult = (val.fVal > val2.fVal); + break; - case TypeCode::DateTime_tc: - val.bVal = val.Get56BitValue() > val2.Get56BitValue(); - break; + case TypeCode::DateTime_tc: + bResult = val.Get56BitValue() > val2.Get56BitValue(); + break; - case TypeCode::Bool: - val.bVal = (val.bVal && !val2.bVal); - break; + case TypeCode::Bool: + bResult = (val.bVal && !val2.bVal); + break; - default: - if (evaluate) - { - ThrowParseException("expected numeric or Boolean operands to comparison operator"); + default: + if (evaluate) + { + ThrowParseException("expected numeric or Boolean operands to comparison operator"); + } + bResult = false; + break; } - val.bVal = false; - break; - } - val.SetType(TypeCode::Bool); - if (invert) - { - val.bVal = !val.bVal; + val.SetBool((invert) ? !bResult : bResult); } break; case '<': BalanceTypes(val, val2, evaluate); - switch (val.GetType()) { - case TypeCode::Int32: - val.bVal = (val.iVal < val2.iVal); - break; - - case TypeCode::Float: - val.bVal = (val.fVal < val2.fVal); - break; - - case TypeCode::DateTime_tc: - val.bVal = val.Get56BitValue() < val2.Get56BitValue(); - break; - - case TypeCode::Bool: - val.bVal = (!val.bVal && val2.bVal); - break; - - default: - if (evaluate) - { - ThrowParseException("expected numeric or Boolean operands to comparison operator"); - } - val.bVal = false; - break; - } - val.SetType(TypeCode::Bool); - if (invert) - { - val.bVal = !val.bVal; - } - break; - - case '=': - // Before balancing, handle comparisons with null - if (val.GetType() == TypeCode::None) - { - val.bVal = (val2.GetType() == TypeCode::None); - } - else if (val2.GetType() == TypeCode::None) - { - val.bVal = false; - } - else - { - BalanceTypes(val, val2, evaluate); + bool bResult; switch (val.GetType()) { - case TypeCode::ObjectModel_tc: - ThrowParseException("cannot compare objects"); - case TypeCode::Int32: - val.bVal = (val.iVal == val2.iVal); - break; - - case TypeCode::Uint32: - val.bVal = (val.uVal == val2.uVal); + bResult = (val.iVal < val2.iVal); break; case TypeCode::Float: - val.bVal = (val.fVal == val2.fVal); + bResult = (val.fVal < val2.fVal); break; case TypeCode::DateTime_tc: - val.bVal = val.Get56BitValue() == val2.Get56BitValue(); + bResult = val.Get56BitValue() < val2.Get56BitValue(); break; case TypeCode::Bool: - val.bVal = (val.bVal == val2.bVal); - break; - - case TypeCode::CString: - val.bVal = (strcmp(val.sVal, (val2.GetType() == TypeCode::HeapString) ? val2.shVal.Get().Ptr() : val2.sVal) == 0); - break; - - case TypeCode::HeapString: - val.bVal = (strcmp(val.shVal.Get().Ptr(), (val2.GetType() == TypeCode::HeapString) ? val2.shVal.Get().Ptr() : val2.sVal) == 0); + bResult = (!val.bVal && val2.bVal); break; default: if (evaluate) { - ThrowParseException("unexpected operand type to equality operator"); + ThrowParseException("expected numeric or Boolean operands to comparison operator"); } - val.bVal = false; + bResult = false; break; } + val.SetBool((invert) ? !bResult : bResult); } - val.SetType(TypeCode::Bool); - if (invert) + break; + + case '=': { - val.bVal = !val.bVal; + bool bResult; + // Before balancing, handle comparisons with null + if (val.GetType() == TypeCode::None) + { + bResult = (val2.GetType() == TypeCode::None); + } + else if (val2.GetType() == TypeCode::None) + { + bResult = false; + } + else + { + BalanceTypes(val, val2, evaluate); + switch (val.GetType()) + { + case TypeCode::ObjectModel_tc: + ThrowParseException("cannot compare objects"); + + case TypeCode::Int32: + bResult = (val.iVal == val2.iVal); + break; + + case TypeCode::Uint32: + bResult = (val.uVal == val2.uVal); + break; + + case TypeCode::Float: + bResult = (val.fVal == val2.fVal); + break; + + case TypeCode::DateTime_tc: + bResult = val.Get56BitValue() == val2.Get56BitValue(); + break; + + case TypeCode::Bool: + bResult = (val.bVal == val2.bVal); + break; + + case TypeCode::CString: + bResult = (strcmp(val.sVal, (val2.GetType() == TypeCode::HeapString) ? val2.shVal.Get().Ptr() : val2.sVal) == 0); + break; + + case TypeCode::HeapString: + bResult = (strcmp(val.shVal.Get().Ptr(), (val2.GetType() == TypeCode::HeapString) ? val2.shVal.Get().Ptr() : val2.sVal) == 0); + break; + + default: + if (evaluate) + { + ThrowParseException("unexpected operand type to equality operator"); + } + bResult = false; + break; + } + } + val.SetBool((invert) ? !bResult : bResult); } break; @@ -536,7 +530,7 @@ void ExpressionParser::ParseInternal(ExpressionValue& val, bool evaluate, uint8_ val.AppendAsString(str.GetRef()); val2.AppendAsString(str.GetRef()); StringHandle sh(str.c_str()); - val.Set(sh); + val.SetStringHandle(sh); } bool ExpressionParser::ParseBoolean() THROWS(GCodeException) @@ -673,8 +667,8 @@ void ExpressionParser::BalanceNumericTypes(ExpressionValue& val1, ExpressionValu { ThrowParseException("expected numeric operands"); } - val1.Set((int32_t)0); - val2.Set((int32_t)0); + val1.SetInt(0); + val2.SetInt(0); } } @@ -734,34 +728,29 @@ void ExpressionParser::BalanceTypes(ExpressionValue& val1, ExpressionValue& val2 { ThrowParseException("cannot convert operands to same type"); } - val1.Set((int32_t)0); - val2.Set((int32_t)0); + val1.SetInt(0); + val2.SetInt(0); } } void ExpressionParser::ConvertToFloat(ExpressionValue& val, bool evaluate) const THROWS(GCodeException) { + float fVal; switch (val.GetType()) { + case TypeCode::Float: + return; // no conversion needed, leave the precision alone + case TypeCode::Uint32: - val.SetType(TypeCode::Float); - val.fVal = (float)val.uVal; - val.param = 1; + fVal = (float)val.uVal; break; case TypeCode::Uint64: - val.SetType(TypeCode::Float); - val.fVal = (float)val.Get56BitValue(); - val.param = 1; + fVal = (float)val.Get56BitValue(); break; case TypeCode::Int32: - val.fVal = (float)val.iVal; - val.SetType(TypeCode::Float); - val.param = 1; - break; - - case TypeCode::Float: + fVal = (float)val.iVal; break; default: @@ -769,8 +758,10 @@ void ExpressionParser::ConvertToFloat(ExpressionValue& val, bool evaluate) const { ThrowParseException("expected numeric operand"); } - val.Set(0.0f, 1); + fVal = 0.0f; + break; } + val.SetFloat(fVal, 1); } void ExpressionParser::ConvertToBool(ExpressionValue& val, bool evaluate) const THROWS(GCodeException) @@ -781,7 +772,7 @@ void ExpressionParser::ConvertToBool(ExpressionValue& val, bool evaluate) const { ThrowParseException("expected Boolean operand"); } - val.Set(false); + val.SetBool(false); } } @@ -794,11 +785,11 @@ void ExpressionParser::ConvertToString(ExpressionValue& val, bool evaluate) noex String<MaxStringExpressionLength> str; val.AppendAsString(str.GetRef()); StringHandle sh(str.c_str()); - val.Set(sh); + val.SetStringHandle(sh); } else { - val.Set(""); + val.SetCString(""); } } } @@ -812,9 +803,9 @@ void ExpressionParser::ConvertToDriverId(ExpressionValue& val, bool evaluate) co case TypeCode::Int32: #if SUPPORT_CAN_EXPANSION - val.Set(DriverId(0, val.uVal)); + val.SetDriverId(DriverId(0, val.uVal)); #else - val.Set(DriverId(val.uVal)); + val.SetDriverId(DriverId(val.uVal)); #endif break; @@ -825,12 +816,12 @@ void ExpressionParser::ConvertToDriverId(ExpressionValue& val, bool evaluate) co #if SUPPORT_CAN_EXPANSION if (ival >= 0 && fabsf(f10val - (float)ival) <= 0.002) { - val.Set(DriverId(ival/10, ival % 10)); + val.SetDriverId(DriverId(ival/10, ival % 10)); } #else if (ival >= 0 && ival < 10 && fabsf(f10val - (float)ival) <= 0.002) { - val.Set(DriverId(ival % 10)); + val.SetDriverId(DriverId(ival % 10)); } #endif else @@ -874,11 +865,11 @@ void ExpressionParser::ParseNumber(ExpressionValue& rslt) noexcept if (conv.FitsInInt32()) { - rslt.Set(conv.GetInt32()); + rslt.SetInt(conv.GetInt32()); } else { - rslt.Set(conv.GetFloat(), constrain<unsigned int>(conv.GetDigitsAfterPoint(), 1, MaxFloatDigitsDisplayedAfterPoint)); + rslt.SetFloat(conv.GetFloat(), constrain<unsigned int>(conv.GetDigitsAfterPoint(), 1, MaxFloatDigitsDisplayedAfterPoint)); } } @@ -917,7 +908,7 @@ void ExpressionParser::ParseIdentifierExpression(ExpressionValue& rslt, bool eva { ThrowParseException("expected integer expression"); } - index.Set((int32_t)0); + index.SetInt(0); } AdvancePointer(); // skip the ']' context.ProvideIndex(index.iVal); @@ -941,19 +932,19 @@ void ExpressionParser::ParseIdentifierExpression(ExpressionValue& rslt, bool eva switch (whichConstant.RawValue()) { case NamedConstant::_true: - rslt.Set(true); + rslt.SetBool(true); return; case NamedConstant::_false: - rslt.Set(false); + rslt.SetBool(false); return; case NamedConstant::_null: - rslt.Set(nullptr); + rslt.SetNull(nullptr); return; case NamedConstant::pi: - rslt.Set(Pi); + rslt.SetFloat(Pi); return; case NamedConstant::iterations: @@ -963,7 +954,7 @@ void ExpressionParser::ParseIdentifierExpression(ExpressionValue& rslt, bool eva { ThrowParseException("'iterations' used when not inside a loop"); } - rslt.Set(v); + rslt.SetInt(v); } return; @@ -985,12 +976,12 @@ void ExpressionParser::ParseIdentifierExpression(ExpressionValue& rslt, bool eva res = 2; break; } - rslt.Set(res); + rslt.SetInt(res); } return; case NamedConstant::line: - rslt.Set((int32_t)gb.GetLineNumber()); + rslt.SetInt((int32_t)gb.GetLineNumber()); return; default: @@ -1043,7 +1034,7 @@ void ExpressionParser::ParseIdentifierExpression(ExpressionValue& rslt, bool eva { ThrowParseException("expected numeric operand"); } - rslt.Set((int32_t)0); + rslt.SetInt(0); } break; @@ -1122,8 +1113,7 @@ void ExpressionParser::ParseIdentifierExpression(ExpressionValue& rslt, bool eva case Function::isnan: ConvertToFloat(rslt, evaluate); - rslt.SetType(TypeCode::Bool); - rslt.bVal = (std::isnan(rslt.fVal) != 0); + rslt.SetBool(std::isnan(rslt.fVal) != 0); break; case Function::floor: @@ -1132,8 +1122,7 @@ void ExpressionParser::ParseIdentifierExpression(ExpressionValue& rslt, bool eva const float f = floorf(rslt.fVal); if (f <= (float)std::numeric_limits<int32_t>::max() && f >= (float)std::numeric_limits<int32_t>::min()) { - rslt.SetType(TypeCode::Int32); - rslt.iVal = (int32_t)f; + rslt.SetInt((int32_t)f); } else { @@ -1237,7 +1226,7 @@ void ExpressionParser::ParseIdentifierExpression(ExpressionValue& rslt, bool eva { ThrowParseException("expected positive integer"); } - rslt.Set((int32_t)random(limit)); + rslt.SetInt((int32_t)random(limit)); } break; @@ -1270,8 +1259,7 @@ void ExpressionParser::ParseIdentifierExpression(ExpressionValue& rslt, bool eva default: ThrowParseException("can't convert value to DateTime"); } - rslt.SetType(TypeCode::DateTime_tc); - rslt.Set56BitValue(val); + rslt.SetDateTime(val); } break; @@ -1316,7 +1304,7 @@ void ExpressionParser::ParseIdentifierExpression(ExpressionValue& rslt, bool eva // "exists(global)" will anyway because "global" is a root key in the object model. Handle the other two here. if (applyExists && (strcmp(id.c_str(), "param") == 0 || strcmp(id.c_str(), "var") == 0)) { - rslt.Set(true); + rslt.SetBool(true); return; } @@ -1329,7 +1317,7 @@ void ExpressionParser::ParseIdentifierExpression(ExpressionValue& rslt, bool eva } return; } - rslt.Set(nullptr); + rslt.SetNull(nullptr); } // Parse a string to a DateTime @@ -1349,7 +1337,7 @@ void ExpressionParser::GetVariableValue(ExpressionValue& rslt, const VariableSet const Variable* var = vars->Lookup(name); if (wantExists) { - rslt.Set(var != nullptr); + rslt.SetBool(var != nullptr); return; } @@ -1381,7 +1369,7 @@ void ExpressionParser::ParseQuotedString(ExpressionValue& rslt) THROWS(GCodeExce if (CurrentCharacter() != c) { StringHandle sh(str.c_str()); - rslt.Set(sh); + rslt.SetStringHandle(sh); return; } AdvancePointer(); |