]> gitweb.hamatoma.de Git - reqt/commitdiff
dayly work
authorhama <hama@siduction.net>
Mon, 4 Aug 2014 21:58:06 +0000 (23:58 +0200)
committerhama <hama@siduction.net>
Mon, 4 Aug 2014 21:58:06 +0000 (23:58 +0200)
rplexpr/rplasclasses.cpp
rplexpr/rplasclasses.hpp
rplexpr/rplvm.cpp
rplexpr/rplvm.hpp
rplexpr/rplvm_test.cpp

index c8c5fc8af2280af94f904a030ec8028af824dc22..446087d0e2dc41dfadb8bcd10b0184845c0422d6 100644 (file)
@@ -103,6 +103,21 @@ RplASClass* RplSymbolSpace::findClass(const QString& name) const
     return rc;
 }
 
+/**
+ * @brief Find a method in the instance.
+ *
+ * @param name  the method's name
+ * @return      NULL: method not found
+ *              otherwise: the method description
+ */
+RplASMethod* RplSymbolSpace::findMethod(const QString& name) const
+{
+    RplASMethod* rc = NULL;
+    if (m_methods.contains(name))
+        rc = m_methods[name];
+    return rc;
+}
+
 /**
  * @brief Writes the content of the instance into a file.
  *
index 888d5ac4dbcb520d599e149c6ce0b359488dbbb7..02c3fd0519f02f474517e53ee26b82cb6f0dcb7e 100644 (file)
@@ -49,6 +49,7 @@ public:
 public:
     RplVariable* findVariable(const QString& name) const;
     RplASClass* findClass(const QString& name) const;
+    RplASMethod* findMethod(const QString& name) const;
     void dump(FILE* fp, int indent, const char* header = NULL);
     const QString& name() const;
     RplASItem* body() const;
index e63779c284e692b96f8c96b292ffd80f56bea619..47ddbb6030001cd0db03549e960204b786b2d990 100644 (file)
@@ -150,21 +150,53 @@ RplVirtualMachine::RplVirtualMachine(RplASTree* tree, RplSource* source,
     m_threads.reserve(8);
 }
 
+/**
+ * @brief Executes the program in a module.
+ *
+ * @param module    the module's name
+ */
+void RplVirtualMachine::executeModule(const QString& module)
+{
+    RplSymbolSpace* space = m_tree.findmodule(module);
+    if (space == NULL)
+        throw RplVmException("module not found: %s", module.toUtf8().constData());
+    RplStackFrame frame(space);
+    RplSymbolSpace* mainSpace = NULL;
+    RplASItem* mainStatements = NULL;
+    RplASMethod* method = space->findMethod("main");
+    if (method != NULL){
+        mainStatements = method->child();
+        mainSpace = method->symbols();
+    }
+    addThread(space->body(), space, mainStatements, mainSpace);
+}
+
 /**
  * @brief Adds a thread to the instance.
  *
- * @param program   the statement list to execute
- * @param space     the current symbol space
+ * @param initialization        the statements for initialization
+ * @param spaceInitialization   the symbol space of the initialization
+ * @param statements            the statement list to execute. This is normally
+ *                              the body of the main program
+ * @param space                 the symbol space of the statements, normally
+ *                              the symbol space of the main program
  * @param maxStack  the maximal number of nested stack frames.
  *                  <= 0: use the default
  */
-void RplVirtualMachine::addThread(RplASItem* program, RplSymbolSpace* space,
+void RplVirtualMachine::addThread(RplASItem* initialization,
+                                  RplSymbolSpace* spaceInitialization,
+                                  RplASItem* statements,
+                                  RplSymbolSpace* space,
                                   int maxStack)
 {
     RplVMThread* thread = new RplVMThread(
                 maxThread <= 0 ? m_maxStack : maxStack, self);
     m_threads.append(thread);
-    thread->execute(program, space);
+    if (initialization != NULL){
+        thread->execute(initialization, spaceInitialization);
+    }
+    if (statements != NULL)
+        thread->execute(statements, space);
 }
 /**
  * @brief Tests whether a given flag is set.
index 2fa0a07dc9aa96ecf3ec84392751b80285ae919e..24eeb37cfe615b09b0ae1cb2d1a7e4da4da0e4fb 100644 (file)
@@ -59,7 +59,11 @@ public:
 public:
     RplVirtualMachine(RplASTree& tree, RplSource& source, int maxStack = 1024);
 public:
-    void addThread(RplASItem* program, RplSymbolSpace* space, int maxStack = 0);
+    void executeModule(const QString& module);
+    void addThread(RplASItem* initialization,
+                   RplSymbolSpace* spaceInitialization,
+                   RplASItem* statements, RplSymbolSpace* space,
+                   int maxStack = 0);
     bool hasFlag(VMFlag flag) const;
     void setFlag(VMFlag flag);
     void clearFlag(VMFlag flag);
index d3acb7f41932a6924eb2d57ecb92116a1dc1837e..6c194f08b4c09aa0fdd999da660b1004b933a61b 100644 (file)
@@ -45,6 +45,7 @@ private:
         fnExpected += (char) QDir::separator().toLatin1();
         fnExpected += fileExpected;
         QByteArray fnCurrent = getTempFile(fileExpected, "rplvm");
+        vm.executeModule("<test>");
         vm.dump(fnCurrent, RplASTree::DMP_NO_GLOBALS);
         assertEqualFiles(fnExpected.constData(), fnCurrent.constData(),
                          __FILE__, lineNo);