From 84280e3cf7ea4744555ce528b1c9b8641c0999dc Mon Sep 17 00:00:00 2001 From: hama Date: Mon, 7 Jul 2014 00:20:33 +0200 Subject: [PATCH] day's work --- rplexpr/rplasclasses.cpp | 72 ++++++++++++++++-- rplexpr/rplasclasses.hpp | 14 +++- rplexpr/rplastree.cpp | 155 +++++++++++++++++++++++++++++---------- rplexpr/rplastree.hpp | 46 ++++++++++-- 4 files changed, 232 insertions(+), 55 deletions(-) diff --git a/rplexpr/rplasclasses.cpp b/rplexpr/rplasclasses.cpp index 25f29e9..72f2a60 100644 --- a/rplexpr/rplasclasses.cpp +++ b/rplexpr/rplasclasses.cpp @@ -17,6 +17,7 @@ RplASInteger RplASInteger::m_instance; RplASString RplASString::m_instance; RplASBoolean RplASBoolean::m_instance; RplASVoid RplASVoid::m_instance; +RplASFormula RplASFormula::m_instance; /** @class RplSymbolSpace rplastree.hpp "rplexpr/rplastree.hpp" * @@ -66,6 +67,7 @@ RplSymbolSpace::RplSymbolSpace(RplSymbolSpace::SymbolSpaceType type, m_classes[RplASList::m_instance.name()] = &RplASList::m_instance; m_classes[RplASMap::m_instance.name()] = &RplASMap::m_instance; m_classes[RplASVoid::m_instance.name()] = &RplASVoid::m_instance; + m_classes[RplASFormula::m_instance.name()] = &RplASFormula::m_instance; } } @@ -488,11 +490,11 @@ RplASList::RplASList() : */ void* RplASList::newValueInstance(void* source) const { - ListValueType* rc = new ListValueType(); + ListOfVariants* rc = new ListOfVariants(); if (source != NULL){ - ListValueType* source2 = (ListValueType*) source; + ListOfVariants* source2 = (ListOfVariants*) source; rc->reserve(source2->size()); - ListValueType::iterator it; + ListOfVariants::iterator it; for (it = source2->begin(); it != source2->end(); it++){ @@ -512,7 +514,7 @@ void* RplASList::newValueInstance(void* source) const */ void RplASList::destroyValueInstance(void* object) const { - delete static_cast(object); + delete static_cast(object); } /** @@ -526,7 +528,7 @@ bool RplASList::boolValueOf(void* object) const { bool rc = false; if (object != NULL){ - ListValueType* list = static_cast(object); + ListOfVariants* list = static_cast(object); if (list == NULL) throw RplException("RplASList.boolValueOf(): not a list"); rc = ! list->empty(); @@ -545,8 +547,8 @@ QString RplASList::toString(void* object, int maxLength) const QString rc; rc.reserve(maxLength); rc += "["; - ListValueType* list = reinterpret_cast(object); - ListValueType::iterator it; + ListOfVariants* list = reinterpret_cast(object); + ListOfVariants::iterator it; bool first = true; for(it = list->begin(); it != list->end(); it++){ if (first) @@ -767,3 +769,59 @@ QString RplASVoid::toString(void*, int) const { return QString(""); } + +/** @class RplASFormula rplastree.hpp "rplexpr/rplastree.hpp" + * + * @brief Implements a data type representing a calculated value. + */ + +RplASFormula::RplASFormula() : + RplASClass("Formula") +{ +} + +/** + * @brief Instantiates a new object. + * + * In this case we do nothing. + * + * @param source ignored + * @return + */ +void*RplASFormula::newValueInstance(void*) const +{ + return NULL; +} + +/** + * @brief Destroys an object created by newValueInstance. + * + * In this case we do nothing. + * + * @param object not used + */ +void RplASFormula::destroyValueInstance(void*) const +{ +} + +/** + * @brief Returns the bool value of the given object + * @param object ignored + * @return false + */ +bool RplASFormula::boolValueOf(void*) const +{ + return false; +} + +/** + * @brief Converts the object into a string. + * + * @param object ignored + * @param maxLength ignored + * @return the empty string + */ +QString RplASFormula::toString(void*, int) const +{ + return QString(""); +} diff --git a/rplexpr/rplasclasses.hpp b/rplexpr/rplasclasses.hpp index f4bf433..1eca844 100644 --- a/rplexpr/rplasclasses.hpp +++ b/rplexpr/rplasclasses.hpp @@ -113,7 +113,7 @@ public: class RplASList : public RplASClass { public: - typedef QList ListValueType; + typedef QList ListOfVariants; public: RplASList(); public: @@ -151,5 +151,17 @@ public: static RplASVoid m_instance; }; +class RplASFormula : public RplASClass { +public: + RplASFormula(); +public: + void* newValueInstance(void* source = NULL) const; + void destroyValueInstance(void* object) const; + virtual bool boolValueOf(void* object) const; + virtual QString toString(void *object, int maxLength = 80) const; +public: + static RplASFormula m_instance; +}; + #endif // RPLASCLASSES_HPP diff --git a/rplexpr/rplastree.cpp b/rplexpr/rplastree.cpp index b6f6c5f..3faf305 100644 --- a/rplexpr/rplastree.cpp +++ b/rplexpr/rplastree.cpp @@ -79,7 +79,7 @@ RplASException::RplASException() : * */ RplASVariant::RplASVariant() : - m_dataType(DT_UNDEF), + m_variantType(VT_UNDEF), m_flags(VF_UNDEF), // m_value(), m_class(NULL) @@ -91,7 +91,7 @@ RplASVariant::RplASVariant() : RplASVariant::~RplASVariant() { destroyValue(); - m_dataType = DT_UNDEF; + m_variantType = VT_UNDEF; } /** @@ -99,7 +99,7 @@ RplASVariant::~RplASVariant() * @param source the source to copy */ RplASVariant::RplASVariant(const RplASVariant& source): - m_dataType(source.m_dataType), + m_variantType(source.m_variantType), m_flags(source.m_flags), // m_value m_class(source.m_class) @@ -116,7 +116,7 @@ RplASVariant::RplASVariant(const RplASVariant& source): RplASVariant&RplASVariant::operator=(const RplASVariant& source) { destroyValue(); - m_dataType = source.m_dataType; + m_variantType = source.m_variantType; m_flags = source.m_flags; m_class = source.m_class; copyValue(source); @@ -129,18 +129,18 @@ RplASVariant&RplASVariant::operator=(const RplASVariant& source) */ void RplASVariant::copyValue(const RplASVariant& source) { - switch(source.m_dataType) + switch(source.m_variantType) { - case DT_BOOL: + case VT_BOOL: m_value.m_bool = source.m_value.m_bool; break; - case DT_FLOAT: + case VT_FLOAT: m_value.m_float = source.m_value.m_float; break; - case DT_INTEGER: + case VT_INTEGER: m_value.m_int = source.m_value.m_int; break; - case DT_UNDEF: + case VT_UNDEF: break; default: m_value.m_object = m_class->newValueInstance(source.m_value.m_object); @@ -152,12 +152,12 @@ void RplASVariant::copyValue(const RplASVariant& source) */ void RplASVariant::destroyValue() { - switch(m_dataType) + switch(m_variantType) { - case DT_BOOL: - case DT_FLOAT: - case DT_INTEGER: - case DT_UNDEF: + case VT_BOOL: + case VT_FLOAT: + case VT_INTEGER: + case VT_UNDEF: break; default: if ((m_flags & VF_IS_COPY) == 0) @@ -166,6 +166,26 @@ void RplASVariant::destroyValue() break; } } +/** + * @brief Returns the variantType of the instance. + * + * @return the variant type + */ +RplASVariant::VariantType RplASVariant::variantType() const +{ + return m_variantType; +} + +/** + * @brief Returns the class (data type) of the instance. + * + * @return the variant type + */ +RplASClass* RplASVariant::getClass() const +{ + return m_class; +} + /** * @brief Returns the numeric value. @@ -176,9 +196,9 @@ void RplASVariant::destroyValue() */ qreal RplASVariant::asFloat() const { - if (m_dataType != DT_FLOAT) + if (m_variantType != VT_FLOAT) throw RplException("RplASVariant::asNumber: not a number: %d", - m_dataType); + m_variantType); return m_value.m_float; } /** @@ -190,9 +210,9 @@ qreal RplASVariant::asFloat() const */ int RplASVariant::asInt() const { - if (m_dataType != DT_INTEGER) + if (m_variantType != VT_INTEGER) throw RplException("RplASVariant::asInt: not an integer: %d", - m_dataType); + m_variantType); return m_value.m_int; } @@ -205,9 +225,9 @@ int RplASVariant::asInt() const */ bool RplASVariant::asBool() const { - if (m_dataType != DT_BOOL) + if (m_variantType != VT_BOOL) throw RplException("RplASVariant::asBool: not a boolean: %d", - m_dataType); + m_variantType); return m_value.m_bool; } @@ -221,9 +241,9 @@ bool RplASVariant::asBool() const */ void* RplASVariant::asObject(const RplASClass** clazz) const { - if (m_dataType != DT_OBJECT) + if (m_variantType != VT_OBJECT) throw RplException("RplASVariant::asObject: not an object: %d", - m_dataType); + m_variantType); if (clazz != NULL) *clazz = m_class; return m_value.m_object; @@ -255,7 +275,7 @@ const QString* RplASVariant::asString() const void RplASVariant::setFloat(qreal number) { destroyValue(); - m_dataType = DT_FLOAT; + m_variantType = VT_FLOAT; m_value.m_float = number; m_class = &RplASFloat::m_instance; } @@ -268,7 +288,7 @@ void RplASVariant::setFloat(qreal number) void RplASVariant::setInt(int integer) { destroyValue(); - m_dataType = DT_INTEGER; + m_variantType = VT_INTEGER; m_value.m_int = integer; m_class = &RplASInteger::m_instance; } @@ -281,7 +301,7 @@ void RplASVariant::setInt(int integer) void RplASVariant::setBool(bool value) { destroyValue(); - m_dataType = DT_BOOL; + m_variantType = VT_BOOL; m_value.m_bool = value; m_class = &RplASBoolean::m_instance; } @@ -305,22 +325,22 @@ void RplASVariant::setString(const QString& string) QString RplASVariant::toString(int maxLength) const { QString rc; - switch(m_dataType) + switch(m_variantType) { - case DT_BOOL: + case VT_BOOL: rc = m_value.m_bool ? "True" : "False"; break; - case DT_FLOAT: + case VT_FLOAT: rc.sprintf("%f", m_value.m_float); break; - case DT_INTEGER: + case VT_INTEGER: rc.sprintf("%d", m_value.m_int); break; - case DT_OBJECT: + case VT_OBJECT: rc = m_class->toString(m_value.m_object, maxLength); break; default: - case DT_UNDEF: + case VT_UNDEF: rc = "None"; break; } @@ -335,7 +355,7 @@ QString RplASVariant::toString(int maxLength) const void RplASVariant::setObject(void* object, const RplASClass* clazz) { destroyValue(); - m_dataType = DT_OBJECT; + m_variantType = VT_OBJECT; m_value.m_object = object; m_class = clazz; } @@ -494,6 +514,66 @@ RplASVariant& RplASConstant::value() return m_value; } +/** @class RplASListConstant rplastree.hpp "rplexpr/rplastree.hpp" + * + * @brief Implements a container for constant list entries. + * + */ + +/** + * @brief Constructor. + */ +RplASListConstant::RplASListConstant() : + RplASNode1(AST_LIST_CONSTANT), + RplASCalculable() +{ +} + +/** + * @brief Calculates the expression. + * + * @param value OUT: the calculated value + */ +void RplASListConstant::calc(RplASVariant& value) +{ + value = m_value; +} + +/** + * @brief Writes the internals into a file. + * + * @param fp target file + * @param indent nesting level + */ +void RplASListConstant::dump(FILE* fp, int indent) +{ + DEFINE_TABS(indent); + char buffer[256]; + fprintf(fp, "%slistConst id: %d %s\n", tabs, m_id, + positionStr(buffer, sizeof buffer)); + + RplASList::ListOfVariants* list = (RplASList::ListOfVariants*) + m_value.asObject(); + QString sValue = m_value.toString(8092); + fprintf(fp, "%s\t%s\n", tabs, sValue.toUtf8().constData()); +} + +/** + * @brief Returns the value of the constant. + * + * This method will be used to set the value of the constant: + *
RplASConstant constant;
+ * constant.value().setString("Jonny");
+ *
+ * + * @return the internal value + */ +RplASVariant& RplASListConstant::value() +{ + return m_value; +} + + /** @class RplASNamedValue rplastree.hpp "rplexpr/rplastree.hpp" * * @brief Implements a named values, a constant or a variable @@ -1015,14 +1095,14 @@ bool RplASCondition::calcAsBool() bool rc = false; RplASVariant value; calc(value); - switch(value.m_dataType){ - case RplASVariant::DT_FLOAT: + switch(value.m_variantType){ + case RplASVariant::VT_FLOAT: rc = value.m_value.m_float == 0; break; - case RplASVariant::DT_BOOL: + case RplASVariant::VT_BOOL: rc = value.m_value.m_bool; break; - case RplASVariant::DT_OBJECT: + case RplASVariant::VT_OBJECT: rc = value.m_class->boolValueOf(value.m_value.m_object); break; default: @@ -1798,6 +1878,3 @@ RplASArgument::RplASArgument() : RplASNode2(AST_ARGUMENT) { } - - - diff --git a/rplexpr/rplastree.hpp b/rplexpr/rplastree.hpp index 9d9d825..68a6183 100644 --- a/rplexpr/rplastree.hpp +++ b/rplexpr/rplastree.hpp @@ -13,6 +13,10 @@ enum RplASItemType { AST_UNDEF, AST_CONSTANT, + AST_LIST_CONSTANT, + AST_LIST_ENTRY, + AST_MAP_CONSTANT, + AST_MAP_ENTRY, AST_NAMED_VALUE, AST_VAR_DEFINITION, AST_EXPR_STATEMENT, @@ -50,12 +54,12 @@ class RplASCondition; class RplASVariant { public: - enum DataType { - DT_UNDEF, - DT_FLOAT, - DT_INTEGER, - DT_BOOL, - DT_OBJECT + enum VariantType { + VT_UNDEF, + VT_FLOAT, + VT_INTEGER, + VT_BOOL, + VT_OBJECT }; enum VariantFlags { VF_UNDEF, @@ -82,12 +86,13 @@ public: void setObject(void* object, const RplASClass* clazz); void setString(const QString& string); QString toString(int maxLength = 80) const; - QString repr() const; + VariantType variantType() const; + RplASClass* getClass() const; protected: void copyValue(const RplASVariant& source); void destroyValue(); private: - DataType m_dataType:8; + VariantType m_variantType:8; /// bitmap of VF_... flags: int m_flags:8; union { @@ -227,6 +232,31 @@ protected: RplASItem* m_child5; }; +class RplASListConstant : public RplASNode1, public RplASCalculable +{ +public: + RplASListConstant(); +public: + virtual void calc(RplASVariant& value); +public: + virtual void dump(FILE* fp, int indent); + RplASVariant& value(); +private: + RplASVariant m_value; +}; +class RplASMapConstant : public RplASNode1, public RplASCalculable +{ +public: + RplASMapConstant(); +public: + virtual void calc(RplASVariant& value); +public: + virtual void dump(FILE* fp, int indent); + RplASVariant& value(); +private: + RplASVariant m_value; +}; + class RplASNamedValue : public RplASNode1, public RplASCalculable { public: -- 2.39.5