[Gridflow-cvs] [svn] commit: r6350 - in /trunk: doc/flow_classes/#expr-help.pd src/expr.cxx tests/#expr-test.pd

svn-gridflow at artengine.ca svn-gridflow at artengine.ca
Fri Sep 24 14:29:31 EDT 2010


Author: matju
Date: Fri Sep 24 14:29:29 2010
New Revision: 6350

Log:
add support for sin() cos() exp() log() and document recent changes

Modified:
    trunk/doc/flow_classes/#expr-help.pd
    trunk/src/expr.cxx
    trunk/tests/#expr-test.pd

Modified: trunk/doc/flow_classes/#expr-help.pd
==============================================================================
--- trunk/doc/flow_classes/#expr-help.pd (original)
+++ trunk/doc/flow_classes/#expr-help.pd Fri Sep 24 14:29:29 2010
@@ -1,38 +1,54 @@
-#N canvas 467 70 632 446 10;
+#N canvas 467 70 632 582 10;
 #X obj 0 0 doc_h;
-#X obj 3 392 doc_also;
-#X obj 0 412 doc_f;
-#X obj 3 180 doc_c 1..;
-#X obj 14 210 doc_cc any;
-#X obj 97 210 doc_m cany <atom>;
-#X text 232 210 floats and symbols (and commas and semicolons) that
+#X obj 3 528 doc_also;
+#X obj 0 548 doc_f;
+#X obj 3 267 doc_c 1..;
+#X obj 14 297 doc_cc any;
+#X obj 97 297 doc_m cany <atom>;
+#X text 232 297 floats and symbols (and commas and semicolons) that
 will be joined together to form a string which will be interpreted
 in a manner quite similar to what [expr] does.;
-#X obj 3 268 doc_i 1;
-#X obj 14 298 doc_ii 0;
-#X obj 97 298 doc_m i0 bang;
-#X text 232 298 evaluate expression and output result.;
-#X obj 3 330 doc_o 1;
-#X obj 14 360 doc_oo 0;
-#X obj 97 360 doc_m o0 float;
-#X text 232 360 the result.;
-#X obj 103 392 expr;
-#X obj 139 392 expr~;
-#X obj 181 392 fexpr~;
+#X obj 14 385 doc_ii 0;
+#X obj 97 385 doc_m i0 bang;
+#X text 232 381 evaluate expression and output result.;
+#X obj 103 528 expr;
+#X obj 139 528 expr~;
+#X obj 181 528 fexpr~;
 #X obj 98 43 #expr;
 #X text 135 43 computes the result of the formula(s) you write in it.
 ;
-#X text 96 128 it supports C-like operator precedence \, and the use
+#X text 96 142 it supports C-like operator precedence \, and the use
 of () for overriding operator precedence.;
-#X text 96 157 it supports float literals \, hex literals.;
+#X text 96 171 it supports float literals \, hex literals.;
 #X text 96 62 it supports those infix operators: + - * / & ^ | << >>
 ;
 #X text 96 78 and these: == != < <= > >= && ||;
 #X text 118 94 unlike [expr] \, a||b may return non-bool b;
 #X text 118 108 unlike [expr] \, a&&b may return non-bool a;
-#X connect 1 1 15 0;
-#X connect 1 1 16 0;
-#X connect 1 1 17 0;
+#X text 118 123 unlike [expr] \, a%b is like [# %] and [mod] \, not
+like [%].;
+#X text 96 188 it supports $f1 \, $f2 \, $f3 \, etc.;
+#X text 96 203 it does not support $s1 \, $v1 \, etc.;
+#X obj 3 355 doc_i 1..;
+#X obj 97 402 doc_m i0 float;
+#X text 232 396 set value of $f1 and then do like bang.;
+#X obj 14 399 doc_ii other;
+#X obj 97 399 doc_m iother float;
+#X text 232 399 set value of any other $f variable \, such as $f2 \,
+$f3 \, etc. \; for example \, a float into inlet 6 will set $f7.;
+#X obj 3 440 doc_o 1..;
+#X obj 14 470 doc_oo any;
+#X obj 97 470 doc_m oany float;
+#X text 232 470 the results \, output from right to left \, even though
+they get computed left-to-right. [#expr] makes a list of the results
+and then outputs it in reverse order.;
+#X text 97 219 it supports some functions that have a single arguments
+\, which are the following : sin cos exp log;
+#X connect 1 1 10 0;
+#X connect 1 1 11 0;
+#X connect 1 1 12 0;
 #X connect 5 1 6 0;
-#X connect 9 1 10 0;
-#X connect 13 1 14 0;
+#X connect 8 1 9 0;
+#X connect 25 1 26 0;
+#X connect 28 1 29 0;
+#X connect 32 1 33 0;

Modified: trunk/src/expr.cxx
==============================================================================
--- trunk/src/expr.cxx (original)
+++ trunk/src/expr.cxx Fri Sep 24 14:29:29 2010
@@ -32,7 +32,7 @@
 	#define A_OPEN  t_atomtype(0x1000) /* only between next() and parse() */
 	#define A_CLOSE t_atomtype(0x1001) /* only between next() and parse() */
 	#define A_VAR   t_atomtype(0x1002) /* for $f1-style variables, not other variables */
-	#define A_OP1   t_atomtype(0x1003) /* operator: unary prefix */
+	#define A_OP1   t_atomtype(0x1003) /* unary prefix operator or unary function */
 	#define A_OP    t_atomtype(0x1004) /* operator: binary infix, or not parsed yet */
 
 	string args;
@@ -66,6 +66,11 @@
 			tok = t_atom2(A_VAR,int(i-1+256*'f'));
 			if (int(inputs.size())<i) {inputs.resize(i); ninlets_set(i);}
 		}
+		else if (isalpha(*s)) {
+			char *e=(char *)s; while (isalpha(*e)) e++;
+			tok = symprintf("%.*s",e-s,s);
+			s=e;
+		}
 		else RAISE("syntax error at character '%c'",*s);
 		toks.push_back(tok);
 	}
@@ -81,10 +86,10 @@
            	next(s);
            top:
 		switch (int(tok.a_type)) {
-		  case A_OP: {
+		  case A_OP: { // unary
 			t_symbol *o = tok.a_symbol;
 			if      (o==gensym("+")) {parse(level,t_atom2(A_OP1,tok.a_symbol));}
-			else if (o==gensym("-")) {parse(level,t_atom2(A_OP1,tok.a_symbol));}
+			else if (o==gensym("-")) {parse(level,t_atom2(A_OP1,gensym("inv+")));}
 			else if (o==gensym("!")) {parse(level,t_atom2(A_OP1,gensym("==")));}
 			else if (o==gensym("~")) {parse(level,t_atom2(A_OP1,tok.a_symbol));}
 			else RAISE("can't use '%s' as a unary prefix operator",tok.a_symbol->s_name);
@@ -94,7 +99,7 @@
 		  infix: // this section could become another method
 			next(s);
 			switch (int(tok.a_type)) {
-				case A_OP: {
+				case A_OP: { // binary
 					int priority1 = prevop.a_type!=A_NULL ? priorities[prevop] : 42;
 					int priority2 =                       priorities[tok];
 					if (!priority2) RAISE("unknown operator '%s'",tok.a_symbol->s_name);
@@ -106,6 +111,17 @@
 						parse(level,tok);
 						if (prevop.a_type!=A_NULL) add(prevop);
 					}
+				} break;
+				case A_OPEN: { // function (1 arg only)
+					t_atom2 a = code.back(); code.pop_back();
+					if (a.a_type!=A_SYMBOL) {
+						string z=tok.to_s(), zt=atomtype_to_s(tok.a_type);
+						RAISE("syntax error (%dc) tok=%s type=%s",level,z.data(),zt.data());
+					}
+					parse(level);
+					a.a_type = A_OP1;
+					if (!priorities[a]) RAISE("unknown function '%s'",a.a_symbol->s_name);
+					code.push_back(a);
 				} break;
 				case A_CLOSE: case A_NULL: case A_SEMI: {
 					if (level && int(tok.a_type)!=A_CLOSE) RAISE("missing ')' %d times",level);
@@ -159,9 +175,8 @@
 			  } break;
 			  case A_OP1: {
 				  Numop *op = TO(Numop *,t_atom2(code[i].a_symbol->s_name));
-				  float b = stack.back(); stack.pop_back();
-				  float a = 0;
-				  op->map(1,&a,b);
+				  float a = stack.back(); stack.pop_back();
+				  op->map(1,&a,0.f);
 				  stack.push_back(a);
 			  } break;
 			  default: {
@@ -184,7 +199,8 @@
 void startup_classes4 () {
 	#define PR1(SYM) priorities[t_atom2(A_OP1,gensym(#SYM))]
 	#define PR(SYM)  priorities[t_atom2(A_OP ,gensym(#SYM))]
-	PR1(+) = PR1(-) = PR1(~) = PR1(==) = 3; // unary "==" is "!"
+	PR1(sin) = PR1(cos) = PR1(exp) = PR1(log) = 2;
+	PR1(+) = PR1(inv+) = PR1(~) = PR1(==) = 3; // unary "==" is "!"; unary "-" is "inv+"
 	PR(*) = PR(/) = PR(%) = 5;
 	PR(+) = PR(-) = 6;
 	PR(<<) = PR(>>) = 7;

Modified: trunk/tests/#expr-test.pd
==============================================================================
--- trunk/tests/#expr-test.pd (original)
+++ trunk/tests/#expr-test.pd Fri Sep 24 14:29:29 2010
@@ -1,2 +1,152 @@
-#N canvas 529 150 450 300 10;
-#X obj 30 30 #expr $f2+($f1-$f4)*($f5-$f4)/($f3-$f2);
+#N canvas 545 149 533 523 10;
+#X obj 57 11 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 57 47 display;
+#X obj 57 28 #expr 2+2;
+#X obj 309 11 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 309 47 display;
+#X obj 309 28 #expr 5+5*12+12;
+#X text 79 47 4;
+#X text 212 47 889;
+#X obj 117 11 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 117 47 display;
+#X text 139 47 4;
+#X obj 117 28 #expr (2+2);
+#X text 332 47 77;
+#X obj 405 10 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 405 46 display;
+#X text 428 46 169;
+#X obj 405 27 #expr 5*5+12*12;
+#X obj 9 81 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 9 98 #expr 73%15;
+#X obj 9 117 display;
+#X obj 81 80 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 81 116 display;
+#X obj 81 97 #expr 0x5555 | 0xAAAA;
+#X text 35 117 13;
+#X text 117 116 65535;
+#X obj 213 80 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 213 116 display;
+#X obj 213 97 #expr 4<5;
+#X obj 273 79 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 273 115 display;
+#X obj 273 96 #expr 4<<5;
+#X obj 339 79 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 339 115 display;
+#X obj 339 96 #expr 0||3;
+#X obj 301 208 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144
+-1 -1;
+#X obj 301 244 display;
+#X obj 9 11 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 9 28 #expr 2;
+#X obj 7 293 display;
+#X obj 9 47 display;
+#X text 236 116 1;
+#X text 308 115 128;
+#X text 368 115 3;
+#X obj 189 11 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 189 47 display;
+#X obj 189 28 #expr 1000-100-10-1;
+#X obj 405 78 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 405 114 display;
+#X text 434 114 3;
+#X obj 405 95 #expr 42!=43;
+#X obj 9 146 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 9 182 display;
+#X obj 9 163 #expr -5+3;
+#X text 29 182 -2;
+#X obj 75 145 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1
+-1;
+#X obj 75 181 display;
+#X obj 75 162 #expr + + + 4;
+#X text 105 181 4;
+#X obj 159 145 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144
+-1 -1;
+#X obj 159 181 display;
+#X obj 159 162 #expr - - + - - + - 4;
+#X text 189 181 -4;
+#X obj 299 147 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144
+-1 -1;
+#X obj 299 183 display;
+#X obj 299 164 #expr ~9;
+#X obj 353 147 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144
+-1 -1;
+#X obj 353 183 display;
+#X obj 353 164 #expr !0;
+#X obj 301 225 #expr -14%13.51;
+#X obj 407 164 #expr 2 \; 3 \; 5 \; 7;
+#X obj 407 244 print;
+#X obj 407 147 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144
+-1 -1;
+#X obj 7 240 unpack 0 0 0 0 0;
+#X msg 7 221 \$1 3 5 7 11;
+#X floatatom 7 203 5 0 0 0 - - -;
+#X obj 7 274 #expr $f4+($f5-$f4)*($f1-$f2)/($f3-$f2);
+#X floatatom 296 312 5 0 0 0 - - -;
+#X obj 296 349 display;
+#X text 327 182 -10;
+#X text 379 182 1;
+#X obj 296 330 #expr log($f1*3);
+#X connect 0 0 2 0;
+#X connect 2 0 1 0;
+#X connect 3 0 5 0;
+#X connect 5 0 4 0;
+#X connect 8 0 11 0;
+#X connect 11 0 9 0;
+#X connect 13 0 16 0;
+#X connect 16 0 14 0;
+#X connect 17 0 18 0;
+#X connect 18 0 19 0;
+#X connect 20 0 22 0;
+#X connect 22 0 21 0;
+#X connect 25 0 27 0;
+#X connect 27 0 26 0;
+#X connect 28 0 30 0;
+#X connect 30 0 29 0;
+#X connect 31 0 33 0;
+#X connect 33 0 32 0;
+#X connect 34 0 68 0;
+#X connect 36 0 37 0;
+#X connect 37 0 39 0;
+#X connect 43 0 45 0;
+#X connect 45 0 44 0;
+#X connect 46 0 49 0;
+#X connect 49 0 47 0;
+#X connect 50 0 52 0;
+#X connect 52 0 51 0;
+#X connect 54 0 56 0;
+#X connect 56 0 55 0;
+#X connect 58 0 60 0;
+#X connect 60 0 59 0;
+#X connect 62 0 64 0;
+#X connect 64 0 63 0;
+#X connect 65 0 67 0;
+#X connect 67 0 66 0;
+#X connect 68 0 35 0;
+#X connect 69 0 70 0;
+#X connect 69 1 70 0;
+#X connect 69 2 70 0;
+#X connect 69 3 70 0;
+#X connect 71 0 69 0;
+#X connect 72 0 75 0;
+#X connect 72 1 75 1;
+#X connect 72 2 75 2;
+#X connect 72 3 75 3;
+#X connect 72 4 75 4;
+#X connect 73 0 72 0;
+#X connect 74 0 73 0;
+#X connect 75 0 38 0;
+#X connect 76 0 80 0;
+#X connect 80 0 77 0;



More information about the Gridflow-cvs mailing list