66import io .polypen .parse .Parser .MinusExpr ;
77import io .polypen .parse .Parser .MultExpr ;
88import io .polypen .parse .Parser .MultListExpr ;
9- import io .polypen .parse .Parser .NumberExpr ;
109import io .polypen .parse .Parser .PlusExpr ;
1110import io .polypen .parse .Parser .PlusListExpr ;
12- import io .polypen .parse .Parser .VarExp ;
1311
1412import java .util .ArrayList ;
1513import java .util .List ;
1614
1715public class Macro {
1816
19- static Expr minusMacro (Expr exprs ) {
17+ public static Expr minusMacro (Expr exprs ) {
2018 if (exprs .size () == 1 ) {
2119 return exprs ;
2220 }
23- Expr old = null ;
2421 Expr previous = null ;
2522 List <Expr > result = new ArrayList <>(exprs .size ());
2623 for (Expr expr : exprs .getExprs ()) {
2724 if (previous instanceof MinusExpr ) {
28- if (needsPlusInsert (old )) {
29- result .add (Parser .PLUS );
30- }
3125 result .add (BindingMinusExpr .of (minusMacro (expr )));
3226 } else {
3327 if (!(expr instanceof MinusExpr )) {
3428 result .add (minusMacro (expr ));
3529 }
3630 }
37- old = previous ;
3831 previous = expr ;
3932 }
4033 return new ListExpr (result );
4134 }
4235
43- private static boolean needsPlusInsert (Expr prev ) {
44- if (prev == null ) {
45- return false ;
46- }
47- return switch (prev ) {
48- case PlusExpr ignored -> false ;
49- case MultExpr ignored -> false ;
50- default -> true ;
51- };
52- }
53-
54- static Expr applyStarMacro (List <Expr > exprs ) {
36+ public static Expr applyStarMacro (List <Expr > exprs ) {
5537 if (exprs .size () == 1 ) {
5638 return expandRecursively (exprs .getFirst ());
5739 }
5840 PlusListExpr exprsCopy = PlusListExpr .create (exprs .size ());
5941 MultListExpr region = MultListExpr .create (exprs .size ());
60- Expr previous = null ;
61- for (Expr expr : exprs ) {
62- if (isStrongBind (previous ) && (isStrongBind (expr ) || !region .isEmpty ())) {
63- region .add (previous );
42+ int [] bound = new int [exprs .size ()];
43+ for (int i = 0 ; i < exprs .size () - 1 ; i ++) {
44+ Expr left = exprs .get (i );
45+ Expr right = exprs .get (i + 1 );
46+ if (isStrong (left , right )) {
47+ bound [i ] = 1 ;
48+ bound [i + 1 ] = 1 ;
49+ } else {
50+ bound [i ] *= 2 ;
51+ }
52+ }
53+ for (int i = 0 ; i < exprs .size (); i ++) {
54+ Expr expr = exprs .get (i );
55+ if (bound [i ] == 1 ) {
56+ region .add (expandRecursively (expr ));
57+ } else if (bound [i ] == 2 ) {
58+ region .add (expandRecursively (expr ));
59+ exprsCopy .add (region .copy ());
60+ region .clear ();
6461 } else {
6562 if (!region .isEmpty ()) {
6663 exprsCopy .add (region .copy ());
6764 region .clear ();
6865 }
69- if (previous != null ) {
70- exprsCopy .add (previous );
71- }
66+ exprsCopy .add (expandRecursively (expr ));
7267 }
73- previous = expandRecursively (expr );
7468 }
7569 if (exprsCopy .isEmpty ()) {
76- region .add (expandRecursively (previous ));
7770 return region ;
7871 }
79- if (region .isEmpty ()) {
80- exprsCopy .add (expandRecursively (previous ));
81- } else {
82- region .add (expandRecursively (previous ));
72+ if (!region .isEmpty ()) {
8373 exprsCopy .add (region );
8474 }
8575 return exprsCopy ;
@@ -91,24 +81,21 @@ private static Expr expandRecursively(Expr expr) {
9181 }
9282 return switch (expr ) {
9383 case ListExpr x -> applyStarMacro (x .value ());
94- case BindingMinusExpr x -> BindingMinusExpr .of (applyStarMacro ( List . of ( x .expr () )));
84+ case BindingMinusExpr x -> BindingMinusExpr .of (expandRecursively ( x .expr ()));
9585 default -> expr ;
9686 };
9787 }
9888
99- public static boolean isStrongBind (Expr expr ) {
100- if (expr == null ) {
89+ public static boolean isStrong (Expr left , Expr right ) {
90+ if (left instanceof MultExpr || right instanceof MultExpr ) {
91+ return true ;
92+ }
93+ if (left instanceof PlusExpr || right instanceof PlusExpr ) {
10194 return false ;
10295 }
103- return switch (expr ) {
104- case ListExpr ignored -> true ;
105- case PlusListExpr ignored -> true ;
106- case MultListExpr ignored -> true ;
107- case MultExpr ignored -> true ;
108- case NumberExpr ignored -> true ;
109- case VarExp ignored -> true ;
110- case BindingMinusExpr ignored -> true ;
111- default -> false ;
112- };
96+ if (right instanceof BindingMinusExpr ) {
97+ return false ;
98+ }
99+ return true ;
113100 }
114101}
0 commit comments