2014-11-04 20:35:56 +01:00
|
|
|
/****************************************************************************
|
2014-11-04 22:17:05 +01:00
|
|
|
* apps/interpreters/bas/bas.c
|
2014-11-04 20:35:56 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-03 16:11:25 +01:00
|
|
|
#include <nuttx/config.h>
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "auto.h"
|
|
|
|
#include "bas.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "fs.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "program.h"
|
|
|
|
#include "value.h"
|
|
|
|
#include "var.h"
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define DIRECTMODE (pc.line== -1)
|
2014-11-03 15:33:39 +01:00
|
|
|
#define _(String) String
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
enum LabelType
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
L_IF = 1,
|
|
|
|
L_ELSE,
|
|
|
|
L_DO,
|
|
|
|
L_DOcondition,
|
|
|
|
L_FOR,
|
|
|
|
L_FOR_VAR,
|
|
|
|
L_FOR_LIMIT,
|
|
|
|
L_FOR_BODY,
|
|
|
|
L_REPEAT,
|
|
|
|
L_SELECTCASE,
|
|
|
|
L_WHILE,
|
|
|
|
L_FUNC
|
|
|
|
};
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
struct LabelStack
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
enum LabelType type;
|
|
|
|
struct Pc patch;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static unsigned int labelStackPointer, labelStackCapacity;
|
2014-10-27 14:53:12 +01:00
|
|
|
static struct LabelStack *labelStack;
|
|
|
|
static struct Pc *lastdata;
|
|
|
|
static struct Pc curdata;
|
|
|
|
static struct Pc nextdata;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
static enum
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
DECLARE,
|
|
|
|
COMPILE,
|
|
|
|
INTERPRET
|
|
|
|
} pass;
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
static int stopped;
|
|
|
|
static int optionbase;
|
|
|
|
static struct Pc pc;
|
|
|
|
static struct Auto stack;
|
|
|
|
static struct Program program;
|
|
|
|
static struct Global globals;
|
|
|
|
static int run_restricted;
|
|
|
|
|
2014-11-04 20:35:56 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
int bas_argc;
|
|
|
|
char *bas_argv0;
|
|
|
|
char **bas_argv;
|
|
|
|
int bas_end;
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
static struct Value *statements(struct Value *value);
|
|
|
|
static struct Value *compileProgram(struct Value *v, int clearGlobals);
|
|
|
|
static struct Value *eval(struct Value *value, const char *desc);
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int cat(const char *filename)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
char buf[4096];
|
|
|
|
ssize_t l;
|
|
|
|
int err;
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if ((fd = open(filename, O_RDONLY)) == -1)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
while ((l = read(fd, buf, sizeof(buf))) > 0)
|
|
|
|
{
|
|
|
|
ssize_t off, w;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
off = 0;
|
|
|
|
while (off < l)
|
|
|
|
{
|
|
|
|
if ((w = write(1, buf + off, l - off)) == -1)
|
|
|
|
{
|
|
|
|
err = errno;
|
|
|
|
close(fd);
|
|
|
|
errno = err;
|
|
|
|
return -1;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
off += w;
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (l == -1)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
err = errno;
|
|
|
|
close(fd);
|
|
|
|
errno = err;
|
|
|
|
return -1;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *lvalue(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Symbol *sym;
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Pc lvpc = pc;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
sym = pc.token->u.identifier->sym;
|
|
|
|
assert(pass == DECLARE || sym->type == GLOBALVAR || sym->type == GLOBALARRAY
|
|
|
|
|| sym->type == LOCALVAR);
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if ((pc.token + 1)->type == T_OP)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Pc idxpc;
|
|
|
|
unsigned int dim, capacity;
|
|
|
|
int *idx;
|
|
|
|
|
|
|
|
pc.token += 2;
|
|
|
|
dim = 0;
|
|
|
|
capacity = 0;
|
|
|
|
idx = (int *)0;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (dim == capacity && pass == INTERPRET) /* enlarge idx */
|
|
|
|
{
|
|
|
|
int *more;
|
|
|
|
|
|
|
|
more =
|
|
|
|
realloc(idx,
|
|
|
|
sizeof(unsigned int) *
|
|
|
|
(capacity ? (capacity *= 2) : (capacity = 3)));
|
|
|
|
if (!more)
|
|
|
|
{
|
|
|
|
if (capacity)
|
|
|
|
free(idx);
|
|
|
|
return Value_new_ERROR(value, OUTOFMEMORY);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
idx = more;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
idxpc = pc;
|
|
|
|
if (eval(value, _("index"))->type == V_ERROR ||
|
|
|
|
VALUE_RETYPE(value, V_INTEGER)->type == V_ERROR)
|
|
|
|
{
|
|
|
|
if (capacity)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
free(idx);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
pc = idxpc;
|
|
|
|
return value;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pass == INTERPRET)
|
|
|
|
{
|
|
|
|
idx[dim] = value->u.integer;
|
|
|
|
++dim;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(value);
|
|
|
|
if (pc.token->type == T_COMMA)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
++pc.token;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pc.token->type != T_CP)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
assert(pass != INTERPRET);
|
|
|
|
return Value_new_ERROR(value, MISSINGCP);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
switch (pass)
|
|
|
|
{
|
|
|
|
case INTERPRET:
|
|
|
|
{
|
|
|
|
if ((value =
|
|
|
|
Var_value(&(sym->u.var), dim, idx, value))->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
pc = lvpc;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
free(idx);
|
|
|
|
return value;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case DECLARE:
|
|
|
|
{
|
|
|
|
return Value_nullValue(V_INTEGER);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case COMPILE:
|
|
|
|
{
|
|
|
|
return Value_nullValue(sym->type ==
|
2014-11-04 20:35:56 +01:00
|
|
|
GLOBALARRAY ? sym->u.
|
|
|
|
var.type : Auto_varType(&stack, sym));
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
return (struct Value *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
++pc.token;
|
|
|
|
switch (pass)
|
|
|
|
{
|
|
|
|
case INTERPRET:
|
|
|
|
return VAR_SCALAR_VALUE(sym->type ==
|
|
|
|
GLOBALVAR ? &(sym->u.var) : Auto_local(&stack,
|
2014-11-04 20:35:56 +01:00
|
|
|
sym->
|
|
|
|
u.local.offset));
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case DECLARE:
|
|
|
|
return Value_nullValue(V_INTEGER);
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case COMPILE:
|
|
|
|
return Value_nullValue(sym->type ==
|
2014-11-04 20:35:56 +01:00
|
|
|
GLOBALVAR ? sym->u.
|
|
|
|
var.type : Auto_varType(&stack, sym));
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
return (struct Value *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *func(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Identifier *ident;
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Pc funcpc = pc;
|
|
|
|
int firstslot = -99;
|
|
|
|
int args = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
struct Symbol *sym;
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
assert(pc.token->type == T_IDENTIFIER);
|
2014-11-04 20:35:56 +01:00
|
|
|
|
|
|
|
/* Evaluating a function in direct mode may start a program, so it needs to
|
2014-11-02 01:06:28 +01:00
|
|
|
* be compiled. If in direct mode, programs will be compiled after the
|
|
|
|
* direct mode pass DECLARE, but errors are ignored at that point, because
|
|
|
|
* the program may not be needed. If the program is fine, its symbols will
|
|
|
|
* be available during the compile phase already. If not and we need it at
|
2014-11-04 20:35:56 +01:00
|
|
|
* this point, compile it again to get the error and abort.
|
|
|
|
*/
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (DIRECTMODE && !program.runnable && pass != DECLARE)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (compileProgram(value, 0)->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(value);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
ident = pc.token->u.identifier;
|
|
|
|
assert(pass == DECLARE || ident->sym->type == BUILTINFUNCTION ||
|
|
|
|
ident->sym->type == USERFUNCTION);
|
|
|
|
++pc.token;
|
|
|
|
if (pass != DECLARE)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
firstslot = stack.stackPointer;
|
|
|
|
if (ident->sym->type == USERFUNCTION &&
|
|
|
|
ident->sym->u.sub.retType != V_VOID)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Var *v = Auto_pushArg(&stack);
|
|
|
|
Var_new(v, ident->sym->u.sub.retType, 0, (const unsigned int *)0, 0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pc.token->type == T_OP) /* push arguments to stack */
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
++pc.token;
|
|
|
|
if (pc.token->type != T_CP)
|
|
|
|
{
|
2014-11-04 20:35:56 +01:00
|
|
|
while (1)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-04 20:35:56 +01:00
|
|
|
if (pass == DECLARE)
|
|
|
|
{
|
|
|
|
if (eval(value, _("actual parameter"))->type == V_ERROR)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value_destroy(value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct Var *v = Auto_pushArg(&stack);
|
|
|
|
|
|
|
|
Var_new_scalar(v);
|
|
|
|
if (eval(v->value, (const char *)0)->type == V_ERROR)
|
|
|
|
{
|
|
|
|
Value_clone(value, v->value);
|
|
|
|
while (stack.stackPointer > firstslot)
|
|
|
|
{
|
|
|
|
Var_destroy(&stack.slot[--stack.stackPointer].var);
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->type = v->value->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
++args;
|
|
|
|
if (pc.token->type == T_COMMA)
|
|
|
|
{
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
|
|
|
if (pc.token->type != T_CP)
|
|
|
|
{
|
|
|
|
if (pass != DECLARE)
|
|
|
|
{
|
|
|
|
while (stack.stackPointer > firstslot)
|
|
|
|
{
|
|
|
|
Var_destroy(&stack.slot[--stack.stackPointer].var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Value_new_ERROR(value, MISSINGCP);
|
|
|
|
}
|
|
|
|
|
|
|
|
++pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pass == DECLARE)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Value_new_null(value, ident->defaultType);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
int i;
|
|
|
|
int nomore;
|
|
|
|
int argerr;
|
|
|
|
int overloaded;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pass == INTERPRET && ident->sym->type == USERFUNCTION)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
for (i = 0; i < ident->sym->u.sub.u.def.localLength; ++i)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Var *v = Auto_pushArg(&stack);
|
|
|
|
Var_new(v, ident->sym->u.sub.u.def.localTypes[i], 0,
|
|
|
|
(const unsigned int *)0, 0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Auto_pushFuncRet(&stack, firstslot, &pc);
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
sym = ident->sym;
|
|
|
|
overloaded = (pass == COMPILE && sym->type == BUILTINFUNCTION &&
|
|
|
|
sym->u.sub.u.bltin.next);
|
|
|
|
do
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
nomore = (pass == COMPILE &&
|
|
|
|
!(sym->type == BUILTINFUNCTION && sym->u.sub.u.bltin.next));
|
|
|
|
argerr = 0;
|
|
|
|
if (args < sym->u.sub.argLength)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (nomore)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Value_new_ERROR(value, TOOFEW);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
argerr = 1;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
else if (args > sym->u.sub.argLength)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (nomore)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Value_new_ERROR(value, TOOMANY);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
argerr = 1;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
for (i = 0; i < args; ++i)
|
|
|
|
{
|
|
|
|
struct Value *arg =
|
|
|
|
Var_value(Auto_local(&stack, i), 0, (int *)0, value);
|
|
|
|
|
|
|
|
assert(arg->type != V_ERROR);
|
|
|
|
if (overloaded)
|
|
|
|
{
|
|
|
|
if (arg->type != sym->u.sub.argTypes[i])
|
|
|
|
{
|
|
|
|
if (nomore)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Value_new_ERROR(value, TYPEMISMATCH2, i + 1);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
argerr = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (Value_retype(arg, sym->u.sub.argTypes[i])->type ==
|
|
|
|
V_ERROR)
|
|
|
|
{
|
|
|
|
if (nomore)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Value_new_ERROR(value, TYPEMISMATCH3,
|
|
|
|
arg->u.error.msg, i + 1);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
argerr = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
if (argerr)
|
|
|
|
{
|
|
|
|
if (nomore)
|
|
|
|
{
|
|
|
|
Auto_funcReturn(&stack, (struct Pc *)0);
|
|
|
|
pc = funcpc;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
sym = sym->u.sub.u.bltin.next;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (argerr);
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
ident->sym = sym;
|
|
|
|
if (sym->type == BUILTINFUNCTION)
|
|
|
|
{
|
|
|
|
if (pass == INTERPRET)
|
|
|
|
{
|
|
|
|
if (sym->u.sub.u.bltin.call(value, &stack)->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
pc = funcpc;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Value_new_null(value, sym->u.sub.retType);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else if (sym->type == USERFUNCTION)
|
|
|
|
{
|
|
|
|
if (pass == INTERPRET)
|
|
|
|
{
|
|
|
|
int r = 1;
|
|
|
|
|
|
|
|
pc = sym->u.sub.u.def.scope.start;
|
|
|
|
if (pc.token->type == T_COLON)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
++pc.token;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Program_skipEOL(&program, &pc, STDCHANNEL, 1);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (statements(value)->type == V_ERROR)
|
|
|
|
{
|
|
|
|
if (strchr(value->u.error.msg, '\n') == (char *)0)
|
|
|
|
{
|
|
|
|
Auto_setError(&stack,
|
|
|
|
Program_lineNumber(&program, &pc), &pc,
|
|
|
|
value);
|
|
|
|
Program_PCtoError(&program, &pc, value);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (stack.onerror.line != -1)
|
|
|
|
{
|
|
|
|
stack.resumeable = 1;
|
|
|
|
pc = stack.onerror;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Auto_frameToError(&stack, &program, value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (value->type != V_NIL)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(value);
|
|
|
|
}
|
|
|
|
while ((r = Program_skipEOL(&program, &pc, STDCHANNEL, 1)));
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (!r)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Value_new_VOID(value);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Value_new_null(value, sym->u.sub.retType);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Auto_funcReturn(&stack, pass == INTERPRET &&
|
|
|
|
value->type != V_ERROR ? &pc : (struct Pc *)0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-03 16:11:25 +01:00
|
|
|
#ifdef CONFIG_INTERPRETER_BAS_USE_LR0
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
/* Grammar with LR(0) sets */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
|
|
|
/* Grammar:
|
|
|
|
*
|
|
|
|
* 1 EV -> E
|
|
|
|
* 2 E -> E op E
|
|
|
|
* 3 E -> op E
|
|
|
|
* 4 E -> ( E )
|
|
|
|
* 5 E -> value
|
|
|
|
*
|
|
|
|
* i0:
|
|
|
|
* EV -> . E goto(0,E)=5
|
|
|
|
* E -> . E op E goto(0,E)=5
|
|
|
|
* E -> . op E +,- shift 2
|
|
|
|
* E -> . ( E ) ( shift 3
|
|
|
|
* E -> . value value shift 4
|
|
|
|
*
|
|
|
|
* i5:
|
|
|
|
* EV -> E . else accept
|
|
|
|
* E -> E . op E op shift 1
|
|
|
|
*
|
|
|
|
* i2:
|
|
|
|
* E -> op . E goto(2,E)=6
|
|
|
|
* E -> . E op E goto(2,E)=6
|
|
|
|
* E -> . op E +,- shift 2
|
|
|
|
* E -> . ( E ) ( shift 3
|
|
|
|
* E -> . value value shift 4
|
|
|
|
*
|
|
|
|
* i3:
|
|
|
|
* E -> ( . E ) goto(3,E)=7
|
|
|
|
* E -> . E op E goto(3,E)=7
|
|
|
|
* E -> . op E +,- shift 2
|
|
|
|
* E -> . ( E ) ( shift 3
|
|
|
|
* E -> . value value shift 4
|
|
|
|
*
|
|
|
|
* i4:
|
|
|
|
* E -> value . reduce 5
|
|
|
|
*
|
|
|
|
* i1:
|
|
|
|
* E -> E op . E goto(1,E)=8
|
|
|
|
* E -> . E op E goto(1,E)=8
|
|
|
|
* E -> . op E +,- shift 2
|
|
|
|
* E -> . ( E ) ( shift 3
|
|
|
|
* E -> . value value shift 4
|
|
|
|
*
|
|
|
|
* i6:
|
|
|
|
* E -> op E . reduce 3
|
|
|
|
* E -> E . op E op* shift 1 *=if stack[-2] contains op of unary lower priority
|
|
|
|
*
|
|
|
|
* i7:
|
|
|
|
* E -> ( E . ) ) shift 9
|
|
|
|
* E -> E . op E op shift 1
|
|
|
|
*
|
|
|
|
* i8:
|
|
|
|
* E -> E op E . reduce 2
|
|
|
|
* E -> E . op E op* shift 1 *=if stack[-2] contains op of lower priority or if
|
|
|
|
* if it is of equal priority and right associative
|
|
|
|
* i9:
|
|
|
|
* E -> ( E ) . reduce 4
|
|
|
|
*/
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *eval(struct Value *value, const char *desc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-04 20:35:56 +01:00
|
|
|
/* Variables */
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
static const int gotoState[10] = { 5, 8, 6, 7, -1, -1, -1, -1, -1, -1 };
|
|
|
|
int capacity = 10;
|
2014-10-27 14:53:12 +01:00
|
|
|
struct Pdastack
|
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
union
|
|
|
|
{
|
|
|
|
enum TokenType token;
|
|
|
|
struct Value value;
|
|
|
|
} u;
|
|
|
|
char state;
|
|
|
|
};
|
|
|
|
struct Pdastack *pdastack = malloc(capacity * sizeof(struct Pdastack));
|
|
|
|
struct Pdastack *sp = pdastack;
|
|
|
|
struct Pdastack *stackEnd = pdastack + capacity - 1;
|
2014-10-27 14:53:12 +01:00
|
|
|
enum TokenType ip;
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
sp->state = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
while (1)
|
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (sp == stackEnd)
|
|
|
|
{
|
|
|
|
pdastack =
|
|
|
|
realloc(pdastack, (capacity + 10) * sizeof(struct Pdastack));
|
|
|
|
sp = pdastack + capacity - 1;
|
|
|
|
capacity += 10;
|
|
|
|
stackEnd = pdastack + capacity - 1;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
ip = pc.token->type;
|
|
|
|
switch (sp->state)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
case 2:
|
2014-11-04 20:35:56 +01:00
|
|
|
case 3: /* including 4 */
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (ip == T_IDENTIFIER)
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 4\n",sp->state); */
|
|
|
|
/* printf("state 4: reduce E -> value\n"); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = gotoState[(sp - 1)->state];
|
|
|
|
if (pass == COMPILE)
|
|
|
|
{
|
|
|
|
if (((pc.token + 1)->type == T_OP ||
|
|
|
|
Auto_find(&stack, pc.token->u.identifier) == 0) &&
|
|
|
|
Global_find(&globals, pc.token->u.identifier,
|
|
|
|
(pc.token + 1)->type == T_OP) == 0)
|
|
|
|
{
|
|
|
|
Value_new_ERROR(value, UNDECLARED);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pass != DECLARE &&
|
|
|
|
(pc.token->u.identifier->sym->type == GLOBALVAR ||
|
|
|
|
pc.token->u.identifier->sym->type == GLOBALARRAY ||
|
|
|
|
pc.token->u.identifier->sym->type == LOCALVAR))
|
|
|
|
{
|
|
|
|
struct Value *l;
|
|
|
|
|
|
|
|
if ((l = lvalue(value))->type == V_ERROR)
|
|
|
|
goto error;
|
|
|
|
Value_clone(&sp->u.value, l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct Pc var = pc;
|
|
|
|
|
|
|
|
func(&sp->u.value);
|
|
|
|
if (sp->u.value.type == V_VOID)
|
|
|
|
{
|
|
|
|
pc = var;
|
|
|
|
Value_new_ERROR(value, VOIDVALUE);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ip == T_INTEGER)
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 4\n",sp->state); */
|
|
|
|
/* printf("state 4: reduce E -> value\n"); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = gotoState[(sp - 1)->state];
|
|
|
|
VALUE_NEW_INTEGER(&sp->u.value, pc.token->u.integer);
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else if (ip == T_REAL)
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 4\n",sp->state); */
|
|
|
|
/* printf("state 4: reduce E -> value\n"); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = gotoState[(sp - 1)->state];
|
|
|
|
VALUE_NEW_REAL(&sp->u.value, pc.token->u.real);
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else if (TOKEN_ISUNARYOPERATOR(ip))
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 2\n",sp->state); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = 2;
|
|
|
|
sp->u.token = ip;
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else if (ip == T_HEXINTEGER)
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 4\n",sp->state); */
|
|
|
|
/* printf("state 4: reduce E -> value\n"); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = gotoState[(sp - 1)->state];
|
|
|
|
VALUE_NEW_INTEGER(&sp->u.value, pc.token->u.hexinteger);
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else if (ip == T_OCTINTEGER)
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 4\n",sp->state); */
|
|
|
|
/* printf("state 4: reduce E -> value\n"); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = gotoState[(sp - 1)->state];
|
|
|
|
VALUE_NEW_INTEGER(&sp->u.value, pc.token->u.octinteger);
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else if (ip == T_OP)
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 3\n",sp->state); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = 3;
|
|
|
|
sp->u.token = T_OP;
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else if (ip == T_STRING)
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 4\n",sp->state); */
|
|
|
|
/* printf("state 4: reduce E -> value\n"); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = gotoState[(sp - 1)->state];
|
|
|
|
Value_new_STRING(&sp->u.value);
|
|
|
|
String_destroy(&sp->u.value.u.string);
|
|
|
|
String_clone(&sp->u.value.u.string, pc.token->u.string);
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char state = sp->state;
|
|
|
|
|
|
|
|
if (state == 0)
|
|
|
|
{
|
|
|
|
if (desc)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Value_new_ERROR(value, MISSINGEXPR, desc);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
value = (struct Value *)0;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Value_new_ERROR(value, MISSINGEXPR, _("operand"));
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
goto error;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
case 5:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (TOKEN_ISBINARYOPERATOR(ip))
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 1\n",sp->state); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = 1;
|
|
|
|
sp->u.token = ip;
|
|
|
|
++pc.token;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(sp == pdastack + 1);
|
|
|
|
*value = sp->u.value;
|
|
|
|
free(pdastack);
|
|
|
|
return value;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case 6:
|
|
|
|
{
|
|
|
|
if (TOKEN_ISBINARYOPERATOR(ip) &&
|
|
|
|
TOKEN_UNARYPRIORITY((sp - 1)->u.token) <
|
|
|
|
TOKEN_BINARYPRIORITY(ip))
|
|
|
|
{
|
|
|
|
assert(TOKEN_ISUNARYOPERATOR((sp - 1)->u.token));
|
2014-11-04 20:35:56 +01:00
|
|
|
|
|
|
|
/* printf("state %d: shift 1 (not reducing E -> op E)\n", sp->state); */
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = 1;
|
|
|
|
sp->u.token = ip;
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
enum TokenType op;
|
|
|
|
|
|
|
|
/* printf("reduce E -> op E\n"); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
--sp;
|
|
|
|
op = sp->u.token;
|
|
|
|
sp->u.value = (sp + 1)->u.value;
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case T_PLUS:
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case T_MINUS:
|
|
|
|
Value_uneg(&sp->u.value, pass == INTERPRET);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case T_NOT:
|
|
|
|
Value_unot(&sp->u.value, pass == INTERPRET);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
sp->state = gotoState[(sp - 1)->state];
|
|
|
|
if (sp->u.value.type == V_ERROR)
|
|
|
|
{
|
|
|
|
*value = sp->u.value;
|
|
|
|
--sp;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-11-04 20:35:56 +01:00
|
|
|
case 7: /* including 9 */
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (TOKEN_ISBINARYOPERATOR(ip))
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 1\n"sp->state); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = 1;
|
|
|
|
sp->u.token = ip;
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else if (ip == T_CP)
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 9\n",sp->state); */
|
|
|
|
/* printf("state 9: reduce E -> ( E )\n"); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
--sp;
|
|
|
|
sp->state = gotoState[(sp - 1)->state];
|
|
|
|
sp->u.value = (sp + 1)->u.value;
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Value_new_ERROR(value, MISSINGCP);
|
|
|
|
goto error;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case 8:
|
|
|
|
{
|
|
|
|
int p1, p2;
|
|
|
|
|
|
|
|
if (TOKEN_ISBINARYOPERATOR(ip)
|
|
|
|
&&
|
|
|
|
(((p1 = TOKEN_BINARYPRIORITY((sp - 1)->u.token)) < (p2 =
|
|
|
|
TOKEN_BINARYPRIORITY
|
|
|
|
(ip))) ||
|
|
|
|
(p1 == p2 && TOKEN_ISRIGHTASSOCIATIVE((sp - 1)->u.token))))
|
|
|
|
{
|
|
|
|
/* printf("state %d: shift 1\n",sp->state); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++sp;
|
|
|
|
sp->state = 1;
|
|
|
|
sp->u.token = ip;
|
|
|
|
++pc.token;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* printf("state %d: reduce E -> E op E\n",sp->state); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (Value_commonType[(sp - 2)->u.value.type][sp->u.value.type]
|
|
|
|
== V_ERROR)
|
|
|
|
{
|
|
|
|
Value_destroy(&sp->u.value);
|
|
|
|
sp -= 2;
|
|
|
|
Value_destroy(&sp->u.value);
|
|
|
|
Value_new_ERROR(value, INVALIDOPERAND);
|
|
|
|
--sp;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
switch ((sp - 1)->u.token)
|
|
|
|
{
|
|
|
|
case T_LT:
|
|
|
|
Value_lt(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-02 01:06:28 +01:00
|
|
|
pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LE:
|
|
|
|
Value_le(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-02 01:06:28 +01:00
|
|
|
pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_EQ:
|
|
|
|
Value_eq(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_GE:
|
|
|
|
Value_ge(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_GT:
|
|
|
|
Value_gt(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_NE:
|
|
|
|
Value_ne(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_PLUS:
|
|
|
|
Value_add(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
case T_MINUS:
|
|
|
|
Value_sub(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MULT:
|
|
|
|
Value_mult(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_DIV:
|
|
|
|
Value_div(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_IDIV:
|
|
|
|
Value_idiv(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MOD:
|
|
|
|
Value_mod(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_POW:
|
|
|
|
Value_pow(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_AND:
|
|
|
|
Value_and(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_OR:
|
|
|
|
Value_or(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_XOR:
|
|
|
|
Value_xor(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_EQV:
|
|
|
|
Value_eqv(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_IMP:
|
|
|
|
Value_imp(&(sp - 2)->u.value, &sp->u.value,
|
|
|
|
pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(&sp->u.value);
|
|
|
|
sp -= 2;
|
|
|
|
sp->state = gotoState[(sp - 1)->state];
|
|
|
|
if (sp->u.value.type == V_ERROR)
|
|
|
|
{
|
|
|
|
*value = sp->u.value;
|
|
|
|
--sp;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
error:
|
|
|
|
while (sp > pdastack)
|
|
|
|
{
|
|
|
|
switch (sp->state)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
case 5:
|
|
|
|
case 6:
|
|
|
|
case 7:
|
|
|
|
case 8:
|
|
|
|
Value_destroy(&sp->u.value);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
--sp;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
free(pdastack);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2014-11-04 20:35:56 +01:00
|
|
|
static inline struct Value *binarydown(struct Value *value,
|
|
|
|
struct Value *(level) (struct Value *
|
|
|
|
value),
|
|
|
|
const int prio)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
enum TokenType op;
|
|
|
|
struct Pc oppc;
|
|
|
|
|
|
|
|
if (level(value) == (struct Value *)0)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return (struct Value *)0;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (value->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
struct Value x;
|
|
|
|
|
|
|
|
op = pc.token->type;
|
|
|
|
if (!TOKEN_ISBINARYOPERATOR(op) || TOKEN_BINARYPRIORITY(op) != prio)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
oppc = pc;
|
|
|
|
++pc.token;
|
|
|
|
if (level(&x) == (struct Value *)0)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(value);
|
|
|
|
return Value_new_ERROR(value, MISSINGEXPR, _("binary operand"));
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (x.type == V_ERROR)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(value);
|
|
|
|
*value = x;
|
2014-10-27 14:53:12 +01:00
|
|
|
return value;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (Value_commonType[value->type][x.type] == V_ERROR)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(value);
|
|
|
|
Value_destroy(&x);
|
|
|
|
return Value_new_ERROR(value, INVALIDOPERAND);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case T_LT:
|
|
|
|
Value_lt(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LE:
|
|
|
|
Value_le(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_EQ:
|
|
|
|
Value_eq(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_GE:
|
|
|
|
Value_ge(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_GT:
|
|
|
|
Value_gt(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_NE:
|
|
|
|
Value_ne(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_PLUS:
|
|
|
|
Value_add(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MINUS:
|
|
|
|
Value_sub(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MULT:
|
|
|
|
Value_mult(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_DIV:
|
|
|
|
Value_div(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_IDIV:
|
|
|
|
Value_idiv(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MOD:
|
|
|
|
Value_mod(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_POW:
|
|
|
|
Value_pow(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_AND:
|
|
|
|
Value_and(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_OR:
|
|
|
|
Value_or(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_XOR:
|
|
|
|
Value_xor(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_EQV:
|
|
|
|
Value_eqv(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_IMP:
|
|
|
|
Value_imp(value, &x, pass == INTERPRET);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
|
|
|
Value_destroy(&x);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
while (value->type != V_ERROR);
|
|
|
|
|
|
|
|
if (value->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
pc = oppc;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return value;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-11-04 20:35:56 +01:00
|
|
|
static inline struct Value *unarydown(struct Value *value,
|
|
|
|
struct Value *(level) (struct Value *
|
|
|
|
value),
|
|
|
|
const int prio)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
enum TokenType op;
|
|
|
|
struct Pc oppc;
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
op = pc.token->type;
|
|
|
|
if (!TOKEN_ISUNARYOPERATOR(op) || TOKEN_UNARYPRIORITY(op) != prio)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return level(value);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
oppc = pc;
|
2014-10-27 14:53:12 +01:00
|
|
|
++pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
if (unarydown(value, level, prio) == (struct Value *)0)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGEXPR, _("unary operand"));
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
if (value->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
switch (op)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
case T_PLUS:
|
|
|
|
Value_uplus(value, pass == INTERPRET);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case T_MINUS:
|
|
|
|
Value_uneg(value, pass == INTERPRET);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case T_NOT:
|
|
|
|
Value_unot(value, pass == INTERPRET);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
pc = oppc;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return value;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *eval8(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (pc.token->type)
|
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
case T_IDENTIFIER:
|
|
|
|
{
|
|
|
|
struct Pc var;
|
|
|
|
struct Value *l;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
var = pc;
|
|
|
|
if (pass == COMPILE)
|
|
|
|
{
|
|
|
|
if (((pc.token + 1)->type == T_OP ||
|
|
|
|
Auto_find(&stack, pc.token->u.identifier) == 0) &&
|
|
|
|
Global_find(&globals, pc.token->u.identifier,
|
|
|
|
(pc.token + 1)->type == T_OP) == 0)
|
|
|
|
return Value_new_ERROR(value, UNDECLARED);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
assert(pass == DECLARE || pc.token->u.identifier->sym);
|
|
|
|
if (pass != DECLARE &&
|
|
|
|
(pc.token->u.identifier->sym->type == GLOBALVAR ||
|
|
|
|
pc.token->u.identifier->sym->type == GLOBALARRAY ||
|
|
|
|
pc.token->u.identifier->sym->type == LOCALVAR))
|
|
|
|
{
|
|
|
|
if ((l = lvalue(value))->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_clone(value, l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
func(value);
|
|
|
|
if (value->type == V_VOID)
|
|
|
|
{
|
|
|
|
Value_destroy(value);
|
|
|
|
pc = var;
|
|
|
|
return Value_new_ERROR(value, VOIDVALUE);
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case T_INTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
VALUE_NEW_INTEGER(value, pc.token->u.integer);
|
|
|
|
++pc.token;
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
case T_REAL:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
VALUE_NEW_REAL(value, pc.token->u.real);
|
|
|
|
++pc.token;
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
case T_STRING:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_new_STRING(value);
|
|
|
|
String_destroy(&value->u.string);
|
|
|
|
String_clone(&value->u.string, pc.token->u.string);
|
|
|
|
++pc.token;
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
case T_HEXINTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
VALUE_NEW_INTEGER(value, pc.token->u.hexinteger);
|
|
|
|
++pc.token;
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
case T_OCTINTEGER:
|
|
|
|
{
|
|
|
|
VALUE_NEW_INTEGER(value, pc.token->u.octinteger);
|
|
|
|
++pc.token;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case T_OP:
|
|
|
|
{
|
|
|
|
++pc.token;
|
|
|
|
if (eval(value, _("parenthetic"))->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pc.token->type != T_CP)
|
|
|
|
{
|
|
|
|
Value_destroy(value);
|
|
|
|
return Value_new_ERROR(value, MISSINGCP);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++pc.token;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
return (struct Value *)0;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return value;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *eval7(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
return binarydown(value, eval8, 7);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *eval6(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
return unarydown(value, eval7, 6);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *eval5(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
return binarydown(value, eval6, 5);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *eval4(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
return binarydown(value, eval5, 4);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *eval3(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
return binarydown(value, eval4, 3);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *eval2(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
return unarydown(value, eval3, 2);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *eval1(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
return binarydown(value, eval2, 1);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *eval(struct Value *value, const char *desc)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-04 20:35:56 +01:00
|
|
|
/* Avoid function calls for atomic expression */
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
switch (pc.token->type)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-10-27 14:53:12 +01:00
|
|
|
case T_STRING:
|
|
|
|
case T_REAL:
|
|
|
|
case T_INTEGER:
|
|
|
|
case T_HEXINTEGER:
|
|
|
|
case T_OCTINTEGER:
|
2014-11-02 01:06:28 +01:00
|
|
|
case T_IDENTIFIER:
|
|
|
|
if (!TOKEN_ISBINARYOPERATOR((pc.token + 1)->type) &&
|
|
|
|
(pc.token + 1)->type != T_OP)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return eval7(value);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (binarydown(value, eval1, 0) == (struct Value *)0)
|
|
|
|
{
|
|
|
|
if (desc)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGEXPR, desc);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return (struct Value *)0;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
static void new(void)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
Global_destroy(&globals);
|
|
|
|
Global_new(&globals);
|
|
|
|
Auto_destroy(&stack);
|
|
|
|
Auto_new(&stack);
|
|
|
|
Program_destroy(&program);
|
|
|
|
Program_new(&program);
|
|
|
|
FS_closefiles();
|
2014-11-02 01:06:28 +01:00
|
|
|
optionbase = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static void pushLabel(enum LabelType type, struct Pc *patch)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (labelStackPointer == labelStackCapacity)
|
|
|
|
{
|
|
|
|
struct LabelStack *more;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
more =
|
|
|
|
realloc(labelStack,
|
|
|
|
sizeof(struct LabelStack) *
|
|
|
|
(labelStackCapacity ? (labelStackCapacity *= 2) : (32)));
|
|
|
|
labelStack = more;
|
|
|
|
}
|
|
|
|
|
|
|
|
labelStack[labelStackPointer].type = type;
|
|
|
|
labelStack[labelStackPointer].patch = *patch;
|
2014-10-27 14:53:12 +01:00
|
|
|
++labelStackPointer;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Pc *popLabel(enum LabelType type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (labelStackPointer == 0 || labelStack[labelStackPointer - 1].type != type)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return (struct Pc *)0;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return &labelStack[--labelStackPointer].patch;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Pc *findLabel(enum LabelType type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
for (i = labelStackPointer - 1; i >= 0; --i)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
if (labelStack[i].type == type)
|
|
|
|
{
|
|
|
|
return &labelStack[i].patch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
return (struct Pc *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static void labelStackError(struct Value *v)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
assert(labelStackPointer);
|
2014-11-02 01:06:28 +01:00
|
|
|
pc = labelStack[labelStackPointer - 1].patch;
|
|
|
|
switch (labelStack[labelStackPointer - 1].type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_IF:
|
|
|
|
Value_new_ERROR(v, STRAYIF);
|
2014-10-27 14:53:12 +01:00
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_DO:
|
|
|
|
Value_new_ERROR(v, STRAYDO);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_DOcondition:
|
|
|
|
Value_new_ERROR(v, STRAYDOcondition);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_ELSE:
|
|
|
|
Value_new_ERROR(v, STRAYELSE2);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_FOR_BODY:
|
|
|
|
{
|
|
|
|
Value_new_ERROR(v, STRAYFOR);
|
|
|
|
pc = *findLabel(L_FOR);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_WHILE:
|
|
|
|
Value_new_ERROR(v, STRAYWHILE);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_REPEAT:
|
|
|
|
Value_new_ERROR(v, STRAYREPEAT);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_SELECTCASE:
|
|
|
|
Value_new_ERROR(v, STRAYSELECTCASE);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_FUNC:
|
|
|
|
Value_new_ERROR(v, STRAYFUNC);
|
|
|
|
break;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static const char *topLabelDescription(void)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (labelStackPointer == 0)
|
|
|
|
{
|
|
|
|
return _("program");
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
switch (labelStack[labelStackPointer - 1].type)
|
|
|
|
{
|
|
|
|
case L_IF:
|
|
|
|
return _("`if' branch");
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_DO:
|
|
|
|
return _("`do' loop");
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_DOcondition:
|
|
|
|
return _("`do while' or `do until' loop");
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_ELSE:
|
|
|
|
return _("`else' branch");
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_FOR_BODY:
|
|
|
|
return _("`for' loop");
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_WHILE:
|
|
|
|
return _("`while' loop");
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_REPEAT:
|
|
|
|
return _("`repeat' loop");
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_SELECTCASE:
|
|
|
|
return _("`select case' control structure");
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case L_FUNC:
|
|
|
|
return _("function or procedure");
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
/* NOTREACHED */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
return (const char *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *assign(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Pc expr;
|
|
|
|
|
2014-11-04 20:35:56 +01:00
|
|
|
if (strcasecmp(pc.token->u.identifier->name, "mid$") == 0)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
long int n, m;
|
|
|
|
struct Value *l;
|
|
|
|
|
|
|
|
++pc.token;
|
|
|
|
if (pc.token->type != T_OP)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGOP);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++pc.token;
|
|
|
|
if (pc.token->type != T_IDENTIFIER)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGSTRIDENT);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pass == DECLARE)
|
|
|
|
{
|
|
|
|
if (((pc.token + 1)->type == T_OP ||
|
|
|
|
Auto_find(&stack, pc.token->u.identifier) == 0) &&
|
|
|
|
Global_variable(&globals, pc.token->u.identifier,
|
|
|
|
pc.token->u.identifier->defaultType,
|
|
|
|
(pc.token + 1)->type ==
|
|
|
|
T_OP ? GLOBALARRAY : GLOBALVAR, 0) == 0)
|
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, REDECLARATION);
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if ((l = lvalue(value))->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pass == COMPILE && l->type != V_STRING)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, TYPEMISMATCH4);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pc.token->type != T_COMMA)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGCOMMA);
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
++pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
if (eval(value, _("position"))->type == V_ERROR ||
|
|
|
|
Value_retype(value, V_INTEGER)->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
n = value->u.integer;
|
2014-10-27 14:53:12 +01:00
|
|
|
Value_destroy(value);
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pass == INTERPRET && n < 1)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, OUTOFRANGE, "position");
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pc.token->type == T_COMMA)
|
|
|
|
{
|
|
|
|
++pc.token;
|
|
|
|
if (eval(value, _("length"))->type == V_ERROR ||
|
|
|
|
Value_retype(value, V_INTEGER)->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
m = value->u.integer;
|
|
|
|
if (pass == INTERPRET && m < 0)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, OUTOFRANGE, _("length"));
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(value);
|
|
|
|
}
|
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
m = -1;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pc.token->type != T_CP)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGCP);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++pc.token;
|
|
|
|
if (pc.token->type != T_EQ)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGEQ);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++pc.token;
|
|
|
|
if (eval(value, _("rhs"))->type == V_ERROR ||
|
|
|
|
Value_retype(value, V_STRING)->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pass == INTERPRET)
|
|
|
|
{
|
|
|
|
if (m == -1)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
m = value->u.string.length;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
String_set(&l->u.string, n - 1, &value->u.string, m);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Value **l = (struct Value **)0;
|
|
|
|
int i, used = 0, capacity = 0;
|
|
|
|
struct Value retyped_value;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
for (;;)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (used == capacity)
|
|
|
|
{
|
|
|
|
struct Value **more;
|
|
|
|
|
|
|
|
capacity = capacity ? 2 * capacity : 2;
|
|
|
|
more = realloc(l, capacity * sizeof(*l));
|
|
|
|
l = more;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pass == DECLARE)
|
|
|
|
{
|
|
|
|
if (((pc.token + 1)->type == T_OP ||
|
|
|
|
Auto_find(&stack, pc.token->u.identifier) == 0) &&
|
|
|
|
Global_variable(&globals, pc.token->u.identifier,
|
|
|
|
pc.token->u.identifier->defaultType,
|
|
|
|
(pc.token + 1)->type ==
|
|
|
|
T_OP ? GLOBALARRAY : GLOBALVAR, 0) == 0)
|
|
|
|
{
|
|
|
|
if (capacity)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
free(l);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
return Value_new_ERROR(value, REDECLARATION);
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if ((l[used] = lvalue(value))->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++used;
|
|
|
|
if (pc.token->type == T_COMMA)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
++pc.token;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
if (pc.token->type != T_EQ)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGEQ);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
++pc.token;
|
|
|
|
expr = pc;
|
|
|
|
if (eval(value, _("rhs"))->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
for (i = 0; i < used; ++i)
|
|
|
|
{
|
|
|
|
Value_clone(&retyped_value, value);
|
|
|
|
if (pass != DECLARE &&
|
|
|
|
VALUE_RETYPE(&retyped_value, (l[i])->type)->type == V_ERROR)
|
|
|
|
{
|
|
|
|
pc = expr;
|
|
|
|
free(l);
|
|
|
|
Value_destroy(value);
|
|
|
|
*value = retyped_value;
|
|
|
|
return value;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pass == INTERPRET)
|
|
|
|
{
|
|
|
|
Value_destroy(l[i]);
|
|
|
|
*(l[i]) = retyped_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(l);
|
|
|
|
Value_destroy(value);
|
|
|
|
*value = retyped_value; /* for status only */
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return value;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static struct Value *compileProgram(struct Value *v, int clearGlobals)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Pc begin;
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
stack.resumeable = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
if (clearGlobals)
|
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
Global_destroy(&globals);
|
|
|
|
Global_new(&globals);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Global_clearFunctions(&globals);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
if (Program_beginning(&program, &begin))
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Pc savepc;
|
|
|
|
int savepass;
|
|
|
|
|
|
|
|
savepc = pc;
|
|
|
|
savepass = pass;
|
2014-10-27 14:53:12 +01:00
|
|
|
Program_norun(&program);
|
2014-11-02 01:06:28 +01:00
|
|
|
for (pass = DECLARE; pass != INTERPRET; ++pass)
|
|
|
|
{
|
|
|
|
if (pass == DECLARE)
|
|
|
|
{
|
|
|
|
stack.begindata.line = -1;
|
|
|
|
lastdata = &stack.begindata;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
optionbase = 0;
|
|
|
|
stopped = 0;
|
|
|
|
program.runnable = 1;
|
|
|
|
pc = begin;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
statements(v);
|
|
|
|
if (v->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(v);
|
|
|
|
if (!Program_skipEOL(&program, &pc, 0, 0))
|
|
|
|
{
|
|
|
|
Value_new_NIL(v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (v->type != V_ERROR && labelStackPointer > 0)
|
|
|
|
{
|
|
|
|
Value_destroy(v);
|
|
|
|
labelStackError(v);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (v->type == V_ERROR)
|
|
|
|
{
|
|
|
|
labelStackPointer = 0;
|
|
|
|
Program_norun(&program);
|
|
|
|
if (stack.cur)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Auto_funcEnd(&stack); /* Always correct? */
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
pass = savepass;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
pc = begin;
|
|
|
|
if (Program_analyse(&program, &pc, v))
|
|
|
|
{
|
|
|
|
labelStackPointer = 0;
|
|
|
|
Program_norun(&program);
|
|
|
|
if (stack.cur)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Auto_funcEnd(&stack); /* Always correct? */
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
pass = savepass;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
curdata = stack.begindata;
|
|
|
|
pc = savepc;
|
|
|
|
pass = savepass;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return Value_new_NIL(v);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
static void runline(struct Token *line)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Value value;
|
|
|
|
|
|
|
|
FS_flush(STDCHANNEL);
|
2014-11-02 01:06:28 +01:00
|
|
|
for (pass = DECLARE; pass != INTERPRET; ++pass)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
curdata.line = -1;
|
|
|
|
pc.line = -1;
|
|
|
|
pc.token = line;
|
|
|
|
optionbase = 0;
|
|
|
|
stopped = 0;
|
|
|
|
statements(&value);
|
|
|
|
if (value.type != V_ERROR && pc.token->type != T_EOL)
|
|
|
|
{
|
|
|
|
Value_destroy(&value);
|
|
|
|
Value_new_ERROR(&value, SYNTAX);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (value.type != V_ERROR && labelStackPointer > 0)
|
|
|
|
{
|
|
|
|
Value_destroy(&value);
|
|
|
|
labelStackError(&value);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (value.type == V_ERROR)
|
|
|
|
{
|
|
|
|
struct String s;
|
|
|
|
|
|
|
|
Auto_setError(&stack, Program_lineNumber(&program, &pc), &pc, &value);
|
|
|
|
Program_PCtoError(&program, &pc, &value);
|
|
|
|
labelStackPointer = 0;
|
|
|
|
FS_putChars(STDCHANNEL, _("Error: "));
|
|
|
|
String_new(&s);
|
|
|
|
Value_toString(&value, &s, ' ', -1, 0, 0, 0, 0, -1, 0, 0);
|
|
|
|
Value_destroy(&value);
|
|
|
|
FS_putString(STDCHANNEL, &s);
|
|
|
|
String_destroy(&s);
|
|
|
|
return;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (!program.runnable && pass == COMPILE)
|
|
|
|
{
|
|
|
|
Value_destroy(&value);
|
|
|
|
(void)compileProgram(&value, 0);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
pc.line = -1;
|
|
|
|
pc.token = line;
|
|
|
|
optionbase = 0;
|
|
|
|
curdata = stack.begindata;
|
|
|
|
nextdata.line = -1;
|
2014-10-27 14:53:12 +01:00
|
|
|
Value_destroy(&value);
|
2014-11-02 01:06:28 +01:00
|
|
|
pass = INTERPRET;
|
2014-11-03 15:19:14 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
do
|
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
assert(pass == INTERPRET);
|
|
|
|
statements(&value);
|
|
|
|
assert(pass == INTERPRET);
|
|
|
|
if (value.type == V_ERROR)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (strchr(value.u.error.msg, '\n') == (char *)0)
|
|
|
|
{
|
|
|
|
Auto_setError(&stack, Program_lineNumber(&program, &pc), &pc,
|
|
|
|
&value);
|
|
|
|
Program_PCtoError(&program, &pc, &value);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (stack.onerror.line != -1)
|
|
|
|
{
|
|
|
|
stack.resumeable = 1;
|
|
|
|
pc = stack.onerror;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct String s;
|
|
|
|
|
|
|
|
String_new(&s);
|
|
|
|
if (!stopped)
|
|
|
|
{
|
|
|
|
stopped = 0;
|
|
|
|
FS_putChars(STDCHANNEL, _("Error: "));
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Auto_frameToError(&stack, &program, &value);
|
|
|
|
Value_toString(&value, &s, ' ', -1, 0, 0, 0, 0, -1, 0, 0);
|
|
|
|
while (Auto_gosubReturn(&stack, (struct Pc *)0));
|
|
|
|
FS_putString(STDCHANNEL, &s);
|
|
|
|
String_destroy(&s);
|
|
|
|
Value_destroy(&value);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(&value);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
while (pc.token->type != T_EOL ||
|
|
|
|
Program_skipEOL(&program, &pc, STDCHANNEL, 1));
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-11-04 20:35:56 +01:00
|
|
|
static struct Value *evalGeometry(struct Value *value, unsigned int *dim,
|
|
|
|
unsigned int geometry[])
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Pc exprpc = pc;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (eval(value, _("dimension"))->type == V_ERROR ||
|
|
|
|
(pass != DECLARE && Value_retype(value, V_INTEGER)->type == V_ERROR))
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pass == INTERPRET && value->u.integer < optionbase)
|
|
|
|
{
|
|
|
|
Value_destroy(value);
|
|
|
|
pc = exprpc;
|
|
|
|
return Value_new_ERROR(value, OUTOFRANGE, _("dimension"));
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
geometry[0] = value->u.integer - optionbase + 1;
|
2014-10-27 14:53:12 +01:00
|
|
|
Value_destroy(value);
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pc.token->type == T_COMMA)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
++pc.token;
|
|
|
|
exprpc = pc;
|
|
|
|
if (eval(value, _("dimension"))->type == V_ERROR ||
|
|
|
|
(pass != DECLARE && Value_retype(value, V_INTEGER)->type == V_ERROR))
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pass == INTERPRET && value->u.integer < optionbase)
|
|
|
|
{
|
|
|
|
Value_destroy(value);
|
|
|
|
pc = exprpc;
|
|
|
|
return Value_new_ERROR(value, OUTOFRANGE, _("dimension"));
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
geometry[1] = value->u.integer - optionbase + 1;
|
2014-10-27 14:53:12 +01:00
|
|
|
Value_destroy(value);
|
2014-11-02 01:06:28 +01:00
|
|
|
*dim = 2;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
*dim = 1;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (pc.token->type == T_CP)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
++pc.token;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGCP);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
return (struct Value *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-11-04 20:35:56 +01:00
|
|
|
static struct Value *convert(struct Value *value, struct Value *l,
|
|
|
|
struct Token *t)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
switch (l->type)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-10-27 14:53:12 +01:00
|
|
|
case V_INTEGER:
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
char *datainput;
|
|
|
|
char *end;
|
|
|
|
long int v;
|
|
|
|
int overflow;
|
|
|
|
|
|
|
|
if (t->type != T_DATAINPUT)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, BADCONVERSION, _("integer"));
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
datainput = t->u.datainput;
|
|
|
|
v = Value_vali(datainput, &end, &overflow);
|
|
|
|
if (end == datainput || (*end != '\0' && *end != ' ' && *end != '\t'))
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, BADCONVERSION, _("integer"));
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (overflow)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, OUTOFRANGE, _("converted value"));
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(l);
|
|
|
|
VALUE_NEW_INTEGER(l, v);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
case V_REAL:
|
|
|
|
{
|
|
|
|
char *datainput;
|
|
|
|
char *end;
|
|
|
|
double v;
|
|
|
|
int overflow;
|
|
|
|
|
|
|
|
if (t->type != T_DATAINPUT)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, BADCONVERSION, _("real"));
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
datainput = t->u.datainput;
|
|
|
|
v = Value_vald(datainput, &end, &overflow);
|
|
|
|
if (end == datainput || (*end != '\0' && *end != ' ' && *end != '\t'))
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, BADCONVERSION, _("real"));
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (overflow)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, OUTOFRANGE, _("converted value"));
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_destroy(l);
|
|
|
|
VALUE_NEW_REAL(l, v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case V_STRING:
|
|
|
|
{
|
|
|
|
Value_destroy(l);
|
|
|
|
Value_new_STRING(l);
|
|
|
|
if (t->type == T_STRING)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
String_appendString(&l->u.string, t->u.string);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
String_appendChars(&l->u.string, t->u.datainput);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
return (struct Value *)0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct Value *dataread(struct Value *value, struct Value *l)
|
|
|
|
{
|
|
|
|
if (curdata.line == -1)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
return Value_new_ERROR(value, ENDOFDATA);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (curdata.token->type == T_DATA)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
nextdata = curdata.token->u.nextdata;
|
|
|
|
++curdata.token;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (convert(value, l, curdata.token))
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
return value;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
++curdata.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
if (curdata.token->type == T_COMMA)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
++curdata.token;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
curdata = nextdata;
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
return (struct Value *)0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
static struct Value more_statements;
|
|
|
|
#include "statement.c"
|
2014-11-02 01:06:28 +01:00
|
|
|
static struct Value *statements(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
more:
|
2014-10-27 14:53:12 +01:00
|
|
|
if (pc.token->statement)
|
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Value *v;
|
|
|
|
|
|
|
|
if ((v = pc.token->statement(value)))
|
|
|
|
{
|
|
|
|
if (v == &more_statements)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
goto more;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGSTATEMENT);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-03 15:19:14 +01:00
|
|
|
if (pc.token->type == T_COLON && (pc.token + 1)->type == T_ELSE)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
++pc.token;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else if ((pc.token->type == T_COLON && (pc.token + 1)->type != T_ELSE) ||
|
|
|
|
pc.token->type == T_QUOTE)
|
|
|
|
{
|
|
|
|
++pc.token;
|
|
|
|
goto more;
|
|
|
|
}
|
|
|
|
else if ((pass == DECLARE || pass == COMPILE) && pc.token->type != T_EOL &&
|
|
|
|
pc.token->type != T_ELSE)
|
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGCOLON);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
return Value_new_NIL(value);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void bas_init(int backslash_colon, int restricted, int uppercase, int lpfd)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
stack.begindata.line = -1;
|
|
|
|
Token_init(backslash_colon, uppercase);
|
2014-10-27 14:53:12 +01:00
|
|
|
Global_new(&globals);
|
|
|
|
Auto_new(&stack);
|
|
|
|
Program_new(&program);
|
2014-11-02 01:06:28 +01:00
|
|
|
FS_opendev(STDCHANNEL, 0, 1);
|
|
|
|
FS_opendev(LPCHANNEL, -1, lpfd);
|
|
|
|
run_restricted = restricted;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
void bas_runFile(const char *runFile)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Value value;
|
|
|
|
int dev;
|
|
|
|
|
|
|
|
new();
|
2014-11-02 01:06:28 +01:00
|
|
|
if ((dev = FS_openin(runFile)) == -1)
|
|
|
|
{
|
|
|
|
const char *errmsg = FS_errmsg;
|
|
|
|
|
|
|
|
FS_putChars(0, _("bas: Executing `"));
|
|
|
|
FS_putChars(0, runFile);
|
|
|
|
FS_putChars(0, _("' failed ("));
|
|
|
|
FS_putChars(0, errmsg);
|
|
|
|
FS_putChars(0, _(").\n"));
|
|
|
|
}
|
|
|
|
else if (Program_merge(&program, dev, &value))
|
|
|
|
{
|
|
|
|
struct String s;
|
|
|
|
|
|
|
|
FS_putChars(0, "bas: ");
|
|
|
|
String_new(&s);
|
|
|
|
Value_toString(&value, &s, ' ', -1, 0, 0, 0, 0, -1, 0, 0);
|
|
|
|
FS_putString(0, &s);
|
|
|
|
String_destroy(&s);
|
|
|
|
FS_putChar(0, '\n');
|
|
|
|
Value_destroy(&value);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
else
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
struct Token line[2];
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Program_setname(&program, runFile);
|
|
|
|
line[0].type = T_RUN;
|
|
|
|
line[0].statement = stmt_RUN;
|
|
|
|
line[1].type = T_EOL;
|
|
|
|
line[1].statement = stmt_COLON_EOL;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
FS_close(dev);
|
|
|
|
runline(line);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
void bas_runLine(const char *runLine)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
struct Token *line;
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
line = Token_newCode(runLine);
|
|
|
|
runline(line + 1);
|
2014-10-27 14:53:12 +01:00
|
|
|
Token_destroy(line);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
void bas_interpreter(void)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
if (FS_istty(STDCHANNEL))
|
|
|
|
{
|
2014-11-03 16:11:25 +01:00
|
|
|
FS_putChars(STDCHANNEL, "bas " CONFIG_INTERPRETER_BAS_VERSION "\n");
|
2014-11-02 01:06:28 +01:00
|
|
|
FS_putChars(STDCHANNEL, "Copyright 1999-2014 Michael Haardt.\n");
|
2014-11-04 20:35:56 +01:00
|
|
|
FS_putChars(STDCHANNEL,
|
|
|
|
"This is free software with ABSOLUTELY NO WARRANTY.\n");
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
new();
|
|
|
|
while (1)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Token *line;
|
|
|
|
struct String s;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
stopped = 0;
|
|
|
|
FS_nextline(STDCHANNEL);
|
|
|
|
if (FS_istty(STDCHANNEL))
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
FS_putChars(STDCHANNEL, "> ");
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
FS_flush(STDCHANNEL);
|
|
|
|
String_new(&s);
|
|
|
|
if (FS_appendToString(STDCHANNEL, &s, 1) == -1)
|
|
|
|
{
|
2014-11-03 15:19:14 +01:00
|
|
|
FS_putChars(STDCHANNEL, FS_errmsg);
|
|
|
|
FS_flush(STDCHANNEL);
|
|
|
|
String_destroy(&s);
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (s.length == 0)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
String_destroy(&s);
|
|
|
|
break;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
line = Token_newCode(s.character);
|
|
|
|
String_destroy(&s);
|
|
|
|
if (line->type != T_EOL)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
if (line->type == T_INTEGER && line->u.integer > 0)
|
|
|
|
{
|
|
|
|
if (program.numbered)
|
|
|
|
{
|
|
|
|
if ((line + 1)->type == T_EOL)
|
|
|
|
{
|
|
|
|
struct Pc where;
|
|
|
|
|
|
|
|
if (Program_goLine(&program, line->u.integer, &where) ==
|
|
|
|
(struct Pc *)0)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
FS_putChars(STDCHANNEL, (NOSUCHLINE));
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Program_delete(&program, &where, &where);
|
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Token_destroy(line);
|
|
|
|
}
|
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Program_store(&program, line, line->u.integer);
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FS_putChars(STDCHANNEL,
|
|
|
|
_("Use `renum' to number program first"));
|
|
|
|
Token_destroy(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (line->type == T_UNNUMBERED)
|
|
|
|
{
|
|
|
|
runline(line + 1);
|
|
|
|
Token_destroy(line);
|
|
|
|
if (FS_istty(STDCHANNEL) && bas_end > 0)
|
|
|
|
{
|
|
|
|
FS_putChars(STDCHANNEL, _("END program\n"));
|
|
|
|
bas_end = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FS_putChars(STDCHANNEL, _("Invalid line\n"));
|
|
|
|
Token_destroy(line);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
Token_destroy(line);
|
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
void bas_exit(void)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
Auto_destroy(&stack);
|
|
|
|
Global_destroy(&globals);
|
|
|
|
Program_destroy(&program);
|
2014-11-02 01:06:28 +01:00
|
|
|
if (labelStack)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
free(labelStack);
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
FS_closefiles();
|
|
|
|
FS_close(LPCHANNEL);
|
|
|
|
FS_close(STDCHANNEL);
|
|
|
|
}
|