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.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* 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
|
|
|
|
|
|
|
struct Auto *Auto_new(struct Auto *this)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 15:49:20 +01:00
|
|
|
this->stackPointer = 0;
|
|
|
|
this->stackCapacity = 0;
|
|
|
|
this->framePointer = 0;
|
|
|
|
this->frameSize = 0;
|
|
|
|
this->onerror.line = -1;
|
|
|
|
this->erl = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
Value_new_NIL(&this->err);
|
|
|
|
Value_new_NIL(&this->lastdet);
|
2014-11-02 15:49:20 +01:00
|
|
|
this->begindata.line = -1;
|
|
|
|
this->slot = (union AutoSlot *)0;
|
|
|
|
this->cur = this->all = (struct Symbol *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
return this;
|
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
|
|
|
void Auto_destroy(struct Auto *this)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol *l;
|
|
|
|
|
|
|
|
Value_destroy(&this->err);
|
|
|
|
Value_destroy(&this->lastdet);
|
2014-11-02 15:49:20 +01:00
|
|
|
if (this->stackCapacity)
|
|
|
|
{
|
|
|
|
free(this->slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (l = this->all; l != (struct Symbol *)0;)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
struct Var *Auto_pushArg(struct Auto *this)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 15:49:20 +01:00
|
|
|
if ((this->stackPointer + 1) >= this->stackCapacity)
|
|
|
|
{
|
|
|
|
this->slot =
|
|
|
|
realloc(this->slot,
|
|
|
|
sizeof(this->slot[0]) *
|
|
|
|
(this->
|
|
|
|
stackCapacity ? (this->stackCapacity =
|
|
|
|
this->stackPointer +
|
|
|
|
INCREASE_STACK) : (this->stackCapacity =
|
|
|
|
INCREASE_STACK)));
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return &this->slot[this->stackPointer++].var;
|
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
|
|
|
void Auto_pushFuncRet(struct Auto *this, int firstarg, struct Pc *pc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 15:49:20 +01:00
|
|
|
if (this->stackPointer + 2 >= this->stackCapacity)
|
|
|
|
{
|
|
|
|
this->slot =
|
|
|
|
realloc(this->slot,
|
|
|
|
sizeof(this->slot[0]) *
|
|
|
|
(this->
|
|
|
|
stackCapacity ? (this->stackCapacity =
|
|
|
|
this->stackCapacity +
|
|
|
|
INCREASE_STACK) : (this->stackCapacity =
|
|
|
|
INCREASE_STACK)));
|
|
|
|
}
|
|
|
|
|
|
|
|
this->slot[this->stackPointer].retException.onerror = this->onerror;
|
|
|
|
this->slot[this->stackPointer].retException.resumeable = this->resumeable;
|
2014-10-27 14:53:12 +01:00
|
|
|
++this->stackPointer;
|
2014-11-02 15:49:20 +01:00
|
|
|
this->slot[this->stackPointer].retFrame.pc = *pc;
|
|
|
|
this->slot[this->stackPointer].retFrame.framePointer = this->framePointer;
|
|
|
|
this->slot[this->stackPointer].retFrame.frameSize = this->frameSize;
|
2014-10-27 14:53:12 +01:00
|
|
|
++this->stackPointer;
|
2014-11-02 15:49:20 +01:00
|
|
|
this->framePointer = firstarg;
|
|
|
|
this->frameSize = this->stackPointer - firstarg;
|
|
|
|
this->onerror.line = -1;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
|
|
|
void Auto_pushGosubRet(struct Auto *this, struct Pc *pc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 15:49:20 +01:00
|
|
|
if ((this->stackPointer + 1) >= this->stackCapacity)
|
|
|
|
{
|
|
|
|
this->slot =
|
|
|
|
realloc(this->slot,
|
|
|
|
sizeof(this->slot[0]) *
|
|
|
|
(this->
|
|
|
|
stackCapacity ? (this->stackCapacity =
|
|
|
|
this->stackPointer +
|
|
|
|
INCREASE_STACK) : (this->stackCapacity =
|
|
|
|
INCREASE_STACK)));
|
|
|
|
}
|
|
|
|
|
|
|
|
this->slot[this->stackPointer].retFrame.pc = *pc;
|
2014-10-27 14:53:12 +01:00
|
|
|
++this->stackPointer;
|
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
|
|
|
struct Var *Auto_local(struct Auto *this, int l)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 15:49:20 +01:00
|
|
|
assert(this->frameSize > (l + 2));
|
|
|
|
return &(this->slot[this->framePointer + l].var);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
|
|
|
int Auto_funcReturn(struct Auto *this, 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
|
|
|
|
|
|
|
if (this->stackPointer == 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
assert(this->frameSize);
|
2014-11-02 15:49:20 +01:00
|
|
|
retFrame = this->framePointer + this->frameSize - 1;
|
|
|
|
retException = this->framePointer + this->frameSize - 2;
|
|
|
|
assert(retException >= 0 && retFrame < this->stackPointer);
|
|
|
|
for (i = 0; i < this->frameSize - 2; ++i)
|
|
|
|
{
|
|
|
|
Var_destroy(&this->slot[this->framePointer + i].var);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->stackPointer = this->framePointer;
|
|
|
|
if (pc != (struct Pc *)0)
|
|
|
|
{
|
|
|
|
*pc = this->slot[retFrame].retFrame.pc;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->frameSize = this->slot[retFrame].retFrame.frameSize;
|
|
|
|
this->framePointer = this->slot[retFrame].retFrame.framePointer;
|
|
|
|
this->onerror = this->slot[retException].retException.onerror;
|
2014-10-27 14:53:12 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
|
|
|
int Auto_gosubReturn(struct Auto *this, struct Pc *pc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 15:49:20 +01:00
|
|
|
if (this->stackPointer <= this->framePointer + this->frameSize)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
--this->stackPointer;
|
2014-11-02 15:49:20 +01:00
|
|
|
if (pc)
|
|
|
|
{
|
|
|
|
*pc = this->slot[this->stackPointer].retFrame.pc;
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2014-11-02 15:49:20 +01:00
|
|
|
|
|
|
|
void Auto_frameToError(struct Auto *this, 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;
|
|
|
|
int i = this->stackPointer;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 15:49:20 +01:00
|
|
|
framePointer = this->framePointer;
|
|
|
|
frameSize = this->frameSize;
|
|
|
|
while (i > framePointer + frameSize)
|
|
|
|
{
|
|
|
|
p = this->slot[--i].retFrame.pc;
|
|
|
|
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;
|
|
|
|
p = this->slot[retFrame].retFrame.pc;
|
|
|
|
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
|
|
|
|
|
|
|
void Auto_setError(struct Auto *this, long int line, struct Pc *pc, struct Value *v)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 15:49:20 +01:00
|
|
|
this->erpc = *pc;
|
|
|
|
this->erl = line;
|
2014-10-27 14:53:12 +01:00
|
|
|
Value_destroy(&this->err);
|
2014-11-02 15:49:20 +01:00
|
|
|
Value_clone(&this->err, v);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* compilation methods */
|
2014-11-02 15:49:20 +01:00
|
|
|
int Auto_find(struct Auto *this, struct Identifier *ident)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol *find;
|
|
|
|
|
2014-11-02 15:49:20 +01:00
|
|
|
for (find = this->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
|
|
|
|
|
|
|
int Auto_variable(struct Auto *this, const struct Identifier *ident)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol **tail;
|
|
|
|
int offset;
|
|
|
|
|
2014-11-02 15:49:20 +01:00
|
|
|
for (offset = 0, tail = &this->cur;
|
|
|
|
*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;
|
|
|
|
|
2014-10-27 14:53:12 +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 =
|
|
|
|
offset - (this->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
|
|
|
|
|
|
|
enum ValueType Auto_argType(const struct Auto *this, int l)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol *find;
|
|
|
|
int offset;
|
|
|
|
|
2014-11-02 15:49:20 +01:00
|
|
|
if (this->cur->u.local.type == V_VOID)
|
|
|
|
{
|
|
|
|
++l;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (offset = 0, find = this->cur; l != offset; find = find->next, ++offset)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
enum ValueType Auto_varType(const struct Auto *this, struct Symbol *sym)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol *find;
|
|
|
|
|
2014-11-02 15:49:20 +01:00
|
|
|
for (find = this->cur;
|
|
|
|
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
|
|
|
|
|
|
|
void Auto_funcEnd(struct Auto *this)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol **tail;
|
|
|
|
|
2014-11-02 15:49:20 +01:00
|
|
|
for (tail = &this->all; *tail != (struct Symbol *)0; tail = &(*tail)->next);
|
|
|
|
*tail = this->cur;
|
|
|
|
this->cur = (struct Symbol *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|