CpgExpression

CpgExpression — Mathematical expression evaluation

Synopsis


#include <cpg-network/cpg-expression.h>

                    CpgInstruction;
enum                CpgInstructionCode;
enum                CpgInstructionBinding;
                    CpgInstructionProperty;
                    CpgExpression;
CpgExpression *     cpg_expression_new                  (gchar const *expression);
GSList *            cpg_expression_get_dependencies     (CpgExpression *expression);
const gchar *       cpg_expression_get_as_string        (CpgExpression *expression);
gint                cpg_expression_compile              (CpgExpression *expression,
                                                         CpgCompileContext *context,
                                                         GError **error);
gdouble             cpg_expression_evaluate             (CpgExpression *expression);
void                cpg_expression_set_value            (CpgExpression *expression,
                                                         gdouble value);
void                cpg_expression_reset                (CpgExpression *expression);
gboolean            cpg_expression_equal                (CpgExpression *expression,
                                                         CpgExpression *other);
void                cpg_expression_set_from_string      (CpgExpression *expression,
                                                         gchar const *value);
void                cpg_expression_reset_cache          (CpgExpression *expression);
GSList *            cpg_expression_get_instructions     (CpgExpression *expression);
gboolean            cpg_expression_set_instructions     (CpgExpression *expression,
                                                         GSList *instructions);
CpgInstruction *    cpg_instruction_function_new        (guint id,
                                                         gchar const *name,
                                                         gint arguments,
                                                         gboolean variable);
CpgInstruction *    cpg_instruction_custom_function_new (CpgFunction *function,
                                                         gint arguments);
CpgInstruction *    cpg_instruction_number_new          (gdouble value);
CpgInstruction *    cpg_instruction_operator_new        (guint id,
                                                         gchar const *name,
                                                         gint arguments);
CpgInstruction *    cpg_instruction_property_new        (CpgProperty *property,
                                                         CpgInstructionBinding binding);
CpgInstruction *    cpg_instruction_copy                (CpgInstruction *instruction);
void                cpg_instruction_free                (CpgInstruction *instruction);
gchar *             cpg_instruction_to_string           (CpgInstruction *instruction);

Description

A CpgExpression contains a mathematical expression. The expression in string format can be compiled and evaluated. At the compilation phase, a list of CpgObject is provided as a context in which variables are mapped to CpgProperty in this context.

Details

CpgInstruction

typedef struct {
	CpgInstructionCode type;
} CpgInstruction;


enum CpgInstructionCode

typedef enum {
	CPG_INSTRUCTION_TYPE_NONE,
	CPG_INSTRUCTION_TYPE_FUNCTION,
	CPG_INSTRUCTION_TYPE_NUMBER,
	CPG_INSTRUCTION_TYPE_OPERATOR,
	CPG_INSTRUCTION_TYPE_PROPERTY,
	CPG_INSTRUCTION_TYPE_CUSTOM_FUNCTION
} CpgInstructionCode;

Enum used to indicate instruction type

CPG_INSTRUCTION_TYPE_NONE

none

CPG_INSTRUCTION_TYPE_FUNCTION

function

CPG_INSTRUCTION_TYPE_NUMBER

number

CPG_INSTRUCTION_TYPE_OPERATOR

operator

CPG_INSTRUCTION_TYPE_PROPERTY

property

CPG_INSTRUCTION_TYPE_CUSTOM_FUNCTION

custom function

enum CpgInstructionBinding

typedef enum
{
	CPG_INSTRUCTION_BINDING_NONE = 0,
	CPG_INSTRUCTION_BINDING_FROM,
	CPG_INSTRUCTION_BINDING_TO,
} CpgInstructionBinding;


CpgInstructionProperty

typedef struct {
	CpgInstruction parent;

	CPG_FORWARD_DECL (CpgProperty) *property;
	CpgInstructionBinding binding;
} CpgInstructionProperty;


CpgExpression

typedef struct _CpgExpression CpgExpression;


cpg_expression_new ()

CpgExpression *     cpg_expression_new                  (gchar const *expression);

Create a new CpgExpression containing the expression expression

expression :

an expression

Returns :

a new CpgExpression

cpg_expression_get_dependencies ()

GSList *            cpg_expression_get_dependencies     (CpgExpression *expression);

Get a list of CpgProperty on which the expression depends. The list is owned by expression and should not be freed or modified

expression :

a CpgExpression

Returns :

a list of CpgProperty

cpg_expression_get_as_string ()

const gchar *       cpg_expression_get_as_string        (CpgExpression *expression);

Get the string representation of the expression

expression :

a CpgExpression

Returns :

the string representation of the expression

cpg_expression_compile ()

gint                cpg_expression_compile              (CpgExpression *expression,
                                                         CpgCompileContext *context,
                                                         GError **error);

Compile the expression. The context is a list of CpgObject from which properties can be looked up (such as global constants, or from/to objects). If there were any errors during compilation, error will be set accordingly

expression :

a CpgExpression

context :

the evaluation context

error :

a GError

Returns :

TRUE if the expression compiled successfully, FALSE otherwise

cpg_expression_evaluate ()

gdouble             cpg_expression_evaluate             (CpgExpression *expression);

Get the result of evaluating the expression. If the expression is not yet compiled, 0.0 is returned. The result of the evaluation is cached in the expression. Make sure to call cpg_expression_reset_cache to clear the cache if needed

expression :

a CpgExpression

Returns :

the result of evaluating the expression

cpg_expression_set_value ()

void                cpg_expression_set_value            (CpgExpression *expression,
                                                         gdouble value);

Sets the cached/instant value of an expression. If the expression is reset, this value will no longer be used and the expression will be evaluated as normal

expression :

a CpgExpression

value :

a value

cpg_expression_reset ()

void                cpg_expression_reset                (CpgExpression *expression);

Resets all the expression flags (cache, instant)

expression :

a CpgExpression

cpg_expression_equal ()

gboolean            cpg_expression_equal                (CpgExpression *expression,
                                                         CpgExpression *other);

Get whether two expressions are equal. If the expressions are compiled, they are evaluated for equality by means of their instructions. Otherwise the comparison is done on their string representations

expression :

a CpgExpression

other :

a CpgExpression

Returns :

TRUE if the expressions are equal, FALSE otherwise

cpg_expression_set_from_string ()

void                cpg_expression_set_from_string      (CpgExpression *expression,
                                                         gchar const *value);

Set a new expression for expression

expression :

a CpgExpression

value :

the value

cpg_expression_reset_cache ()

void                cpg_expression_reset_cache          (CpgExpression *expression);

Resets the possibly cached result of the value

expression :

a CpgExpression

cpg_expression_get_instructions ()

GSList *            cpg_expression_get_instructions     (CpgExpression *expression);

Get list of CpgInstruction. The list is owned by expression and should not be freed or modified

expression :

a CpgExpression

Returns :

list of CpgInstruction

cpg_expression_set_instructions ()

gboolean            cpg_expression_set_instructions     (CpgExpression *expression,
                                                         GSList *instructions);

Set the instructions used to evaluate the expression. You should never have to use this function. It's main purpose is for optimization of expressions in cpgrawc.

expression :

A CpgExpression

instructions :

A GSList of CpgInstruction

Returns :

TRUE if the new instruction set is valid, FALSE otherwise

cpg_instruction_function_new ()

CpgInstruction *    cpg_instruction_function_new        (guint id,
                                                         gchar const *name,
                                                         gint arguments,
                                                         gboolean variable);

Creates a new function call instruction. The id id of the instruction specifies which function the instruction represents (see CpgMathFunctionType). If variable is TRUE, the instruction expects the first value on the stack, when the instruction is executed, to be the number of arguments to process.

id :

function id

name :

function name

arguments :

the number of arguments this function takes

variable :

if the number of arguments is variable for this function

Returns :

the new CpgInstruction

cpg_instruction_custom_function_new ()

CpgInstruction *    cpg_instruction_custom_function_new (CpgFunction *function,
                                                         gint arguments);

Creates a new custom function call instruction. If the number of arguments (arguments) is not equal to the number of arguments function takes, the instruction expects the first value on the stack, when the instruction is executed, to be the number of arguments to process.

function :

A CpgFunction

arguments :

The number of arguments this function takes

Returns :

A CpgInstruction

cpg_instruction_number_new ()

CpgInstruction *    cpg_instruction_number_new          (gdouble value);

Creates a new number instruction. This is the most basic instruction, which simply pushes a number onto the stack when executed.

value :

the numeric value

Returns :

the new CpgInstruction

cpg_instruction_operator_new ()

CpgInstruction *    cpg_instruction_operator_new        (guint id,
                                                         gchar const *name,
                                                         gint arguments);

Creates a new operator instruction. The id id of the instruction specifies which operator the instruction represents (see CpgMathOperatorType).

id :

operator id

name :

operator name

arguments :

the number of arguments this operator takes

Returns :

the new CpgInstruction

cpg_instruction_property_new ()

CpgInstruction *    cpg_instruction_property_new        (CpgProperty *property,
                                                         CpgInstructionBinding binding);

Creates a new property call instruction. When the instruction is executed, the property value expression is evaluated and its return value is pushed on the stack.

property :

a CpgProperty

binding :

the property binding

Returns :

the new CpgInstruction

cpg_instruction_copy ()

CpgInstruction *    cpg_instruction_copy                (CpgInstruction *instruction);

Create a copy of a CpgInstruction

instruction :

a CpgInstruction

Returns :

a new CpgInstruction

cpg_instruction_free ()

void                cpg_instruction_free                (CpgInstruction *instruction);

Free previously created CpgInstruction object

instruction :

a CpgInstruction

cpg_instruction_to_string ()

gchar *             cpg_instruction_to_string           (CpgInstruction *instruction);

Get the string representation of an instruction.

instruction :

A CpgInstruction

Returns :

The string representation of the instruction.