1d0ebd5999
* NEW: The project tree filter box now tries to keep the focus. * NEW: Hitting ENTER in the project filter box opens the current selected file. * NEW: Using arrows keys in the project filter box now moves the project tree cursor. * NEW: CTRL+L now sets the focus back to the project filter box. * NEW: Hitting ESC in the project filter box now resets the filter. * NEW: Add support for gb.gsl component in the project creation dialog. [INTERPRETER] * NEW: Support for floating-point constants. * BUG: The equality operators of the _operator interface now can return -1 when a datatype is not supported. [GB.COMPLEX] * NEW: Remove methods that are implemented by the _operator interface. [GB.QT4] * OPT: Load clipboard format list only if the clipboard has changed. [GB.GSL] * NEW: Matrix can be compared with real or complex numbers. The number is then multiplied by the identity matrix before doing the comparison. git-svn-id: svn://localhost/gambas/trunk@4958 867c0c6c-44f3-4631-809d-bfa615b0a4ec
336 lines
7.7 KiB
C
336 lines
7.7 KiB
C
/***************************************************************************
|
|
|
|
gbx_exec_operator.c
|
|
|
|
(c) 2000-2012 Benoît Minisini <gambas@users.sourceforge.net>
|
|
|
|
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.
|
|
|
|
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., 51 Franklin Street, Fifth Floor, Boston,
|
|
MA 02110-1301, USA.
|
|
|
|
***************************************************************************/
|
|
|
|
#define __GBX_EXEC_OPERATOR_C
|
|
|
|
#include "gb_common.h"
|
|
#include "gbx_type.h"
|
|
#include "gbx_api.h"
|
|
#include "gbx_exec.h"
|
|
|
|
typedef
|
|
void *(*FUNC_O_OF)(void *, double, bool);
|
|
|
|
typedef
|
|
void *(*FUNC_O_OO)(void *, void *, bool);
|
|
|
|
typedef
|
|
int (*FUNC_I_OF)(void *, double, bool);
|
|
|
|
typedef
|
|
int (*FUNC_I_OO)(void *, void *, bool);
|
|
|
|
typedef
|
|
void *(*FUNC_O_O)(void *);
|
|
|
|
typedef
|
|
double (*FUNC_F_O)(void *);
|
|
|
|
static void raise_error(void *o1, void *o2)
|
|
{
|
|
if (OBJECT_class(o2) == OBJECT_class(o1))
|
|
GB_Error((char *)E_TYPE, "Number", TYPE_get_name((TYPE)OBJECT_class(o1)));
|
|
else
|
|
GB_Error((char *)E_TYPE, TYPE_get_name((TYPE)OBJECT_class(o1)), o2 ? TYPE_get_name((TYPE)OBJECT_class(o2)) : "Number");
|
|
}
|
|
|
|
bool EXEC_check_operator_single(VALUE *P1, uchar op)
|
|
{
|
|
return (TYPE_is_object(P1->type) && P1->_object.object && OBJECT_class(P1->_object.object)->has_operators
|
|
&& CLASS_has_operator(OBJECT_class(P1->_object.object), op));
|
|
}
|
|
|
|
int EXEC_check_operator(VALUE *P1, VALUE *P2, uchar op)
|
|
{
|
|
CLASS *class1, *class2;
|
|
|
|
if (TYPE_is_number(P1->type) && TYPE_is_object(P2->type))
|
|
{
|
|
if (P2->_object.object && OBJECT_class(P2->_object.object)->has_operators && CLASS_has_operator(OBJECT_class(P2->_object.object), op + 1))
|
|
{
|
|
//*dynamic = P2->type == T_OBJECT;
|
|
return OP_FLOAT_OBJECT;
|
|
}
|
|
}
|
|
else if (TYPE_is_number(P2->type) && TYPE_is_object(P1->type))
|
|
{
|
|
if (P1->_object.object && OBJECT_class(P1->_object.object)->has_operators && CLASS_has_operator(OBJECT_class(P1->_object.object), op + 1))
|
|
{
|
|
//*dynamic = P1->type == T_OBJECT;
|
|
return OP_OBJECT_FLOAT;
|
|
}
|
|
}
|
|
else if (TYPE_are_objects(P1->type, P2->type) && OBJECT_are_not_null(P1->_object.object, P2->_object.object))
|
|
{
|
|
class1 = OBJECT_class(P1->_object.object);
|
|
class2 = OBJECT_class(P2->_object.object);
|
|
|
|
//*dynamic = P1->type == T_OBJECT || P2->type = T_OBJECT;
|
|
|
|
if (class1->has_operators)
|
|
{
|
|
if (class1 == class2 && CLASS_has_operator(class1, op))
|
|
return OP_OBJECT_OBJECT;
|
|
|
|
if (class2->has_operators)
|
|
{
|
|
if (CLASS_get_operator_strength(class1) > CLASS_get_operator_strength(class2) && CLASS_has_operator(class1, op + 2))
|
|
return OP_OBJECT_OTHER;
|
|
else if (CLASS_has_operator(class2, op + 2))
|
|
return OP_OTHER_OBJECT;
|
|
}
|
|
else if (CLASS_has_operator(class1, op))
|
|
return OP_OBJECT_OTHER;
|
|
}
|
|
else if (class2->has_operators && CLASS_has_operator(class2, op + 2))
|
|
return OP_OTHER_OBJECT;
|
|
}
|
|
|
|
return OP_NOTHING;
|
|
}
|
|
|
|
void EXEC_operator(uchar what, uchar op, VALUE *P1, VALUE *P2)
|
|
{
|
|
static void *jump[] = { NULL, &&__OBJECT_FLOAT, &&__FLOAT_OBJECT, &&__OBJECT_OTHER, &&__OTHER_OBJECT, &&__OBJECT_OBJECT };
|
|
|
|
void *func;
|
|
void *result;
|
|
bool invert;
|
|
void *o1, *o2;
|
|
|
|
goto *jump[what];
|
|
|
|
__OBJECT_FLOAT:
|
|
|
|
o1 = P1->_object.object;
|
|
if (!o1)
|
|
THROW(E_NULL);
|
|
|
|
func = OBJECT_class(o1)->operators[op];
|
|
VALUE_conv_float(P2);
|
|
result = (*(FUNC_O_OF)func)(o1, P2->_float.value, FALSE);
|
|
OBJECT_REF(result, "EXEC_operator");
|
|
OBJECT_UNREF(o1, "EXEC_operator");
|
|
|
|
if (!result)
|
|
{
|
|
if (op != CO_DIVF)
|
|
raise_error(o1, NULL);
|
|
}
|
|
|
|
goto __END;
|
|
|
|
__FLOAT_OBJECT:
|
|
|
|
o1 = P2->_object.object;
|
|
if (!o1)
|
|
THROW(E_NULL);
|
|
|
|
func = OBJECT_class(o1)->operators[op];
|
|
VALUE_conv_float(P1);
|
|
result = (*(FUNC_O_OF)func)(o1, P1->_float.value, TRUE);
|
|
OBJECT_REF(result, "EXEC_operator");
|
|
P1->_object.class = P2->_object.class;
|
|
OBJECT_UNREF(o1, "EXEC_operator");
|
|
|
|
if (!result && !EXEC_has_native_error())
|
|
raise_error(o1, NULL);
|
|
|
|
goto __END;
|
|
|
|
__OTHER_OBJECT:
|
|
|
|
o2 = P1->_object.object;
|
|
o1 = P2->_object.object;
|
|
|
|
invert = TRUE;
|
|
goto __OTHER;
|
|
|
|
__OBJECT_OTHER:
|
|
__OBJECT_OBJECT:
|
|
|
|
o1 = P1->_object.object;
|
|
o2 = P2->_object.object;
|
|
|
|
invert = FALSE;
|
|
goto __OTHER;
|
|
|
|
__OTHER:
|
|
|
|
if (!OBJECT_are_not_null(o1, o2))
|
|
THROW(E_NULL);
|
|
|
|
func = OBJECT_class(o1)->operators[op];
|
|
result = (*(FUNC_O_OO)func)(o1, o2, invert);
|
|
OBJECT_REF(result, "EXEC_operator");
|
|
OBJECT_UNREF(o1, "EXEC_operator");
|
|
OBJECT_UNREF(o2, "EXEC_operator");
|
|
|
|
if (!result && !EXEC_has_native_error())
|
|
raise_error(o1, o2);
|
|
|
|
__END:
|
|
|
|
P1->_object.object = result;
|
|
|
|
if (EXEC_has_native_error())
|
|
{
|
|
EXEC_set_native_error(FALSE);
|
|
PROPAGATE();
|
|
}
|
|
}
|
|
|
|
void EXEC_operator_object_add_quick(VALUE *P1, double val)
|
|
{
|
|
if (P1->_object.object)
|
|
{
|
|
void *func = OBJECT_class(P1->_object.object)->operators[CO_ADDF];
|
|
void *result = (*(FUNC_O_OF)func)(P1->_object.object, val, FALSE);
|
|
OBJECT_REF(result, "EXEC_operator_object_float_direct");
|
|
OBJECT_UNREF(P1->_object.object, "EXEC_operator_object_float_direct");
|
|
P1->_object.object = result;
|
|
}
|
|
else
|
|
THROW(E_NULL);
|
|
|
|
if (EXEC_has_native_error())
|
|
{
|
|
EXEC_set_native_error(FALSE);
|
|
PROPAGATE();
|
|
}
|
|
}
|
|
|
|
|
|
bool EXEC_comparator(uchar what, uchar op, VALUE *P1, VALUE *P2)
|
|
{
|
|
static void *jump[] = { NULL, &&__OBJECT_FLOAT, &&__FLOAT_OBJECT, &&__OBJECT_OTHER, &&__OTHER_OBJECT, &&__OBJECT_OBJECT };
|
|
|
|
void *func;
|
|
int result;
|
|
bool invert;
|
|
void *o1, *o2;
|
|
|
|
goto *jump[what];
|
|
|
|
__OBJECT_FLOAT:
|
|
|
|
o1 = P1->_object.object;
|
|
func = OBJECT_class(o1)->operators[op];
|
|
VALUE_conv_float(P2);
|
|
result = (*(FUNC_I_OF)func)(o1, P2->_float.value, FALSE);
|
|
OBJECT_UNREF(o1, "EXEC_comparator");
|
|
|
|
if (result < 0)
|
|
raise_error(o1, NULL);
|
|
|
|
goto __END;
|
|
|
|
__FLOAT_OBJECT:
|
|
|
|
o2 = P2->_object.object;
|
|
func = OBJECT_class(o2)->operators[op];
|
|
VALUE_conv_float(P1);
|
|
result = (*(FUNC_I_OF)func)(o2, P1->_float.value, TRUE);
|
|
OBJECT_UNREF(o2, "EXEC_comparator");
|
|
|
|
if (result < 0)
|
|
raise_error(o2, NULL);
|
|
|
|
goto __END;
|
|
|
|
__OTHER_OBJECT:
|
|
|
|
o2 = P1->_object.object;
|
|
o1 = P2->_object.object;
|
|
invert = TRUE;
|
|
goto __OTHER;
|
|
|
|
__OBJECT_OTHER:
|
|
__OBJECT_OBJECT:
|
|
|
|
o1 = P1->_object.object;
|
|
o2 = P2->_object.object;
|
|
invert = FALSE;
|
|
goto __OTHER;
|
|
|
|
__OTHER:
|
|
|
|
func = OBJECT_class(o1)->operators[op];
|
|
result = (*(FUNC_I_OO)func)(o1, o2, invert);
|
|
OBJECT_UNREF(o1, "EXEC_comparator");
|
|
OBJECT_UNREF(o2, "EXEC_comparator");
|
|
//result = !!result; // result != 0;
|
|
|
|
if (result < 0)
|
|
raise_error(o1, o2);
|
|
|
|
__END:
|
|
|
|
if (EXEC_has_native_error())
|
|
{
|
|
EXEC_set_native_error(FALSE);
|
|
PROPAGATE();
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void EXEC_operator_object_abs(VALUE *P1)
|
|
{
|
|
if (P1->_object.object)
|
|
{
|
|
void *func = OBJECT_class(P1->_object.object)->operators[CO_ABS];
|
|
double result = (*(FUNC_F_O)func)(P1->_object.object);
|
|
OBJECT_UNREF(P1->_object.object, "EXEC_operator_object_abs");
|
|
P1->type = T_FLOAT;
|
|
P1->_float.value = result;
|
|
}
|
|
else
|
|
THROW(E_NULL);
|
|
|
|
if (EXEC_has_native_error())
|
|
{
|
|
EXEC_set_native_error(FALSE);
|
|
PROPAGATE();
|
|
}
|
|
}
|
|
|
|
void EXEC_operator_object_single(uchar op, VALUE *P1)
|
|
{
|
|
if (P1->_object.object)
|
|
{
|
|
void *func = OBJECT_class(P1->_object.object)->operators[op];
|
|
void *result = (*(FUNC_O_O)func)(P1->_object.object);
|
|
OBJECT_REF(result, "EXEC_operator_object_single");
|
|
OBJECT_UNREF(P1->_object.object, "EXEC_operator_object_single");
|
|
P1->_object.object = result;
|
|
}
|
|
else
|
|
THROW(E_NULL);
|
|
|
|
if (EXEC_has_native_error())
|
|
{
|
|
EXEC_set_native_error(FALSE);
|
|
PROPAGATE();
|
|
}
|
|
}
|