2014-11-02 15:49:20 +01:00
|
|
|
/****************************************************************************
|
2014-11-11 19:34:00 +01:00
|
|
|
* apps/interpreters/bas/bas_auto.c
|
2014-11-02 15:49:20 +01:00
|
|
|
* BASIC file system interface.
|
|
|
|
*
|
|
|
|
* Copyright (c) 1999-2014 Michael Haardt
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
2023-02-22 06:39:24 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
2014-11-02 15:49:20 +01:00
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-03 16:11:25 +01:00
|
|
|
#include <nuttx/config.h>
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-11-11 19:34:00 +01:00
|
|
|
#include "bas_auto.h"
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 15:49:20 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
#define INCREASE_STACK 16
|
2014-11-03 15:33:39 +01:00
|
|
|
#define _(String) String
|
2014-11-02 15:49:20 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
/* interpretation methods */
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Auto *Auto_new(struct Auto *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->stackPointer = 0;
|
|
|
|
self->stackCapacity = 0;
|
|
|
|
self->framePointer = 0;
|
|
|
|
self->frameSize = 0;
|
|
|
|
self->onerror.line = -1;
|
|
|
|
self->erl = 0;
|
|
|
|
Value_new_NIL(&self->err);
|
|
|
|
Value_new_NIL(&self->lastdet);
|
|
|
|
self->begindata.line = -1;
|
|
|
|
self->slot = (union AutoSlot *)0;
|
|
|
|
self->cur = self->all = (struct Symbol *)0;
|
|
|
|
return self;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Auto_destroy(struct Auto *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol *l;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
Value_destroy(&self->err);
|
|
|
|
Value_destroy(&self->lastdet);
|
|
|
|
if (self->stackCapacity)
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
free(self->slot);
|
2014-11-02 15:49:20 +01:00
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
for (l = self->all; l != (struct Symbol *)0; )
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
|
|
|
struct Symbol *f;
|
|
|
|
|
|
|
|
f = l;
|
|
|
|
l = l->next;
|
|
|
|
free(f->name);
|
|
|
|
free(f);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Var *Auto_pushArg(struct Auto *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if ((self->stackPointer + 1) >= self->stackCapacity)
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->slot =
|
|
|
|
realloc(self->slot,
|
|
|
|
sizeof(self->slot[0]) *
|
|
|
|
(self->
|
|
|
|
stackCapacity ? (self->stackCapacity =
|
|
|
|
self->stackPointer +
|
|
|
|
INCREASE_STACK) : (self->stackCapacity =
|
2014-11-02 15:49:20 +01:00
|
|
|
INCREASE_STACK)));
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
return &self->slot[self->stackPointer++].var;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Auto_pushFuncRet(struct Auto *self, int firstarg, struct Pc *pc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->stackPointer + 2 >= self->stackCapacity)
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->slot =
|
|
|
|
realloc(self->slot,
|
|
|
|
sizeof(self->slot[0]) *
|
|
|
|
(self->
|
|
|
|
stackCapacity ? (self->stackCapacity =
|
|
|
|
self->stackCapacity +
|
|
|
|
INCREASE_STACK) : (self->stackCapacity =
|
2014-11-02 15:49:20 +01:00
|
|
|
INCREASE_STACK)));
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
self->slot[self->stackPointer].retException.onerror = self->onerror;
|
|
|
|
self->slot[self->stackPointer].retException.resumeable = self->resumeable;
|
|
|
|
++self->stackPointer;
|
|
|
|
self->slot[self->stackPointer].retFrame.pc = *pc;
|
|
|
|
self->slot[self->stackPointer].retFrame.framePointer = self->framePointer;
|
|
|
|
self->slot[self->stackPointer].retFrame.frameSize = self->frameSize;
|
|
|
|
++self->stackPointer;
|
|
|
|
self->framePointer = firstarg;
|
|
|
|
self->frameSize = self->stackPointer - firstarg;
|
|
|
|
self->onerror.line = -1;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Auto_pushGosubRet(struct Auto *self, struct Pc *pc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if ((self->stackPointer + 1) >= self->stackCapacity)
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->slot =
|
|
|
|
realloc(self->slot,
|
|
|
|
sizeof(self->slot[0]) *
|
|
|
|
(self->
|
|
|
|
stackCapacity ? (self->stackCapacity =
|
|
|
|
self->stackPointer +
|
|
|
|
INCREASE_STACK) : (self->stackCapacity =
|
2014-11-02 15:49:20 +01:00
|
|
|
INCREASE_STACK)));
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
self->slot[self->stackPointer].retFrame.pc = *pc;
|
|
|
|
++self->stackPointer;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
struct Var *Auto_local(struct Auto *self, int l)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self->frameSize > (l + 2));
|
|
|
|
return &(self->slot[self->framePointer + l].var);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
int Auto_funcReturn(struct Auto *self, struct Pc *pc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-25 22:14:28 +01:00
|
|
|
int retException;
|
|
|
|
int retFrame;
|
|
|
|
int i;
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->stackPointer == 0)
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
assert(self->frameSize);
|
|
|
|
retFrame = self->framePointer + self->frameSize - 1;
|
|
|
|
retException = self->framePointer + self->frameSize - 2;
|
|
|
|
assert(retException >= 0 && retFrame < self->stackPointer);
|
|
|
|
for (i = 0; i < self->frameSize - 2; ++i)
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
Var_destroy(&self->slot[self->framePointer + i].var);
|
2014-11-02 15:49:20 +01:00
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
self->stackPointer = self->framePointer;
|
2014-11-02 15:49:20 +01:00
|
|
|
if (pc != (struct Pc *)0)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
*pc = self->slot[retFrame].retFrame.pc;
|
2014-11-02 15:49:20 +01:00
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
self->frameSize = self->slot[retFrame].retFrame.frameSize;
|
|
|
|
self->framePointer = self->slot[retFrame].retFrame.framePointer;
|
|
|
|
self->onerror = self->slot[retException].retException.onerror;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
int Auto_gosubReturn(struct Auto *self, struct Pc *pc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->stackPointer <= self->framePointer + self->frameSize)
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
--self->stackPointer;
|
2014-11-02 15:49:20 +01:00
|
|
|
if (pc)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
*pc = self->slot[self->stackPointer].retFrame.pc;
|
2014-11-02 15:49:20 +01:00
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Auto_frameToError(struct Auto *self,
|
2023-02-22 06:39:24 +01:00
|
|
|
struct Program *program, struct Value *v)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Pc p;
|
2014-11-25 22:14:28 +01:00
|
|
|
int framePointer;
|
2014-11-25 22:43:38 +01:00
|
|
|
int frameSize;
|
2014-11-25 22:14:28 +01:00
|
|
|
int retFrame;
|
2023-09-11 06:02:05 +02:00
|
|
|
int i = self->stackPointer;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
framePointer = self->framePointer;
|
|
|
|
frameSize = self->frameSize;
|
2014-11-02 15:49:20 +01:00
|
|
|
while (i > framePointer + frameSize)
|
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
p = self->slot[--i].retFrame.pc;
|
2014-11-02 15:49:20 +01:00
|
|
|
Value_errorSuffix(v, _("Called"));
|
|
|
|
Program_PCtoError(program, &p, v);
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
if (i)
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
|
|
|
retFrame = framePointer + frameSize - 1;
|
2023-09-11 06:02:05 +02:00
|
|
|
p = self->slot[retFrame].retFrame.pc;
|
2014-11-02 15:49:20 +01:00
|
|
|
Value_errorSuffix(v, _("Proc Called"));
|
|
|
|
Program_PCtoError(program, &p, v);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Auto_setError(struct Auto *self, long int line,
|
2023-02-22 06:39:24 +01:00
|
|
|
struct Pc *pc, struct Value *v)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2023-09-11 06:02:05 +02:00
|
|
|
self->erpc = *pc;
|
|
|
|
self->erl = line;
|
|
|
|
Value_destroy(&self->err);
|
|
|
|
Value_clone(&self->err, v);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compilation methods */
|
2023-02-22 06:39:24 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
int Auto_find(struct Auto *self, struct Identifier *ident)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol *find;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
for (find = self->cur; find != (struct Symbol *)0; find = find->next)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 15:49:20 +01:00
|
|
|
const char *s = ident->name;
|
|
|
|
const char *r = find->name;
|
|
|
|
|
|
|
|
while (*s && tolower(*s) == tolower(*r))
|
|
|
|
{
|
|
|
|
++s;
|
|
|
|
++r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tolower(*s) == tolower(*r))
|
|
|
|
{
|
|
|
|
ident->sym = find;
|
|
|
|
return 1;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
int Auto_variable(struct Auto *self, const struct Identifier *ident)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol **tail;
|
|
|
|
int offset;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
for (offset = 0, tail = &self->cur;
|
2014-11-02 15:49:20 +01:00
|
|
|
*tail != (struct Symbol *)0;
|
|
|
|
tail = &(*tail)->next, ++offset)
|
|
|
|
{
|
|
|
|
const char *s = ident->name;
|
|
|
|
const char *r = (*tail)->name;
|
|
|
|
|
|
|
|
while (*s && tolower(*s) == tolower(*r))
|
|
|
|
{
|
|
|
|
++s;
|
|
|
|
++r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tolower(*s) == tolower(*r))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(*tail) = malloc(sizeof(struct Symbol));
|
|
|
|
(*tail)->next = (struct Symbol *)0;
|
2023-02-09 12:01:32 +01:00
|
|
|
(*tail)->name = strdup(ident->name);
|
2014-11-02 15:49:20 +01:00
|
|
|
(*tail)->type = LOCALVAR;
|
|
|
|
(*tail)->u.local.type = ident->defaultType;
|
|
|
|
|
2023-02-22 06:39:24 +01:00
|
|
|
/* the offset -1 of the V_VOID procedure return symbol is ok,
|
|
|
|
* it is not used
|
|
|
|
*/
|
2014-11-02 15:49:20 +01:00
|
|
|
|
|
|
|
(*tail)->u.local.offset =
|
2023-09-11 06:02:05 +02:00
|
|
|
offset - (self->cur->u.local.type == V_VOID ? 1 : 0);
|
2014-10-27 14:53:12 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
enum ValueType Auto_argType(const struct Auto *self, int l)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol *find;
|
|
|
|
int offset;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
if (self->cur->u.local.type == V_VOID)
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
|
|
|
++l;
|
|
|
|
}
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
for (offset = 0, find = self->cur; l != offset;
|
2023-02-22 06:39:24 +01:00
|
|
|
find = find->next, ++offset)
|
2014-11-02 15:49:20 +01:00
|
|
|
{
|
|
|
|
assert(find != (struct Symbol *)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(find != (struct Symbol *)0);
|
2014-10-27 14:53:12 +01:00
|
|
|
return find->u.local.type;
|
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
enum ValueType Auto_varType(const struct Auto *self, struct Symbol *sym)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol *find;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
for (find = self->cur;
|
2014-11-02 15:49:20 +01:00
|
|
|
find->u.local.offset != sym->u.local.offset;
|
|
|
|
find = find->next)
|
|
|
|
{
|
|
|
|
assert(find != (struct Symbol *)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(find != (struct Symbol *)0);
|
2014-10-27 14:53:12 +01:00
|
|
|
return find->u.local.type;
|
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
void Auto_funcEnd(struct Auto *self)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol **tail;
|
|
|
|
|
2023-09-11 06:02:05 +02:00
|
|
|
for (tail = &self->all; *tail != NULL; tail = &(*tail)->next);
|
|
|
|
*tail = self->cur;
|
|
|
|
self->cur = (struct Symbol *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|