1
0
mirror of https://gitlab.com/rnger/amath synced 2025-10-05 18:39:41 +00:00

4 Commits

11 changed files with 80 additions and 33 deletions

View File

@ -37,6 +37,7 @@ Evaluator::Evaluator(const char* input)
{ {
this->input = input; this->input = input;
output = new CharBuffer(); output = new CharBuffer();
ResultOnly = false;
} }
Evaluator::~Evaluator() Evaluator::~Evaluator()
@ -52,6 +53,7 @@ void Evaluator::Evaluate() const
output->ClearBuffer(); output->ClearBuffer();
Parser* parser = new Parser(input); Parser* parser = new Parser(input);
parser->ResultOnly = ResultOnly;
SyntaxNode* node = parser->Parse(); SyntaxNode* node = parser->Parse();
delete parser; delete parser;

View File

@ -35,14 +35,20 @@
class Evaluator class Evaluator
{ {
public: public:
explicit Evaluator(const char* input); explicit Evaluator(const char *input);
~Evaluator(); ~Evaluator();
/**
* @brief Do not include expression string in evaluation result.
*/
bool ResultOnly;
void Evaluate() const; void Evaluate() const;
char* GetResult() const; char *GetResult() const;
private: private:
const char* input; const char *input;
CharBuffer* output; CharBuffer *output;
}; };
#endif #endif

View File

@ -46,6 +46,7 @@ Parser::Parser(const char* input)
token = nullptr; token = nullptr;
errorNode = nullptr; errorNode = nullptr;
syntaxError = 0; syntaxError = 0;
ResultOnly = false;
} }
Parser::~Parser() Parser::~Parser()
@ -224,7 +225,7 @@ SyntaxNode* Parser::ParseStatement()
res = new ListFunctionsStatement(); res = new ListFunctionsStatement();
break; break;
case symeval: case symeval:
res = new EvalStatement(ParseExpression()); res = new EvalStatement(ParseExpression(), ResultOnly);
break; break;
default: default:
PutToken(); PutToken();
@ -266,7 +267,7 @@ SyntaxNode* Parser::ParseEvaluation()
return nullptr; return nullptr;
} }
StatementNode* sta = new EvalStatement(exp); StatementNode* sta = new EvalStatement(exp, ResultOnly);
if (exp->IsSilent()) if (exp->IsSilent())
{ {
sta = new SilentStatement(sta); sta = new SilentStatement(sta);

View File

@ -52,6 +52,11 @@ public:
explicit Parser(const char* input); explicit Parser(const char* input);
~Parser(); ~Parser();
/**
* @brief Do not include expression string in evaluation result.
*/
bool ResultOnly;
/** /**
* @brief Parses the input into a syntax tree. * @brief Parses the input into a syntax tree.
* *

View File

@ -32,9 +32,10 @@
#include "amathc.h" #include "amathc.h"
#include "system/program.h" #include "system/program.h"
EvalStatement::EvalStatement(ExpressionNode* expression) : EvalStatement::EvalStatement(ExpressionNode *expression, bool resultOnly) :
StatementNode(), expression(expression) StatementNode(), expression(expression)
{ {
this->resultOnly = resultOnly;
} }
EvalStatement::~EvalStatement() EvalStatement::~EvalStatement()
@ -45,16 +46,25 @@ EvalStatement::~EvalStatement()
} }
} }
char* EvalStatement::Execute() char *EvalStatement::Execute()
{ {
Number* result = expression->Evaluate(); Number *result = expression->Evaluate();
Number* temp = result->Clone(); Number *temp = result->Clone();
const char* text = expression->GetText(); const char *val = Program->Output->GetText(result);
const char* val = Program->Output->GetText(result);
Program->SetLastResult(temp); Program->SetLastResult(temp);
delete temp; delete temp;
if (resultOnly)
{
output->Empty();
output->EnsureSize(StrLen(val) + 1);
output->Append(val);
}
else
{
const char *text = expression->GetText();
output->Empty(); output->Empty();
output->EnsureSize( output->EnsureSize(
StrLen(text) + 3 + StrLen(text) + 3 +
@ -65,11 +75,12 @@ char* EvalStatement::Execute()
output->Append(" = "); output->Append(" = ");
output->Append(val); output->Append(val);
output->Append(NEWLINE); output->Append(NEWLINE);
}
return output->GetString(); return output->GetString();
} }
SyntaxNode* EvalStatement::GetNext() SyntaxNode *EvalStatement::GetNext()
{ {
if (iterator == nullptr) if (iterator == nullptr)
{ {
@ -80,16 +91,16 @@ SyntaxNode* EvalStatement::GetNext()
return nullptr; return nullptr;
} }
void EvalStatement::Attach(SyntaxNode* node) void EvalStatement::Attach(SyntaxNode *node)
{ {
if (expression == nullptr) if (expression == nullptr)
{ {
expression = static_cast<ExpressionNode*>(node); expression = static_cast<ExpressionNode *>(node);
node->SetParent(this); node->SetParent(this);
} }
} }
void EvalStatement::Detach(SyntaxNode* node) void EvalStatement::Detach(SyntaxNode *node)
{ {
if (expression == 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) if (expression == n)
{ {
delete expression; delete expression;
expression = static_cast<ExpressionNode*>(x); expression = static_cast<ExpressionNode *>(x);
} }
} }

View File

@ -39,7 +39,7 @@
class EvalStatement : public virtual StatementNode class EvalStatement : public virtual StatementNode
{ {
public: public:
explicit EvalStatement(ExpressionNode* expression); explicit EvalStatement(ExpressionNode* expression, bool resultOnly);
~EvalStatement(); ~EvalStatement();
char* Execute(); char* Execute();
@ -50,6 +50,7 @@ public:
private: private:
ExpressionNode* expression; ExpressionNode* expression;
bool resultOnly;
}; };
#endif #endif

View File

@ -193,15 +193,17 @@ bool AmigaARexx::HandleMessage(struct RexxMsg *message)
message->rm_Result1 = 0; message->rm_Result1 = 0;
Evaluator *evaluator = new Evaluator(message->rm_Args[0]);
evaluator->ResultOnly = true;
evaluator->Evaluate();
if (message->rm_Action & RXFF_RESULT) if (message->rm_Action & RXFF_RESULT)
{ {
Evaluator *evaluator = new Evaluator(message->rm_Args[0]);
evaluator->Evaluate();
const char *result = evaluator->GetResult(); const char *result = evaluator->GetResult();
message->rm_Result2 = (LONG)CreateArgstring((STRPTR)result, (LONG)StrLen(result)); message->rm_Result2 = (LONG)CreateArgstring((STRPTR)result, (LONG)StrLen(result));
delete evaluator;
} }
delete evaluator;
return true; return true;
} }

View File

@ -80,6 +80,11 @@ void AmigaShellConsole::ReadLine()
{ {
Flush(Input()); Flush(Input());
FGets(Input(), line, linesize); FGets(Input(), line, linesize);
if (CheckSignal(SIGBREAKF_CTRL_C))
{
exit = true;
}
} }
void AmigaShellConsole::WriteString(const char* string) void AmigaShellConsole::WriteString(const char* string)

View File

@ -104,7 +104,12 @@ char* AmigaLanguage::Translate(identhelpdef *def)
char AmigaLanguage::GetFractionPoint() char AmigaLanguage::GetFractionPoint()
{ {
#if defined(STRICT_LOCAL)
return locale->loc_DecimalPoint[0]; return locale->loc_DecimalPoint[0];
#else
// To ensure compatibility, set fraction point to full stop / period
return '.';
#endif
} }
bool AmigaLanguage::CharIsAlNum(long unsigned int character) bool AmigaLanguage::CharIsAlNum(long unsigned int character)

View File

@ -71,7 +71,7 @@ static struct GfxBase *GfxBase = nullptr;
static struct LocaleBase *LocaleBase = nullptr; static struct LocaleBase *LocaleBase = nullptr;
static struct IntuitionBase *IntuitionBase = nullptr; static struct IntuitionBase *IntuitionBase = nullptr;
#define ARGS_FORMAT "SHELL/S,NOANSI/S,INPUT/F" #define ARGS_FORMAT "SHELL/S,NOANSI/S,AREXX/S,INPUT/F"
#if defined(AOS3) #if defined(AOS3)
#define RDPTR LONG * #define RDPTR LONG *
@ -90,6 +90,7 @@ AmigaProgram::AmigaProgram()
rdargs = nullptr; rdargs = nullptr;
args.shell = FALSE; args.shell = FALSE;
args.noansi = FALSE; args.noansi = FALSE;
args.arexx = FALSE;
args.input = nullptr; args.input = nullptr;
Console = nullptr; Console = nullptr;
} }
@ -179,7 +180,14 @@ void AmigaProgram::Start()
AmigaARexx *arexx = new AmigaARexx(); AmigaARexx *arexx = new AmigaARexx();
arexx->Start(); arexx->Start();
if (args.arexx)
{
Wait(SIGBREAKF_CTRL_C);
}
else
{
Console->Start(); Console->Start();
}
arexx->Stop(); arexx->Stop();
delete arexx; delete arexx;

View File

@ -37,6 +37,7 @@ struct amathargs
{ {
long shell; long shell;
long noansi; long noansi;
long arexx;
char* input; char* input;
}; };