mirror of https://gitlab.com/rnger/amath
Only show result when using ARexx
This commit is contained in:
parent
93c8fdbef4
commit
0240d3a439
|
@ -37,6 +37,7 @@ Evaluator::Evaluator(const char* input)
|
|||
{
|
||||
this->input = input;
|
||||
output = new CharBuffer();
|
||||
ResultOnly = false;
|
||||
}
|
||||
|
||||
Evaluator::~Evaluator()
|
||||
|
@ -52,6 +53,7 @@ void Evaluator::Evaluate() const
|
|||
output->ClearBuffer();
|
||||
|
||||
Parser* parser = new Parser(input);
|
||||
parser->ResultOnly = ResultOnly;
|
||||
SyntaxNode* node = parser->Parse();
|
||||
delete parser;
|
||||
|
||||
|
|
|
@ -35,14 +35,20 @@
|
|||
class Evaluator
|
||||
{
|
||||
public:
|
||||
explicit Evaluator(const char* input);
|
||||
explicit Evaluator(const char *input);
|
||||
~Evaluator();
|
||||
|
||||
/**
|
||||
* @brief Do not include expression string in evaluation result.
|
||||
*/
|
||||
bool ResultOnly;
|
||||
|
||||
void Evaluate() const;
|
||||
char* GetResult() const;
|
||||
char *GetResult() const;
|
||||
|
||||
private:
|
||||
const char* input;
|
||||
CharBuffer* output;
|
||||
const char *input;
|
||||
CharBuffer *output;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -46,6 +46,7 @@ Parser::Parser(const char* input)
|
|||
token = nullptr;
|
||||
errorNode = nullptr;
|
||||
syntaxError = 0;
|
||||
ResultOnly = false;
|
||||
}
|
||||
|
||||
Parser::~Parser()
|
||||
|
@ -224,7 +225,7 @@ SyntaxNode* Parser::ParseStatement()
|
|||
res = new ListFunctionsStatement();
|
||||
break;
|
||||
case symeval:
|
||||
res = new EvalStatement(ParseExpression());
|
||||
res = new EvalStatement(ParseExpression(), ResultOnly);
|
||||
break;
|
||||
default:
|
||||
PutToken();
|
||||
|
@ -266,7 +267,7 @@ SyntaxNode* Parser::ParseEvaluation()
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
StatementNode* sta = new EvalStatement(exp);
|
||||
StatementNode* sta = new EvalStatement(exp, ResultOnly);
|
||||
if (exp->IsSilent())
|
||||
{
|
||||
sta = new SilentStatement(sta);
|
||||
|
|
|
@ -52,6 +52,11 @@ public:
|
|||
explicit Parser(const char* input);
|
||||
~Parser();
|
||||
|
||||
/**
|
||||
* @brief Do not include expression string in evaluation result.
|
||||
*/
|
||||
bool ResultOnly;
|
||||
|
||||
/**
|
||||
* @brief Parses the input into a syntax tree.
|
||||
*
|
||||
|
|
|
@ -32,9 +32,10 @@
|
|||
#include "amathc.h"
|
||||
#include "system/program.h"
|
||||
|
||||
EvalStatement::EvalStatement(ExpressionNode* expression) :
|
||||
EvalStatement::EvalStatement(ExpressionNode *expression, bool resultOnly) :
|
||||
StatementNode(), expression(expression)
|
||||
{
|
||||
this->resultOnly = resultOnly;
|
||||
}
|
||||
|
||||
EvalStatement::~EvalStatement()
|
||||
|
@ -45,31 +46,41 @@ EvalStatement::~EvalStatement()
|
|||
}
|
||||
}
|
||||
|
||||
char* EvalStatement::Execute()
|
||||
char *EvalStatement::Execute()
|
||||
{
|
||||
Number* result = expression->Evaluate();
|
||||
Number* temp = result->Clone();
|
||||
const char* text = expression->GetText();
|
||||
const char* val = Program->Output->GetText(result);
|
||||
Number *result = expression->Evaluate();
|
||||
Number *temp = result->Clone();
|
||||
const char *val = Program->Output->GetText(result);
|
||||
|
||||
Program->SetLastResult(temp);
|
||||
delete temp;
|
||||
|
||||
output->Empty();
|
||||
output->EnsureSize(
|
||||
StrLen(text) + 3 +
|
||||
StrLen(val) +
|
||||
StrLen(NEWLINE) + 1);
|
||||
if (resultOnly)
|
||||
{
|
||||
output->Empty();
|
||||
output->EnsureSize(StrLen(val) + 1);
|
||||
output->Append(val);
|
||||
}
|
||||
else
|
||||
{
|
||||
const char *text = expression->GetText();
|
||||
|
||||
output->Append(text);
|
||||
output->Append(" = ");
|
||||
output->Append(val);
|
||||
output->Append(NEWLINE);
|
||||
output->Empty();
|
||||
output->EnsureSize(
|
||||
StrLen(text) + 3 +
|
||||
StrLen(val) +
|
||||
StrLen(NEWLINE) + 1);
|
||||
|
||||
output->Append(text);
|
||||
output->Append(" = ");
|
||||
output->Append(val);
|
||||
output->Append(NEWLINE);
|
||||
}
|
||||
|
||||
return output->GetString();
|
||||
}
|
||||
|
||||
SyntaxNode* EvalStatement::GetNext()
|
||||
SyntaxNode *EvalStatement::GetNext()
|
||||
{
|
||||
if (iterator == nullptr)
|
||||
{
|
||||
|
@ -80,16 +91,16 @@ SyntaxNode* EvalStatement::GetNext()
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
void EvalStatement::Attach(SyntaxNode* node)
|
||||
void EvalStatement::Attach(SyntaxNode *node)
|
||||
{
|
||||
if (expression == nullptr)
|
||||
{
|
||||
expression = static_cast<ExpressionNode*>(node);
|
||||
expression = static_cast<ExpressionNode *>(node);
|
||||
node->SetParent(this);
|
||||
}
|
||||
}
|
||||
|
||||
void EvalStatement::Detach(SyntaxNode* node)
|
||||
void EvalStatement::Detach(SyntaxNode *node)
|
||||
{
|
||||
if (expression == node)
|
||||
{
|
||||
|
@ -97,11 +108,11 @@ void EvalStatement::Detach(SyntaxNode* node)
|
|||
}
|
||||
}
|
||||
|
||||
void EvalStatement::Replace(SyntaxNode* n, SyntaxNode* x)
|
||||
void EvalStatement::Replace(SyntaxNode *n, SyntaxNode *x)
|
||||
{
|
||||
if (expression == n)
|
||||
{
|
||||
delete expression;
|
||||
expression = static_cast<ExpressionNode*>(x);
|
||||
expression = static_cast<ExpressionNode *>(x);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@
|
|||
class EvalStatement : public virtual StatementNode
|
||||
{
|
||||
public:
|
||||
explicit EvalStatement(ExpressionNode* expression);
|
||||
explicit EvalStatement(ExpressionNode* expression, bool resultOnly);
|
||||
~EvalStatement();
|
||||
|
||||
char* Execute();
|
||||
|
@ -50,6 +50,7 @@ public:
|
|||
|
||||
private:
|
||||
ExpressionNode* expression;
|
||||
bool resultOnly;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -193,15 +193,17 @@ bool AmigaARexx::HandleMessage(struct RexxMsg *message)
|
|||
|
||||
message->rm_Result1 = 0;
|
||||
|
||||
Evaluator *evaluator = new Evaluator(message->rm_Args[0]);
|
||||
evaluator->ResultOnly = true;
|
||||
evaluator->Evaluate();
|
||||
|
||||
if (message->rm_Action & RXFF_RESULT)
|
||||
{
|
||||
Evaluator *evaluator = new Evaluator(message->rm_Args[0]);
|
||||
evaluator->Evaluate();
|
||||
const char *result = evaluator->GetResult();
|
||||
message->rm_Result2 = (LONG)CreateArgstring((STRPTR)result, (LONG)StrLen(result));
|
||||
delete evaluator;
|
||||
}
|
||||
|
||||
delete evaluator;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue