Compare commits

...

9 Commits

13 changed files with 214 additions and 151 deletions

View File

@ -29,104 +29,81 @@
#ifndef AMATH_H
#define AMATH_H
/******************************************************************************/
#ifdef __AMIGA__
# define SYS_NAME "AmigaOS 3"
# ifndef AOS3
# define AOS3
# endif
#endif
/******************************************************************************/
#ifdef __AROS__
# define SYS_NAME "Aros"
# ifndef AROS
# define AROS
# endif
#endif
/******************************************************************************/
#ifdef __MORPHOS__
/* Operating System */
#if defined(__MORPHOS__)
# define SYS_NAME "MorphOS"
# ifndef MORPHOS
# define MORPHOS
# endif
#endif
/******************************************************************************/
#ifdef __amigaos4__
#elif defined(__amigaos4__)
# define SYS_NAME "AmigaOS 4"
# ifndef AOS4
# define AOS4
# endif
#endif
/******************************************************************************/
#ifdef __HAIKU__
#elif defined(__AMIGA__) || defined(__amigaos3__)
# define SYS_NAME "AmigaOS 3"
# ifndef AOS3
# define AOS3
# endif
#elif defined(__AROS__)
# define SYS_NAME "AROS"
# ifndef AROS
# define AROS
# endif
#elif defined(__HAIKU__)
# define SYS_NAME "Haiku"
# ifndef HAIKU
# define HAIKU
# endif
#endif
/******************************************************************************/
#ifdef __APPLE__
#elif defined(__APPLE__) && defined(__MACH__)
# define SYS_NAME "Apple"
# ifndef APPLE
# define APPLE
# endif
#endif
/******************************************************************************/
#ifdef __FreeBSD__
#elif defined(__FreeBSD__)
# define SYS_NAME "FreeBSD"
# ifndef FREEBSD
# define FREEBSD
# endif
#endif
/******************************************************************************/
#ifdef __NetBSD__
#elif defined(__NetBSD__)
# define SYS_NAME "NetBSD"
# ifndef NETBSD
# define NETBSD
# endif
#endif
/******************************************************************************/
#ifdef __OpenBSD__
#elif defined(__OpenBSD__)
# define SYS_NAME "OpenBSD"
# ifndef OPENBSD
# define OPENBSD
# endif
#endif
/******************************************************************************/
#ifdef __DragonFly__
#elif defined(__DragonFly__)
# define SYS_NAME "DragonFly BSD"
# ifndef DRAGONFLY
# define DRAGONFLY
# endif
#endif
/******************************************************************************/
#ifdef __linux__
#elif defined(__linux__)
# define SYS_NAME "Linux"
# ifndef LINUX
# define LINUX
# endif
#endif
/******************************************************************************/
#ifdef _WIN32
#elif defined(_WIN32)
# define SYS_NAME "Windows"
# ifndef WINDOWS
# define WINDOWS
# endif
#endif
/******************************************************************************/
#ifdef _WIN64
#elif defined(_WIN64)
# define SYS_NAME "Windows 64 bit"
# ifndef WINDOWS
# define WINDOWS
# endif
#endif
/******************************************************************************/
#if defined(AOS3) || defined(AOS4) || defined(AROS) || defined (MORPHOS)
# ifndef AMIGA
# define AMIGA
# endif
#endif
/******************************************************************************/
#if defined(unix) || defined(__unix__) || defined(__unix)
# ifndef UNIX
# define UNIX
@ -135,7 +112,7 @@
# define SYS_NAME "Unix variant"
# endif
#endif
/******************************************************************************/
#if defined(HAIKU) || defined(UNIX) || defined(APPLE)
# ifndef TERMIOS
# define TERMIOS
@ -143,39 +120,38 @@
# include <stdint.h>
# include <unistd.h>
#endif
/******************************************************************************/
#if defined(WINDOWS)
# include <stdint.h>
#endif
/******************************************************************************/
#if defined(AROS) || defined(MORPHOS) || defined(AOS4)
# include <stdint.h>
# include <sys/types.h>
#endif
/******************************************************************************/
#if defined(AOS3)
# include <sys/types.h>
# define IPTR LONG*
# define uintptr_t uint32_t
typedef u_int8_t uint8_t;
typedef u_int16_t uint16_t;
typedef u_int32_t uint32_t;
typedef u_int64_t uint64_t;
#endif
/******************************************************************************/
#if (__cplusplus <= 199711L && !defined(WINDOWS)) || !defined(__cplusplus)
#ifndef nullptr
#define nullptr 0
#endif
# ifndef nullptr
# define nullptr 0
# endif
#elif (__cplusplus <= 199711L) && (defined(__GNUC__) || defined(__GNUG__))
#ifndef nullptr
#define nullptr 0
# ifndef nullptr
# define nullptr 0
# endif
#endif
#endif
/******************************************************************************/
#define assert(x)
/******************************************************************************/
/* Compilers*/
/* Compilers */
#if defined(__clang__)
/* Clang */
# if defined(__apple_build_version__)
@ -228,7 +204,9 @@ typedef u_int64_t uint64_t;
# elif (_MSC_VER == 1900)
# define COMP_VERS "14.0"
# elif (_MSC_VER > 1900)
# define COMP_VERS "15.0+"
# define COMP_VERS "15.x"
# elif (_MSC_VER > 1920)
# define COMP_VERS "16.0+"
# else
# define str(x) #x
# define MSC_VER_STR(x) str(x)
@ -261,11 +239,11 @@ typedef u_int64_t uint64_t;
# define COMP_VERS GCC_VER_STR(__GNUC__) DOT GCC_VER_STR(__GNUC_MINOR__)
# endif
#endif
/******************************************************************************/
#define EMPTYSTRING ""
#define SPACE " "
#define DOT "."
/******************************************************************************/
#if defined(WINDOWS)
#define NEWLINE "\r\n"
#elif defined(APPLE)
@ -273,17 +251,17 @@ typedef u_int64_t uint64_t;
#else
#define NEWLINE "\n"
#endif
/******************************************************************************/
#ifndef __cplusplus
typedef int bool;
#define true 1
#define false 0
#endif
/******************************************************************************/
#define LONG_BIT 32
#define wsize sizeof(unsigned int)
#define wmask (wsize - 1)
/******************************************************************************/
#if defined(INTELCPU) || defined(i386) || defined(i486) || \
defined(intel) || defined(x86) || defined(i86pc) || \
defined(__i386__) || defined(_M_IX86)
@ -295,18 +273,18 @@ typedef int bool;
#if defined(__x86_64__) || defined(_M_AMD64)
# define TXTCPU "amd64"
#endif
/******************************************************************************/
#if defined(__powerpc__) || defined(__powerpc64__)
# define TXTCPU "PowerPC"
#endif
/******************************************************************************/
#if defined(__arm__) || defined(_M_ARM) || defined(__ARM_ARCH_6__)
# define TXTCPU "arm"
#endif
#if defined(__aarch64__) || defined(_M_ARM64)
# define TXTCPU "arm64"
#endif
/******************************************************************************/
#if defined(m68060) || defined(mc68060)
# define TXTCPU "68060"
#elif defined(m68040) || defined(mc68040)
@ -320,33 +298,33 @@ typedef int bool;
#elif defined(m68000) || defined(mc68000)
# define TXTCPU "68000+"
#endif
/******************************************************************************/
#ifndef TXTCPU
//#error what cpu is this ?!
# define TXTCPU EMPTYSTRING
#endif
/******************************************************************************/
#if defined(TXTCPU) && defined(__HAVE_68881__)
# define TXTFPU SPACE "FPU"
#else
# define TXTFPU EMPTYSTRING
#endif
/******************************************************************************/
#define TXTARCH TXTCPU TXTFPU
#define RELDATESTAMP "(11-01-2021)"
#define TXTDOSVERSION "\0$VER: amath 1.90" SPACE RELDATESTAMP SPACE TXTARCH
#define TXTTITLE "amath version 1.9.0"
#define TXTCOPYRIGHT "(c) 2021 Carsten Sonne Larsen"
#define TXTSTARTMSG TXTTITLE SPACE TXTCOPYRIGHT
/******************************************************************************/
#define TXTVERSMSG TXTTITLE SPACE RELDATESTAMP SPACE TXTARCH
#define TXTCOMPMSG "Compiled with " COMP_NAME SPACE COMP_VERS
/******************************************************************************/
#ifdef SYS_NAME
# define TXTSYSMSG TXTCOMPMSG " for " SYS_NAME
# define SYSKNOWN 1
#else
# define TXTSYSMSG TXTCOMPMSG
#endif
/******************************************************************************/
#endif

View File

@ -35,18 +35,22 @@
#include <exec/memory.h>
#include <exec/semaphores.h>
#include <clib/exec_protos.h>
#define ALLOC_MEM(x) AllocVec(x, MEMF_ANY | MEMF_CLEAR)
#define FREE_MEM(x) FreeVec(x)
#define Debug(x,y,z)
#define ALLOC_LIST(x) AllocVec(x, MEMF_ANY | MEMF_CLEAR)
#define FREE_LIST(x) FreeVec(x)
#define ALLOC_MEM(x, y) AllocPooledZero(x, y)
#define FREE_MEM(x, y, z) FreePooled(x, y, z)
#define Debug(x, y, z)
#else
#include <stdlib.h>
#define ALLOC_MEM(x) calloc(1L,x)
#define FREE_MEM(x) free(x)
#define Debug(x,y,z)
#define ALLOC_LIST(x) calloc(1L, x)
#define FREE_LIST(x) free(x)
#define ALLOC_MEM(x, y) calloc(1L, y)
#define FREE_MEM(x, y, z) free(y)
#define Debug(x, y, z)
#endif
#if defined(__x86_64__) || defined(__aarch64__) || \
defined(_M_AMD64) || defined(_M_ARM64) || \
defined(_M_AMD64) || defined(_M_ARM64) || \
defined(__powerpc64__)
#define P64BIT
#endif
@ -56,9 +60,9 @@
*/
struct MemoryBlock
{
struct MemoryBlock* next;
struct MemoryBlock *next;
size_t size;
void* address;
void *address;
};
/**
@ -66,7 +70,8 @@ struct MemoryBlock
*/
struct MemoryList
{
struct MemoryBlock* first;
struct MemoryBlock *first;
void *pool;
size_t peak;
size_t size;
long count;
@ -75,28 +80,56 @@ struct MemoryList
/**
* @brief Global list of allocated memory.
*/
struct MemoryList* list = nullptr;
struct MemoryList *list = nullptr;
void alloc_error(char*, size_t);
void dealloc_error(char*, void*);
void alloc_error(char *, size_t);
void dealloc_error(char *, void *);
#if defined(AMIGA)
static void MemZero(void *address, ULONG size)
{
char *c = (char *)address;
int n = size;
do
{
*c++ = '\0';
} while (--n);
}
static void *AllocPooledZero(void *pool, size_t size)
{
ULONG memSize = (ULONG)size;
void *memory = AllocPooled(pool, memSize);
if (memory != NULL)
{
MemZero(memory, memSize);
}
return memory;
}
#endif
/**
* @brief Allocate memory and add it to the global memory list.
*/
void* AllocMemSafe(size_t size)
void *AllocMemSafe(size_t size)
{
struct MemoryBlock* newblock;
struct MemoryBlock *newblock;
size_t allocsize;
if (list == nullptr)
{
list = (struct MemoryList*)ALLOC_MEM(sizeof(struct MemoryList));
list = (struct MemoryList *)ALLOC_LIST(sizeof(struct MemoryList));
if (!list)
{
alloc_error("list", sizeof(struct MemoryList));
return 0;
}
#if defined(AMIGA)
list->pool = CreatePool(MEMF_ANY, 4096, 512);
#endif
list->first = nullptr;
list->peak = 0;
list->size = 0;
@ -104,24 +137,24 @@ void* AllocMemSafe(size_t size)
}
#ifdef P64BIT
// Align to bytes of 8
allocsize = (size + 7) & ~0x07;
// Align to bytes of 8, remove 0 bytes allocations
allocsize = (size + 8) & ~0x07;
#else
// Align to bytes of 4
allocsize = (size + 3) & ~0x03;
// Align to bytes of 4, remove 0 bytes allocations
allocsize = (size + 4) & ~0x03;
#endif
newblock = (struct MemoryBlock*)ALLOC_MEM(sizeof(struct MemoryBlock));
if (!newblock)
newblock = (struct MemoryBlock *)ALLOC_MEM(list->pool, sizeof(struct MemoryBlock));
if (newblock == NULL)
{
alloc_error("block", sizeof(struct MemoryBlock));
return 0;
}
newblock->address = (struct MemoryBlock*)ALLOC_MEM(allocsize);
if (!newblock->address)
newblock->address = (struct MemoryBlock *)ALLOC_MEM(list->pool, allocsize);
if (newblock->address == NULL)
{
FREE_MEM(newblock);
FREE_MEM(list->pool, newblock, sizeof(struct MemoryBlock));
alloc_error("memory", allocsize);
return 0;
}
@ -141,7 +174,7 @@ void* AllocMemSafe(size_t size)
return newblock->address;
}
void RemoveMemSafe(void* block, bool deallocate)
void RemoveMemSafe(void *block, bool deallocate)
{
struct MemoryBlock *current, *previous;
@ -185,19 +218,19 @@ void RemoveMemSafe(void* block, bool deallocate)
if (deallocate)
{
FREE_MEM(current->address);
FREE_MEM(list->pool, current->address, current->size);
}
current->address = nullptr;
current->next = nullptr;
current->size = 0;
FREE_MEM(current);
FREE_MEM(list->pool, current, sizeof(struct MemoryBlock));
}
/**
* @brief Deallocate memory from the global memory list.
*/
void FreeMemSafe(void* block)
void FreeMemSafe(void *block)
{
RemoveMemSafe(block, true);
}
@ -206,7 +239,7 @@ void FreeMemSafe(void* block)
* @brief Detach an allocated memory from the global memory list.
* @details The memory block is only detached, not deallocated.
*/
void DetachMemSafe(void* block)
void DetachMemSafe(void *block)
{
RemoveMemSafe(block, false);
}
@ -227,29 +260,34 @@ void FreeAllSafe()
while (current != nullptr)
{
next = current->next;
FREE_MEM(current->address);
FREE_MEM(current);
FREE_MEM(list->pool, current->address, current->size);
FREE_MEM(list->pool, current, sizeof(struct MemoryBlock));
current = next;
}
FREE_MEM(list);
#if defined(AMIGA)
DeletePool(list->pool);
#endif
FREE_LIST(list);
list = nullptr;
}
/**
* @brief Get memory usage in the global memory list.
*/
void MemUsage(long* blocks, long* size, long* peak)
void MemUsage(long *blocks, long *size, long *peak)
{
*blocks = list->count;
*size = (long)list->size;
*peak = (long)list->peak;;
*peak = (long)list->peak;
;
}
/**
* @brief Log a memory allocation error
*/
void alloc_error(char* descr, size_t size)
void alloc_error(char *descr, size_t size)
{
Debug("Memory allocation error (%s) with size (%d)\n", descr, size);
//if (size == 0)
@ -259,7 +297,7 @@ void alloc_error(char* descr, size_t size)
/**
* @brief Log a memory deallocation error
*/
void dealloc_error(char* descr, void* p)
void dealloc_error(char *descr, void *p)
{
Debug("Memory deallocation error (%s) address (%x)\n", descr, p);
}

View File

@ -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;

View File

@ -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

View File

@ -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);

View File

@ -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.
*

View File

@ -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);
}
}

View File

@ -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

View File

@ -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;
}

View File

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

View File

@ -104,7 +104,12 @@ char* AmigaLanguage::Translate(identhelpdef *def)
char AmigaLanguage::GetFractionPoint()
{
#if defined(STRICT_LOCAL)
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)

View File

@ -71,12 +71,12 @@ static struct GfxBase *GfxBase = nullptr;
static struct LocaleBase *LocaleBase = 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)
#define RDPTR LONG *
#else
#if defined(AROS)
#define RDPTR IPTR *
#else
#define RDPTR LONG *
#endif
AmigaProgram::AmigaProgram()
@ -90,6 +90,7 @@ AmigaProgram::AmigaProgram()
rdargs = nullptr;
args.shell = FALSE;
args.noansi = FALSE;
args.arexx = FALSE;
args.input = nullptr;
Console = nullptr;
}
@ -179,7 +180,14 @@ void AmigaProgram::Start()
AmigaARexx *arexx = new AmigaARexx();
arexx->Start();
Console->Start();
if (args.arexx)
{
Wait(SIGBREAKF_CTRL_C);
}
else
{
Console->Start();
}
arexx->Stop();
delete arexx;

View File

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