set() function + userdata storage + Expr instance access for functions
- functions can take string arguments (i.e. variable names that may be undefined) - set(<varname>, <value>) sets a variable to a user-defined value - any userdata can be stored via expr_set_userdata() and retrieved via expr_get_userdata(); also accessible to functions - functions gain access to the expr instance and consequently all of its variables including userdata
This commit is contained in:
21
expr.h
21
expr.h
@@ -5,16 +5,29 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct _Expr Expr;
|
||||
|
||||
typedef struct {
|
||||
size_t start, end;
|
||||
const char *err;
|
||||
} ExprError;
|
||||
|
||||
typedef enum {
|
||||
ExprArgTypeStr,
|
||||
ExprArgTypeNum,
|
||||
} ExprArgType;
|
||||
|
||||
typedef union {
|
||||
char *Str;
|
||||
double Num;
|
||||
} ExprArg;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
const char *description;
|
||||
double (*func)(double *args);
|
||||
double (*func)(Expr *e, ExprArg *args);
|
||||
const char **arg_names;
|
||||
ExprArgType *arg_types;
|
||||
size_t n_args;
|
||||
} ExprBuiltinFunc;
|
||||
|
||||
@@ -24,8 +37,6 @@ typedef struct {
|
||||
double val;
|
||||
} ExprBuiltinVar;
|
||||
|
||||
typedef struct _Expr Expr;
|
||||
|
||||
extern ExprBuiltinFunc *expr_builtin_funcs;
|
||||
extern const size_t expr_n_builtin_funcs;
|
||||
extern ExprBuiltinVar *expr_builtin_vars;
|
||||
@@ -37,6 +48,8 @@ ExprError expr_set(Expr *e, const char *expr) __attribute__((warn_unused_result)
|
||||
ExprError expr_eval(Expr *e, double *out_res) __attribute__((warn_unused_result));
|
||||
void expr_set_var(Expr *e, const char *name, double val);
|
||||
bool expr_get_var(Expr *e, const char *name, double *out); /* Returns false if not present */
|
||||
void expr_set_func(Expr *e, const char *name, double (*func)(double *args), size_t n_args);
|
||||
void expr_set_func(Expr *e, const char *name, double (*func)(Expr *e, ExprArg *args), ExprArgType *arg_types, size_t n_args);
|
||||
void expr_set_userdata(Expr *e, void *userdata);
|
||||
void *expr_get_userdata(Expr *e);
|
||||
|
||||
#endif /* __EXPR_H__ */
|
||||
|
||||
Reference in New Issue
Block a user