[Gridflow-cvs] [svn] commit: r6336 - /trunk/src/expr.cxx

svn-gridflow at artengine.ca svn-gridflow at artengine.ca
Fri Sep 24 11:51:50 EDT 2010


Author: matju
Date: Fri Sep 24 11:51:48 2010
New Revision: 6336

Log:
support unary + -

Modified:
    trunk/src/expr.cxx

Modified: trunk/src/expr.cxx
==============================================================================
--- trunk/src/expr.cxx (original)
+++ trunk/src/expr.cxx Fri Sep 24 11:51:48 2010
@@ -20,7 +20,12 @@
 
 #include "gridflow.hxx.fcs"
 
-map<t_symbol *, int> priorities;
+bool operator < (const t_atom2 &a, const t_atom2 &b) {
+	if (a.a_type!=b.a_type) return a.a_type<b.a_type;
+	return uintptr_t(a.a_symbol)<uintptr_t(b.a_symbol);
+}
+	
+map<t_atom2, int> priorities;
 
 // FOR FUTURE USE
 \class GFExpr : FObject {
@@ -59,11 +64,9 @@
            top:
 		switch (int(tok.a_type)) {
 		  case A_OP:
-			if (tok==gensym("+")) {}
-			if (tok==gensym("-")) {
-				parse(s,level,t_atom2(A_OP1,tok.a_symbol));
-			}
-			
+			if      (tok.a_symbol==gensym("+")) {parse(s,level,t_atom2(A_OP1,tok.a_symbol));}
+			else if (tok.a_symbol==gensym("-")) {parse(s,level,t_atom2(A_OP1,tok.a_symbol));}
+			else RAISE("can't use '%s' as a unary prefix operator",tok.a_symbol->s_name);
 			break;
 		  case A_FLOAT: case A_SYMBOL: case A_VAR:
 			code.push_back(tok);
@@ -71,9 +74,10 @@
 			next(s);
 			switch (int(tok.a_type)) {
 				case A_OP: {
-					int priority1 = prevop.a_type!=A_NULL ? priorities[prevop.a_symbol] : 42;
-					int priority2 =                       priorities[tok.a_symbol];
+					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);
+					//post("priorities %d %d",priority1,priority2);
 					if (priority1 <= priority2) {
 						code.push_back(prevop);
 						parse(s,level,tok);
@@ -131,6 +135,13 @@
 				  op->map(1,&a,b);
 				  stack.push_back(a);
 			  } 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);
+				  stack.push_back(a);
+			  } break;
 			  default: {
 				string z = code[i].to_s();
 				RAISE("can't interpret %s (atomtype %s)",z.data(),atomtype_to_s(code[i].a_type).data());
@@ -143,7 +154,9 @@
 \end class {install("#expr",1,1,CLASS_NOPARENS);}
 
 void startup_classes4 () {
-	#define PR(SYM) priorities[gensym(#SYM)]
+	#define PR1(SYM) priorities[t_atom2(A_OP1,gensym(#SYM))]
+	#define PR(SYM)  priorities[t_atom2(A_OP ,gensym(#SYM))]
+	PR1(-) = 3;
 	PR(*) = PR(/) = PR(%) = 5;
 	PR(+) = PR(-) = 6;
 	PR(<<) = PR(>>) = 7;



More information about the Gridflow-cvs mailing list