2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
gbc_trans.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-17 12:41:51 +02:00
|
|
|
(c) 2000-2009 Benoît Minisini <gambas@users.sourceforge.net>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
2009-08-17 12:41:51 +02:00
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
2007-12-30 17:41:49 +01:00
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBC_TRANS_C
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <math.h>
|
2009-12-10 03:48:25 +01:00
|
|
|
#include <float.h>
|
2010-11-27 15:46:25 +01:00
|
|
|
#include <limits.h>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
|
|
|
|
#include "gbc_compile.h"
|
|
|
|
#include "gbc_read.h"
|
|
|
|
#include "gbc_trans.h"
|
|
|
|
#include "gb_reserved.h"
|
|
|
|
#include "gb_code.h"
|
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
#define IS_PURE_INTEGER(_int64_val) ((_int64_val) == ((int)(_int64_val)))
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
int TRANS_in_affectation = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void TRANS_reset(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
JOB->line = 1;
|
|
|
|
JOB->current = JOB->pattern;
|
|
|
|
JOB->end = &(JOB->pattern[JOB->pattern_count]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-27 15:46:25 +01:00
|
|
|
static bool read_integer(char *number, int base, bool minus, int64_t *result)
|
2008-01-17 22:39:26 +01:00
|
|
|
{
|
2009-12-10 03:48:25 +01:00
|
|
|
uint64_t nbr2, nbr;
|
2010-05-29 02:11:55 +02:00
|
|
|
int d, n;
|
2009-12-10 03:48:25 +01:00
|
|
|
unsigned char c;
|
2010-05-29 02:11:55 +02:00
|
|
|
int nmax;
|
2009-12-10 03:48:25 +01:00
|
|
|
|
|
|
|
n = 0;
|
|
|
|
nbr = 0;
|
|
|
|
|
|
|
|
switch (base)
|
|
|
|
{
|
|
|
|
case 2: nmax = 64; break;
|
|
|
|
case 10: nmax = 19; break;
|
|
|
|
case 16: nmax = 16; break;
|
|
|
|
}
|
|
|
|
|
2010-05-29 02:11:55 +02:00
|
|
|
if (base == 10)
|
2009-12-10 03:48:25 +01:00
|
|
|
{
|
2010-05-29 02:11:55 +02:00
|
|
|
c = *number++;
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2010-05-29 02:11:55 +02:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (isdigit(c))
|
|
|
|
d = c - '0';
|
|
|
|
else
|
|
|
|
break;
|
2009-12-10 03:48:25 +01:00
|
|
|
|
2010-05-29 02:11:55 +02:00
|
|
|
n++;
|
|
|
|
if (n < nmax)
|
|
|
|
nbr = nbr * 10 + d;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nbr2 = nbr * 10 + d;
|
|
|
|
|
2010-11-27 15:46:25 +01:00
|
|
|
if ((nbr2 / 10) != nbr || nbr2 > (LLONG_MAX + minus))
|
2010-05-29 02:11:55 +02:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
nbr = nbr2;
|
|
|
|
}
|
2009-12-10 03:48:25 +01:00
|
|
|
|
2010-05-29 02:11:55 +02:00
|
|
|
c = *number++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
}
|
2009-12-10 03:48:25 +01:00
|
|
|
}
|
2010-05-29 02:11:55 +02:00
|
|
|
else
|
2009-12-10 03:48:25 +01:00
|
|
|
{
|
2010-05-29 02:11:55 +02:00
|
|
|
c = *number++;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (isdigit(c))
|
|
|
|
d = c - '0';
|
|
|
|
else if (c >= 'A' && c <='Z')
|
|
|
|
d = c - 'A' + 10;
|
|
|
|
else if (c >= 'a' && c <='z')
|
|
|
|
d = c - 'a' + 10;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (d >= base)
|
|
|
|
break;
|
|
|
|
|
|
|
|
n++;
|
|
|
|
if (n > nmax)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
nbr = nbr * base + d;
|
|
|
|
|
|
|
|
c = *number++;
|
|
|
|
if (!c)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if ((c == '&' || c == 'u' || c == 'U') && base != 10)
|
|
|
|
c = *number++;
|
|
|
|
else
|
|
|
|
{
|
2010-01-02 16:04:06 +01:00
|
|
|
if ((base == 16 && n == 4) || (base == 2 && n == 16))
|
|
|
|
{
|
|
|
|
if (nbr >= 0x8000L && nbr <= 0xFFFFL)
|
|
|
|
nbr |= INT64_C(0xFFFFFFFFFFFF0000);
|
|
|
|
}
|
|
|
|
else if ((base == 16 && n == 8) || (base == 2 && n == 32))
|
|
|
|
{
|
|
|
|
if (nbr >= 0x80000000L && nbr <= 0xFFFFFFFFL)
|
|
|
|
nbr |= INT64_C(0xFFFFFFFF00000000);
|
|
|
|
}
|
2009-12-10 03:48:25 +01:00
|
|
|
}
|
|
|
|
}
|
2010-05-29 02:11:55 +02:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (c)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (n == 0)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
*((int64_t *)result) = nbr;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool read_float(char *number, double *result)
|
|
|
|
{
|
|
|
|
unsigned char c;
|
|
|
|
double nint;
|
|
|
|
double nfrac, n;
|
|
|
|
int nexp;
|
|
|
|
bool nexp_minus;
|
|
|
|
|
|
|
|
nint = 0.0;
|
|
|
|
nfrac = 0.0;
|
|
|
|
nexp = 0;
|
|
|
|
nexp_minus = FALSE;
|
|
|
|
|
|
|
|
c = *number++;
|
|
|
|
|
|
|
|
/* Integer part */
|
2008-01-17 22:39:26 +01:00
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
2009-12-10 03:48:25 +01:00
|
|
|
if (c == '.')
|
|
|
|
{
|
|
|
|
c = *number++;
|
2008-01-17 22:39:26 +01:00
|
|
|
break;
|
2009-12-10 03:48:25 +01:00
|
|
|
}
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (!c || !isdigit(c))
|
|
|
|
return TRUE;
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
nint = nint * 10 + (c - '0');
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
c = *number++;
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (c == 'e' || c == 'E')
|
|
|
|
break;
|
2008-01-17 22:39:26 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (!c || isspace(c))
|
|
|
|
goto __END;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
/* Decimal part */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
n = 0.1;
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if (!c || !isdigit(c))
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
nfrac += n * (c - '0');
|
|
|
|
n /= 10;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
c = *number++;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
/* Exponent */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (c == 'e' || c == 'E')
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-12-10 03:48:25 +01:00
|
|
|
c = *number++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (c == '+' || c == '-')
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-12-10 03:48:25 +01:00
|
|
|
if (c == '-')
|
|
|
|
nexp_minus = TRUE;
|
|
|
|
|
|
|
|
c = *number++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2009-12-10 03:48:25 +01:00
|
|
|
|
|
|
|
if (!c || !isdigit(c))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
for(;;)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-12-10 03:48:25 +01:00
|
|
|
nexp = nexp * 10 + (c - '0');
|
|
|
|
if (nexp > DBL_MAX_10_EXP)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
c = *number++;
|
|
|
|
if (!c || !isdigit(c))
|
|
|
|
break;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (c)
|
2007-12-30 17:41:49 +01:00
|
|
|
return TRUE;
|
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
__END:
|
|
|
|
|
|
|
|
*result = (nint + nfrac) * pow(10, nexp_minus ? (-nexp) : nexp);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
bool TRANS_get_number(int index, TRANS_NUMBER *result)
|
|
|
|
{
|
2010-05-29 02:11:55 +02:00
|
|
|
char buffer[68];
|
|
|
|
SYMBOL *sym;
|
|
|
|
char *number;
|
2009-12-10 03:48:25 +01:00
|
|
|
unsigned char c;
|
|
|
|
int64_t val = 0;
|
|
|
|
double dval = 0.0;
|
|
|
|
int type;
|
|
|
|
int base = 10;
|
|
|
|
bool minus = FALSE;
|
|
|
|
|
2010-05-29 02:11:55 +02:00
|
|
|
sym = TABLE_get_symbol(JOB->class->table, index);
|
|
|
|
if (sym->len > 66)
|
|
|
|
return TRUE;
|
|
|
|
memcpy(buffer, sym->name, sym->len);
|
|
|
|
buffer[sym->len] = 0;
|
|
|
|
number = buffer;
|
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
c = *number++;
|
|
|
|
|
|
|
|
if (c == '+' || c == '-')
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-12-10 03:48:25 +01:00
|
|
|
minus = (c == '-');
|
|
|
|
c = *number++;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (c == '&')
|
|
|
|
{
|
|
|
|
c = *number++;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (c == 'H' || c == 'h')
|
|
|
|
{
|
|
|
|
base = 16;
|
|
|
|
c = *number++;
|
|
|
|
}
|
|
|
|
else if (c == 'X' || c == 'x')
|
|
|
|
{
|
|
|
|
base = 2;
|
|
|
|
c = *number++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
base = 16;
|
|
|
|
}
|
|
|
|
else if (c == '%')
|
|
|
|
{
|
|
|
|
base = 2;
|
|
|
|
c = *number++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!c)
|
|
|
|
return TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (c == '-' || c == '+')
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
number--;
|
|
|
|
|
2010-11-27 15:46:25 +01:00
|
|
|
if (!read_integer(number, base, minus, &val))
|
2009-12-10 03:48:25 +01:00
|
|
|
{
|
2010-12-22 19:31:44 +01:00
|
|
|
if (minus) val = (-val);
|
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (IS_PURE_INTEGER(val))
|
|
|
|
{
|
|
|
|
type = T_INTEGER;
|
|
|
|
goto __END;
|
2008-01-17 22:39:26 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-10 03:48:25 +01:00
|
|
|
type = T_LONG;
|
|
|
|
goto __END;
|
2008-01-17 22:39:26 +01:00
|
|
|
}
|
2009-12-10 03:48:25 +01:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-12-10 03:48:25 +01:00
|
|
|
if (base == 10)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2009-12-10 03:48:25 +01:00
|
|
|
if (!read_float(number, &dval))
|
|
|
|
{
|
2010-12-22 19:31:44 +01:00
|
|
|
if (minus) dval = (-dval);
|
2009-12-10 03:48:25 +01:00
|
|
|
type = T_FLOAT;
|
|
|
|
goto __END;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2009-12-10 03:48:25 +01:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
__END:
|
|
|
|
|
|
|
|
result->type = type;
|
|
|
|
|
|
|
|
if (type == T_INTEGER)
|
2010-12-22 19:31:44 +01:00
|
|
|
result->lval = result->ival = val;
|
2009-12-10 03:48:25 +01:00
|
|
|
else if (type == T_LONG)
|
2010-12-22 19:31:44 +01:00
|
|
|
result->lval = val;
|
2007-12-30 17:41:49 +01:00
|
|
|
else
|
2010-12-22 19:31:44 +01:00
|
|
|
result->dval = dval;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-28 08:24:14 +02:00
|
|
|
static PATTERN *trans_embedded_array(PATTERN *look, int mode, TRANS_DECL *result)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
TRANS_NUMBER tnum;
|
|
|
|
int i;
|
|
|
|
|
2010-05-19 14:43:57 +02:00
|
|
|
if (!(mode & TT_CAN_STATIC))
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (PATTERN_is(*look, RS_LSQR))
|
2010-05-19 14:43:57 +02:00
|
|
|
THROW("Static arrays are forbidden here");
|
2007-12-30 17:41:49 +01:00
|
|
|
return look;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PATTERN_is(*look, RS_LSQR))
|
|
|
|
return look;
|
|
|
|
|
|
|
|
look++;
|
|
|
|
|
|
|
|
if (mode && TT_CAN_ARRAY)
|
|
|
|
{
|
|
|
|
for (i = 0;; i++)
|
|
|
|
{
|
|
|
|
if (i >= MAX_ARRAY_DIM)
|
|
|
|
THROW("Too many dimensions");
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
if (!PATTERN_is_number(*look))
|
|
|
|
THROW(E_SYNTAX);
|
2007-12-30 17:41:49 +01:00
|
|
|
if (TRANS_get_number(PATTERN_index(*look), &tnum))
|
|
|
|
THROW(E_SYNTAX);
|
|
|
|
if (tnum.type != T_INTEGER)
|
|
|
|
THROW(E_SYNTAX);
|
|
|
|
if (tnum.ival < 1 || tnum.ival > (2 << 22)) /* 4 Mo, ca devrait suffire... ;-) */
|
|
|
|
THROW("Bad subscript range");
|
|
|
|
|
|
|
|
result->array.dim[i] = tnum.ival;
|
|
|
|
result->array.ndim++;
|
|
|
|
look++;
|
|
|
|
|
|
|
|
if (PATTERN_is(*look, RS_RSQR))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!PATTERN_is(*look, RS_COMMA))
|
2009-09-21 18:57:21 +02:00
|
|
|
THROW(E_MISSING, "','");
|
2007-12-30 17:41:49 +01:00
|
|
|
look++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PATTERN_is(*look, RS_RSQR))
|
2009-09-21 18:57:21 +02:00
|
|
|
THROW(E_MISSING, "']'");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
look++;
|
|
|
|
return look;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-28 08:24:14 +02:00
|
|
|
static int TRANS_get_class(PATTERN pattern, bool array)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
int index = PATTERN_index(pattern);
|
2008-09-01 01:45:47 +02:00
|
|
|
int index_array;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!CLASS_exist_class(JOB->class, index))
|
2008-09-01 01:45:47 +02:00
|
|
|
{
|
2010-05-28 08:24:14 +02:00
|
|
|
if (array)
|
2008-09-01 01:45:47 +02:00
|
|
|
{
|
2010-05-28 08:24:14 +02:00
|
|
|
// Maybe a compound class?
|
|
|
|
|
|
|
|
CLASS_SYMBOL *sym = CLASS_get_symbol(JOB->class, index);
|
|
|
|
int i;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
//fprintf(stderr, "TRANS_get_class: %.*s\n", sym->symbol.len, sym->symbol.name);
|
|
|
|
|
|
|
|
for (i = sym->symbol.len - 1; i >= 0; i--)
|
2008-09-01 01:45:47 +02:00
|
|
|
{
|
2010-05-28 08:24:14 +02:00
|
|
|
c = sym->symbol.name[i];
|
|
|
|
if (c == '[')
|
|
|
|
{
|
|
|
|
//fprintf(stderr, "TRANS_get_class: find %.*s\n", i, sym->symbol.name);
|
|
|
|
if (TABLE_find_symbol(JOB->class->table, sym->symbol.name, i, &index_array))
|
|
|
|
{
|
|
|
|
index_array = TRANS_get_class(PATTERN_make(RT_CLASS, index_array), TRUE);
|
|
|
|
if (JOB->class->class[index_array].exported)
|
|
|
|
return CLASS_add_class_exported(JOB->class, index);
|
|
|
|
else
|
|
|
|
return CLASS_add_class(JOB->class, index);
|
|
|
|
}
|
2008-09-01 01:45:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
THROW("Unknown identifier: &1", TABLE_get_symbol_name(JOB->class->table, index));
|
2008-09-01 01:45:47 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
return CLASS_add_class(JOB->class, index);
|
|
|
|
}
|
|
|
|
|
2010-06-05 01:48:53 +02:00
|
|
|
static bool check_structure(int cindex)
|
|
|
|
{
|
|
|
|
SYMBOL *sym = TABLE_get_symbol(JOB->class->table, JOB->class->class[cindex].index);
|
|
|
|
int len = sym->len;
|
|
|
|
char name[sym->len + 1];
|
|
|
|
int index;
|
|
|
|
bool is_array;
|
|
|
|
|
|
|
|
strncpy(name, sym->name, len);
|
|
|
|
while (name[len - 1] == ']')
|
|
|
|
len -= 2;
|
|
|
|
name[len] = 0;
|
|
|
|
|
|
|
|
if (len < sym->len)
|
|
|
|
{
|
|
|
|
if (!TABLE_find_symbol(JOB->class->table, name, len, &index))
|
|
|
|
goto __ERROR;
|
|
|
|
|
|
|
|
index = CLASS_add_class(JOB->class, index);
|
|
|
|
is_array = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
index = cindex;
|
|
|
|
is_array = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (JOB->class->class[index].structure)
|
|
|
|
return is_array;
|
|
|
|
|
|
|
|
__ERROR:
|
|
|
|
|
|
|
|
THROW("&1 is not a structure", name);
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
bool TRANS_type(int mode, TRANS_DECL *result)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
PATTERN *look = JOB->current;
|
2008-08-31 02:32:21 +02:00
|
|
|
short id;
|
|
|
|
int value;
|
2007-12-30 17:41:49 +01:00
|
|
|
int flag = 0;
|
2010-06-05 01:48:53 +02:00
|
|
|
bool is_array;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-09-01 01:45:47 +02:00
|
|
|
/* Do not fill the structure with zeros */
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
TYPE_clear(&result->type);
|
|
|
|
result->is_new = FALSE;
|
|
|
|
result->init = NULL;
|
|
|
|
result->array.ndim = 0;
|
|
|
|
|
2010-05-28 08:24:14 +02:00
|
|
|
look = trans_embedded_array(look, mode, result);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!PATTERN_is(*look, RS_AS))
|
|
|
|
{
|
|
|
|
if (mode & TT_DO_NOT_CHECK_AS)
|
|
|
|
return FALSE;
|
|
|
|
else
|
2009-09-21 18:57:21 +02:00
|
|
|
THROW(E_MISSING, "AS");
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
look++;
|
2010-05-25 13:19:00 +02:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (mode & TT_CAN_NEW)
|
|
|
|
{
|
|
|
|
if (PATTERN_is(*look, RS_NEW))
|
|
|
|
{
|
|
|
|
if (TYPE_get_id(result->type) == T_ARRAY)
|
2010-05-28 08:24:14 +02:00
|
|
|
THROW("Cannot mix NEW and embedded array");
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
result->is_new = TRUE;
|
|
|
|
look++;
|
|
|
|
result->init = look;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
if ((mode & TT_CAN_STATIC) && PATTERN_is(*look, RS_STRUCT))
|
2008-08-31 02:32:21 +02:00
|
|
|
{
|
2010-05-25 13:19:00 +02:00
|
|
|
id = T_STRUCT;
|
|
|
|
look++;
|
|
|
|
|
|
|
|
if (!PATTERN_is_class(*look))
|
|
|
|
THROW_UNEXPECTED(look);
|
2010-06-05 01:48:53 +02:00
|
|
|
|
2010-05-30 21:37:40 +02:00
|
|
|
value = TRANS_get_class(*look, TRUE);
|
2010-06-05 01:48:53 +02:00
|
|
|
is_array = check_structure(value);
|
|
|
|
|
|
|
|
if (!is_array)
|
|
|
|
{
|
|
|
|
if (result->is_new)
|
|
|
|
THROW("Cannot mix NEW and embedded structure");
|
2010-07-08 00:06:05 +02:00
|
|
|
//if (result->array.ndim > 0)
|
|
|
|
// THROW("Cannot mix embedded array and embedded structure");
|
2010-06-05 01:48:53 +02:00
|
|
|
}
|
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
look++;
|
2008-08-31 02:32:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-25 13:19:00 +02:00
|
|
|
if (!PATTERN_is_type(*look) && !PATTERN_is_class(*look))
|
|
|
|
THROW_UNEXPECTED(look);
|
|
|
|
|
|
|
|
if (PATTERN_is_type(*look))
|
|
|
|
{
|
|
|
|
id = RES_get_type(PATTERN_index(*look));
|
|
|
|
value = -1;
|
|
|
|
}
|
|
|
|
else
|
2008-08-31 02:32:21 +02:00
|
|
|
{
|
2010-05-25 13:19:00 +02:00
|
|
|
id = T_OBJECT;
|
2010-05-28 08:24:14 +02:00
|
|
|
value = TRANS_get_class(*look, TRUE);
|
2010-05-25 13:19:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PATTERN_is(look[1], RS_LSQR))
|
|
|
|
{
|
|
|
|
value = CLASS_get_array_class(JOB->class, id, value);
|
|
|
|
id = T_OBJECT;
|
|
|
|
|
|
|
|
if (!PATTERN_is(look[2], RS_RSQR))
|
2008-08-31 02:32:21 +02:00
|
|
|
{
|
2010-05-25 13:19:00 +02:00
|
|
|
if ((mode & TT_CAN_NEW) && result->is_new)
|
|
|
|
{
|
|
|
|
//if (TYPE_get_id(result->type) == T_ARRAY)
|
|
|
|
// THROW("Cannot mix NEW and static array declaration");
|
2008-08-31 02:32:21 +02:00
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
//result->is_new = TRUE;
|
|
|
|
result->init = look;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
THROW("Syntax error");
|
2008-08-31 02:32:21 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-05-25 13:19:00 +02:00
|
|
|
while (!PATTERN_is_newline(*look))
|
|
|
|
look++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//if (id == T_OBJECT)
|
|
|
|
// value = (-1);
|
2008-08-31 02:32:21 +02:00
|
|
|
look++;
|
2010-05-25 13:19:00 +02:00
|
|
|
}
|
2008-08-31 02:32:21 +02:00
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
if (id == T_VOID)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
if (result->is_array && result->array.ndim == 0)
|
|
|
|
result->is_array = FALSE;
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (result->array.ndim > 0)
|
|
|
|
{
|
|
|
|
result->array.type = TYPE_make(id, value, flag);
|
|
|
|
result->type = TYPE_make(T_ARRAY, CLASS_add_array(JOB->class, &result->array), 0);
|
|
|
|
}
|
2010-05-25 13:19:00 +02:00
|
|
|
else if (id == T_STRUCT)
|
|
|
|
{
|
|
|
|
result->type = TYPE_make(id, value, flag);
|
|
|
|
}
|
|
|
|
else
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
result->type = TYPE_make(id, value, flag);
|
|
|
|
|
|
|
|
if ((mode & TT_CAN_NEW) && !result->is_new && PATTERN_is(*look, RS_EQUAL))
|
|
|
|
{
|
|
|
|
look++;
|
|
|
|
result->init = look;
|
|
|
|
while (!PATTERN_is_newline(*look))
|
|
|
|
look++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JOB->current = look;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
bool TRANS_check_declaration(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
PATTERN *look = JOB->current;
|
|
|
|
|
|
|
|
if (!PATTERN_is_identifier(*look))
|
|
|
|
return FALSE;
|
|
|
|
look++;
|
|
|
|
|
|
|
|
if (PATTERN_is(*look, RS_LSQR))
|
|
|
|
{
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
look++;
|
|
|
|
if (PATTERN_is(*look, RS_RSQR))
|
|
|
|
break;
|
|
|
|
if (PATTERN_is_newline(*look))
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
look++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PATTERN_is(*look, RS_AS))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
PATTERN *TRANS_get_constant_value(TRANS_DECL *decl, PATTERN *current)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
TRANS_NUMBER number;
|
|
|
|
int type;
|
|
|
|
PATTERN value;
|
|
|
|
|
|
|
|
type = TYPE_get_id(decl->type);
|
|
|
|
|
|
|
|
value = *current++;
|
|
|
|
index = PATTERN_index(value);
|
|
|
|
|
|
|
|
if (type == T_STRING)
|
|
|
|
{
|
|
|
|
if (PATTERN_is(value, RS_LBRA))
|
|
|
|
{
|
|
|
|
value = *current++;
|
|
|
|
if (!PATTERN_is_string(value))
|
|
|
|
THROW("Syntax error");
|
|
|
|
index = PATTERN_index(value);
|
|
|
|
value = *current++;
|
|
|
|
if (!PATTERN_is(value, RS_RBRA))
|
|
|
|
THROW("Syntax error");
|
|
|
|
TYPE_set_id(&decl->type, T_CSTRING);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!PATTERN_is_string(value))
|
|
|
|
THROW("Syntax error");
|
|
|
|
}
|
|
|
|
|
|
|
|
decl->is_integer = FALSE;
|
|
|
|
decl->value = index;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (PATTERN_is_string(value))
|
|
|
|
THROW("Syntax error");
|
|
|
|
|
|
|
|
if (type != T_BOOLEAN && type <= T_FLOAT)
|
|
|
|
{
|
|
|
|
if (TRANS_get_number(index, &number))
|
|
|
|
THROW("Type mismatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case T_BOOLEAN:
|
|
|
|
|
|
|
|
decl->is_integer = TRUE;
|
|
|
|
|
|
|
|
if (PATTERN_is(value, RS_TRUE))
|
|
|
|
decl->value = -1L;
|
|
|
|
else if (PATTERN_is(value, RS_FALSE))
|
|
|
|
decl->value = 0L;
|
|
|
|
else
|
|
|
|
THROW("Type mismatch");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_BYTE: case T_SHORT: case T_INTEGER:
|
|
|
|
|
|
|
|
decl->is_integer = TRUE;
|
|
|
|
|
|
|
|
if (number.type != T_INTEGER)
|
|
|
|
{
|
|
|
|
if (number.type == T_LONG)
|
|
|
|
THROW("Out of range");
|
|
|
|
else
|
|
|
|
THROW("Type mismatch");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((type == T_BYTE) && (number.ival < 0 || number.ival > 255))
|
|
|
|
|| ((type == T_SHORT) && (number.ival < -32768L || number.ival > 32767L)))
|
|
|
|
THROW("Out of range");
|
|
|
|
|
|
|
|
decl->value = number.ival;
|
|
|
|
|
|
|
|
//fprintf(stderr, "TRANS_get_constant_value: INT: %ld\n", decl->value);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_FLOAT: case T_SINGLE:
|
|
|
|
|
|
|
|
if (type == T_SINGLE && !finite((float)number.dval))
|
|
|
|
THROW("Out of range");
|
|
|
|
|
|
|
|
decl->is_integer = FALSE;
|
|
|
|
decl->value = index;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
|
|
|
|
if (number.type == T_FLOAT)
|
|
|
|
THROW("Type mismatch");
|
|
|
|
|
|
|
|
decl->is_integer = FALSE;
|
|
|
|
decl->value = index;
|
|
|
|
decl->lvalue = number.lval;
|
|
|
|
|
|
|
|
//fprintf(stderr, "TRANS_get_constant_value: LONG: %lld\n", decl->lvalue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
THROW("Bad constant type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void TRANS_want(int reserved, char *msg)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (!PATTERN_is(*JOB->current, reserved))
|
|
|
|
THROW("Syntax error. &1 expected", msg ? msg : COMP_res_info[reserved].name);
|
|
|
|
JOB->current++;
|
|
|
|
}
|
|
|
|
|
2010-05-05 22:58:55 +02:00
|
|
|
void TRANS_want_newline()
|
|
|
|
{
|
|
|
|
if (!TRANS_newline())
|
|
|
|
THROW_UNEXPECTED(JOB->current);
|
|
|
|
}
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
void TRANS_ignore(int reserved)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (PATTERN_is(*JOB->current, reserved))
|
|
|
|
JOB->current++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-01-06 19:49:23 +01:00
|
|
|
bool TRANS_is_end_function(bool is_proc, PATTERN *look)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (PATTERN_is_newline(*look))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (is_proc)
|
|
|
|
return PATTERN_is(*look, RS_PROCEDURE) || PATTERN_is(*look, RS_SUB);
|
|
|
|
else
|
|
|
|
return PATTERN_is(*look, RS_FUNCTION);
|
|
|
|
}
|
2008-01-19 18:52:05 +01:00
|
|
|
|
|
|
|
char *TRANS_get_num_desc(int num)
|
|
|
|
{
|
|
|
|
static const char *num_desc[3] = { "first", "second", "third" };
|
|
|
|
static char desc[6];
|
|
|
|
|
|
|
|
if (num < 1)
|
|
|
|
return NULL;
|
|
|
|
|
2009-09-21 18:57:21 +02:00
|
|
|
if (ERROR_translate)
|
|
|
|
{
|
|
|
|
snprintf(desc, sizeof(desc), "#%d", num);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (num < 4)
|
|
|
|
return (char *)num_desc[num - 1];
|
2008-01-19 18:52:05 +01:00
|
|
|
|
2009-09-21 18:57:21 +02:00
|
|
|
snprintf(desc, sizeof(desc), "%dth", num);
|
|
|
|
}
|
|
|
|
|
2008-01-19 18:52:05 +01:00
|
|
|
return desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|