Max 5 API Reference
00001 /* ext_expr.h -- header file for writing expressions */ 00002 00003 #ifndef _EXT_EXPR_H_ 00004 #define _EXT_EXPR_H_ 00005 00006 #ifdef __cplusplus 00007 extern "C" { 00008 #endif 00009 00010 #if C74_PRAGMA_STRUCT_PACKPUSH 00011 #pragma pack(push, 2) 00012 #elif C74_PRAGMA_STRUCT_PACK 00013 #pragma pack(2) 00014 #endif 00015 00016 00017 #define MAXMAX /* <z> added to users of this stuff won't be screwed */ 00018 00019 #define MAX_VARS 9 00020 00021 00022 struct ex_funcs { 00023 char *f_name; // function name 00024 long (*f_func)(); // the real function performing the function 00025 long f_argc; // number of arguments 00026 }; 00027 00028 #define name_ok(c) (((c)=='_') || ((c)>='a' && (c)<='z') || \ 00029 ((c)>='A' && (c)<='Z') || ((c) >= '0' && (c) <= '9')) 00030 #define unary_op(x) ((x) == OP_NOT || (x) == OP_NEG || (x) == OP_UMINUS) 00031 00032 00033 #define ex_int ex_cont.v_int ///< shortcut for accessing members of an #Ex_ex struct's ex_cont union. @ingroup expr 00034 #define ex_flt ex_cont.v_flt ///< shortcut for accessing members of an #Ex_ex struct's ex_cont union. @ingroup expr 00035 #define ex_op ex_cont.op ///< shortcut for accessing members of an #Ex_ex struct's ex_cont union. @ingroup expr 00036 #define ex_ptr ex_cont.ptr ///< shortcut for accessing members of an #Ex_ex struct's ex_cont union. @ingroup expr 00037 #define exNULL ((struct ex_ex *)0) 00038 00039 00040 /** ex_ex. 00041 @ingroup expr 00042 */ 00043 typedef struct ex_ex { 00044 union { 00045 long v_int; 00046 float v_flt; 00047 long op; 00048 char **ptr; 00049 } ex_cont; ///< content 00050 long ex_type; ///< type of the node 00051 } Ex_ex; 00052 00053 00054 00055 /** Defines for ex_type. 00056 We treat parenthesis and brackets special to keep a pointer to their match in the content. 00057 @ingroup expr 00058 */ 00059 typedef enum { 00060 ET_INT = 0x1, ///< an int 00061 ET_FLT = 0x2, ///< a float 00062 ET_OP = 0x3, ///< operator 00063 ET_STR = 0x4, ///< string 00064 ET_TBL = 0x5, ///< a table, the content is a pointer 00065 ET_FUNC = 0x6, ///< a function 00066 ET_SYM = 0x7, ///< symbol ("string") 00067 ET_VSYM = 0x8, ///< variable symbol ("$s?") 00068 ET_LP = 0x9, ///< left parenthesis 00069 ET_LB = 0x10, ///< left bracket 00070 ET_II = 0x11, ///< and integer inlet 00071 ET_FI = 0x12, ///< float inlet 00072 ET_SI = 0x13 ///< string inlet 00073 } e_max_expr_types; 00074 00075 00076 00077 00078 /** Struct for an instance of expr. 00079 @ingroup expr 00080 */ 00081 typedef struct expr { 00082 struct object exp_ob; 00083 void *exp_outlet; 00084 struct ex_ex **exp_stack; 00085 struct ex_ex exp_var[MAX_VARS]; 00086 struct ex_ex exp_res; ///< the result of last evaluation 00087 } Expr, t_expr; 00088 00089 00090 /** Create a new expr object. 00091 @ingroup expr 00092 00093 @param argc Count of arguments in argv. 00094 @param argv Arguments that are used to create the expr. See the example below for details. 00095 @param types A pre-existing array of nine t_atoms, that will hold the 00096 types of any variable arguments created in the expr. 00097 The types are returned in the a_type field of each 00098 #t_atom. If an argument was not present, #A_NOTHING is returned. 00099 @return expr_new() creates an expr object from the arguments in argv and 00100 returns the type of any expr-style arguments contained in argv (i.e. 00101 $i1, etc.) in atoms in an array pointed to by types. 00102 00103 @remark types should already exist as an array of nine Atoms, all of which will be filled in by 00104 expr_new(). If an argument was not present, it will set to type 00105 #A_NOTHING. For example, suppose argv pointed to the following atoms: 00106 @code 00107 $i1 (A_SYM) 00108 + (A_SYM) 00109 $f3 (A_SYM) 00110 + (A_SYM) 00111 3 (A_LONG) 00112 @endcode 00113 00114 After calling expr_new, types would contain the following: 00115 @code 00116 Index Argument Type Value 00117 0 1 ($i1) A_LONG 0 00118 1 2 A_NOTHING 0 00119 2 3 ($f3) A_FLOAT 0.0 00120 3 4 A_NOTHING 0 00121 4 5 A_NOTHING 0 00122 5 6 A_NOTHING 0 00123 6 7 A_NOTHING 0 00124 7 8 A_NOTHING 0 00125 8 9 A_NOTHING 0 00126 @endcode 00127 */ 00128 void *expr_new(short argc, t_atom *argv, t_atom *types); 00129 00130 00131 /** Evaluate an expression in an expr object. 00132 @ingroup expr 00133 00134 @param x The expr object to evaluate. 00135 @param argc Count of arguments in argv. 00136 @param argv Array of nine Atoms that will be substituted for 00137 variable arguments (such as $i1) in the expression. 00138 Unused arguments should be of type #A_NOTHING. 00139 @param result A pre-existing Atom that will hold the type and value 00140 of the result of evaluating the expression. 00141 @return . 00142 00143 @remark Evaluates the expression in an expr object with arguments in argv and 00144 returns the type and value of the evaluated expression as a t_atom in 00145 result. result need only point to a single #t_atom, but argv should 00146 contain at least argc Atoms. If, as in the example shown above under 00147 expr_new(), there are “gaps” between arguments, they should be filled 00148 in with t_atom of type #A_NOTHING. 00149 */ 00150 short expr_eval(t_expr *x, short argc, t_atom *argv, t_atom *result); 00151 00152 00153 void expr_install(exprmethod fun, const char *buf, short argc); 00154 00155 00156 00157 #if C74_PRAGMA_STRUCT_PACKPUSH 00158 #pragma pack(pop) 00159 #elif C74_PRAGMA_STRUCT_PACK 00160 #pragma pack() 00161 #endif 00162 00163 #ifdef __cplusplus 00164 } 00165 #endif 00166 00167 #endif // _EXT_EXPR_H_
Copyright © 2008, Cycling '74