Changeset View
Changeset View
Standalone View
Standalone View
src/backends/lua/luaexpression.cpp
Show All 26 Lines | |||||
27 | #include "helpresult.h" | 27 | #include "helpresult.h" | ||
28 | 28 | | |||
29 | #include <lua.hpp> | 29 | #include <lua.hpp> | ||
30 | 30 | | |||
31 | #include <QDebug> | 31 | #include <QDebug> | ||
32 | #include <QString> | 32 | #include <QString> | ||
33 | #include <QStringList> | 33 | #include <QStringList> | ||
34 | 34 | | |||
35 | LuaExpression::LuaExpression( Cantor::Session* session, lua_State* L) | 35 | LuaExpression::LuaExpression( Cantor::Session* session) | ||
36 | : Cantor::Expression(session), m_L(L) | 36 | : Cantor::Expression(session) | ||
37 | { | 37 | { | ||
38 | } | 38 | } | ||
39 | 39 | | |||
40 | LuaExpression::~LuaExpression() | 40 | LuaExpression::~LuaExpression() | ||
41 | { | 41 | { | ||
42 | } | 42 | } | ||
43 | 43 | | |||
44 | void LuaExpression::evaluate() | 44 | void LuaExpression::evaluate() | ||
45 | { | 45 | { | ||
46 | QString ret; | 46 | /* | ||
47 | Cantor::Expression::Status status; | 47 | * start evaluating the current expression | ||
48 | execute(ret, status); | 48 | * set the status to computing | ||
49 | 49 | * decide what needs to be done if the user is trying to define a function etc | |||
50 | if(status == Cantor::Expression::Done) | 50 | */ | ||
51 | { | 51 | setStatus(Cantor::Expression::Computing); | ||
52 | QString cmd = command().simplified(); | 52 | if (command().isEmpty()) { | ||
53 | 53 | setStatus(Cantor::Expression::Done); | |||
54 | if( cmd.startsWith(QLatin1String("show(")) || cmd.startsWith(QLatin1String("show (")) ) | 54 | return; | ||
55 | setResult(new Cantor::ImageResult(QUrl::fromLocalFile(ret), ret)); | | |||
56 | else | | |||
57 | setResult(new Cantor::TextResult(ret)); | | |||
58 | } | | |||
59 | else | | |||
60 | { | | |||
61 | setErrorMessage(ret); | | |||
62 | } | 55 | } | ||
63 | 56 | | |||
64 | setStatus(status); | 57 | LuaSession* currentSession = dynamic_cast<LuaSession*>(session()); | ||
65 | } | 58 | currentSession->runExpression(this); | ||
66 | 59 | | |||
67 | void LuaExpression::interrupt() | | |||
68 | { | | |||
69 | setStatus(Cantor::Expression::Interrupted); | | |||
70 | } | 60 | } | ||
71 | 61 | | |||
72 | void LuaExpression::execute(QString& ret, Cantor::Expression::Status& status) | 62 | void LuaExpression::parseOutput(QString &output) | ||
73 | { | 63 | { | ||
74 | int top = lua_gettop(m_L); | 64 | output.replace(command(), QLatin1String("")); | ||
65 | output.replace(QLatin1String("return"), QLatin1String("")); | ||||
filipesaraiva: I think we need to use `return` in order to reproduce the 'interpreter behavior' of Lua in… | |||||
It would be great if we can eliminate the use of 'print' statement from user's side. There's a case where using 'return' statement produces wrong output/behavior, I can't recall that case atm. rishabhg: It would be great if we can eliminate the use of 'print' statement from user's side. There's a… | |||||
66 | output.replace(QLatin1String(">"), QLatin1String("")); | ||||
67 | output = output.trimmed(); | ||||
75 | 68 | | |||
76 | // execute the command | 69 | qDebug() << "final output of the command " << command() << ": " << output << endl; | ||
77 | QString err = luahelper_dostring(m_L, QLatin1String("return ") + command() ); // try to return values... | | |||
78 | if( !err.isNull() ) err = luahelper_dostring(m_L, command() ); // try the original expression | | |||
79 | 70 | | |||
80 | if( err.isNull() ) | 71 | setResult(new Cantor::TextResult(output)); | ||
81 | { | | |||
82 | QStringList list; | | |||
83 | int n_out = lua_gettop(m_L) - top; | | |||
84 | 72 | | |||
85 | for(int i = -n_out; i < 0; ++i) | 73 | setStatus(Cantor::Expression::Done); | ||
86 | list << luahelper_tostring(m_L, i); | | |||
87 | 74 | | |||
88 | ret = list.join(QLatin1String("\n")) + luahelper_getprinted(m_L); | | |||
89 | status = Cantor::Expression::Done; | | |||
90 | } | 75 | } | ||
91 | else | 76 | | ||
77 | void LuaExpression::interrupt() | ||||
92 | { | 78 | { | ||
93 | qDebug() << "error when executing" << command() << ":" << err; | 79 | setStatus(Cantor::Expression::Interrupted); | ||
94 | ret = err; | | |||
95 | status = Cantor::Expression::Error; | | |||
96 | } | 80 | } | ||
97 | 81 | | |||
98 | lua_settop(m_L, top); | | |||
99 | } | | |||
100 | 82 | | |||
101 | #include "luaexpression.moc" | 83 | #include "luaexpression.moc" |
I think we need to use return in order to reproduce the 'interpreter behavior' of Lua in Cantor (that behavior like send-a-command-get-the-answer-without-print-command).