11package net .marcellperger .mathexpr ;
22
33import net .marcellperger .mathexpr .util .Util ;
4+ import net .marcellperger .mathexpr .util .UtilCollectors ;
45import org .jetbrains .annotations .NotNull ;
56import org .jetbrains .annotations .Nullable ;
67
1819
1920public enum SymbolInfo {
2021 // Let's say that precedence 0 is for (parens) OR literals - TODO add a class?? but it wouldn't actually be used !
21- // POW(PowOperation.class, 1, GroupingDirection.RightToLeft, "**"),
22- MUL (MulOperation .class , 1 , GroupingDirection .LeftToRight , "*" , MulOperation ::new ),
23- DIV (DivOperation .class , 1 , GroupingDirection .LeftToRight , "/" , DivOperation ::new ),
22+ POW (PowOperation .class , 1 , GroupingDirection .RightToLeft , "**" , "" , PowOperation ::new ),
2423
25- ADD (AddOperation .class , 2 , GroupingDirection .LeftToRight , "+" , AddOperation ::new ),
26- SUB (SubOperation .class , 2 , GroupingDirection .LeftToRight , "-" , SubOperation ::new ),
24+ MUL (MulOperation .class , 2 , GroupingDirection .LeftToRight , "*" , " " , MulOperation ::new ),
25+ DIV (DivOperation .class , 2 , GroupingDirection .LeftToRight , "/" , " " , DivOperation ::new ),
26+
27+ ADD (AddOperation .class , 3 , GroupingDirection .LeftToRight , "+" , " " , AddOperation ::new ),
28+ SUB (SubOperation .class , 3 , GroupingDirection .LeftToRight , "-" , " " , SubOperation ::new ),
2729 ;
2830
2931 public static final Map <Class <? extends MathSymbol >, SymbolInfo > CLS_TO_INFO_MAP ;
3032 public static final Map <Integer , Set <SymbolInfo >> PREC_TO_INFO_MAP ;
3133 public static final List <Entry <Integer , Set <SymbolInfo >>> PREC_SORTED_INFO ;
34+ public static final int MAX_PRECEDENCE ;
35+ public static final Map <Integer , PrecedenceLevelInfo > PREC_LEVELS_INFO ;
3236
3337 public final int precedence ; // TODO make this Integer
3438 public final Class <? extends MathSymbol > cls ;
3539 public final GroupingDirection groupingDirection ;
3640 public final String infix ;
41+ public final @ Nullable String spacesAroundInfix ;
3742
43+ @ SuppressWarnings ("unused" ) // useful later
3844 SymbolInfo (Class <? extends MathSymbol > cls , int precedence ,
39- GroupingDirection groupingDirection , @ Nullable String infix ) {
45+ GroupingDirection groupingDirection , @ Nullable String spacesAroundInfix , @ Nullable String infix ) {
4046 this .precedence = precedence ;
41- this .cls = cls ; // TODO: private + getters?
47+ this .cls = cls ;
4248 this .groupingDirection = groupingDirection ;
4349 this .infix = infix ;
50+ this .spacesAroundInfix = spacesAroundInfix ;
4451 }
4552 SymbolInfo (Class <? extends BinaryOperationLeftRight > cls , int precedence ,
46- GroupingDirection groupingDirection , @ Nullable String infix ,
47- BinOpBiConstructor <?> biConstructor ) {
53+ GroupingDirection groupingDirection , @ Nullable String infix , @ Nullable String spacesAroundInfix ,
54+ BinOpBiConstructor biConstructor ) {
4855 this .precedence = precedence ;
4956 this .cls = cls ; // TODO: private + getters?
5057 this .groupingDirection = groupingDirection ;
5158 this .infix = infix ;
59+ this .spacesAroundInfix = spacesAroundInfix ;
5260 this .biConstructorCache = biConstructor ;
5361 }
5462
@@ -66,11 +74,13 @@ public enum SymbolInfo {
6674 public static @ Nullable String infixFromClass (Class <? extends MathSymbol > cls ) {
6775 return Util .chainNulls (fromClass (cls ), x -> x .infix );
6876 }
77+ public static @ Nullable String spacesAroundInfixFromClass (Class <? extends MathSymbol > cls ) {
78+ return Util .chainNulls (fromClass (cls ), s -> s .spacesAroundInfix );
79+ }
6980
70- private BinOpBiConstructor <?> biConstructorCache = null ;
71- public @ NotNull BinOpBiConstructor <?> getBiConstructor () {
81+ private BinOpBiConstructor biConstructorCache = null ;
82+ public @ NotNull BinOpBiConstructor getBiConstructor () {
7283 if (biConstructorCache != null ) return biConstructorCache ;
73- // TODO could add a BiFunction<> arg to SymbolInfo(), then pass AddOperation::new etc.
7484 assert BinaryOperationLeftRight .class .isAssignableFrom (cls );
7585
7686 Constructor <? extends BinaryOperationLeftRight > ctor ;
@@ -104,5 +114,7 @@ public enum SymbolInfo {
104114 CLS_TO_INFO_MAP = Arrays .stream (values ()).collect (Collectors .toUnmodifiableMap (p -> p .cls , p -> p ));
105115 PREC_TO_INFO_MAP = Arrays .stream (values ()).collect (Collectors .groupingBy (s -> s .precedence , Collectors .toUnmodifiableSet ()));
106116 PREC_SORTED_INFO = PREC_TO_INFO_MAP .entrySet ().stream ().sorted (Comparator .comparingInt (Entry ::getKey )).toList ();
117+ MAX_PRECEDENCE = PREC_SORTED_INFO .getLast ().getKey ();
118+ PREC_LEVELS_INFO = PREC_TO_INFO_MAP .keySet ().stream ().map (PrecedenceLevelInfo ::newMapEntry ).collect (UtilCollectors .entriesToMap ());
107119 }
108120}
0 commit comments