22
33import io .polypen .Expressions .Expression ;
44import io .polypen .Expressions .Product ;
5+ import io .polypen .Expressions .Sum ;
56import org .apache .commons .numbers .fraction .Fraction ;
67
78import java .util .ArrayList ;
1213final class Parser {
1314
1415 static List <Fraction > parsePolynomial (String s ) {
15- List <SignedToken > strings = split (s .trim ());
16+ List <SignedString > strings = split (s .trim ());
1617 TreeMap <Integer , Fraction > cof = new TreeMap <>();
17- for (SignedToken term : strings ) {
18+ for (SignedString term : strings ) {
1819 String [] tokens = term .token .split ("[a-z]" , 3 );
1920 String coefficient = tokens [0 ].replace ("*" , "" ).trim ();
2021 if (coefficient .isEmpty ()) {
@@ -45,24 +46,44 @@ static List<Fraction> parsePolynomial(String s) {
4546 return result ;
4647 }
4748
48- static Expression parseProduct (String s ) {
49- List <String > result = new ArrayList <>();
49+ static Expression parse (String s ) {
50+ List <SignedString > result = new ArrayList <>();
5051 StringBuilder sb = new StringBuilder ();
5152 int nestingLevel = -1 ;
53+ Type outerop = Type .PRODUCT ;
54+ Sign sign = Sign .PLUS ;
5255 for (int i = 0 ; i < s .length (); i ++) {
5356 char c = s .charAt (i );
5457 switch (c ) {
5558 case '(' -> nestingLevel = Math .max (0 , nestingLevel ) + 1 ;
5659 case ')' -> {
5760 nestingLevel --;
5861 if (!sb .isEmpty () && nestingLevel == 0 ) {
59- result .add (sb .toString ());
62+ result .add (new SignedString ( sign , sb .toString () ));
6063 sb .setLength (0 );
64+ sign = Sign .PLUS ;
6165 }
6266 if (nestingLevel < 0 ) {
6367 throw new IllegalStateException ("Illegal nesting" );
6468 }
6569 }
70+ case '-' -> {
71+ if (nestingLevel == 0 ) {
72+ outerop = Type .SUM ;
73+ }
74+ if (nestingLevel != 0 ) {
75+ sb .append (c );
76+ }
77+ sign = Sign .MINUS ;
78+ }
79+ case '+' -> {
80+ if (nestingLevel == 0 ) {
81+ outerop = Type .SUM ;
82+ }
83+ if (nestingLevel != 0 ) {
84+ sb .append (c );
85+ }
86+ }
6687 default -> {
6788 if (nestingLevel != 0 ) {
6889 sb .append (c );
@@ -74,9 +95,12 @@ static Expression parseProduct(String s) {
7495 throw new IllegalStateException ("Illegal nesting" );
7596 }
7697 if (!sb .isEmpty ()) {
77- result .add (sb .toString ());
98+ result .add (new SignedString ( sign , sb .toString () ));
7899 }
79- return new Product (result );
100+ return switch (outerop ) {
101+ case PRODUCT -> new Product (result );
102+ case SUM -> new Sum (result );
103+ };
80104 }
81105
82106 static Type outerop (String s ) {
@@ -119,26 +143,26 @@ enum Sign {
119143 }
120144 }
121145
122- record SignedToken (Sign sign , String token ) {
146+ public record SignedString (Sign sign , String token ) {
123147 }
124148
125- private static List <SignedToken > split (String s ) {
126- List <SignedToken > result = new ArrayList <>();
149+ private static List <SignedString > split (String s ) {
150+ List <SignedString > result = new ArrayList <>();
127151 Sign sign = Sign .PLUS ;
128152 int pos = -1 ;
129153 for (int i = 0 ; i < s .length (); i ++) {
130154 if (s .charAt (i ) == '-' ) {
131- result .add (new SignedToken (sign , s .substring (pos + 1 , i ).trim ()));
155+ result .add (new SignedString (sign , s .substring (pos + 1 , i ).trim ()));
132156 sign = Sign .MINUS ;
133157 pos = i ;
134158 } else if (s .charAt (i ) == '+' ) {
135- result .add (new SignedToken (sign , s .substring (pos + 1 , i ).trim ()));
159+ result .add (new SignedString (sign , s .substring (pos + 1 , i ).trim ()));
136160 sign = Sign .PLUS ;
137161 pos = i ;
138162 }
139163 }
140164 if (pos < s .length () - 1 ) {
141- result .add (new SignedToken (sign , s .substring (pos + 1 ).trim ()));
165+ result .add (new SignedString (sign , s .substring (pos + 1 ).trim ()));
142166 }
143167 return result ;
144168 }
0 commit comments