![]() |
![]() |
Gwyddion Library Reference Manual | ![]() |
|
---|---|---|---|---|
#define GWY_EXPR_ERROR enum GwyExprError; GwyExpr; GQuark gwy_expr_error_quark (void); GwyExpr* gwy_expr_new (void); void gwy_expr_free (GwyExpr *expr); gboolean gwy_expr_evaluate (GwyExpr *expr, const gchar *text, gdouble *result, GError **err); gboolean gwy_expr_compile (GwyExpr *expr, const gchar *text, GError **err); gint gwy_expr_resolve_variables (GwyExpr *expr, guint n, gchar *const *names, guint *indices); gint gwy_expr_get_variables (GwyExpr *expr, gchar ***names); gdouble gwy_expr_execute (GwyExpr *expr, const gdouble *values);
GwyExpr is an expression evaluator, more precisely parser, compiler, and
evaluator. A new GwyExpr can be created with gwy_expr_new()
, then it can be
used to evaluate any number of expressions; when it's no longer needed, it
should be destroyed with gwy_expr_free()
.
Simple arithmetic expressions without variables can be directly evaluated with
gwy_expr_evaluate()
.
Expression with variables have to be compiled first with gwy_expr_compile()
.
The either gwy_expr_resolve_variables()
or gwy_expr_get_variables()
can be
used to obtain information what variables are present in the expression
and at which positions (variables are references by position, not name during
final evaluation because of effieiency reasons). Subsequent evaluation with
variable substitution is performed by gwy_expr_execute()
.
#define GWY_EXPR_ERROR gwy_expr_error_quark()
Error domain for expression parsing and evaluation. Errors in this domain will be from the GwyExprError enumeration. See GError for information on error domains.
Since 1.9
typedef enum { GWY_EXPR_ERROR_CLOSING_PARENTHESIS, GWY_EXPR_ERROR_EMPTY, GWY_EXPR_ERROR_EMPTY_PARENTHESES, GWY_EXPR_ERROR_GARBAGE, GWY_EXPR_ERROR_INVALID_ARGUMENT, GWY_EXPR_ERROR_INVALID_TOKEN, GWY_EXPR_ERROR_MISSING_ARGUMENT, GWY_EXPR_ERROR_NOT_EXECUTABLE, GWY_EXPR_ERROR_OPENING_PARENTHESIS, GWY_EXPR_ERROR_STRAY_COMMA, GWY_EXPR_ERROR_UNRESOLVED_IDENTIFIERS } GwyExprError;
Error codes returned by expression parsing and execution.
Since 1.9
typedef struct _GwyExpr GwyExpr;
GwyExpr is an opaque data structure and should be only manipulated with the functions below.
Since 1.9
GQuark gwy_expr_error_quark (void);
Returns error domain for expression parsin and evaluation.
See and use GWY_EXPR_ERROR
.
Returns : | The error domain. |
Since 1.9
GwyExpr* gwy_expr_new (void);
Creates a new expression evaluator.
Returns : | A newly created expression evaluator. |
Since 1.9
void gwy_expr_free (GwyExpr *expr);
Frees all memory used by and expression evaluator.
expr : |
An expression evaluator. |
Since 1.9
gboolean gwy_expr_evaluate (GwyExpr *expr, const gchar *text, gdouble *result, GError **err);
Evaulates an arithmetic expression.
expr : |
An expression evaluator. |
text : |
String containing expression to evaluate. |
result : |
Location to store result to. |
err : |
Location to store compilation error to. |
Returns : | TRUE on success, FALSE if evaluation failed.
|
Since 1.9
gboolean gwy_expr_compile (GwyExpr *expr, const gchar *text, GError **err);
Compiles an expression for later execution.
This function is useful for expressions with variables. For normal
arithmetic expressions it's easier to use gwy_expr_evaluate()
.
expr : |
An expression evaluator. |
text : |
String containing expression to compile. |
err : |
Location to store compilation error to. |
Returns : | TRUE on success, FALSE if compilation failed.
|
Since 1.9
gint gwy_expr_resolve_variables (GwyExpr *expr, guint n, gchar *const *names, guint *indices);
Finds positions of variables in an expression.
expr : |
An expression evaluator. |
n : |
Length of names and indices arrays.
|
names : |
List of variable names to get positions of. |
indices : |
Array to store variable positions to. The positions are the same
as in gwy_expr_execute() . Zero is stored for variables not
present in the expression to allow to safely substitute values
without caring which variables are actually used.
|
Returns : | The number of remaining (not asked for) variables in expr .
|
Since 1.9
gint gwy_expr_get_variables (GwyExpr *expr, gchar ***names);
Get the number, names, and indices of unresolved identifiers in expr
.
It is an error to call this function after an unsuccessful compilation.
If you only care about variables from a prefedined set, use
gwy_expr_resolve_variables()
.
The position of each variable in names
corresponds to value position in
values
array in gwy_expr_execute()
call. Namely, the first item in the
array is always reserved and do not correspond to any variable.
expr : |
An expression evaluator. |
names : |
Location to store pointer to list of variable names to (may be NULL
to get just number of variables). The string array returned in
this argument in owned by expr and is valid only until next
gwy_expr_compile() , gwy_expr_evaluate() , eventually gwy_expr_free()
call.
|
Returns : | The length of array stored to names . This is the number of
variables plus one (for the first reserved item).
On failure, 0 is returned.
|
Since 1.9
gdouble gwy_expr_execute (GwyExpr *expr, const gdouble *values);
Executes a compiled expression with variables, substituting given values.
expr : |
An expression evaluator. |
values : |
Array with variable values. Its zeroth item is always unused.
Variable list can be obtained by gwy_expr_get_variables() .
|
Returns : | The result. |
Since 1.9