@@ -171,9 +171,12 @@ namespace Pebbles {
171171 exp = exp. strip ();
172172 exp = space_removal (exp);
173173
174- // Take care of unary subtraction
175- exp = uniminus_convert (exp);
176- // stdout.printf ("'%s'\n", exp);
174+ // Intelligently convert expressions based on common rules
175+ exp = algebraic_parenthesis_product_convert (exp);
176+ exp = unary_minus_convert (exp);
177+
178+ // exp = space_removal (exp);
179+ print (" Final exp: " + exp + " \n " );
177180 return exp;
178181 }
179182 else {
@@ -199,27 +202,65 @@ namespace Pebbles {
199202 }
200203 return result;
201204 }
202- private static string uniminus_convert (string exp ) {
205+ private static string unary_minus_convert (string exp ) {
203206 print(" >%s <\n " , exp);
204207 string uniminus_converted = " " ;
205208 string [] tokens = exp. split (" " );
206209 for (int i = 0 ; i < tokens. length; i++ ) {
207210 if (tokens[i] == " -" ) {
208- print(" token: %d \n " , i );
211+ print(" token: %s \n " , tokens[i + 1 ] );
209212 if (i == 0 ) {
210- tokens [i] = " u" ;
213+ if (i < tokens. length) {
214+ tokens [i] = " ( 0 u" ;
215+ tokens [i + 1 ] = tokens [i + 1 ] + " )" ;
216+ }
211217 } else if (tokens [i - 1 ] == " )" || tokens [i - 1 ] == " x" || is_number (tokens [i - 1 ]) ) {
212218 tokens [i] = " -" ;
213219 } else {
214- tokens [i] = " u" ;
220+ if (i < tokens. length) {
221+ tokens [i] = " ( 0 u" ;
222+ tokens [i + 1 ] = tokens [i + 1 ] + " )" ;
223+ }
215224 }
216225 }
217226 }
218227 uniminus_converted = string . joinv (" " , tokens);
219- uniminus_converted = uniminus_converted. replace (" u" , " 0 u" );
220- print(" converted: %s\n " , uniminus_converted);
228+ // uniminus_converted = uniminus_converted.replace ("u", "0 u");
229+ print(" unary converted: %s\n " , uniminus_converted);
221230 return uniminus_converted;
222231 }
232+
233+ public static string algebraic_variable_product_convert (string exp ) {
234+ string converted_exp = " " ;
235+ string [] tokens = exp. replace(" x" , " x " ). split (" " );
236+ for (int i = 1 ; i < tokens. length; i++ ) {
237+ if (tokens[i] == " x" && is_number(tokens[i - 1 ]) && tokens[i - 1 ] != " (" ) {
238+ tokens[i] = " * x" ;
239+ }
240+ }
241+ converted_exp = space_removal(string . joinv (" " , tokens));
242+ // print("algebraic converted: %s\n", converted_exp);
243+ return converted_exp;
244+ }
245+
246+ public static string algebraic_parenthesis_product_convert (string exp ) {
247+ string [] tokens = exp. split (" " );
248+ for (int i = 1 ; i < tokens. length - 1 ; i++ ) {
249+ if (tokens[i] == " (" ) {
250+ if (is_number (tokens[i - 1 ])) {
251+ tokens[i] = " * (" ;
252+ }
253+ }
254+ if (tokens[i] == " )" ) {
255+ if (is_number (tokens[i + 1 ]) || tokens[i + 1 ] == " (" ) {
256+ tokens[i] = " ) *" ;
257+ }
258+ }
259+ }
260+ string converted_exp = space_removal(string . joinv (" " , tokens));
261+ print(" algebraic converted: %s\n " , converted_exp);
262+ return converted_exp;
263+ }
223264
224265 private static bool is_number (string exp ) {
225266 if (exp. has_suffix (" 0" ) ||
@@ -232,7 +273,8 @@ namespace Pebbles {
232273 exp. has_suffix (" 7" ) ||
233274 exp. has_suffix (" 8" ) ||
234275 exp. has_suffix (" 9" ) ||
235- exp. has_suffix (" ." )
276+ exp. has_suffix (" ." ) ||
277+ exp. has_suffix (" x" )
236278 ) {
237279 return true ;
238280 }
0 commit comments