File indexing completed on 2024-10-06 09:24:50
0001 /* 0002 SPDX-License-Identifier: GPL-2.0-or-later 0003 SPDX-FileCopyrightText: 2014 Lucas Hermann Negri <lucashnegri@gmail.com> 0004 SPDX-FileCopyrightText: 2023 Alexander Semke <alexander.semke@web.de> 0005 */ 0006 0007 #include "luaexpression.h" 0008 #include "luasession.h" 0009 #include "luahelper.h" 0010 0011 #include "textresult.h" 0012 #include "imageresult.h" 0013 #include "helpresult.h" 0014 0015 #include <lua.hpp> 0016 0017 #include <QDebug> 0018 #include <QString> 0019 #include <QStringList> 0020 0021 LuaExpression::LuaExpression(Cantor::Session* session, bool internal) 0022 : Cantor::Expression(session, internal) 0023 { 0024 } 0025 0026 void LuaExpression::evaluate() 0027 { 0028 /* 0029 * start evaluating the current expression 0030 * set the status to computing 0031 * decide what needs to be done if the user is trying to define a function etc 0032 */ 0033 if (command().isEmpty()) { 0034 setStatus(Cantor::Expression::Done); 0035 return; 0036 } 0037 0038 session()->enqueueExpression(this); 0039 } 0040 0041 void LuaExpression::parseError(const QString &error) 0042 { 0043 qDebug() << error; 0044 setErrorMessage(error); 0045 setStatus(Error); 0046 } 0047 0048 void LuaExpression::parseOutput(const QString& output) 0049 { 0050 0051 qDebug()<<"parsing the output " << output; 0052 auto* luaSession = static_cast<LuaSession*>(session()); 0053 0054 if (luaSession->isLuaJIT()) 0055 { 0056 QString result = output; 0057 0058 // in case the expression is incomplete, Lua is answering with the sub-promt ">> ". 0059 // since we don't handle it yet, replace it with the prompt string so we can handle it easier below 0060 // when splitting the whole output into the separate results 0061 // TODO: add handling for the sub-promt 0062 result.replace(QLatin1String(">> "), QLatin1String("> ")); 0063 0064 const auto& results = result.split(QLatin1String("> ")); 0065 for (auto& result : results) { 0066 if (result.simplified() == QLatin1String(">") || result.simplified().isEmpty()) 0067 continue; 0068 0069 addResult(new Cantor::TextResult(result)); 0070 } 0071 } 0072 else 0073 { 0074 // the parsing of Lua's output was already done in LuaSession::readOutputLua() 0075 // where the information about the actual commands is present and required. 0076 // here we only set the final result without any further parsing. 0077 if (!output.isEmpty()) 0078 setResult(new Cantor::TextResult(output)); 0079 } 0080 0081 setStatus(Cantor::Expression::Done); 0082 }