@@ -16,18 +16,21 @@ abstract mixin class ScoreStoreable {
1616 // block, bossbar, entity, score, storage
1717 String get_assignable_right ();
1818
19- ScoreOperation asScore () => StoreScoreOperation (Score .tmp (), this );
20-
21- BinaryScoreOperation operator + (dynamic other) => asScore () + other;
22- BinaryScoreOperation operator - (dynamic other) => asScore () - other;
23- BinaryScoreOperation operator % (dynamic other) => asScore () % other;
24- BinaryScoreOperation operator / (dynamic other) => asScore () / other;
25- BinaryScoreOperation operator * (dynamic other) => asScore () * other;
26- ScoreCondition operator > (dynamic other) => asScore () > other;
27- ScoreCondition operator >= (dynamic other) => asScore () >= other;
28- ScoreCondition operator <= (dynamic other) => asScore () <= other;
29- ScoreCondition operator < (dynamic other) => asScore () < other;
30- ScoreCondition operator & (dynamic other) => asScore () & other;
19+ ScoreOperation toScore ({Score ? out}) => StoreScoreOperation (
20+ out ?? Score .tmp (),
21+ this ,
22+ );
23+
24+ BinaryScoreOperation operator + (dynamic other) => toScore () + other;
25+ BinaryScoreOperation operator - (dynamic other) => toScore () - other;
26+ BinaryScoreOperation operator % (dynamic other) => toScore () % other;
27+ BinaryScoreOperation operator / (dynamic other) => toScore () / other;
28+ BinaryScoreOperation operator * (dynamic other) => toScore () * other;
29+ ScoreCondition operator > (dynamic other) => toScore () > other;
30+ ScoreCondition operator >= (dynamic other) => toScore () >= other;
31+ ScoreCondition operator <= (dynamic other) => toScore () <= other;
32+ ScoreCondition operator < (dynamic other) => toScore () < other;
33+ ScoreCondition operator & (dynamic other) => toScore () & other;
3134}
3235
3336abstract mixin class ScoreAssignable {
@@ -43,58 +46,59 @@ abstract mixin class ScoreAssignable {
4346
4447sealed class ScoreOperation extends Widget implements ScoreStoreable {
4548 /// add
49+ @override
4650 BinaryScoreOperation operator + (dynamic other) => switch (other) {
4751 int val => add (val),
4852 ScoreOperation s => addScore (s),
49- ScoreStoreable s => addScore (s.asScore ()),
53+ ScoreStoreable s => addScore (s.toScore ()),
5054 _ => throw ('Please use either a Score or an Int in the operator +' )
5155 };
5256
5357 /// subtract
5458 BinaryScoreOperation operator - (dynamic other) => switch (other) {
5559 int val => subtract (val),
5660 ScoreOperation s => subtractScore (s),
57- ScoreStoreable s => subtractScore (s.asScore ()),
61+ ScoreStoreable s => subtractScore (s.toScore ()),
5862 _ => throw ('Please use either a Score or an Int in the operator -' )
5963 };
6064
6165 /// modulo by
6266 BinaryScoreOperation operator % (dynamic other) => switch (other) {
6367 int val => modulo (Score .con (val)),
6468 ScoreOperation s => modulo (s),
65- ScoreStoreable s => modulo (s.asScore ()),
69+ ScoreStoreable s => modulo (s.toScore ()),
6670 _ => throw ('Please use either a Score or an Int in the operator %' )
6771 };
6872
6973 /// divide by
7074 BinaryScoreOperation operator / (dynamic other) => switch (other) {
7175 int val => divideByScore (Score .con (val)),
7276 ScoreOperation s => divideByScore (s),
73- ScoreStoreable s => divideByScore (s.asScore ()),
77+ ScoreStoreable s => divideByScore (s.toScore ()),
7478 _ => throw ('Please use either a Score or an Int in the operator /' )
7579 };
7680
7781 /// multiply by
7882 BinaryScoreOperation operator * (dynamic other) => switch (other) {
7983 int val => multiplyByScore (Score .con (val)),
8084 ScoreOperation s => multiplyByScore (s),
81- ScoreStoreable s => multiplyByScore (s.asScore ()),
85+ ScoreStoreable s => multiplyByScore (s.toScore ()),
8286 _ => throw ('Please use either a Score or an Int in the operator *' )
8387 };
8488
8589 /// greater than
8690 ScoreCondition operator > (dynamic other) => switch (other) {
8791 int val => matchesRange (Range .from (val + 1 )),
8892 ScoreOperation s => isBigger (s),
89- ScoreStoreable s => isBigger (s.asScore ()),
93+ ScoreStoreable s => isBigger (s.toScore ()),
9094 _ => throw ('Please use either a Score or an Int in the operator >' )
9195 };
9296
9397 /// less than
9498 ScoreCondition operator < (dynamic other) => switch (other) {
9599 int val => matchesRange (Range .to (val - 1 )),
96100 ScoreOperation s => isSmaller (s),
97- ScoreStoreable s => isSmaller (s.asScore ()),
101+ ScoreStoreable s => isSmaller (s.toScore ()),
98102 _ => throw ('Please use either a Score or an Int in the operator <' )
99103 };
100104
@@ -103,7 +107,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
103107 int val => matchesRange (Range .from (val)),
104108 ScoreOperation s => isBiggerOrEqual (s),
105109 ScoreStoreable s => isBiggerOrEqual (
106- s.asScore (),
110+ s.toScore (),
107111 ),
108112 _ => throw ('Please use either a Score or an Int in the operator >=' )
109113 };
@@ -113,7 +117,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
113117 int val => matchesRange (Range .to (val)),
114118 ScoreOperation s => isSmallerOrEqual (s),
115119 ScoreStoreable s => isSmallerOrEqual (
116- s.asScore (),
120+ s.toScore (),
117121 ),
118122 _ => throw ('Please use either a Score or an Int in the operator <=' )
119123 };
@@ -124,7 +128,7 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
124128 Range r => matchesRange (r),
125129 ScoreOperation s => isEqual (s),
126130 ScoreStoreable s => isEqual (
127- s.asScore (),
131+ s.toScore (),
128132 ),
129133 _ =>
130134 throw ('Please use either a Score, Range or an Int in the operator &' )
@@ -261,7 +265,8 @@ sealed class ScoreOperation extends Widget implements ScoreStoreable {
261265 }
262266
263267 @override
264- ScoreOperation asScore () => this ;
268+ ScoreOperation toScore ({Score ? out}) =>
269+ out == null ? this : BinaryScoreOperation .assign (out, this );
265270}
266271
267272sealed class ElementaryScoreOperation extends ScoreOperation {}
@@ -302,7 +307,10 @@ final class ResetScoreOperation extends ElementaryScoreOperation {
302307 ResetScoreOperation (this .score);
303308
304309 @override
305- generate (Context context) => Command ('scoreboard players reset $score ' );
310+ generate (Context context) => For .of ([
311+ score,
312+ Command ('scoreboard players reset $score ' ),
313+ ]);
306314
307315 @override
308316 String toString () => 'reset $score ' ;
@@ -345,11 +353,11 @@ final class ElementaryBinaryScoreOperation extends ElementaryScoreOperation {
345353
346354 final ScoreOperator operation;
347355
348- ElementaryBinaryScoreOperation ({
349- required this .left,
350- required this .right ,
351- required this .operation ,
352- } );
356+ ElementaryBinaryScoreOperation (
357+ this .left,
358+ this .operation ,
359+ this .right ,
360+ );
353361
354362 ElementaryBinaryScoreOperation .assign (this .left, this .right)
355363 : operation = ScoreOperator .Assign ;
@@ -390,12 +398,41 @@ class BinaryScoreOperation extends ScoreOperation {
390398 BinaryScoreOperation (this .left, this .operation, this .right);
391399
392400 BinaryScoreOperation .assign (this .left, this .right)
393- : this . operation = ScoreOperator .Assign ;
401+ : operation = ScoreOperator .Assign ;
394402
395403 @override
396- generate (Context context) {
397- // TODO: implement generate
398- throw UnimplementedError ();
404+ Widget generate (Context context) {
405+ final ScoreBuilder builder = ScoreBuilder ();
406+
407+ final (rScore, rActions) = right.copy (compact: true , builder: builder);
408+
409+ print (left);
410+
411+ if (left is Score ) {
412+ return For .of ([
413+ left,
414+ ...rActions,
415+ ...builder.compileBinary (left, operation, rScore),
416+ ]);
417+ }
418+
419+ if (left case StoreScoreOperation (right: ScoreAssignable store)) {
420+ final (lScore, lActions) = left.copy (compact: true , builder: builder);
421+ return For .of ([
422+ ...lActions,
423+ ...rActions,
424+ ...builder.compileBinary (lScore, operation, rScore),
425+ store << lScore
426+ ]);
427+ }
428+
429+ final (lScore, lActions) = left.copy (builder: builder);
430+
431+ return For .of ([
432+ ...lActions,
433+ ...rActions,
434+ ...builder.compileBinary (lScore, operation, rScore),
435+ ]);
399436 }
400437
401438 @override
@@ -441,7 +478,7 @@ class ConstScore extends Score {
441478}
442479
443480class Score extends ElementaryScoreOperation
444- with ScoreAssignable , ScoreStoreable {
481+ implements ScoreAssignable , ScoreStoreable {
445482 final Entity entity;
446483 String score;
447484 final String type;
@@ -563,7 +600,7 @@ class Score extends ElementaryScoreOperation
563600 /// assign value(int, Score, Data or Condition)
564601 ScoreOperation setTo (dynamic other) {
565602 if (other is int ) return set (other);
566- if (other is Score ) return setEqual (other);
603+ if (other is ScoreOperation ) return setEqual (other);
567604
568605 ///if (other is Data) return setToData(other);
569606 // if (other is Condition) return setToCondition(other);
0 commit comments