2010-12-18 05:27:59 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
gbc_preprocess.c
|
2010-12-18 05:27:59 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2010-12-18 05:27:59 +01:00
|
|
|
|
2011-12-31 03:39:20 +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
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2010-12-18 05:27:59 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
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.
|
2010-12-18 05:27:59 +01:00
|
|
|
|
2011-12-31 03:39:20 +01:00
|
|
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2010-12-18 05:27:59 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __GBC_PREPROCESS_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gb_common_case.h"
|
|
|
|
#include "gb_error.h"
|
|
|
|
#include "gb_table.h"
|
|
|
|
|
|
|
|
#include "gbc_compile.h"
|
|
|
|
#include "gbc_read.h"
|
2011-04-16 15:25:46 +02:00
|
|
|
#include "gbc_trans.h"
|
2010-12-18 05:27:59 +01:00
|
|
|
#include "gbc_preprocess.h"
|
|
|
|
|
|
|
|
typedef
|
|
|
|
struct {
|
|
|
|
bool ignore;
|
|
|
|
int ignore_level;
|
|
|
|
}
|
|
|
|
PREP_STATE;
|
|
|
|
|
|
|
|
#define MAX_LEVEL 16
|
2011-04-16 15:25:46 +02:00
|
|
|
|
|
|
|
int PREP_next_line;
|
2010-12-18 05:27:59 +01:00
|
|
|
|
|
|
|
static PREP_STATE _stack[MAX_LEVEL];
|
|
|
|
|
|
|
|
static int _level;
|
|
|
|
static bool _ignore;
|
|
|
|
static int _ignore_level;
|
|
|
|
static PATTERN *_current;
|
|
|
|
|
|
|
|
|
|
|
|
static int get_expression(void);
|
|
|
|
|
|
|
|
static bool is_current(int res)
|
|
|
|
{
|
|
|
|
if (PATTERN_is(*_current, res))
|
|
|
|
{
|
|
|
|
_current++;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2010-12-18 15:41:43 +01:00
|
|
|
static bool compare_value(const char *value)
|
2010-12-18 05:27:59 +01:00
|
|
|
{
|
|
|
|
PATTERN op;
|
|
|
|
SYMBOL *sym;
|
|
|
|
char version[8];
|
|
|
|
int n, major, minor;
|
|
|
|
int diff;
|
|
|
|
|
|
|
|
op = *_current++;
|
|
|
|
if (!PATTERN_is_reserved(op))
|
|
|
|
THROW("Missing operator");
|
|
|
|
|
|
|
|
if (!PATTERN_is_string(*_current))
|
2010-12-18 15:41:43 +01:00
|
|
|
THROW("String expected");
|
2010-12-18 05:27:59 +01:00
|
|
|
|
|
|
|
sym = TABLE_get_symbol(JOB->class->string, PATTERN_index(*_current));
|
|
|
|
_current++;
|
|
|
|
|
2010-12-18 15:41:43 +01:00
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
if (strlen(value) != sym->len)
|
|
|
|
diff = 1;
|
|
|
|
else
|
|
|
|
diff = strncasecmp(value, sym->name, sym->len);
|
|
|
|
|
|
|
|
switch (PATTERN_index(op))
|
|
|
|
{
|
|
|
|
case RS_EQUAL: return diff == 0;
|
|
|
|
case RS_NE: return diff != 0;
|
|
|
|
default: THROW("Equality or inequality operator expected");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2010-12-18 05:27:59 +01:00
|
|
|
{
|
2013-01-06 17:13:31 +01:00
|
|
|
if (sym->len < 1 || sym->len >= sizeof(version))
|
2010-12-18 15:41:43 +01:00
|
|
|
THROW("Bad version string");
|
|
|
|
|
|
|
|
memcpy(version, sym->name, sym->len);
|
|
|
|
version[sym->len] = 0;
|
|
|
|
n = sscanf(version, "%d.%d", &major, &minor);
|
|
|
|
if (n == 0)
|
|
|
|
THROW("Bad version string");
|
|
|
|
else if (n == 1)
|
|
|
|
minor = 0;
|
|
|
|
|
|
|
|
diff = GAMBAS_VERSION - major;
|
|
|
|
if (!diff)
|
|
|
|
diff = GAMBAS_MINOR_VERSION - minor;
|
|
|
|
|
|
|
|
switch (PATTERN_index(op))
|
|
|
|
{
|
|
|
|
case RS_EQUAL: return diff == 0;
|
|
|
|
case RS_NE: return diff != 0;
|
|
|
|
case RS_GT: return diff > 0;
|
|
|
|
case RS_LT: return diff < 0;
|
|
|
|
case RS_GE: return diff >= 0;
|
|
|
|
case RS_LE: return diff <= 0;
|
|
|
|
default: THROW("Comparison operator expected");
|
|
|
|
}
|
2010-12-18 05:27:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-18 15:41:43 +01:00
|
|
|
#define compare_symbol(_symbol, _name, _len) ((strlen(_symbol) == _len) && !strncasecmp((_symbol), (_name), (_len)))
|
2010-12-18 05:27:59 +01:00
|
|
|
|
|
|
|
static int get_symbol(const char *name, int len)
|
|
|
|
{
|
2010-12-18 15:41:43 +01:00
|
|
|
if (compare_symbol("system", name, len))
|
|
|
|
return compare_value(SYSTEM);
|
2010-12-18 05:27:59 +01:00
|
|
|
|
2010-12-18 15:41:43 +01:00
|
|
|
if (compare_symbol("arch", name, len) || compare_symbol("architecture", name, len))
|
|
|
|
return compare_value(ARCHITECTURE);
|
2010-12-18 05:27:59 +01:00
|
|
|
|
2010-12-18 15:41:43 +01:00
|
|
|
if (compare_symbol("version", name, len) || compare_symbol("gambas", name, len))
|
|
|
|
return compare_value(NULL);
|
2010-12-19 13:58:58 +01:00
|
|
|
|
2010-12-27 15:35:06 +01:00
|
|
|
/*if (compare_symbol("debug", name, len))
|
|
|
|
return JOB->debug;*/
|
2010-12-27 15:28:59 +01:00
|
|
|
|
2010-12-27 15:35:06 +01:00
|
|
|
/*if (compare_symbol("true", name, len))
|
2010-12-27 15:28:59 +01:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if (compare_symbol("false", name, len))
|
2010-12-27 15:35:06 +01:00
|
|
|
return FALSE;*/
|
2010-12-18 05:27:59 +01:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_value(void)
|
|
|
|
{
|
|
|
|
int value;
|
|
|
|
SYMBOL *sym;
|
|
|
|
|
|
|
|
if (is_current(RS_LBRA))
|
|
|
|
{
|
|
|
|
value = get_expression();
|
|
|
|
if (!is_current(RS_RBRA))
|
|
|
|
THROW("Missing right brace");
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_current(RS_NOT))
|
|
|
|
return !get_value();
|
|
|
|
|
|
|
|
if (PATTERN_is_identifier(*_current))
|
|
|
|
{
|
|
|
|
sym = TABLE_get_symbol(JOB->class->table, PATTERN_index(*_current));
|
|
|
|
_current++;
|
|
|
|
value = get_symbol(sym->name, sym->len);
|
|
|
|
return value;
|
|
|
|
}
|
2010-12-27 15:35:06 +01:00
|
|
|
else if (is_current(RS_FALSE))
|
|
|
|
return 0;
|
|
|
|
else if (is_current(RS_TRUE))
|
|
|
|
return 1;
|
|
|
|
else if (is_current(RS_DEBUG))
|
|
|
|
return JOB->debug;
|
2012-05-05 02:39:43 +02:00
|
|
|
else if (is_current(RS_EXEC))
|
|
|
|
return JOB->exec;
|
2010-12-18 05:27:59 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_expression(void)
|
|
|
|
{
|
|
|
|
int value;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
value = get_value();
|
|
|
|
|
|
|
|
if (is_current(RS_AND))
|
|
|
|
{
|
|
|
|
if (!value)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_current(RS_OR))
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PATTERN_is_newline(*_current) || PATTERN_is(*_current, RS_RBRA))
|
|
|
|
return value;
|
|
|
|
|
|
|
|
THROW(E_SYNTAX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PREP_init(void)
|
|
|
|
{
|
|
|
|
_level = 0;
|
|
|
|
_ignore = FALSE;
|
|
|
|
_current = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PREP_exit(void)
|
|
|
|
{
|
|
|
|
if (_level)
|
|
|
|
THROW("Missing #Endif");
|
|
|
|
}
|
|
|
|
|
2011-04-16 15:25:46 +02:00
|
|
|
int PREP_analyze(PATTERN *line)
|
2010-12-18 05:27:59 +01:00
|
|
|
{
|
|
|
|
bool test;
|
|
|
|
|
|
|
|
if (PATTERN_is(*line, RS_P_IF))
|
|
|
|
{
|
|
|
|
if (_level >= MAX_LEVEL)
|
|
|
|
THROW("Too many imbricated #If...#Endif");
|
|
|
|
|
|
|
|
_stack[_level].ignore = _ignore;
|
|
|
|
_stack[_level].ignore_level = _ignore_level;
|
|
|
|
|
|
|
|
line++;
|
|
|
|
_level++;
|
|
|
|
|
|
|
|
if (!_ignore)
|
|
|
|
{
|
|
|
|
_current = line;
|
|
|
|
if (!get_expression())
|
|
|
|
{
|
|
|
|
_ignore = TRUE;
|
|
|
|
_ignore_level = _level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(*line, RS_P_ELSE))
|
|
|
|
{
|
2011-10-16 21:24:01 +02:00
|
|
|
//bool else_if = FALSE;
|
2010-12-18 05:27:59 +01:00
|
|
|
|
|
|
|
if (!_level)
|
|
|
|
THROW_UNEXPECTED(line);
|
|
|
|
|
|
|
|
test = TRUE;
|
|
|
|
line++;
|
|
|
|
|
|
|
|
if (!_ignore)
|
|
|
|
{
|
|
|
|
_ignore = TRUE;
|
|
|
|
_ignore_level = _level - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (_level == _ignore_level)
|
|
|
|
{
|
|
|
|
if (PATTERN_is(*line, RS_IF))
|
|
|
|
{
|
|
|
|
line++;
|
|
|
|
|
|
|
|
_current = line;
|
|
|
|
test = get_expression();
|
|
|
|
|
2011-10-16 21:24:01 +02:00
|
|
|
//else_if = TRUE;
|
2010-12-18 05:27:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_level == _ignore_level)
|
|
|
|
_ignore = !test;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (PATTERN_is(*line, RS_P_ENDIF))
|
|
|
|
{
|
|
|
|
if (!_level)
|
|
|
|
THROW_UNEXPECTED(line);
|
|
|
|
|
|
|
|
_level--;
|
|
|
|
_ignore = _stack[_level].ignore;
|
|
|
|
_ignore_level = _stack[_level].ignore_level;
|
|
|
|
}
|
2011-04-16 15:25:46 +02:00
|
|
|
else if (PATTERN_is(*line, RS_P_LINE))
|
|
|
|
{
|
|
|
|
TRANS_NUMBER result;
|
|
|
|
|
|
|
|
line++;
|
|
|
|
if (!PATTERN_is_number(*line))
|
|
|
|
THROW_UNEXPECTED(line);
|
|
|
|
|
|
|
|
TRANS_get_number(PATTERN_index(*line), &result);
|
|
|
|
if (result.type != T_INTEGER)
|
|
|
|
THROW_UNEXPECTED(line);
|
|
|
|
|
2011-04-22 18:52:59 +02:00
|
|
|
PREP_next_line = result.ival - 1;
|
2011-04-16 15:25:46 +02:00
|
|
|
return PREP_LINE;
|
|
|
|
}
|
2010-12-27 15:28:59 +01:00
|
|
|
else if (PATTERN_is(*line, RS_P_CONST))
|
|
|
|
{
|
2012-03-18 14:14:17 +01:00
|
|
|
// TODO
|
2010-12-27 15:28:59 +01:00
|
|
|
}
|
2010-12-18 05:27:59 +01:00
|
|
|
else
|
|
|
|
THROW(E_SYNTAX);
|
|
|
|
|
2011-04-16 15:25:46 +02:00
|
|
|
return _ignore ? PREP_IGNORE : PREP_CONTINUE;
|
2010-12-18 05:27:59 +01:00
|
|
|
}
|