From: hama Date: Mon, 4 Aug 2014 21:58:06 +0000 (+0200) Subject: dayly work X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=68b24cb5a5fa55e4abc44d66e350407911112dba;p=reqt dayly work --- diff --git a/rplexpr/rplasclasses.cpp b/rplexpr/rplasclasses.cpp index c8c5fc8..446087d 100644 --- a/rplexpr/rplasclasses.cpp +++ b/rplexpr/rplasclasses.cpp @@ -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. * diff --git a/rplexpr/rplasclasses.hpp b/rplexpr/rplasclasses.hpp index 888d5ac..02c3fd0 100644 --- a/rplexpr/rplasclasses.hpp +++ b/rplexpr/rplasclasses.hpp @@ -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; diff --git a/rplexpr/rplvm.cpp b/rplexpr/rplvm.cpp index e63779c..47ddbb6 100644 --- a/rplexpr/rplvm.cpp +++ b/rplexpr/rplvm.cpp @@ -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. diff --git a/rplexpr/rplvm.hpp b/rplexpr/rplvm.hpp index 2fa0a07..24eeb37 100644 --- a/rplexpr/rplvm.hpp +++ b/rplexpr/rplvm.hpp @@ -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); diff --git a/rplexpr/rplvm_test.cpp b/rplexpr/rplvm_test.cpp index d3acb7f..6c194f0 100644 --- a/rplexpr/rplvm_test.cpp +++ b/rplexpr/rplvm_test.cpp @@ -45,6 +45,7 @@ private: fnExpected += (char) QDir::separator().toLatin1(); fnExpected += fileExpected; QByteArray fnCurrent = getTempFile(fileExpected, "rplvm"); + vm.executeModule(""); vm.dump(fnCurrent, RplASTree::DMP_NO_GLOBALS); assertEqualFiles(fnExpected.constData(), fnCurrent.constData(), __FILE__, lineNo);