BAS: Another file closer to the coding standard

This commit is contained in:
Gregory Nutt 2014-11-02 08:49:20 -06:00
parent 011dde0387
commit 2a0ed1c17b
3 changed files with 308 additions and 157 deletions

View File

@ -1,5 +1,66 @@
/* Local variables and the run time stack. */ /****************************************************************************
/* #includes */ /*{{{C}}}*//*{{{*/ * apps/examples/interpreters/bas/auto.c
* 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.
*
* Adapted to NuttX and re-released under a 3-clause BSD license:
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Authors: Alan Carvalho de Assis <Alan Carvalho de Assis>
* Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#undef _POSIX_SOURCE #undef _POSIX_SOURCE
#define _POSIX_SOURCE 1 #define _POSIX_SOURCE 1
#undef _POSIX_C_SOURCE #undef _POSIX_C_SOURCE
@ -23,13 +84,20 @@
#ifdef USE_DMALLOC #ifdef USE_DMALLOC
# include "dmalloc.h" # include "dmalloc.h"
#endif #endif
/*}}}*/
/* #defines */ /*{{{*/ /****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define INCREASE_STACK 16 #define INCREASE_STACK 16
/*}}}*/
/****************************************************************************
* Public Functions
****************************************************************************/
/* interpretation methods */ /* interpretation methods */
struct Auto *Auto_new(struct Auto *this) /*{{{*/
struct Auto *Auto_new(struct Auto *this)
{ {
this->stackPointer = 0; this->stackPointer = 0;
this->stackCapacity = 0; this->stackCapacity = 0;
@ -44,14 +112,18 @@ struct Auto *Auto_new(struct Auto *this) /*{{{*/
this->cur = this->all = (struct Symbol *)0; this->cur = this->all = (struct Symbol *)0;
return this; return this;
} }
/*}}}*/
void Auto_destroy(struct Auto *this) /*{{{*/ void Auto_destroy(struct Auto *this)
{ {
struct Symbol *l; struct Symbol *l;
Value_destroy(&this->err); Value_destroy(&this->err);
Value_destroy(&this->lastdet); Value_destroy(&this->lastdet);
if (this->stackCapacity) free(this->slot); if (this->stackCapacity)
{
free(this->slot);
}
for (l = this->all; l != (struct Symbol *)0;) for (l = this->all; l != (struct Symbol *)0;)
{ {
struct Symbol *f; struct Symbol *f;
@ -62,22 +134,38 @@ void Auto_destroy(struct Auto *this) /*{{{*/
free(f); free(f);
} }
} }
/*}}}*/
struct Var *Auto_pushArg(struct Auto *this) /*{{{*/ struct Var *Auto_pushArg(struct Auto *this)
{ {
if ((this->stackPointer + 1) >= this->stackCapacity) 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 =
realloc(this->slot,
sizeof(this->slot[0]) *
(this->
stackCapacity ? (this->stackCapacity =
this->stackPointer +
INCREASE_STACK) : (this->stackCapacity =
INCREASE_STACK)));
} }
return &this->slot[this->stackPointer++].var; return &this->slot[this->stackPointer++].var;
} }
/*}}}*/
void Auto_pushFuncRet(struct Auto *this, int firstarg, struct Pc *pc) /*{{{*/ void Auto_pushFuncRet(struct Auto *this, int firstarg, struct Pc *pc)
{ {
if (this->stackPointer + 2 >= this->stackCapacity) 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 =
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.onerror = this->onerror;
this->slot[this->stackPointer].retException.resumeable = this->resumeable; this->slot[this->stackPointer].retException.resumeable = this->resumeable;
++this->stackPointer; ++this->stackPointer;
@ -89,50 +177,78 @@ void Auto_pushFuncRet(struct Auto *this, int firstarg, struct Pc *pc) /*{{{*/
this->frameSize = this->stackPointer - firstarg; this->frameSize = this->stackPointer - firstarg;
this->onerror.line = -1; this->onerror.line = -1;
} }
/*}}}*/
void Auto_pushGosubRet(struct Auto *this, struct Pc *pc) /*{{{*/ void Auto_pushGosubRet(struct Auto *this, struct Pc *pc)
{ {
if ((this->stackPointer + 1) >= this->stackCapacity) 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 =
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; this->slot[this->stackPointer].retFrame.pc = *pc;
++this->stackPointer; ++this->stackPointer;
} }
/*}}}*/
struct Var *Auto_local(struct Auto *this, int l) /*{{{*/ struct Var *Auto_local(struct Auto *this, int l)
{ {
assert(this->frameSize > (l + 2)); assert(this->frameSize > (l + 2));
return &(this->slot[this->framePointer + l].var); return &(this->slot[this->framePointer + l].var);
} }
/*}}}*/
int Auto_funcReturn(struct Auto *this, struct Pc *pc) /*{{{*/ int Auto_funcReturn(struct Auto *this, struct Pc *pc)
{ {
int i, retFrame, retException; int i, retFrame, retException;
if (this->stackPointer==0) return 0; if (this->stackPointer == 0)
{
return 0;
}
assert(this->frameSize); assert(this->frameSize);
retFrame = this->framePointer + this->frameSize - 1; retFrame = this->framePointer + this->frameSize - 1;
retException = this->framePointer + this->frameSize - 2; retException = this->framePointer + this->frameSize - 2;
assert(retException >= 0 && retFrame < this->stackPointer); assert(retException >= 0 && retFrame < this->stackPointer);
for (i=0; i<this->frameSize-2; ++i) Var_destroy(&this->slot[this->framePointer+i].var); for (i = 0; i < this->frameSize - 2; ++i)
{
Var_destroy(&this->slot[this->framePointer + i].var);
}
this->stackPointer = this->framePointer; this->stackPointer = this->framePointer;
if (pc!=(struct Pc*)0) *pc=this->slot[retFrame].retFrame.pc; if (pc != (struct Pc *)0)
{
*pc = this->slot[retFrame].retFrame.pc;
}
this->frameSize = this->slot[retFrame].retFrame.frameSize; this->frameSize = this->slot[retFrame].retFrame.frameSize;
this->framePointer = this->slot[retFrame].retFrame.framePointer; this->framePointer = this->slot[retFrame].retFrame.framePointer;
this->onerror = this->slot[retException].retException.onerror; this->onerror = this->slot[retException].retException.onerror;
return 1; return 1;
} }
/*}}}*/
int Auto_gosubReturn(struct Auto *this, struct Pc *pc) /*{{{*/ int Auto_gosubReturn(struct Auto *this, struct Pc *pc)
{ {
if (this->stackPointer<=this->framePointer+this->frameSize) return 0; if (this->stackPointer <= this->framePointer + this->frameSize)
{
return 0;
}
--this->stackPointer; --this->stackPointer;
if (pc) *pc=this->slot[this->stackPointer].retFrame.pc; if (pc)
{
*pc = this->slot[this->stackPointer].retFrame.pc;
}
return 1; return 1;
} }
/*}}}*/
void Auto_frameToError(struct Auto *this, struct Program *program, struct Value *v) /*{{{*/ void Auto_frameToError(struct Auto *this, struct Program *program, struct Value *v)
{ {
int i = this->stackPointer, framePointer, frameSize, retFrame; int i = this->stackPointer, framePointer, frameSize, retFrame;
struct Pc p; struct Pc p;
@ -145,6 +261,7 @@ void Auto_frameToError(struct Auto *this, struct Program *program, struct Value
Value_errorSuffix(v, _("Called")); Value_errorSuffix(v, _("Called"));
Program_PCtoError(program, &p, v); Program_PCtoError(program, &p, v);
} }
if (i) if (i)
{ {
retFrame = framePointer + frameSize - 1; retFrame = framePointer + frameSize - 1;
@ -156,18 +273,17 @@ void Auto_frameToError(struct Auto *this, struct Program *program, struct Value
Program_PCtoError(program, &p, v); Program_PCtoError(program, &p, v);
} }
} }
/*}}}*/
void Auto_setError(struct Auto *this, long int line, struct Pc *pc, struct Value *v) /*{{{*/ void Auto_setError(struct Auto *this, long int line, struct Pc *pc, struct Value *v)
{ {
this->erpc = *pc; this->erpc = *pc;
this->erl = line; this->erl = line;
Value_destroy(&this->err); Value_destroy(&this->err);
Value_clone(&this->err, v); Value_clone(&this->err, v);
} }
/*}}}*/
/* compilation methods */ /* compilation methods */
int Auto_find(struct Auto *this, struct Identifier *ident) /*{{{*/ int Auto_find(struct Auto *this, struct Identifier *ident)
{ {
struct Symbol *find; struct Symbol *find;
@ -176,60 +292,94 @@ int Auto_find(struct Auto *this, struct Identifier *ident) /*{{{*/
const char *s = ident->name; const char *s = ident->name;
const char *r = find->name; const char *r = find->name;
while (*s && tolower(*s)==tolower(*r)) { ++s; ++r; }; while (*s && tolower(*s) == tolower(*r))
{
++s;
++r;
}
if (tolower(*s) == tolower(*r)) if (tolower(*s) == tolower(*r))
{ {
ident->sym = find; ident->sym = find;
return 1; return 1;
} }
} }
return 0; return 0;
} }
/*}}}*/
int Auto_variable(struct Auto *this, const struct Identifier *ident) /*{{{*/ int Auto_variable(struct Auto *this, const struct Identifier *ident)
{ {
struct Symbol **tail; struct Symbol **tail;
int offset; int offset;
for (offset=0,tail=&this->cur; *tail!=(struct Symbol*)0; tail=&(*tail)->next,++offset) for (offset = 0, tail = &this->cur;
*tail != (struct Symbol *)0;
tail = &(*tail)->next, ++offset)
{ {
const char *s = ident->name; const char *s = ident->name;
const char *r = (*tail)->name; const char *r = (*tail)->name;
while (*s && tolower(*s)==tolower(*r)) { ++s; ++r; }; while (*s && tolower(*s) == tolower(*r))
if (tolower(*s)==tolower(*r)) return 0; {
++s;
++r;
} }
if (tolower(*s) == tolower(*r))
{
return 0;
}
}
(*tail) = malloc(sizeof(struct Symbol)); (*tail) = malloc(sizeof(struct Symbol));
(*tail)->next = (struct Symbol *)0; (*tail)->next = (struct Symbol *)0;
(*tail)->name = strcpy(malloc(strlen(ident->name) + 1), ident->name); (*tail)->name = strcpy(malloc(strlen(ident->name) + 1), ident->name);
(*tail)->type = LOCALVAR; (*tail)->type = LOCALVAR;
(*tail)->u.local.type = ident->defaultType; (*tail)->u.local.type = ident->defaultType;
/* the offset -1 of the V_VOID procedure return symbol is ok, it is not used */ /* the offset -1 of the V_VOID procedure return symbol is ok, it is not used */
(*tail)->u.local.offset=offset-(this->cur->u.local.type==V_VOID?1:0);
(*tail)->u.local.offset =
offset - (this->cur->u.local.type == V_VOID ? 1 : 0);
return 1; return 1;
} }
/*}}}*/
enum ValueType Auto_argType(const struct Auto *this, int l) /*{{{*/ enum ValueType Auto_argType(const struct Auto *this, int l)
{ {
struct Symbol *find; struct Symbol *find;
int offset; int offset;
if (this->cur->u.local.type==V_VOID) ++l; if (this->cur->u.local.type == V_VOID)
for (offset=0,find=this->cur; l!=offset; find=find->next,++offset) assert(find!=(struct Symbol*)0); {
++l;
}
for (offset = 0, find = this->cur; l != offset; find = find->next, ++offset)
{
assert(find != (struct Symbol *)0);
}
assert(find != (struct Symbol *)0); assert(find != (struct Symbol *)0);
return find->u.local.type; return find->u.local.type;
} }
/*}}}*/
enum ValueType Auto_varType(const struct Auto *this, struct Symbol *sym) /*{{{*/ enum ValueType Auto_varType(const struct Auto *this, struct Symbol *sym)
{ {
struct Symbol *find; struct Symbol *find;
for (find=this->cur; find->u.local.offset!=sym->u.local.offset; find=find->next) assert(find!=(struct Symbol*)0); 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); assert(find != (struct Symbol *)0);
return find->u.local.type; return find->u.local.type;
} }
/*}}}*/
void Auto_funcEnd(struct Auto *this) /*{{{*/ void Auto_funcEnd(struct Auto *this)
{ {
struct Symbol **tail; struct Symbol **tail;
@ -237,4 +387,3 @@ void Auto_funcEnd(struct Auto *this) /*{{{*/
*tail = this->cur; *tail = this->cur;
this->cur = (struct Symbol *)0; this->cur = (struct Symbol *)0;
} }
/*}}}*/

View File

@ -168,7 +168,9 @@ static int size(int dev)
file = n; file = n;
for (i = capacity; i <= dev; ++i) for (i = capacity; i <= dev; ++i)
{
file[i] = (struct FileStream *)0; file[i] = (struct FileStream *)0;
}
capacity = dev + 1; capacity = dev + 1;
} }

View File

@ -4243,7 +4243,7 @@ static void yy_load_buffer_state (void)
* In that case, we don't want to reset the lineno or column. * In that case, we don't want to reset the lineno or column.
*/ */
if (b != YY_CURRENT_BUFFER if (b != YY_CURRENT_BUFFER)
{ {
b->yy_bs_lineno = 1; b->yy_bs_lineno = 1;
b->yy_bs_column = 0; b->yy_bs_column = 0;