2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
eval.c
|
|
|
|
|
2013-08-03 17:38:01 +02:00
|
|
|
(c) 2000-2013 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
|
2011-06-03 02:51:09 +02:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
2011-12-31 03:39:20 +01:00
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __EVAL_C
|
|
|
|
|
|
|
|
#include "gb_alloc_override.h"
|
|
|
|
#include "gb_common.h"
|
2009-05-27 00:34:39 +02:00
|
|
|
#include "gb_error.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "gb_array.h"
|
|
|
|
|
2015-09-06 14:36:33 +02:00
|
|
|
#include "eval_analyze.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "eval_trans.h"
|
|
|
|
#include "gb_code.h"
|
|
|
|
#include "eval.h"
|
|
|
|
|
2010-05-22 20:02:34 +02:00
|
|
|
#include "gb_common_case_temp.h"
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
/*#define DEBUG*/
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
EXPRESSION *EVAL;
|
|
|
|
EXPRESSION EVAL_read_expr;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
void EVAL_init(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
RESERVED_init();
|
|
|
|
CLEAR(&EVAL_read_expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
void EVAL_exit(void)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-08-29 22:51:10 +02:00
|
|
|
EVAL_clear(&EVAL_read_expr, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
RESERVED_exit();
|
2015-09-06 14:36:33 +02:00
|
|
|
EVAL_analyze_exit();
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
GB_VALUE *EVAL_expression(EXPRESSION *expr, EVAL_FUNCTION get_value)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
EVAL = expr;
|
|
|
|
|
|
|
|
/* Creates a class and a function for the evaluation context */
|
|
|
|
|
|
|
|
CLEAR(&EVAL->func);
|
|
|
|
EVAL->func.type = T_VARIANT;
|
|
|
|
EVAL->func.n_param = EVAL->nvar;
|
|
|
|
EVAL->func.npmin = EVAL->nvar;
|
|
|
|
EVAL->func.stack_usage = EVAL->stack_usage;
|
|
|
|
EVAL->func.code = EVAL->code;
|
|
|
|
|
|
|
|
CLEAR(&EVAL->class_load);
|
|
|
|
EVAL->class_load.cst = EVAL->cst;
|
|
|
|
EVAL->class_load.func = &EVAL->func;
|
|
|
|
EVAL->class_load.class_ref = EVAL->class;
|
|
|
|
EVAL->class_load.unknown = EVAL->unknown;
|
|
|
|
|
|
|
|
CLEAR(&EVAL->exec_class);
|
|
|
|
/*_class.class = CLASS_class;*/
|
|
|
|
EVAL->exec_class.ref = 1;
|
2009-01-29 17:11:30 +01:00
|
|
|
EVAL->exec_class.count = 1;
|
|
|
|
EVAL->exec_class.name = ".Eval";
|
2014-07-28 07:35:58 +02:00
|
|
|
EVAL->exec_class.loaded = TRUE;
|
|
|
|
EVAL->exec_class.ready = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
EVAL->exec_class.load = &EVAL->class_load;
|
|
|
|
|
|
|
|
return GB.Eval(EVAL, get_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-29 22:51:10 +02:00
|
|
|
void EVAL_clear(EXPRESSION *expr, bool keep_error)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
ARRAY_delete(&expr->tree);
|
|
|
|
|
|
|
|
ARRAY_delete(&expr->var);
|
|
|
|
ARRAY_delete(&expr->unknown);
|
|
|
|
ARRAY_delete(&expr->class);
|
|
|
|
ARRAY_delete(&expr->cst);
|
|
|
|
|
|
|
|
TABLE_delete(&expr->string);
|
|
|
|
TABLE_delete(&expr->table);
|
|
|
|
|
|
|
|
if (expr->pattern)
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&expr->pattern);
|
2007-12-30 17:41:49 +01:00
|
|
|
if (expr->code)
|
2013-03-30 00:33:01 +01:00
|
|
|
FREE(&expr->code);
|
2010-08-29 22:51:10 +02:00
|
|
|
|
|
|
|
if (!keep_error)
|
|
|
|
GB.FreeString(&expr->error);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
void EVAL_start(EXPRESSION *expr)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2013-03-30 00:33:01 +01:00
|
|
|
ALLOC(&expr->pattern, sizeof(PATTERN) * (16 + expr->len));
|
2007-12-30 17:41:49 +01:00
|
|
|
expr->pattern_count = 0;
|
|
|
|
|
|
|
|
TABLE_create(&expr->table, sizeof(EVAL_SYMBOL), EVAL->analyze ? TF_NORMAL : TF_IGNORE_CASE);
|
|
|
|
TABLE_create(&expr->string, sizeof(SYMBOL), TF_NORMAL);
|
|
|
|
|
|
|
|
ARRAY_create(&expr->cst);
|
|
|
|
ARRAY_create(&expr->class);
|
|
|
|
ARRAY_create(&expr->unknown);
|
|
|
|
expr->code = NULL;
|
|
|
|
expr->ncode = 0;
|
[DEVELOPMENT ENVIRONMENT]
* BUG: Use TextEdit.RichText insted of TextEdit.Text.
* BUG: END SUB can be the end of a method. The class analyze now takes
that into account.
[HELP]
* BUG: Fixed the generated treeview.
[COMPILER]
* OPT: The NOT operator used just at the beginning of a conditional
expression is optimized. Consequently, an expression like 'IF NOT 2' is
now equivalent to 'IF 2 = 0' and not to 'IF (NOT 2) <> 0' as before. In
other words, the boolean conversion is now done before the NOT, and not
after. The following instructions are concerned: IF, WHILE, UNTIL.
* NEW: BYREF is new keyword that is a more readable synonymous of '@'.
[GB.DB.FORM]
* BUG: Correctly manage data controls inside TabStrip-like containers.
* BUG: Setting the focus on a non-initialized DataControl does not raise
an error anymore.
[GB.GTK]
* BUG: HSplitter.Layout and VSplitter.Layout now work correctly. It is a
list of children widths, hidden children having a zero width.
* BUG: Window arrangement is done before the Open event is raised, as in
gb.qt.
* BUG: Keyboard, focus and mouse events now work correctly on Window and
DrawingArea controls.
[GB.QT]
* BUG: HSplitter.Layout and VSplitter.Layout now work correctly. It is a
list of children widths, hidden children having a zero width.
* BUG: Many warning fixes.
* BUG: Now the Control.Visible property works like in gb.gtk, i.e. it
returns if the control was not explicitely hidden.
git-svn-id: svn://localhost/gambas/trunk@1060 867c0c6c-44f3-4631-809d-bfa615b0a4ec
2008-02-06 01:25:48 +01:00
|
|
|
expr->ncode_max = 0;
|
2007-12-30 17:41:49 +01:00
|
|
|
ARRAY_create(&expr->var);
|
|
|
|
|
|
|
|
expr->nvar = 0;
|
|
|
|
}
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
bool EVAL_compile(EXPRESSION *expr, bool assign)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
bool error = FALSE;
|
|
|
|
|
|
|
|
EVAL = expr;
|
|
|
|
|
2010-08-29 22:51:10 +02:00
|
|
|
EVAL_clear(EVAL, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (expr->len == 0)
|
|
|
|
return TRUE;
|
2010-07-14 18:33:29 +02:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
EVAL_start(EVAL);
|
|
|
|
|
|
|
|
TRY
|
|
|
|
{
|
|
|
|
EVAL_read();
|
2009-05-27 00:34:39 +02:00
|
|
|
|
|
|
|
EVAL->current = EVAL->pattern;
|
|
|
|
|
2010-07-14 18:33:29 +02:00
|
|
|
if (PATTERN_is(*EVAL->current, RS_LET))
|
|
|
|
{
|
|
|
|
EVAL->current++;
|
|
|
|
assign = TRUE;
|
|
|
|
}
|
|
|
|
|
2009-05-27 00:34:39 +02:00
|
|
|
if (assign)
|
|
|
|
{
|
|
|
|
if (!TRANS_affectation())
|
2010-07-14 18:33:29 +02:00
|
|
|
THROW(E_SYNTAX);
|
2009-05-27 00:34:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
TRANS_expression();
|
|
|
|
|
|
|
|
if (!PATTERN_is_end(*EVAL->current))
|
|
|
|
THROW(E_SYNTAX);
|
|
|
|
|
2011-09-09 02:29:06 +02:00
|
|
|
CODE_return(1);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
EVAL->stack_usage = CODE_stack_usage;
|
|
|
|
}
|
|
|
|
CATCH
|
|
|
|
{
|
2010-08-29 22:51:10 +02:00
|
|
|
EVAL_clear(EVAL, TRUE);
|
2007-12-30 17:41:49 +01:00
|
|
|
error = TRUE;
|
|
|
|
}
|
|
|
|
END_TRY
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
CODE_dump(EVAL->code);
|
|
|
|
printf("Stack usage = %d\n", CODE_stack_usage);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
bool EVAL_get_assignment_symbol(EXPRESSION *expr, const char **name, int *len)
|
|
|
|
{
|
|
|
|
ushort code = EVAL->assign_code;
|
|
|
|
int index;
|
|
|
|
EVAL_SYMBOL *sym;
|
|
|
|
|
|
|
|
if ((code & 0xFF00) == C_POP_PARAM)
|
|
|
|
{
|
|
|
|
index = -((signed char)(code & 0xFF)) - 1;
|
|
|
|
sym = (EVAL_SYMBOL *)TABLE_get_symbol(EVAL->table, EVAL->var[index]);
|
|
|
|
*name = sym->sym.name;
|
|
|
|
*len = sym->sym.len;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
void EVAL_new(EXPRESSION **expr, char *src, int len)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
GB.Alloc((void **)expr, sizeof(EXPRESSION));
|
|
|
|
CLEAR(*expr);
|
2010-06-05 01:48:53 +02:00
|
|
|
(*expr)->source = GB.NewString(src, len);
|
2011-09-07 21:43:11 +02:00
|
|
|
(*expr)->source = GB.AddString((*expr)->source, "\n\0", 2);
|
2007-12-30 17:41:49 +01:00
|
|
|
(*expr)->len = len + 2;
|
|
|
|
/*(*expr)->option = option;*/
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
void EVAL_free(EXPRESSION **pexpr)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2010-08-29 22:51:10 +02:00
|
|
|
EVAL_clear(*pexpr, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
GB.FreeString(&(*pexpr)->source);
|
|
|
|
GB.Free((void **)pexpr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
int EVAL_add_constant(CLASS_CONST *cst)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
int num;
|
|
|
|
CLASS_CONST *desc;
|
|
|
|
|
|
|
|
num = ARRAY_count(EVAL->cst);
|
|
|
|
|
|
|
|
desc = ARRAY_add(&EVAL->cst);
|
|
|
|
*desc = *cst;
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
int EVAL_add_class(char *name)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
int num;
|
|
|
|
CLASS **desc;
|
|
|
|
|
|
|
|
num = ARRAY_count(EVAL->class);
|
|
|
|
|
|
|
|
desc = ARRAY_add(&EVAL->class);
|
2009-08-04 21:14:17 +02:00
|
|
|
*desc = (CLASS *)GB.FindClassLocal(name);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
/*sym->class = num + 1;*/
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
int EVAL_add_unknown(char *name)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
int num;
|
|
|
|
char **desc;
|
|
|
|
|
|
|
|
num = ARRAY_count(EVAL->unknown);
|
|
|
|
|
|
|
|
desc = ARRAY_add(&EVAL->unknown);
|
|
|
|
*desc = name;
|
|
|
|
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-24 16:19:32 +02:00
|
|
|
int EVAL_add_variable(int index)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
EVAL_SYMBOL *sym;
|
|
|
|
|
|
|
|
sym = (EVAL_SYMBOL *)TABLE_get_symbol(EVAL->table, index);
|
|
|
|
|
|
|
|
if (sym->local == 0)
|
|
|
|
{
|
|
|
|
EVAL->nvar++;
|
|
|
|
sym->local = EVAL->nvar;
|
|
|
|
|
|
|
|
*((int *)ARRAY_add(&EVAL->var)) = index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (-sym->local);
|
|
|
|
}
|
|
|
|
|