github.com/256dpi/max-go@v0.7.0/lib/max/ext_expr.h (about) 1 /* ext_expr.h -- header file for writing expressions */ 2 3 #ifndef _EXT_EXPR_H_ 4 #define _EXT_EXPR_H_ 5 6 #include "ext_prefix.h" 7 #include "ext_mess.h" 8 9 BEGIN_USING_C_LINKAGE 10 11 #if C74_PRAGMA_STRUCT_PACKPUSH 12 #pragma pack(push, 2) 13 #elif C74_PRAGMA_STRUCT_PACK 14 #pragma pack(2) 15 #endif 16 17 #define EXPR_MAX_VARS 9 18 19 /** Defines for ex_type. 20 We treat parenthesis and brackets special to keep a pointer to their match in the content. 21 @ingroup expr 22 */ 23 typedef enum { 24 ET_INT = 0x1, ///< an int 25 ET_FLT = 0x2, ///< a float 26 ET_OP = 0x3, ///< operator 27 ET_STR = 0x4, ///< string 28 ET_TBL = 0x5, ///< a table, the content is a pointer 29 ET_FUNC = 0x6, ///< a function 30 ET_SYM = 0x7, ///< symbol ("string") 31 ET_VSYM = 0x8, ///< variable symbol ("$s?") 32 ET_LP = 0x9, ///< left parenthesis 33 ET_LB = 0x10, ///< left bracket 34 ET_II = 0x11, ///< and integer inlet 35 ET_FI = 0x12, ///< float inlet 36 ET_SI = 0x13 ///< string inlet 37 } e_max_expr_types; 38 39 /** ex_ex. 40 @ingroup expr 41 */ 42 typedef struct ex_ex { 43 union { 44 t_atom_long v_int; 45 double v_flt; 46 t_atom_long op; 47 char **ptr; 48 } ex_cont; ///< content 49 long ex_type; ///< type of the node 50 } t_ex_ex; 51 52 53 /** Struct for an instance of expr. 54 @ingroup expr 55 */ 56 typedef struct expr { 57 t_object exp_ob; 58 void *exp_outlet; 59 t_ex_ex *exp_stack; 60 t_ex_ex exp_var[EXPR_MAX_VARS]; 61 t_ex_ex exp_res; ///< the result of last evaluation 62 } t_expr; 63 64 /** Create a new expr object. 65 @ingroup expr 66 67 @param argc Count of arguments in argv. 68 @param argv Arguments that are used to create the expr. See the example below for details. 69 @param types A pre-existing array of nine t_atoms, that will hold the 70 types of any variable arguments created in the expr. 71 The types are returned in the a_type field of each 72 #t_atom. If an argument was not present, #A_NOTHING is returned. 73 @return expr_new() creates an expr object from the arguments in argv and 74 returns the type of any expr-style arguments contained in argv (i.e. 75 $i1, etc.) in atoms in an array pointed to by types. 76 77 @remark types should already exist as an array of nine t_atom values, all of which will be filled in by 78 expr_new(). If an argument was not present, it will set to type 79 #A_NOTHING. For example, suppose argv pointed to the following atoms: 80 @code 81 $i1 (A_SYM) 82 + (A_SYM) 83 $f3 (A_SYM) 84 + (A_SYM) 85 3 (A_LONG) 86 @endcode 87 88 After calling expr_new, types would contain the following: 89 @code 90 Index Argument Type Value 91 0 1 ($i1) A_LONG 0 92 1 2 A_NOTHING 0 93 2 3 ($f3) A_FLOAT 0.0 94 3 4 A_NOTHING 0 95 4 5 A_NOTHING 0 96 5 6 A_NOTHING 0 97 6 7 A_NOTHING 0 98 7 8 A_NOTHING 0 99 8 9 A_NOTHING 0 100 @endcode 101 */ 102 void *expr_new(long argc, t_atom *argv, t_atom *types); 103 104 105 /** Evaluate an expression in an expr object. 106 @ingroup expr 107 108 @param x The expr object to evaluate. 109 @param argc Count of arguments in argv. 110 @param argv Array of nine t_atom values that will be substituted for 111 variable arguments (such as $i1) in the expression. 112 Unused arguments should be of type #A_NOTHING. 113 @param result A pre-existing t_atom that will hold the type and value 114 of the result of evaluating the expression. 115 @return . 116 117 @remark Evaluates the expression in an expr object with arguments in argv and 118 returns the type and value of the evaluated expression as a t_atom in 119 result. result need only point to a single #t_atom, but argv should 120 contain at least argc t_atom values. If, as in the example shown above under 121 expr_new(), there are “gaps” between arguments, they should be filled 122 in with t_atom of type #A_NOTHING. 123 */ 124 short expr_eval(t_expr *x, long argc, t_atom *argv, t_atom *result); 125 126 127 void expr_install(method fun, const char *buf, long argc); 128 129 130 131 #if C74_PRAGMA_STRUCT_PACKPUSH 132 #pragma pack(pop) 133 #elif C74_PRAGMA_STRUCT_PACK 134 #pragma pack() 135 #endif 136 137 END_USING_C_LINKAGE 138 139 #endif // _EXT_EXPR_H_