File indexing completed on 2024-02-25 03:50:20

0001 /*
0002     SPDX-FileCopyrightText: 2003-2006 Cies Breijs <cies AT kde DOT nl>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "interpreter.h"
0008 #include "interpreteradaptor.h"
0009 
0010 #include <QtDebug>
0011 
0012 #include <QDebug>
0013 
0014 
0015 
0016 Interpreter::Interpreter(QObject* parent, bool testing)
0017     : QObject(parent), m_testing(testing)
0018 {
0019     if (testing) {
0020         new InterpreterAdaptor(this);
0021         QDBusConnection dbus = QDBusConnection::sessionBus();
0022         dbus.registerObject(QStringLiteral("/Interpreter"), this);
0023     }
0024 
0025     errorList  = new ErrorList();
0026     tokenizer  = new Tokenizer();
0027     parser     = new Parser(testing);
0028     executer   = new Executer(testing);
0029 
0030     m_state = Uninitialized;
0031 }
0032 
0033 Interpreter::~Interpreter()
0034 {
0035     errorList->clear();
0036     delete errorList;
0037     delete tokenizer;
0038     delete parser;
0039     delete executer;
0040 }
0041 
0042 void Interpreter::initialize(const QString& inString)
0043 {
0044     errorList->clear();
0045     tokenizer->initialize(inString);
0046     m_state = Initialized;
0047 }
0048 
0049 void Interpreter::interpret()
0050 {
0051     switch (m_state) {
0052         case Uninitialized:
0053             qCritical("Interpreter::interpret(): called without being initialized");
0054             return;
0055 
0056 
0057         case Initialized:
0058             parser->initialize(tokenizer, errorList);
0059             m_state = Parsing;
0060 //          //qDebug() << "Initialized the parser, parsing the code...";
0061             Q_EMIT parsing();
0062             break;
0063 
0064 
0065         case Parsing:
0066             parser->parse();
0067 
0068             if (encounteredErrors()) {
0069                 m_state = Aborted;
0070 //              //qDebug() << "Error encountered while parsing:";
0071 //              const QStringList lines = errorList->asStringList();
0072 //              foreach (const QString &line, lines)
0073                     //qDebug() << line;
0074 //              //qDebug() << "Parsing was unsuccessful.";
0075                 return;
0076             }
0077 
0078             if (parser->isFinished()) {
0079 //              //qDebug() << "Finished parsing.\n";
0080                 TreeNode* tree = parser->getRootNode();
0081                 Q_EMIT treeUpdated(tree);
0082 //              //qDebug() << "Node tree as returned by parser:";
0083 //              parser->printTree();
0084 //              //qDebug() << "";
0085 
0086                 executer->initialize(tree, errorList);
0087                 m_state = Executing;
0088 //              //qDebug() << "Initialized the executer, executing the node tree...";
0089                 Q_EMIT executing();
0090                 return;
0091             }
0092             break;
0093 
0094 
0095         case Executing:
0096             executer->execute();
0097 
0098             if (executer->isFinished()) {
0099 //              //qDebug() << "Finished executing.\n";
0100                 if (encounteredErrors()) {
0101 //                  //qDebug() << "Execution returned " << errorList->count() << " error(s):";
0102 //                  const QStringList lines = errorList->asStringList();
0103 //                  foreach (const QString &line, lines)
0104 //                      //qDebug() << line;
0105                 } else {
0106 //                  //qDebug() << "No errors encountered.";
0107                 }
0108                 m_state = Finished;
0109                 Q_EMIT finished();
0110                 return;
0111             }
0112             break;
0113 
0114 
0115         case Finished:
0116             qCritical("Interpreter::interpret(): called while already finished");
0117             return;
0118     }
0119 }
0120 
0121 #include "moc_interpreter.cpp"