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.
|
|
|
|
*
|
2020-04-13 05:56:53 +02:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
2014-11-04 20:35:56 +01:00
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* 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>
|
2014-11-08 18:23:13 +01:00
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2014-10-27 14:53:12 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <math.h>
|
2017-02-16 22:58:15 +01:00
|
|
|
#include <strings.h>
|
2014-10-27 14:53:12 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2014-11-11 19:34:00 +01:00
|
|
|
#include "bas_auto.h"
|
2014-10-27 14:53:12 +01:00
|
|
|
#include "bas.h"
|
2014-11-11 19:34:00 +01:00
|
|
|
#include "bas_error.h"
|
|
|
|
#include "bas_fs.h"
|
|
|
|
#include "bas_global.h"
|
|
|
|
#include "bas_program.h"
|
|
|
|
#include "bas_value.h"
|
|
|
|
#include "bas_var.h"
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
#define DIRECTMODE (g_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-11-08 16:08:20 +01:00
|
|
|
enum labeltype_e
|
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
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
struct labelstack_s
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
enum labeltype_e type;
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Pc patch;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
static unsigned int g_labelstack_index;
|
|
|
|
static unsigned int g_labelstack_capacity;
|
|
|
|
static struct labelstack_s *g_labelstack;
|
|
|
|
static struct Pc *g_lastdata;
|
|
|
|
static struct Pc g_curdata;
|
|
|
|
static struct Pc g_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
|
2014-11-08 16:08:20 +01:00
|
|
|
} g_pass;
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
static int g_stopped;
|
2014-11-08 18:47:35 +01:00
|
|
|
static int g_optionbase;
|
2014-11-08 16:08:20 +01:00
|
|
|
static struct Pc g_pc;
|
|
|
|
static struct Auto g_stack;
|
|
|
|
static struct Program g_program;
|
|
|
|
static struct Global g_globals;
|
|
|
|
static int g_run_restricted;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-04 20:35:56 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-11-08 18:23:13 +01:00
|
|
|
int g_bas_argc;
|
|
|
|
char *g_bas_argv0;
|
|
|
|
char **g_bas_argv;
|
|
|
|
bool g_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;
|
2016-06-11 22:49:55 +02:00
|
|
|
int errcode;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
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)
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
ssize_t off;
|
|
|
|
ssize_t 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)
|
|
|
|
{
|
2016-06-11 22:49:55 +02:00
|
|
|
errcode = errno;
|
2014-11-02 01:06:28 +01:00
|
|
|
close(fd);
|
2016-06-11 22:49:55 +02:00
|
|
|
errno = errcode;
|
2014-11-02 01:06:28 +01:00
|
|
|
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
|
|
|
{
|
2016-06-11 22:49:55 +02:00
|
|
|
errcode = errno;
|
2014-11-02 01:06:28 +01:00
|
|
|
close(fd);
|
2016-06-11 22:49:55 +02:00
|
|
|
errno = errcode;
|
2014-11-02 01:06:28 +01:00
|
|
|
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-08 16:08:20 +01:00
|
|
|
struct Pc lvpc = g_pc;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
sym = g_pc.token->u.identifier->sym;
|
2020-04-13 05:56:53 +02:00
|
|
|
assert(g_pass == DECLARE || sym->type == GLOBALVAR
|
|
|
|
|| sym->type == GLOBALARRAY || sym->type == LOCALVAR);
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if ((g_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;
|
2020-04-13 05:56:53 +02:00
|
|
|
unsigned int dim;
|
|
|
|
unsigned int capacity;
|
2014-11-02 01:06:28 +01:00
|
|
|
int *idx;
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc.token += 2;
|
2014-11-02 01:06:28 +01:00
|
|
|
dim = 0;
|
|
|
|
capacity = 0;
|
|
|
|
idx = (int *)0;
|
|
|
|
while (1)
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (dim == capacity && g_pass == INTERPRET) /* enlarge idx */
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
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-08 16:08:20 +01:00
|
|
|
idxpc = g_pc;
|
2014-11-02 01:06:28 +01:00
|
|
|
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-08 16:08:20 +01:00
|
|
|
g_pc = idxpc;
|
2014-11-02 01:06:28 +01:00
|
|
|
return value;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass == INTERPRET)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
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);
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type == T_COMMA)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
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-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type != T_CP)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
assert(g_pass != INTERPRET);
|
2014-11-02 01:06:28 +01:00
|
|
|
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
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
switch (g_pass)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
case INTERPRET:
|
|
|
|
{
|
|
|
|
if ((value =
|
|
|
|
Var_value(&(sym->u.var), dim, idx, value))->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = lvpc;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2014-11-08 16:08:20 +01:00
|
|
|
var.type : Auto_varType(&g_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-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
|
|
|
switch (g_pass)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
case INTERPRET:
|
2020-04-13 05:56:53 +02:00
|
|
|
return VAR_SCALAR_VALUE(sym->type == GLOBALVAR ? &(sym->u.var) :
|
|
|
|
Auto_local(&g_stack, sym->u.local.offset));
|
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
|
|
|
GLOBALVAR ? sym->u.
|
2014-11-08 16:08:20 +01:00
|
|
|
var.type : Auto_varType(&g_stack, sym));
|
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
|
|
|
}
|
|
|
|
}
|
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-08 16:08:20 +01:00
|
|
|
struct Pc funcpc = g_pc;
|
2020-04-13 05:56:53 +02:00
|
|
|
long int firstslot = -99;
|
2014-11-02 01:06:28 +01:00
|
|
|
int args = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
struct Symbol *sym;
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
assert(g_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-08 16:08:20 +01:00
|
|
|
if (DIRECTMODE && !g_program.runnable && g_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-08 16:08:20 +01:00
|
|
|
ident = g_pc.token->u.identifier;
|
|
|
|
assert(g_pass == DECLARE || ident->sym->type == BUILTINFUNCTION ||
|
2014-11-02 01:06:28 +01:00
|
|
|
ident->sym->type == USERFUNCTION);
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
|
|
|
if (g_pass != DECLARE)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
firstslot = g_stack.stackPointer;
|
2014-11-02 01:06:28 +01:00
|
|
|
if (ident->sym->type == USERFUNCTION &&
|
|
|
|
ident->sym->u.sub.retType != V_VOID)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
struct Var *v = Auto_pushArg(&g_stack);
|
2020-04-13 05:56:53 +02:00
|
|
|
Var_new(v, ident->sym->u.sub.retType, 0, NULL, 0);
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type == T_OP) /* push arguments to stack */
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
|
|
|
if (g_pc.token->type != T_CP)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-04 20:35:56 +01:00
|
|
|
while (1)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass == DECLARE)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
if (eval(value, _("actual parameter"))->type == V_ERROR)
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value_destroy(value);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
struct Var *v = Auto_pushArg(&g_stack);
|
2014-11-04 20:35:56 +01:00
|
|
|
|
|
|
|
Var_new_scalar(v);
|
|
|
|
if (eval(v->value, (const char *)0)->type == V_ERROR)
|
|
|
|
{
|
|
|
|
Value_clone(value, v->value);
|
2014-11-08 16:08:20 +01:00
|
|
|
while (g_stack.stackPointer > firstslot)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
long int stackPointer;
|
|
|
|
|
|
|
|
stackPointer = --g_stack.stackPointer;
|
|
|
|
Var_destroy(&g_stack.slot[stackPointer].var);
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
v->type = v->value->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
++args;
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type == T_COMMA)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type != T_CP)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass != DECLARE)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
while (g_stack.stackPointer > firstslot)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
long int stackPointer;
|
|
|
|
|
|
|
|
stackPointer = --g_stack.stackPointer;
|
|
|
|
Var_destroy(&g_stack.slot[stackPointer].var);
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Value_new_ERROR(value, MISSINGCP);
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_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-08 16:08:20 +01:00
|
|
|
if (g_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-08 16:08:20 +01:00
|
|
|
struct Var *v = Auto_pushArg(&g_stack);
|
2014-11-02 01:06:28 +01:00
|
|
|
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-08 16:08:20 +01:00
|
|
|
Auto_pushFuncRet(&g_stack, firstslot, &g_pc);
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
sym = ident->sym;
|
2014-11-08 16:08:20 +01:00
|
|
|
overloaded = (g_pass == COMPILE && sym->type == BUILTINFUNCTION &&
|
2014-11-02 01:06:28 +01:00
|
|
|
sym->u.sub.u.bltin.next);
|
|
|
|
do
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
nomore = (g_pass == COMPILE &&
|
2020-04-13 05:56:53 +02:00
|
|
|
!(sym->type == BUILTINFUNCTION &&
|
|
|
|
sym->u.sub.u.bltin.next));
|
2014-11-02 01:06:28 +01:00
|
|
|
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 =
|
2014-11-08 16:08:20 +01:00
|
|
|
Var_value(Auto_local(&g_stack, i), 0, (int *)0, value);
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
Auto_funcReturn(&g_stack, (struct Pc *)0);
|
|
|
|
g_pc = funcpc;
|
2014-11-02 01:06:28 +01:00
|
|
|
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)
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass == INTERPRET)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (sym->u.sub.u.bltin.call(value, &g_stack)->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = funcpc;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
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)
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass == INTERPRET)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
int r = 1;
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = sym->u.sub.u.def.scope.start;
|
|
|
|
if (g_pc.token->type == T_COLON)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
Program_skipEOL(&g_program, &g_pc, STDCHANNEL, 1);
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (statements(value)->type == V_ERROR)
|
|
|
|
{
|
|
|
|
if (strchr(value->u.error.msg, '\n') == (char *)0)
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
Auto_setError(&g_stack,
|
2020-04-13 05:56:53 +02:00
|
|
|
Program_lineNumber(&g_program, &g_pc),
|
|
|
|
&g_pc, value);
|
2014-11-08 16:08:20 +01:00
|
|
|
Program_PCtoError(&g_program, &g_pc, value);
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_stack.onerror.line != -1)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_stack.resumeable = 1;
|
|
|
|
g_pc = g_stack.onerror;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
Auto_frameToError(&g_stack, &g_program, value);
|
2014-11-02 01:06:28 +01:00
|
|
|
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);
|
|
|
|
}
|
2020-04-13 05:56:53 +02:00
|
|
|
while ((r = Program_skipEOL(&g_program, &g_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-08 16:08:20 +01:00
|
|
|
Auto_funcReturn(&g_stack, g_pass == INTERPRET &&
|
|
|
|
value->type != V_ERROR ? &g_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
|
2020-04-13 05:56:53 +02:00
|
|
|
* E -> E . op E op* shift 1 *=if stack[-2] contains op of unary
|
|
|
|
* lower priority
|
2014-11-04 20:35:56 +01:00
|
|
|
*
|
|
|
|
* i7:
|
|
|
|
* E -> ( E . ) ) shift 9
|
|
|
|
* E -> E . op E op shift 1
|
|
|
|
*
|
|
|
|
* i8:
|
|
|
|
* E -> E op E . reduce 2
|
2020-04-13 05:56:53 +02:00
|
|
|
* E -> E . op E op* shift 1 *=if stack[-2] contains op of lower
|
|
|
|
* priority or if it is of equal
|
|
|
|
* priority and right associative
|
2014-11-04 20:35:56 +01:00
|
|
|
* 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 */
|
|
|
|
|
2020-04-13 05:56:53 +02:00
|
|
|
static const int gotoState[10] =
|
|
|
|
{
|
|
|
|
5, 8, 6, 7, -1, -1, -1, -1, -1, -1
|
|
|
|
};
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
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;
|
|
|
|
};
|
2020-04-13 05:56:53 +02:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
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-08 16:08:20 +01:00
|
|
|
ip = g_pc.token->type;
|
2014-11-02 01:06:28 +01:00
|
|
|
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)
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
/* 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];
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass == COMPILE)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (((g_pc.token + 1)->type == T_OP ||
|
2020-04-13 05:56:53 +02:00
|
|
|
Auto_find(&g_stack, g_pc.token->u.identifier) == 0)
|
|
|
|
&& Global_find(&g_globals, g_pc.token->u.identifier,
|
|
|
|
(g_pc.token + 1)->type == T_OP) == 0)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
Value_new_ERROR(value, UNDECLARED);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass != DECLARE &&
|
|
|
|
(g_pc.token->u.identifier->sym->type == GLOBALVAR ||
|
|
|
|
g_pc.token->u.identifier->sym->type == GLOBALARRAY ||
|
|
|
|
g_pc.token->u.identifier->sym->type == LOCALVAR))
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
struct Value *l;
|
|
|
|
|
|
|
|
if ((l = lvalue(value))->type == V_ERROR)
|
|
|
|
goto error;
|
|
|
|
Value_clone(&sp->u.value, l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
struct Pc var = g_pc;
|
2014-11-02 01:06:28 +01:00
|
|
|
|
|
|
|
func(&sp->u.value);
|
|
|
|
if (sp->u.value.type == V_VOID)
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = var;
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_new_ERROR(value, VOIDVALUE);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ip == T_INTEGER)
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
/* 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];
|
2014-11-08 16:08:20 +01:00
|
|
|
VALUE_NEW_INTEGER(&sp->u.value, g_pc.token->u.integer);
|
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else if (ip == T_REAL)
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
/* 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];
|
2014-11-08 16:08:20 +01:00
|
|
|
VALUE_NEW_REAL(&sp->u.value, g_pc.token->u.real);
|
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
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;
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else if (ip == T_HEXINTEGER)
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
/* 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];
|
2014-11-08 16:08:20 +01:00
|
|
|
VALUE_NEW_INTEGER(&sp->u.value, g_pc.token->u.hexinteger);
|
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else if (ip == T_OCTINTEGER)
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
/* 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];
|
2014-11-08 16:08:20 +01:00
|
|
|
VALUE_NEW_INTEGER(&sp->u.value, g_pc.token->u.octinteger);
|
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
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;
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else if (ip == T_STRING)
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
/* 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);
|
2014-11-08 16:08:20 +01:00
|
|
|
String_clone(&sp->u.value.u.string, g_pc.token->u.string);
|
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
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;
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
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
|
|
|
|
2020-04-13 05:56:53 +02:00
|
|
|
/* printf("state %d: shift 1 (not reducing E -> op E)\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;
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
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:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_uneg(&sp->u.value, g_pass == INTERPRET);
|
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
|
|
|
case T_NOT:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_unot(&sp->u.value, g_pass == INTERPRET);
|
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
|
|
|
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;
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else if (ip == T_CP)
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
/* 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;
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
2020-04-13 05:56:53 +02:00
|
|
|
if (TOKEN_ISBINARYOPERATOR(ip) &&
|
|
|
|
(((p1 = TOKEN_BINARYPRIORITY((sp - 1)->u.token)) <
|
|
|
|
(p2 = TOKEN_BINARYPRIORITY(ip))) ||
|
2014-11-02 01:06:28 +01:00
|
|
|
(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;
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* printf("state %d: reduce E -> E op E\n",sp->state); */
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2020-04-13 05:56:53 +02:00
|
|
|
if (Value_commonType[(sp - 2)->u.value.type]
|
|
|
|
[sp->u.value.type] == V_ERROR)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
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-08 16:08:20 +01:00
|
|
|
g_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-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_EQ:
|
|
|
|
Value_eq(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_GE:
|
|
|
|
Value_ge(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_GT:
|
|
|
|
Value_gt(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_NE:
|
|
|
|
Value_ne(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_PLUS:
|
|
|
|
Value_add(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
case T_MINUS:
|
|
|
|
Value_sub(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MULT:
|
|
|
|
Value_mult(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_DIV:
|
|
|
|
Value_div(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_IDIV:
|
|
|
|
Value_idiv(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MOD:
|
|
|
|
Value_mod(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_POW:
|
|
|
|
Value_pow(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_AND:
|
|
|
|
Value_and(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_OR:
|
|
|
|
Value_or(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_XOR:
|
|
|
|
Value_xor(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_EQV:
|
|
|
|
Value_eqv(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_IMP:
|
|
|
|
Value_imp(&(sp - 2)->u.value, &sp->u.value,
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
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
|
|
|
}
|
2020-04-13 05:56:53 +02: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;
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
op = g_pc.token->type;
|
2014-11-02 01:06:28 +01:00
|
|
|
if (!TOKEN_ISBINARYOPERATOR(op) || TOKEN_BINARYPRIORITY(op) != prio)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
oppc = g_pc;
|
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
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:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_lt(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LE:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_le(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_EQ:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_eq(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_GE:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_ge(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_GT:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_gt(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_NE:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_ne(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_PLUS:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_add(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MINUS:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_sub(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MULT:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_mult(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_DIV:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_div(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_IDIV:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_idiv(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MOD:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_mod(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_POW:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_pow(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_AND:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_and(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_OR:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_or(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_XOR:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_xor(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_EQV:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_eqv(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_IMP:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_imp(value, &x, g_pass == INTERPRET);
|
2014-11-04 20:35:56 +01:00
|
|
|
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
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = oppc;
|
2014-11-04 20:35:56 +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
|
|
|
|
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-08 16:08:20 +01:00
|
|
|
op = g_pc.token->type;
|
2014-11-02 01:06:28 +01:00
|
|
|
if (!TOKEN_ISUNARYOPERATOR(op) || TOKEN_UNARYPRIORITY(op) != prio)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return level(value);
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
oppc = g_pc;
|
|
|
|
++g_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:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_uplus(value, g_pass == INTERPRET);
|
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
|
|
|
case T_MINUS:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_uneg(value, g_pass == INTERPRET);
|
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
|
|
|
case T_NOT:
|
2014-11-08 16:08:20 +01:00
|
|
|
Value_unot(value, g_pass == INTERPRET);
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = oppc;
|
2014-11-04 20:35:56 +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 *eval8(struct Value *value)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
switch (g_pc.token->type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
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-08 16:08:20 +01:00
|
|
|
var = g_pc;
|
|
|
|
if (g_pass == COMPILE)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (((g_pc.token + 1)->type == T_OP ||
|
|
|
|
Auto_find(&g_stack, g_pc.token->u.identifier) == 0) &&
|
|
|
|
Global_find(&g_globals, g_pc.token->u.identifier,
|
|
|
|
(g_pc.token + 1)->type == T_OP) == 0)
|
2014-11-02 01:06:28 +01:00
|
|
|
return Value_new_ERROR(value, UNDECLARED);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
assert(g_pass == DECLARE || g_pc.token->u.identifier->sym);
|
|
|
|
if (g_pass != DECLARE &&
|
|
|
|
(g_pc.token->u.identifier->sym->type == GLOBALVAR ||
|
|
|
|
g_pc.token->u.identifier->sym->type == GLOBALARRAY ||
|
|
|
|
g_pc.token->u.identifier->sym->type == LOCALVAR))
|
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
|
|
|
Value_clone(value, l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
func(value);
|
|
|
|
if (value->type == V_VOID)
|
|
|
|
{
|
|
|
|
Value_destroy(value);
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = var;
|
2014-11-02 01:06:28 +01:00
|
|
|
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-08 16:08:20 +01:00
|
|
|
VALUE_NEW_INTEGER(value, g_pc.token->u.integer);
|
|
|
|
++g_pc.token;
|
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 T_REAL:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
VALUE_NEW_REAL(value, g_pc.token->u.real);
|
|
|
|
++g_pc.token;
|
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 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);
|
2014-11-08 16:08:20 +01:00
|
|
|
String_clone(&value->u.string, g_pc.token->u.string);
|
|
|
|
++g_pc.token;
|
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 T_HEXINTEGER:
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
VALUE_NEW_INTEGER(value, g_pc.token->u.hexinteger);
|
|
|
|
++g_pc.token;
|
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 T_OCTINTEGER:
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
VALUE_NEW_INTEGER(value, g_pc.token->u.octinteger);
|
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case T_OP:
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
if (eval(value, _("parenthetic"))->type == V_ERROR)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type != T_CP)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
Value_destroy(value);
|
|
|
|
return Value_new_ERROR(value, MISSINGCP);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
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-11-08 16:08:20 +01:00
|
|
|
switch (g_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:
|
2014-11-08 16:08:20 +01:00
|
|
|
if (!TOKEN_ISBINARYOPERATOR((g_pc.token + 1)->type) &&
|
|
|
|
(g_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
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
Global_destroy(&g_globals);
|
|
|
|
Global_new(&g_globals);
|
|
|
|
Auto_destroy(&g_stack);
|
|
|
|
Auto_new(&g_stack);
|
|
|
|
Program_destroy(&g_program);
|
|
|
|
Program_new(&g_program);
|
2014-10-27 14:53:12 +01:00
|
|
|
FS_closefiles();
|
2014-11-08 18:47:35 +01:00
|
|
|
g_optionbase = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
static void pushLabel(enum labeltype_e type, struct Pc *patch)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_labelstack_index == g_labelstack_capacity)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
struct labelstack_s *more;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
more =
|
2014-11-08 16:08:20 +01:00
|
|
|
realloc(g_labelstack,
|
|
|
|
sizeof(struct labelstack_s) *
|
2020-04-13 05:56:53 +02:00
|
|
|
(g_labelstack_capacity ? g_labelstack_capacity *= 2 : 32));
|
2014-11-08 16:08:20 +01:00
|
|
|
g_labelstack = more;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
g_labelstack[g_labelstack_index].type = type;
|
|
|
|
g_labelstack[g_labelstack_index].patch = *patch;
|
|
|
|
++g_labelstack_index;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
static struct Pc *popLabel(enum labeltype_e type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
if (g_labelstack_index == 0 ||
|
|
|
|
g_labelstack[g_labelstack_index - 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
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
return &g_labelstack[--g_labelstack_index].patch;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
static struct Pc *findLabel(enum labeltype_e type)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
for (i = g_labelstack_index - 1; i >= 0; --i)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_labelstack[i].type == type)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
return &g_labelstack[i].patch;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
assert(g_labelstack_index);
|
|
|
|
g_pc = g_labelstack[g_labelstack_index - 1].patch;
|
|
|
|
switch (g_labelstack[g_labelstack_index - 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);
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = *findLabel(L_FOR);
|
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
|
|
|
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-08 16:08:20 +01:00
|
|
|
if (g_labelstack_index == 0)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
return _("program");
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
switch (g_labelstack[g_labelstack_index - 1].type)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
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-08 16:08:20 +01:00
|
|
|
if (strcasecmp(g_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;
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
|
|
|
if (g_pc.token->type != T_OP)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGOP);
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
|
|
|
if (g_pc.token->type != T_IDENTIFIER)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGSTRIDENT);
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass == DECLARE)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (((g_pc.token + 1)->type == T_OP ||
|
|
|
|
Auto_find(&g_stack, g_pc.token->u.identifier) == 0) &&
|
|
|
|
Global_variable(&g_globals, g_pc.token->u.identifier,
|
|
|
|
g_pc.token->u.identifier->defaultType,
|
|
|
|
(g_pc.token + 1)->type ==
|
2014-11-02 01:06:28 +01:00
|
|
|
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-08 16:08:20 +01:00
|
|
|
if (g_pass == COMPILE && l->type != V_STRING)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, TYPEMISMATCH4);
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type != T_COMMA)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGCOMMA);
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_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-08 16:08:20 +01:00
|
|
|
if (g_pass == INTERPRET && n < 1)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, OUTOFRANGE, "position");
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type == T_COMMA)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
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;
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_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-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type != T_CP)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGCP);
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
|
|
|
if (g_pc.token->type != T_EQ)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGEQ);
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
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-08 16:08:20 +01:00
|
|
|
if (g_pass == INTERPRET)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2020-04-13 05:56:53 +02: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;
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass == DECLARE)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (((g_pc.token + 1)->type == T_OP ||
|
|
|
|
Auto_find(&g_stack, g_pc.token->u.identifier) == 0) &&
|
|
|
|
Global_variable(&g_globals, g_pc.token->u.identifier,
|
|
|
|
g_pc.token->u.identifier->defaultType,
|
|
|
|
(g_pc.token + 1)->type ==
|
2014-11-02 01:06:28 +01:00
|
|
|
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;
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type == T_COMMA)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
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
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type != T_EQ)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return Value_new_ERROR(value, MISSINGEQ);
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
|
|
|
expr = g_pc;
|
2014-11-02 01:06:28 +01:00
|
|
|
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);
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass != DECLARE &&
|
2014-11-02 01:06:28 +01:00
|
|
|
VALUE_RETYPE(&retyped_value, (l[i])->type)->type == V_ERROR)
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = expr;
|
2014-11-02 01:06:28 +01:00
|
|
|
free(l);
|
|
|
|
Value_destroy(value);
|
|
|
|
*value = retyped_value;
|
|
|
|
return value;
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass == INTERPRET)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
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-08 16:08:20 +01:00
|
|
|
g_stack.resumeable = 0;
|
2014-10-27 14:53:12 +01:00
|
|
|
if (clearGlobals)
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
Global_destroy(&g_globals);
|
|
|
|
Global_new(&g_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
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
Global_clearFunctions(&g_globals);
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (Program_beginning(&g_program, &begin))
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Pc savepc;
|
|
|
|
int savepass;
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
savepc = g_pc;
|
|
|
|
savepass = g_pass;
|
|
|
|
Program_norun(&g_program);
|
|
|
|
for (g_pass = DECLARE; g_pass != INTERPRET; ++g_pass)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pass == DECLARE)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_stack.begindata.line = -1;
|
|
|
|
g_lastdata = &g_stack.begindata;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 18:47:35 +01:00
|
|
|
g_optionbase = 0;
|
2014-11-08 16:08:20 +01:00
|
|
|
g_stopped = 0;
|
|
|
|
g_program.runnable = 1;
|
|
|
|
g_pc = begin;
|
2014-11-02 01:06:28 +01:00
|
|
|
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);
|
2014-11-08 16:08:20 +01:00
|
|
|
if (!Program_skipEOL(&g_program, &g_pc, 0, 0))
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
Value_new_NIL(v);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (v->type != V_ERROR && g_labelstack_index > 0)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_labelstack_index = 0;
|
|
|
|
Program_norun(&g_program);
|
|
|
|
if (g_stack.cur)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
Auto_funcEnd(&g_stack); /* Always correct? */
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass = savepass;
|
2014-11-02 01:06:28 +01:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = begin;
|
|
|
|
if (Program_analyse(&g_program, &g_pc, v))
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_labelstack_index = 0;
|
|
|
|
Program_norun(&g_program);
|
|
|
|
if (g_stack.cur)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
Auto_funcEnd(&g_stack); /* Always correct? */
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass = savepass;
|
2014-11-02 01:06:28 +01:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
g_curdata = g_stack.begindata;
|
|
|
|
g_pc = savepc;
|
|
|
|
g_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-08 16:08:20 +01:00
|
|
|
for (g_pass = DECLARE; g_pass != INTERPRET; ++g_pass)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_curdata.line = -1;
|
|
|
|
g_pc.line = -1;
|
|
|
|
g_pc.token = line;
|
2014-11-08 18:47:35 +01:00
|
|
|
g_optionbase = 0;
|
2014-11-08 16:08:20 +01:00
|
|
|
g_stopped = 0;
|
2014-11-02 01:06:28 +01:00
|
|
|
statements(&value);
|
2014-11-08 16:08:20 +01:00
|
|
|
if (value.type != V_ERROR && g_pc.token->type != T_EOL)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
Value_destroy(&value);
|
|
|
|
Value_new_ERROR(&value, SYNTAX);
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (value.type != V_ERROR && g_labelstack_index > 0)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2020-04-13 05:56:53 +02:00
|
|
|
Auto_setError(&g_stack, Program_lineNumber(&g_program, &g_pc),
|
|
|
|
&g_pc, &value);
|
2014-11-08 16:08:20 +01:00
|
|
|
Program_PCtoError(&g_program, &g_pc, &value);
|
|
|
|
g_labelstack_index = 0;
|
2014-11-02 01:06:28 +01:00
|
|
|
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-08 16:08:20 +01:00
|
|
|
if (!g_program.runnable && g_pass == COMPILE)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
Value_destroy(&value);
|
2020-01-02 13:09:50 +01:00
|
|
|
compileProgram(&value, 0);
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc.line = -1;
|
|
|
|
g_pc.token = line;
|
2014-11-08 18:47:35 +01:00
|
|
|
g_optionbase = 0;
|
2014-11-08 16:08:20 +01:00
|
|
|
g_curdata = g_stack.begindata;
|
|
|
|
g_nextdata.line = -1;
|
2014-10-27 14:53:12 +01:00
|
|
|
Value_destroy(&value);
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pass = INTERPRET;
|
2014-11-03 15:19:14 +01:00
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
do
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
assert(g_pass == INTERPRET);
|
2014-11-02 01:06:28 +01:00
|
|
|
statements(&value);
|
2014-11-08 16:08:20 +01:00
|
|
|
assert(g_pass == INTERPRET);
|
2014-11-02 01:06:28 +01:00
|
|
|
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)
|
|
|
|
{
|
2020-04-13 05:56:53 +02:00
|
|
|
Auto_setError(&g_stack, Program_lineNumber(&g_program, &g_pc),
|
|
|
|
&g_pc, &value);
|
2014-11-08 16:08:20 +01:00
|
|
|
Program_PCtoError(&g_program, &g_pc, &value);
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_stack.onerror.line != -1)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_stack.resumeable = 1;
|
|
|
|
g_pc = g_stack.onerror;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct String s;
|
|
|
|
|
|
|
|
String_new(&s);
|
2014-11-08 16:08:20 +01:00
|
|
|
if (!g_stopped)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_stopped = 0;
|
2014-11-02 01:06:28 +01:00
|
|
|
FS_putChars(STDCHANNEL, _("Error: "));
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
Auto_frameToError(&g_stack, &g_program, &value);
|
2014-11-02 01:06:28 +01:00
|
|
|
Value_toString(&value, &s, ' ', -1, 0, 0, 0, 0, -1, 0, 0);
|
2014-11-08 16:08:20 +01:00
|
|
|
while (Auto_gosubReturn(&g_stack, (struct Pc *)0));
|
2014-11-02 01:06:28 +01:00
|
|
|
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-08 16:08:20 +01:00
|
|
|
while (g_pc.token->type != T_EOL ||
|
|
|
|
Program_skipEOL(&g_program, &g_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-08 16:08:20 +01:00
|
|
|
struct Pc exprpc = g_pc;
|
2014-10-27 14:53:12 +01:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
if (eval(value, _("dimension"))->type == V_ERROR ||
|
2014-11-08 16:08:20 +01:00
|
|
|
(g_pass != DECLARE && Value_retype(value, V_INTEGER)->type == V_ERROR))
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-08 18:47:35 +01:00
|
|
|
if (g_pass == INTERPRET && value->u.integer < g_optionbase)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
Value_destroy(value);
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = exprpc;
|
2014-11-02 01:06:28 +01:00
|
|
|
return Value_new_ERROR(value, OUTOFRANGE, _("dimension"));
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 18:47:35 +01:00
|
|
|
geometry[0] = value->u.integer - g_optionbase + 1;
|
2014-10-27 14:53:12 +01:00
|
|
|
Value_destroy(value);
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type == T_COMMA)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
|
|
|
exprpc = g_pc;
|
2020-04-13 05:56:53 +02:00
|
|
|
if (eval(value, _("dimension"))->type == V_ERROR || (g_pass != DECLARE
|
|
|
|
&& Value_retype(value, V_INTEGER)->type == V_ERROR))
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-11-08 18:47:35 +01:00
|
|
|
if (g_pass == INTERPRET && value->u.integer < g_optionbase)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
Value_destroy(value);
|
2014-11-08 16:08:20 +01:00
|
|
|
g_pc = exprpc;
|
2014-11-02 01:06:28 +01:00
|
|
|
return Value_new_ERROR(value, OUTOFRANGE, _("dimension"));
|
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 18:47:35 +01:00
|
|
|
geometry[1] = value->u.integer - g_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-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type == T_CP)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
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);
|
2020-04-13 05:56:53 +02:00
|
|
|
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);
|
2020-04-13 05:56:53 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-04-13 05:56:53 +02:00
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
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)
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_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-08 16:08:20 +01:00
|
|
|
if (g_curdata.token->type == T_DATA)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_nextdata = g_curdata.token->u.nextdata;
|
|
|
|
++g_curdata.token;
|
2014-10-27 14:53:12 +01:00
|
|
|
}
|
2014-11-04 20:35:56 +01:00
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if (convert(value, l, g_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-11-08 16:08:20 +01:00
|
|
|
++g_curdata.token;
|
|
|
|
if (g_curdata.token->type == T_COMMA)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_curdata.token;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
g_curdata = g_nextdata;
|
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
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
static struct Value more_statements;
|
2014-11-11 19:34:00 +01:00
|
|
|
#include "bas_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-11-08 16:08:20 +01:00
|
|
|
if (g_pc.token->statement)
|
2014-10-27 14:53:12 +01:00
|
|
|
{
|
2014-11-02 01:06:28 +01:00
|
|
|
struct Value *v;
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
if ((v = g_pc.token->statement(value)))
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
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-08 16:08:20 +01:00
|
|
|
if (g_pc.token->type == T_COLON && (g_pc.token + 1)->type == T_ELSE)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
2020-04-13 05:56:53 +02:00
|
|
|
else if ((g_pc.token->type == T_COLON && (g_pc.token + 1)->type != T_ELSE)
|
|
|
|
|| g_pc.token->type == T_QUOTE)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
++g_pc.token;
|
2014-11-02 01:06:28 +01:00
|
|
|
goto more;
|
|
|
|
}
|
2020-04-13 05:56:53 +02:00
|
|
|
else if ((g_pass == DECLARE || g_pass == COMPILE) &&
|
|
|
|
g_pc.token->type != T_EOL && g_pc.token->type != T_ELSE)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
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-08 16:08:20 +01:00
|
|
|
g_stack.begindata.line = -1;
|
2014-11-02 01:06:28 +01:00
|
|
|
Token_init(backslash_colon, uppercase);
|
2014-11-08 16:08:20 +01:00
|
|
|
Global_new(&g_globals);
|
|
|
|
Auto_new(&g_stack);
|
|
|
|
Program_new(&g_program);
|
2014-11-02 01:06:28 +01:00
|
|
|
FS_opendev(STDCHANNEL, 0, 1);
|
|
|
|
FS_opendev(LPCHANNEL, -1, lpfd);
|
2014-11-08 16:08:20 +01:00
|
|
|
g_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"));
|
|
|
|
}
|
2014-11-08 16:08:20 +01:00
|
|
|
else if (Program_merge(&g_program, dev, &value))
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
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-08 16:08:20 +01:00
|
|
|
Program_setname(&g_program, runFile);
|
2014-11-02 01:06:28 +01:00
|
|
|
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-08 16:08:20 +01:00
|
|
|
g_stopped = 0;
|
2014-11-02 01:06:28 +01:00
|
|
|
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)
|
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
if (g_program.numbered)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
if ((line + 1)->type == T_EOL)
|
|
|
|
{
|
|
|
|
struct Pc where;
|
|
|
|
|
2020-04-13 05:56:53 +02:00
|
|
|
if (Program_goLine(&g_program, line->u.integer, &where)
|
|
|
|
== (struct Pc *)0)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2020-03-08 17:13:09 +01:00
|
|
|
FS_putChars(STDCHANNEL, _("No such line\n"));
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
2014-11-02 01:06:28 +01:00
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
Program_delete(&g_program, &where, &where);
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
|
|
|
|
2014-11-02 01:06:28 +01:00
|
|
|
Token_destroy(line);
|
|
|
|
}
|
|
|
|
else
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
Program_store(&g_program, line, line->u.integer);
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
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);
|
2014-11-08 18:23:13 +01:00
|
|
|
if (FS_istty(STDCHANNEL) && g_bas_end)
|
2014-11-02 01:06:28 +01:00
|
|
|
{
|
|
|
|
FS_putChars(STDCHANNEL, _("END program\n"));
|
2014-11-08 18:23:13 +01:00
|
|
|
g_bas_end = false;
|
2014-11-02 01:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
{
|
2014-11-09 16:07:20 +01:00
|
|
|
/* Release resources */
|
|
|
|
|
2014-11-08 16:08:20 +01:00
|
|
|
Auto_destroy(&g_stack);
|
|
|
|
Global_destroy(&g_globals);
|
|
|
|
Program_destroy(&g_program);
|
|
|
|
if (g_labelstack)
|
2014-11-04 20:35:56 +01:00
|
|
|
{
|
2014-11-08 16:08:20 +01:00
|
|
|
free(g_labelstack);
|
|
|
|
g_labelstack = (struct labelstack_s *)0;
|
2014-11-04 20:35:56 +01:00
|
|
|
}
|
|
|
|
|
2014-11-09 16:07:20 +01:00
|
|
|
/* Close files and devices. NOTE that STDCHANNEL is also close here and
|
|
|
|
* can no longer be use
|
|
|
|
*/
|
|
|
|
|
2014-10-27 14:53:12 +01:00
|
|
|
FS_closefiles();
|
|
|
|
FS_close(LPCHANNEL);
|
|
|
|
FS_close(STDCHANNEL);
|
|
|
|
}
|