2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
|
|
|
CExpression.c
|
|
|
|
|
2011-03-21 01:04:10 +01:00
|
|
|
(c) 2000-2011 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 __CEXPRESSION_C
|
|
|
|
|
|
|
|
#include "gb_common.h"
|
|
|
|
#include "gambas.h"
|
|
|
|
|
|
|
|
#include "eval.h"
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
#include "CExpression.h"
|
|
|
|
|
|
|
|
/*#define DEBUG*/
|
|
|
|
|
|
|
|
static CEXPRESSION *_current;
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CEXPRESSION_new)
|
|
|
|
|
|
|
|
THIS->compiled = FALSE;
|
|
|
|
CLEAR(&THIS->expr);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CEXPRESSION_free)
|
|
|
|
|
2010-08-29 22:51:10 +02:00
|
|
|
EVAL_clear(&THIS->expr, FALSE);
|
2007-12-30 17:41:49 +01:00
|
|
|
GB.FreeString(&THIS->text);
|
|
|
|
GB.FreeString(&THIS->expr.source);
|
|
|
|
GB.Unref((void **)&THIS->env);
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CEXPRESSION_text)
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB.ReturnString(THIS->text);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GB.StoreString(PROP(GB_STRING), &THIS->text);
|
|
|
|
GB.FreeString(&THIS->expr.source);
|
2010-06-05 01:48:53 +02:00
|
|
|
THIS->expr.source = GB.NewString(THIS->text, VPROP(GB_STRING).len);
|
2007-12-30 17:41:49 +01:00
|
|
|
THIS->expr.len = VPROP(GB_STRING).len;
|
|
|
|
THIS->compiled = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_PROPERTY(CEXPRESSION_environment)
|
|
|
|
|
|
|
|
if (READ_PROPERTY)
|
|
|
|
GB.ReturnObject(THIS->env);
|
|
|
|
else
|
|
|
|
GB.StoreObject(PROP(GB_OBJECT), &THIS->env);
|
|
|
|
|
|
|
|
END_PROPERTY
|
|
|
|
|
2010-07-14 18:33:29 +02:00
|
|
|
static void prepare(CEXPRESSION *_object)
|
2009-05-27 00:34:39 +02:00
|
|
|
{
|
2007-12-30 17:41:49 +01:00
|
|
|
if (!THIS->compiled && (THIS->expr.len > 0))
|
|
|
|
{
|
2010-07-14 18:33:29 +02:00
|
|
|
if (!EVAL_compile(&THIS->expr, FALSE))
|
2007-12-30 17:41:49 +01:00
|
|
|
THIS->compiled = TRUE;
|
2010-08-29 22:51:10 +02:00
|
|
|
else
|
|
|
|
GB.Error(THIS->expr.error);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
2009-05-27 00:34:39 +02:00
|
|
|
}
|
|
|
|
|
2010-07-14 18:33:29 +02:00
|
|
|
BEGIN_METHOD_VOID(CEXPRESSION_prepare)
|
2009-05-27 00:34:39 +02:00
|
|
|
|
2010-07-14 18:33:29 +02:00
|
|
|
prepare(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static bool get_variable(const char *sym, int len, GB_VARIANT *value)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
|
|
|
if (_current->env)
|
|
|
|
if (!GB.Collection.Get(_current->env, sym, len, value))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
value->type = GB_T_NULL;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2009-05-27 00:34:39 +02:00
|
|
|
static void execute(CEXPRESSION *_object)
|
|
|
|
{
|
2007-12-30 17:41:49 +01:00
|
|
|
if (!THIS->compiled)
|
2010-07-14 18:33:29 +02:00
|
|
|
prepare(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
if (!THIS->compiled)
|
|
|
|
{
|
|
|
|
GB.ReturnNull();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_current = THIS;
|
|
|
|
EVAL_expression(&THIS->expr, (EVAL_FUNCTION)get_variable);
|
2009-05-27 00:34:39 +02:00
|
|
|
}
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-05-27 00:34:39 +02:00
|
|
|
BEGIN_PROPERTY(CEXPRESSION_value)
|
|
|
|
|
|
|
|
execute(THIS);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2009-05-27 00:34:39 +02:00
|
|
|
END_PROPERTY
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GB_DESC CExpressionDesc[] =
|
|
|
|
{
|
|
|
|
GB_DECLARE("Expression", sizeof(CEXPRESSION)),
|
|
|
|
|
|
|
|
//GB_STATIC_METHOD("_init", NULL, CEXPRESSION_init, NULL),
|
|
|
|
//GB_STATIC_METHOD("_exit", NULL, CEXPRESSION_exit, NULL),
|
|
|
|
|
|
|
|
GB_METHOD("_new", NULL, CEXPRESSION_new, NULL),
|
|
|
|
GB_METHOD("_free", NULL, CEXPRESSION_free, NULL),
|
|
|
|
|
|
|
|
GB_PROPERTY("Text", "s", CEXPRESSION_text),
|
|
|
|
GB_PROPERTY("Environment", "Collection;", CEXPRESSION_environment),
|
|
|
|
|
2010-07-14 18:33:29 +02:00
|
|
|
GB_METHOD("Compile", NULL, CEXPRESSION_prepare, NULL),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GB_PROPERTY_READ("Value", "v", CEXPRESSION_value),
|
|
|
|
|
|
|
|
GB_END_DECLARE
|
|
|
|
};
|
|
|
|
|