12
12
import net .earthcomputer .clientcommands .TempRules ;
13
13
import net .minecraft .command .CommandSource ;
14
14
import net .minecraft .server .command .ServerCommandSource ;
15
- import net .minecraft .text .TranslatableText ;
15
+ import net .minecraft .text .* ;
16
16
17
17
import java .util .*;
18
18
import java .util .concurrent .CompletableFuture ;
@@ -89,14 +89,14 @@ private Expression parseExpression1() throws CommandSyntaxException {
89
89
reader .skip ();
90
90
reader .skipWhitespace ();
91
91
Expression right = parseExpression1 ();
92
- return new BinaryOpExpression (left , right , Double ::sum );
92
+ return new BinaryOpExpression (left , right , Double ::sum , "addition" );
93
93
}
94
94
95
95
if (reader .peek () == '-' ) {
96
96
reader .skip ();
97
97
reader .skipWhitespace ();
98
98
Expression right = parseExpression1 ();
99
- return new BinaryOpExpression (left , right , (l , r ) -> l - r );
99
+ return new BinaryOpExpression (left , right , (l , r ) -> l - r , "subtraction" );
100
100
}
101
101
}
102
102
@@ -112,28 +112,28 @@ private Expression parseExpression2() throws CommandSyntaxException {
112
112
reader .skip ();
113
113
reader .skipWhitespace ();
114
114
Expression right = parseExpression2 ();
115
- return new BinaryOpExpression (left , right , (l , r ) -> l * r );
115
+ return new BinaryOpExpression (left , right , (l , r ) -> l * r , "multiplication" );
116
116
}
117
117
118
118
if (reader .peek () == '/' ) {
119
119
reader .skip ();
120
120
reader .skipWhitespace ();
121
121
Expression right = parseExpression2 ();
122
- return new BinaryOpExpression (left , right , (l , r ) -> l / r );
122
+ return new BinaryOpExpression (left , right , (l , r ) -> l / r , "division" );
123
123
}
124
124
125
125
if (reader .peek () == '%' ) {
126
126
reader .skip ();
127
127
reader .skipWhitespace ();
128
128
Expression right = parseExpression2 ();
129
- return new BinaryOpExpression (left , right , (l , r ) -> l % r );
129
+ return new BinaryOpExpression (left , right , (l , r ) -> l % r , "modulo" );
130
130
}
131
131
132
132
if (!StringReader .isAllowedNumber (reader .peek ())) {
133
133
int cursor = reader .getCursor ();
134
134
try {
135
135
Expression right = parseExpression5 ();
136
- return new BinaryOpExpression (left , right , (l , r ) -> l * r );
136
+ return new BinaryOpExpression (left , right , (l , r ) -> l * r , "multiplication" );
137
137
} catch (CommandSyntaxException e ) {
138
138
reader .setCursor (cursor );
139
139
}
@@ -161,7 +161,7 @@ private Expression parseExpression3() throws CommandSyntaxException {
161
161
Expression right = subExpressions .get (subExpressions .size () - 1 );
162
162
for (int i = subExpressions .size () - 2 ; i >= 0 ; i --) {
163
163
Expression left = subExpressions .get (i );
164
- right = new BinaryOpExpression (left , right , Math ::pow );
164
+ right = new BinaryOpExpression (left , right , Math ::pow , "exponentiation" );
165
165
}
166
166
return right ;
167
167
}
@@ -196,7 +196,7 @@ private Expression parseExpression5() throws CommandSyntaxException {
196
196
197
197
if (ConstantExpression .CONSTANTS .containsKey (word )) {
198
198
suggestor = null ;
199
- return new ConstantExpression (ConstantExpression .CONSTANTS .get (word ));
199
+ return new ConstantExpression (word , ConstantExpression .CONSTANTS .get (word ));
200
200
}
201
201
202
202
if (FunctionExpression .FUNCTIONS .containsKey (word )) {
@@ -230,7 +230,7 @@ private Expression parseExpression5() throws CommandSyntaxException {
230
230
throw INVALID_ARGUMENT_COUNT .createWithContext (reader , word , arguments .size ());
231
231
}
232
232
233
- return new FunctionExpression (function , arguments .toArray (new Expression [0 ]));
233
+ return new FunctionExpression (word , function , arguments .toArray (new Expression [0 ]));
234
234
}
235
235
236
236
reader .setCursor (cursor );
@@ -280,24 +280,32 @@ private static boolean isAllowedNumber(char c) {
280
280
public static abstract class Expression {
281
281
public String strVal ;
282
282
public abstract double eval ();
283
+ public abstract Text getParsedTree ();
283
284
}
284
285
285
286
private static class BinaryOpExpression extends Expression {
286
287
287
288
private Expression left ;
288
289
private Expression right ;
289
290
private DoubleBinaryOperator operator ;
291
+ private String type ;
290
292
291
- public BinaryOpExpression (Expression left , Expression right , DoubleBinaryOperator operator ) {
293
+ public BinaryOpExpression (Expression left , Expression right , DoubleBinaryOperator operator , String type ) {
292
294
this .left = left ;
293
295
this .right = right ;
294
296
this .operator = operator ;
297
+ this .type = type ;
295
298
}
296
299
297
300
@ Override
298
301
public double eval () {
299
302
return operator .applyAsDouble (left .eval (), right .eval ());
300
303
}
304
+
305
+ @ Override
306
+ public Text getParsedTree () {
307
+ return new TranslatableText ("commands.ccalc.parse.binaryOperator." + type , left .getParsedTree (), right .getParsedTree ());
308
+ }
301
309
}
302
310
303
311
private static class NegateExpression extends Expression {
@@ -311,6 +319,11 @@ public NegateExpression(Expression right) {
311
319
public double eval () {
312
320
return -right .eval ();
313
321
}
322
+
323
+ @ Override
324
+ public Text getParsedTree () {
325
+ return new TranslatableText ("commands.ccalc.parse.negate" , this .right .getParsedTree ());
326
+ }
314
327
}
315
328
316
329
private static class ConstantExpression extends Expression {
@@ -321,16 +334,23 @@ private static class ConstantExpression extends Expression {
321
334
"ans" , () -> TempRules .calcAnswer
322
335
);
323
336
337
+ private String type ;
324
338
private DoubleSupplier constant ;
325
339
326
- public ConstantExpression (DoubleSupplier constant ) {
340
+ public ConstantExpression (String type , DoubleSupplier constant ) {
341
+ this .type = type ;
327
342
this .constant = constant ;
328
343
}
329
344
330
345
@ Override
331
346
public double eval () {
332
347
return constant .getAsDouble ();
333
348
}
349
+
350
+ @ Override
351
+ public Text getParsedTree () {
352
+ return new TranslatableText ("commands.ccalc.parse.constant" , this .type );
353
+ }
334
354
}
335
355
336
356
public static class FunctionExpression extends Expression {
@@ -403,10 +423,12 @@ public boolean isAcceptableInputCount(int count) {
403
423
.put ("not" , (UnaryFunction ) val -> (double )(~((int )val )))
404
424
.build ();
405
425
426
+ private String type ;
406
427
private IFunction function ;
407
428
private Expression [] arguments ;
408
429
409
- public FunctionExpression (IFunction function , Expression ... arguments ) {
430
+ public FunctionExpression (String type , IFunction function , Expression ... arguments ) {
431
+ this .type = type ;
410
432
this .function = function ;
411
433
this .arguments = arguments ;
412
434
}
@@ -419,6 +441,23 @@ public double eval() {
419
441
return function .eval (args );
420
442
}
421
443
444
+ @ Override
445
+ public Text getParsedTree () {
446
+ MutableText argumentsText = new LiteralText ("" );
447
+ boolean first = true ;
448
+ for (Expression argument : this .arguments ) {
449
+ if (first ) {
450
+ first = false ;
451
+ } else {
452
+ argumentsText .append (", " );
453
+ }
454
+
455
+ argumentsText .append (argument .getParsedTree ());
456
+ }
457
+
458
+ return new TranslatableText ("commands.ccalc.parse.function" , this .type , argumentsText );
459
+ }
460
+
422
461
private static interface IFunction {
423
462
double eval (double ... inputs );
424
463
boolean isAcceptableInputCount (int count );
@@ -475,6 +514,11 @@ public LiteralExpression(double val) {
475
514
public double eval () {
476
515
return val ;
477
516
}
517
+
518
+ @ Override
519
+ public Text getParsedTree () {
520
+ return new TranslatableText ("commands.ccalc.parse.literal" , this .val );
521
+ }
478
522
}
479
523
480
524
}
0 commit comments