diff --git a/src/main/evaluator.cpp b/src/main/evaluator.cpp index dd9e6132..2cd9974e 100644 --- a/src/main/evaluator.cpp +++ b/src/main/evaluator.cpp @@ -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; diff --git a/src/main/evaluator.h b/src/main/evaluator.h index c2d3beb6..87e86bcc 100644 --- a/src/main/evaluator.h +++ b/src/main/evaluator.h @@ -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 diff --git a/src/main/parser.cpp b/src/main/parser.cpp index 0c027db2..af5055e0 100644 --- a/src/main/parser.cpp +++ b/src/main/parser.cpp @@ -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); diff --git a/src/main/parser.h b/src/main/parser.h index 7158f09e..256b5f4c 100644 --- a/src/main/parser.h +++ b/src/main/parser.h @@ -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. * diff --git a/src/main/statement/eval.cpp b/src/main/statement/eval.cpp index 46173d57..124fb4f4 100644 --- a/src/main/statement/eval.cpp +++ b/src/main/statement/eval.cpp @@ -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(node); + expression = static_cast(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(x); + expression = static_cast(x); } } diff --git a/src/main/statement/eval.h b/src/main/statement/eval.h index da0a62dc..be597cca 100644 --- a/src/main/statement/eval.h +++ b/src/main/statement/eval.h @@ -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 diff --git a/src/system/amiga_arexx.cpp b/src/system/amiga_arexx.cpp index 2301db78..683b28aa 100644 --- a/src/system/amiga_arexx.cpp +++ b/src/system/amiga_arexx.cpp @@ -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; }