11package io .polypen .parse ;
22
3- import io .polypen .parse .Parser .BindingMinusExpr ;
43import io .polypen .parse .Parser .Expr ;
54import io .polypen .parse .Parser .ListExpr ;
65import io .polypen .parse .Parser .MinusExpr ;
76import io .polypen .parse .Parser .MultExpr ;
87import io .polypen .parse .Parser .MultListExpr ;
8+ import io .polypen .parse .Parser .NumberExpr ;
99import io .polypen .parse .Parser .PlusExpr ;
1010import io .polypen .parse .Parser .PlusListExpr ;
1111
12- import java .util .ArrayList ;
1312import java .util .List ;
1413
1514public class Macro {
1615
17- public static Expr minusMacro (Expr exprs ) {
18- if (exprs .size () == 1 ) {
19- return exprs ;
20- }
21- Expr previous = null ;
22- List <Expr > result = new ArrayList <>(exprs .size ());
23- for (Expr expr : exprs .getExprs ()) {
24- if (previous instanceof MinusExpr ) {
25- result .add (BindingMinusExpr .of (minusMacro (expr )));
26- } else {
27- if (!(expr instanceof MinusExpr )) {
28- result .add (minusMacro (expr ));
29- }
30- }
31- previous = expr ;
32- }
33- return new ListExpr (result );
34- }
16+ public static final int B_STRONG = 4 ;
17+ public static final int B_MINUSBOUND = 16 ;
18+ public static final int B_END = 1 ;
3519
3620 public static Expr applyStarMacro (List <Expr > exprs ) {
3721 if (exprs .size () == 1 ) {
@@ -44,35 +28,47 @@ public static Expr applyStarMacro(List<Expr> exprs) {
4428 Expr left = exprs .get (i );
4529 Expr right = exprs .get (i + 1 );
4630 if (isStrong (left , right )) {
47- bound [i ] = 1 ;
48- bound [i + 1 ] = 1 ;
49- } else {
50- bound [i ] *= 2 ;
31+ bound [i ] |= B_STRONG ;
32+ bound [i + 1 ] |= B_STRONG ;
33+ if (left instanceof MinusExpr ) {
34+ bound [i + 1 ] |= B_MINUSBOUND ;
35+ }
36+ } else if ((bound [i ] & B_STRONG ) != 0 ) {
37+ bound [i ] |= B_END ;
5138 }
5239 }
5340 for (int i = 0 ; i < exprs .size (); i ++) {
5441 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 ();
42+ int b = bound [i ];
43+ if ((b & B_STRONG ) != 0 ) {
44+ if ((b & B_MINUSBOUND ) != 0 ) {
45+ region .add (MultListExpr .of (NumberExpr .of (-1 ), expandRecursively (expr )));
46+ } else {
47+ region .add (expandRecursively (expr ));
48+ }
49+ if ((b & B_END ) != 0 ) {
50+ exprsCopy .add (unwrap (region .copy ()));
51+ region .clear ();
52+ }
6153 } else {
6254 if (!region .isEmpty ()) {
63- exprsCopy .add (region .copy ());
55+ exprsCopy .add (unwrap ( region .copy () ));
6456 region .clear ();
6557 }
6658 exprsCopy .add (expandRecursively (expr ));
6759 }
6860 }
6961 if (exprsCopy .isEmpty ()) {
70- return region ;
62+ return unwrap ( region ) ;
7163 }
7264 if (!region .isEmpty ()) {
73- exprsCopy .add (region );
65+ exprsCopy .add (unwrap ( region ) );
7466 }
75- return exprsCopy ;
67+ return unwrap (exprsCopy );
68+ }
69+
70+ private static Expr unwrap (Expr expr ) {
71+ return expr .size () == 1 ? expr .getFirst () : expr ;
7672 }
7773
7874 private static Expr expandRecursively (Expr expr ) {
@@ -81,7 +77,6 @@ private static Expr expandRecursively(Expr expr) {
8177 }
8278 return switch (expr ) {
8379 case ListExpr x -> applyStarMacro (x .value ());
84- case BindingMinusExpr x -> BindingMinusExpr .of (expandRecursively (x .expr ()));
8580 default -> expr ;
8681 };
8782 }
@@ -93,7 +88,10 @@ public static boolean isStrong(Expr left, Expr right) {
9388 if (left instanceof PlusExpr || right instanceof PlusExpr ) {
9489 return false ;
9590 }
96- if (right instanceof BindingMinusExpr ) {
91+ if (left instanceof MinusExpr ) {
92+ return true ;
93+ }
94+ if (right instanceof MinusExpr ) {
9795 return false ;
9896 }
9997 return true ;
0 commit comments