@@ -330,136 +330,69 @@ public static ParseTree rewrite(List<ParseTree> list, boolean returnSConcat,
330330 //Exponential
331331 for (int i = 0 ; i < list .size () - 1 ; i ++) {
332332 ParseTree next = list .get (i + 1 );
333- if (next .getData () instanceof CSymbol ) {
334- if (((CSymbol ) next .getData ()).isExponential ()) {
335- ParseTree conversion = new ParseTree (new CFunction (((CSymbol ) next .getData ()).convert (), next .getTarget ()), next .getFileOptions ());
336- conversion .addChild (list .get (i ));
337- conversion .addChild (list .get (i + 2 ));
338- list .set (i , conversion );
339- list .remove (i + 1 );
340- list .remove (i + 1 );
341- i --;
342- }
333+ if (next .getData () instanceof CSymbol sy && sy .isExponential ()) {
334+ rewriteBinaryOperator (list , sy , i --);
343335 }
344336 }
345-
346337 //Multiplicative
347338 for (int i = 0 ; i < list .size () - 1 ; i ++) {
348339 ParseTree next = list .get (i + 1 );
349- if (next .getData () instanceof CSymbol ) {
350- CSymbol nextData = (CSymbol ) next .getData ();
351- if (nextData .isMultaplicative () && !nextData .isAssignment ()) {
352- ParseTree conversion = new ParseTree (new CFunction (((CSymbol ) next .getData ()).convert (), next .getTarget ()), next .getFileOptions ());
353- conversion .addChild (list .get (i ));
354- conversion .addChild (list .get (i + 2 ));
355- list .set (i , conversion );
356- list .remove (i + 1 );
357- list .remove (i + 1 );
358- i --;
359- }
340+ if (next .getData () instanceof CSymbol sy && sy .isMultaplicative () && !sy .isAssignment ()) {
341+ rewriteBinaryOperator (list , sy , i --);
360342 }
361343 }
362344 //Additive
363345 for (int i = 0 ; i < list .size () - 1 ; i ++) {
364346 ParseTree next = list .get (i + 1 );
365- if (next .getData () instanceof CSymbol && ((CSymbol ) next .getData ()).isAdditive () && !((CSymbol ) next .getData ()).isAssignment ()) {
366- ParseTree conversion = new ParseTree (new CFunction (((CSymbol ) next .getData ()).convert (), next .getTarget ()), next .getFileOptions ());
367- conversion .addChild (list .get (i ));
368- conversion .addChild (list .get (i + 2 ));
369- list .set (i , conversion );
370- list .remove (i + 1 );
371- list .remove (i + 1 );
372- i --;
347+ if (next .getData () instanceof CSymbol sy && sy .isAdditive () && !sy .isAssignment ()) {
348+ rewriteBinaryOperator (list , sy , i --);
373349 }
374350 }
375351 //relational
376352 for (int i = 0 ; i < list .size () - 1 ; i ++) {
377- ParseTree node = list .get (i + 1 );
378- if (node .getData () instanceof CSymbol && ((CSymbol ) node .getData ()).isRelational ()) {
379- CSymbol sy = (CSymbol ) node .getData ();
380- ParseTree conversion = new ParseTree (new CFunction (sy .convert (), node .getTarget ()), node .getFileOptions ());
381- conversion .addChild (list .get (i ));
382- conversion .addChild (list .get (i + 2 ));
383- list .set (i , conversion );
384- list .remove (i + 1 );
385- list .remove (i + 1 );
386- i --;
353+ ParseTree next = list .get (i + 1 );
354+ if (next .getData () instanceof CSymbol sy && sy .isRelational ()) {
355+ rewriteBinaryOperator (list , sy , i --);
387356 }
388357 }
389358 //equality
390359 for (int i = 0 ; i < list .size () - 1 ; i ++) {
391- ParseTree node = list .get (i + 1 );
392- if (node .getData () instanceof CSymbol && ((CSymbol ) node .getData ()).isEquality ()) {
393- CSymbol sy = (CSymbol ) node .getData ();
394- ParseTree conversion = new ParseTree (new CFunction (sy .convert (), node .getTarget ()), node .getFileOptions ());
395- conversion .addChild (list .get (i ));
396- conversion .addChild (list .get (i + 2 ));
397- list .set (i , conversion );
398- list .remove (i + 1 );
399- list .remove (i + 1 );
400- i --;
360+ ParseTree next = list .get (i + 1 );
361+ if (next .getData () instanceof CSymbol sy && sy .isEquality ()) {
362+ rewriteBinaryOperator (list , sy , i --);
401363 }
402364 }
403365 // default and
404366 for (int i = 0 ; i < list .size () - 1 ; i ++) {
405- ParseTree node = list .get (i + 1 );
406- if (node .getData () instanceof CSymbol && ((CSymbol ) node .getData ()).isDefaultAnd ()) {
407- CSymbol sy = (CSymbol ) node .getData ();
408- ParseTree conversion = new ParseTree (new CFunction (sy .convert (), node .getTarget ()), node .getFileOptions ());
409- conversion .addChild (list .get (i ));
410- conversion .addChild (list .get (i + 2 ));
411- list .set (i , conversion );
412- list .remove (i + 1 );
413- list .remove (i + 1 );
414- i --;
367+ ParseTree next = list .get (i + 1 );
368+ if (next .getData () instanceof CSymbol sy && sy .isDefaultAnd ()) {
369+ rewriteBinaryOperator (list , sy , i --);
415370 }
416371 }
417-
418372 // default or
419373 for (int i = 0 ; i < list .size () - 1 ; i ++) {
420- ParseTree node = list .get (i + 1 );
421- if (node .getData () instanceof CSymbol && ((CSymbol ) node .getData ()).isDefaultOr ()) {
422- CSymbol sy = (CSymbol ) node .getData ();
423- ParseTree conversion = new ParseTree (new CFunction (sy .convert (), node .getTarget ()), node .getFileOptions ());
424- conversion .addChild (list .get (i ));
425- conversion .addChild (list .get (i + 2 ));
426- list .set (i , conversion );
427- list .remove (i + 1 );
428- list .remove (i + 1 );
429- i --;
374+ ParseTree next = list .get (i + 1 );
375+ if (next .getData () instanceof CSymbol sy && sy .isDefaultOr ()) {
376+ rewriteBinaryOperator (list , sy , i --);
430377 }
431378 }
432-
433379 //logical and
434380 for (int i = 0 ; i < list .size () - 1 ; i ++) {
435- ParseTree node = list .get (i + 1 );
436- if (node .getData () instanceof CSymbol && ((CSymbol ) node .getData ()).isLogicalAnd ()) {
437- CSymbol sy = (CSymbol ) node .getData ();
438- ParseTree conversion = new ParseTree (new CFunction (sy .convert (), node .getTarget ()), node .getFileOptions ());
439- conversion .addChild (list .get (i ));
440- conversion .addChild (list .get (i + 2 ));
441- list .set (i , conversion );
442- list .remove (i + 1 );
443- list .remove (i + 1 );
444- i --;
381+ ParseTree next = list .get (i + 1 );
382+ if (next .getData () instanceof CSymbol sy && sy .isLogicalAnd ()) {
383+ rewriteBinaryOperator (list , sy , i --);
445384 }
446385 }
447386 //logical or
448387 for (int i = 0 ; i < list .size () - 1 ; i ++) {
449- ParseTree node = list .get (i + 1 );
450- if (node .getData () instanceof CSymbol && ((CSymbol ) node .getData ()).isLogicalOr ()) {
451- CSymbol sy = (CSymbol ) node .getData ();
452- ParseTree conversion = new ParseTree (new CFunction (sy .convert (), node .getTarget ()), node .getFileOptions ());
453- conversion .addChild (list .get (i ));
454- conversion .addChild (list .get (i + 2 ));
455- list .set (i , conversion );
456- list .remove (i + 1 );
457- list .remove (i + 1 );
458- i --;
388+ ParseTree next = list .get (i + 1 );
389+ if (next .getData () instanceof CSymbol sy && sy .isLogicalOr ()) {
390+ rewriteBinaryOperator (list , sy , i --);
459391 }
460392 }
461393 } catch (IndexOutOfBoundsException e ) {
462- throw new ConfigCompileException ("Unexpected symbol (" + list .get (list .size () - 1 ).getData ().val () + "). Did you forget to quote your symbols?" , list .get (list .size () - 1 ).getTarget ());
394+ throw new ConfigCompileException ("Unexpected symbol (" + list .get (list .size () - 1 ).getData ().val () + ")" ,
395+ list .get (list .size () - 1 ).getTarget ());
463396 }
464397 }
465398
@@ -630,6 +563,25 @@ public static ParseTree rewrite(List<ParseTree> list, boolean returnSConcat,
630563 }
631564 }
632565
566+ private static void rewriteBinaryOperator (List <ParseTree > list , CSymbol symbol , int leftIndex ) throws ConfigCompileException {
567+ ParseTree left = list .get (leftIndex );
568+ if (left .getData () instanceof CSymbol ) {
569+ throw new ConfigCompileException ("Unexpected symbol (" + left .getData ().val () + ") before binary operator ("
570+ + list .get (leftIndex + 1 ).getData ().val () + ")" , left .getTarget ());
571+ }
572+ ParseTree right = list .get (leftIndex + 2 );
573+ if (right .getData () instanceof CSymbol ) {
574+ throw new ConfigCompileException ("Unexpected symbol (" + right .getData ().val () + ") after binary operator ("
575+ + list .get (leftIndex + 1 ).getData ().val () + ")" , right .getTarget ());
576+ }
577+ ParseTree conversion = new ParseTree (new CFunction (symbol .convert (), symbol .getTarget ()), left .getFileOptions ());
578+ conversion .addChild (left );
579+ conversion .addChild (right );
580+ list .set (leftIndex , conversion );
581+ list .remove (leftIndex + 1 );
582+ list .remove (leftIndex + 1 );
583+ }
584+
633585 private static void rewriteParenthesis (List <ParseTree > list ) throws ConfigCompileException {
634586 for (int listInd = list .size () - 1 ; listInd >= 1 ; listInd --) {
635587 Stack <ParseTree > executes = new Stack <>();
0 commit comments