]> gitweb.hamatoma.de Git - reqt/commitdiff
implementation of Abstract Syntax Tree
authorhama <hama@siduction.net>
Fri, 20 Jun 2014 23:50:49 +0000 (01:50 +0200)
committerhama <hama@siduction.net>
Fri, 20 Jun 2014 23:50:49 +0000 (01:50 +0200)
rplexpr/rplasclasses.cpp [new file with mode: 0644]
rplexpr/rplasclasses.hpp [new file with mode: 0644]
rplexpr/rplastree.cpp
rplexpr/rplastree.hpp
rplexpr/rplexpr.hpp
rplstatic/rplstatic.pro
unittests/unittests.pro

diff --git a/rplexpr/rplasclasses.cpp b/rplexpr/rplasclasses.cpp
new file mode 100644 (file)
index 0000000..f37101d
--- /dev/null
@@ -0,0 +1,183 @@
+/*
+ * Licence:
+ * You can use and modify this file without any restriction.
+ * There is no warranty.
+ * You also can use the licence from http://www.wtfpl.net/.
+ * The original sources can be found on https://github.com/republib.
+*/
+
+
+#include "rplcore/rplcore.hpp"
+#include "rplexpr/rplexpr.hpp"
+
+/** @class RplASList rplastree.hpp "rplexpr/rplastree.hpp"
+ *
+ * @brief Implements the class of a list.
+ *
+ * A list is a container of any values.  Values can be selected by the index.
+ */
+/**
+ * @brief Constructor.
+ */
+RplASList::RplASList() :
+    RplASClass("List")
+{
+}
+
+/**
+ * @brief Creates a value object (used in RplASVariant).
+ *
+ * @param source    NULL or a source to copy
+ * @return          a new value object (specific for the class)
+ */
+void* RplASList::newValueInstance(void* source)
+{
+    QList<RplASVariant*>* rc = new QList<RplASVariant*>();
+    if (source != NULL){
+        QList* source2 = (QList*) source;
+        rc->reserve(source2->size());
+        QList<RplASVariant*>::iterator it;
+        for (it = source->begin();
+                it != source->end();
+                it++){
+            // deleting in destroyValue():
+            rc->append(new RplASValue(*it));
+        }
+    }
+    return (void*) rc;
+}
+
+/**
+ * @brief Destroyes the given object.
+ *
+ * The object must be created by <code>newValueInstance()</code>.
+ *
+ * @param object    object to destroy
+ */
+void RplASList::destroyValueInstance(void* object)
+{
+    delete (QList*) object;
+}
+
+/** @class RplASMap rplastree.hpp "rplexpr/rplastree.hpp"
+ *
+ * @brief Implements the class of a map.
+ *
+ * A map is a container of (key, value) pairs.
+ * Values can be selected by the key.
+ */
+/**
+ * @brief Constructor.
+ */
+RplASMap::RplASMap() :
+    RplASClass("Map")
+{
+}
+/**
+ * @brief Creates a value object (used in RplASVariant).
+ *
+ * @param source    NULL or a source to copy
+ * @return          a new value object (specific for the class)
+ */
+void* RplASMap::newValueInstance(void* source)
+{
+    QMap<QString, RplASVariant*>* rc = new QMap<QString, RplASVariant*>();
+    if (source != NULL){
+        QMap<QString, RplASVariant*>* source2 =
+                (QMap<QString, RplASVariant*>*) source;
+        rc->reserve(source2->size());
+        QMap<QString, RplASVariant*>::iterator it;
+        for (it = source->begin();
+                it != source->end();
+                it++){
+            // deleting in destroyValue():
+            rc->append(new RplASValue(*it));
+        }
+    }
+    return (void*) rc;
+}
+
+/**
+ * @brief Destroyes the given object.
+ *
+ * The object must be created by <code>newValueInstance()</code>.
+ *
+ * @param object    object to destroy
+ */
+void RplASMap::destroyValueInstance(void* object)
+{
+    delete (QMap<QString, RplASVariant*>*) object;
+}
+
+/** @class RplASString rplastree.hpp "rplexpr/rplastree.hpp"
+ *
+ * @brief Implements the class of a string.
+ *
+ * A string is a mutable character sequence.
+ */
+/**
+ * @brief Constructor.
+ */
+RplASString::RplASString() :
+    RplASClass("String")
+{
+}
+/**
+ * @brief Creates a value object (used in RplASVariant).
+ *
+ * @param source    NULL or a source to copy
+ * @return          a new value object (specific for the class)
+ */
+void* RplASString::newValueInstance(void* source)
+{
+    QString* rc = source == NULL ? new QString() : new QString(*(QString*) source);
+    return (void*) rc;
+}
+
+/**
+ * @brief Destroyes the given object.
+ *
+ * The object must be created by <code>newValueInstance()</code>.
+ *
+ * @param object    object to destroy
+ */
+void RplASString::destroyValueInstance(void* object)
+{
+    delete (QString*) object;
+}
+
+/** @class RplASNumber rplastree.hpp "rplexpr/rplastree.hpp"
+ *
+ * @brief Implements the class of a Number.
+ *
+ * A Number is a real value.
+ */
+/**
+ * @brief Constructor.
+ */
+RplASNumber::RplASNumber() :
+    RplASClass("Number")
+{
+}
+/**
+ * @brief Creates a value object (used in RplASVariant).
+ *
+ * @param source    NULL or a source to copy
+ * @return          NULL
+ */
+void* RplASNumber::newValueInstance(void* source)
+{
+    return null;
+}
+
+/**
+ * @brief Destroyes the given object.
+ *
+ * The object must be created by <code>newValueInstance()</code>.
+ *
+ * @param object    object to destroy
+ */
+void RplASNumber::destroyValueInstance(void* object)
+{
+}
+
diff --git a/rplexpr/rplasclasses.hpp b/rplexpr/rplasclasses.hpp
new file mode 100644 (file)
index 0000000..c456b5e
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Licence:
+ * You can use and modify this file without any restriction.
+ * There is no warranty.
+ * You also can use the licence from http://www.wtfpl.net/.
+ * The original sources can be found on https://github.com/republib.
+*/
+
+
+#ifndef RPLASCLASSES_HPP
+#define RPLASCLASSES_HPP
+
+class RplASList : public RplASClass {
+public:
+    RplASList();
+    ~RplASList();
+public:
+    void* newValueInstance(void* source = NULL);
+    void destroyValueInstance(void* object);
+};
+
+class RplASMap : public RplASClass {
+public:
+    RplASMap();
+    ~RplASMap();
+public:
+    void* newValueInstance(void* source = NULL);
+    void destroyValueInstance(void* object);
+};
+
+class RplASNumber : public RplASClass {
+public:
+    RplASNumber();
+    ~RplASNumber();
+public:
+    void* newValueInstance(void* source = NULL);
+    void destroyValueInstance(void* object);
+};
+
+class RplASString : public RplASClass {
+public:
+    RplASString();
+    ~RplASString();
+public:
+    void* newValueInstance(void* source = NULL);
+    void destroyValueInstance(void* object);
+};
+
+#endif // RPLASCLASSES_HPP
index 411614abd72d3564c17dd581bc82c9aa46d61b49..9edb1944d60a8b0079cf0a72b5b41405fad89337 100644 (file)
@@ -9,6 +9,109 @@
 #include "rplcore/rplcore.hpp"
 #include "rplexpr/rplexpr.hpp"
 
+/** @class RplASVariant rplastree.hpp "rplexpr/rplastree.hpp"
+ *
+ * @brief Implements a class which can be one of the basic types.
+ *
+ */
+RplASVariant::RplASVariant() :
+    m_dataType(DT_UNDEF),
+    // m_value(),
+    m_class(NULL)
+{
+}
+/**
+ * @brief Destructor.
+ */
+RplASVariant::~RplASVariant()
+{
+    destroyValue();
+    m_dataType = DT_UNDEF;
+}
+
+/**
+ * @brief Copy constructor.
+ * @param source    the source to copy
+ */
+RplASVariant::RplASVariant(const RplASVariant& source):
+    m_dataType(source.m_dataType),
+    // m_value
+    m_class(source.m_class)
+{
+}
+
+/**
+ * @brief Copies the value.
+ * @param source    the source to copy
+ */
+void RplASVariant::copyValue(const RplASVariant& source)
+{
+    switch(source.m_dataType)
+    {
+    case DT_NUMBER:
+        m_value.m_number = source.m_value.m_number;
+        break;
+    case DT_STRING:
+        // deleting in destroyValue():
+        m_value.m_string = new QString(source.m_value.m_string);
+        break;
+    case DT_LIST:
+    {
+        break;
+    }
+    case DT_MAP:
+    {
+        m_value.m_map = new QList<RplASVariant*>();
+        m_value.m_map.reserve(source.m_value.m_list.size());
+        QList<RplASVariant*>::iterator it;
+        for (it = source.m_value.m_list.begin();
+                it != source.m_value.m_list.end();
+                it++){
+            // deleting in destroyValue():
+            m_value.m_list.append(new RplASValue(*it));
+        }
+        break
+    }
+    case DT_OBJECT:
+        // deleting in destroyValue():
+        m_value.m_object = m_class->newValueInstance(source.m_value.m_object);
+        break;
+    default:
+        break;
+    }
+}
+
+/**
+ * @brief Frees the resources of the instance.
+ */
+void RplASVariant::destroyValue()
+{
+    switch(source.m_dataType)
+    {
+    case DT_NUMBER:
+        break;
+    case DT_STRING:
+        // deleting in destroyValue():
+        m_value.m_string = delete m_value.m_string;
+        m_value.m_string = NULL;
+        break;
+    case DT_LIST:
+        delete m_value.m_list;
+        m_value.m_list = NULL;
+        break;
+    case DT_MAP:
+        delete m_value.m_map;
+        m_value.m_map = NULL;
+        break;
+    case DT_OBJECT:
+        m_map.destroyValueInstance(m_value.m_object);
+        m_value.m_object = NULL;
+        break;
+    default:
+        break;
+    }
+}
+
 /** @class RplASItem rplastree.hpp "rplexpr/rplastree.hpp"
  *
  * @brief Implements the abstract base class of all entries of an AST.
@@ -306,3 +409,21 @@ RplASWhile::RplASWhile() :
  */
 RplASWhile::~RplASWhile()
 {
+}
+/** @class RplASClass rplastree.hpp "rplexpr/rplastree.hpp"
+ *
+ * @brief Implements the base class of a Abstract Syntax Tree class.
+ *
+ * This class is abstract.
+ */
+/**
+ * @brief Constructor.
+ */
+RplASClass::RplASClass(const QString& name) :
+    m_name(name),
+    m_methods(),
+    m_superClass(NULL)
+{
+}
+
+
index f1bfb72d0a1710318b64e892bcbcf1b7f6a64832..690136c64dacdd1f5cdb8d21a5656dcff77f1220 100644 (file)
@@ -59,6 +59,37 @@ enum RplASUnaryOperator {
     ASUOP_POST_INCREMENT,
     ASUOP_PRE_INCREMENT
 };
+class RplASClass;
+class RplASVariant {
+public:
+    enum DataType {
+        DT_UNDEF,
+        DT_NUMBER,
+        DT_STRING,
+        DT_MAP,
+        DT_LIST,
+        DT_OBJECT
+    };
+
+public:
+    RplASVariant();
+    ~RplASVariant();
+    RplASVariant(const RplASVariant& source);
+    RplASVariant& operator=(const RplASVariant& source);
+protected:
+    void copyValue(const RplASVariant& source);
+    void destroyValue();
+private:
+    DataType m_dataType;
+    union {
+        qreal m_number;
+        QString* m_string;
+        QMap<QString, RplASVariant*> m_map;
+        QList<RplAsValue*> m_list;
+        void* m_object;
+    } m_value;
+    RplASClass* m_class;
+};
 
 class RplASTree;
 class RplASItem
@@ -77,7 +108,7 @@ class RplASExpr
 public:
     RplASExpr();
 public:
-    virtual QVariant calc() = null;
+    virtual QVariant calc() = 0;
 };
 
 class RplASValue : public RplASItem, public RplASExpr
@@ -183,7 +214,7 @@ public:
     virtual ~RplArgument();
 };
 
-class RplAsCall : public RplASItem, public RplASStatement
+class RplAsMethodCall : public RplASNode2, public RplASStatement
 {
 public:
     RplArgument();
@@ -203,17 +234,44 @@ private:
     RplASValue* m_default;
 };
 
-class RplASFunction : public RplASNodeMany
+
+class RplASClass;
+class RplASMethod : public RplASNode2, public RplASStatement
 {
 public:
-    RplASFunction(const QString& name);
-    virtual ~RplASFunction();
+    RplASMethod();
+public:
+    void execute();
+private:
+    QString m_name;
+};
+
+class RplASClass {
+public:
+    RplASClass(const QString& name);
+public:
+    /**
+     * @brief Creates a value object (used in RplASVariant).
+     *
+     * @param source    NULL or a source to copy
+     * @return          a new value object (specific for the class)
+     */
+    void* newValueInstance(void* source = NULL) = 0;
+    /**
+     * @brief Destroyes the given object.
+     *
+     * The object must be created by <code>newValueInstance()</code>.
+     *
+     * @param object    object to destroy
+     */
+    void destroyValueInstance(void* object) = 0;
 protected:
-    RplASItemType* body;
-    RplArgument* arg;
     QString m_name;
+    QMap<String, RplASMethod*> m_methods;
+    RplASClass* m_superClass;
 };
 
+
 class RplASTree
 {
 public:
index 62437c1ac858f075bcc167026692dfde17dd6fed..64b2cebb04df7043226278f9c962b841f3f7f5a9 100644 (file)
@@ -21,5 +21,6 @@
 #include "rplexpr/rplsource.hpp"
 #include "rplexpr/rpllexer.hpp"
 #include "rplexpr/rplastree.hpp"
+#include "rplexpr/rplasclasses.hpp"
 
 #endif // RPLEXPR_HPP
index 9d694f69998dc390517052e2785c34420496b89e..ef9e8716712c52d7cc1c491addabfc7a94c75b49 100644 (file)
@@ -33,7 +33,8 @@ SOURCES += \
     ../rplexpr/rpllexer.cpp \
     ../rplexpr/rplsource.cpp \
     ../rplcore/rplqstring.cpp \
-    ../rplexpr/rplastree.cpp
+    ../rplexpr/rplastree.cpp \
+    ../rplexpr/rplasclasses.cpp
 
 HEADERS += ../rplmodules.hpp \
     ../rplcore/rplconfig.hpp \
@@ -58,7 +59,8 @@ HEADERS += ../rplmodules.hpp \
     ../rplexpr/rplexpr.hpp \
     ../rplexpr/rplsource.hpp \
     ../rplcore/rplqstring.hpp \
-    ../rplexpr/rplastree.hpp
+    ../rplexpr/rplastree.hpp \
+    ../rplexpr/rplasclasses.hpp
 
 unix:!symbian {
     maemo5 {
index ca425c5ba055563afb807c598324020c40c450f4..61f3e81462d28bd4949b4b15d5b39042c37f4686 100644 (file)
@@ -31,5 +31,6 @@ SOURCES += main.cpp \
     rplsource_test.cpp \
     rpllexer_test.cpp \
     rplqstring_test.cpp \
-    ../rplcore/rplqstring.cpp
+    ../rplcore/rplqstring.cpp \
+    ../rplexpr/rplasclasses.cpp