Skip to content

Commit 639834d

Browse files
authored
Add the parse flag to the ccalc command (#215)
1 parent 4377444 commit 639834d

File tree

3 files changed

+80
-15
lines changed

3 files changed

+80
-15
lines changed

src/main/java/net/earthcomputer/clientcommands/command/CalcCommand.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package net.earthcomputer.clientcommands.command;
22

33
import com.mojang.brigadier.CommandDispatcher;
4+
import com.mojang.brigadier.tree.LiteralCommandNode;
5+
46
import net.earthcomputer.clientcommands.TempRules;
57
import net.earthcomputer.clientcommands.command.arguments.ExpressionArgumentType;
68
import net.minecraft.server.command.ServerCommandSource;
7-
import net.minecraft.text.LiteralText;
8-
import net.minecraft.text.MutableText;
9+
import net.minecraft.text.*;
910
import net.minecraft.util.Formatting;
1011

1112
import static net.earthcomputer.clientcommands.command.ClientCommandManager.*;
@@ -14,15 +15,24 @@
1415

1516
public class CalcCommand {
1617

18+
private static final int FLAG_PARSE = 1;
19+
1720
public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
1821
addClientSideCommand("ccalc");
1922

23+
LiteralCommandNode<ServerCommandSource> ccalc = dispatcher.register(literal("ccalc"));
2024
dispatcher.register(literal("ccalc")
25+
.then(literal("--parse")
26+
.redirect(ccalc, ctx -> withFlags(ctx.getSource(), FLAG_PARSE, true)))
2127
.then(argument("expr", expression())
2228
.executes(ctx -> evaluateExpression(ctx.getSource(), getExpression(ctx, "expr")))));
2329
}
2430

2531
private static int evaluateExpression(ServerCommandSource source, ExpressionArgumentType.Expression expression) {
32+
if (getFlag(source, FLAG_PARSE)) {
33+
sendFeedback(new TranslatableText("commands.ccalc.parse", expression.getParsedTree()));
34+
}
35+
2636
double result = expression.eval();
2737
TempRules.calcAnswer = result;
2838
int iresult = 0;

src/main/java/net/earthcomputer/clientcommands/command/arguments/ExpressionArgumentType.java

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import net.earthcomputer.clientcommands.TempRules;
1313
import net.minecraft.command.CommandSource;
1414
import net.minecraft.server.command.ServerCommandSource;
15-
import net.minecraft.text.TranslatableText;
15+
import net.minecraft.text.*;
1616

1717
import java.util.*;
1818
import java.util.concurrent.CompletableFuture;
@@ -89,14 +89,14 @@ private Expression parseExpression1() throws CommandSyntaxException {
8989
reader.skip();
9090
reader.skipWhitespace();
9191
Expression right = parseExpression1();
92-
return new BinaryOpExpression(left, right, Double::sum);
92+
return new BinaryOpExpression(left, right, Double::sum, "addition");
9393
}
9494

9595
if (reader.peek() == '-') {
9696
reader.skip();
9797
reader.skipWhitespace();
9898
Expression right = parseExpression1();
99-
return new BinaryOpExpression(left, right, (l, r) -> l - r);
99+
return new BinaryOpExpression(left, right, (l, r) -> l - r, "subtraction");
100100
}
101101
}
102102

@@ -112,28 +112,28 @@ private Expression parseExpression2() throws CommandSyntaxException {
112112
reader.skip();
113113
reader.skipWhitespace();
114114
Expression right = parseExpression2();
115-
return new BinaryOpExpression(left, right, (l, r) -> l * r);
115+
return new BinaryOpExpression(left, right, (l, r) -> l * r, "multiplication");
116116
}
117117

118118
if (reader.peek() == '/') {
119119
reader.skip();
120120
reader.skipWhitespace();
121121
Expression right = parseExpression2();
122-
return new BinaryOpExpression(left, right, (l, r) -> l / r);
122+
return new BinaryOpExpression(left, right, (l, r) -> l / r, "division");
123123
}
124124

125125
if (reader.peek() == '%') {
126126
reader.skip();
127127
reader.skipWhitespace();
128128
Expression right = parseExpression2();
129-
return new BinaryOpExpression(left, right, (l, r) -> l % r);
129+
return new BinaryOpExpression(left, right, (l, r) -> l % r, "modulo");
130130
}
131131

132132
if (!StringReader.isAllowedNumber(reader.peek())) {
133133
int cursor = reader.getCursor();
134134
try {
135135
Expression right = parseExpression5();
136-
return new BinaryOpExpression(left, right, (l, r) -> l * r);
136+
return new BinaryOpExpression(left, right, (l, r) -> l * r, "multiplication");
137137
} catch (CommandSyntaxException e) {
138138
reader.setCursor(cursor);
139139
}
@@ -161,7 +161,7 @@ private Expression parseExpression3() throws CommandSyntaxException {
161161
Expression right = subExpressions.get(subExpressions.size() - 1);
162162
for (int i = subExpressions.size() - 2; i >= 0; i--) {
163163
Expression left = subExpressions.get(i);
164-
right = new BinaryOpExpression(left, right, Math::pow);
164+
right = new BinaryOpExpression(left, right, Math::pow, "exponentiation");
165165
}
166166
return right;
167167
}
@@ -196,7 +196,7 @@ private Expression parseExpression5() throws CommandSyntaxException {
196196

197197
if (ConstantExpression.CONSTANTS.containsKey(word)) {
198198
suggestor = null;
199-
return new ConstantExpression(ConstantExpression.CONSTANTS.get(word));
199+
return new ConstantExpression(word, ConstantExpression.CONSTANTS.get(word));
200200
}
201201

202202
if (FunctionExpression.FUNCTIONS.containsKey(word)) {
@@ -230,7 +230,7 @@ private Expression parseExpression5() throws CommandSyntaxException {
230230
throw INVALID_ARGUMENT_COUNT.createWithContext(reader, word, arguments.size());
231231
}
232232

233-
return new FunctionExpression(function, arguments.toArray(new Expression[0]));
233+
return new FunctionExpression(word, function, arguments.toArray(new Expression[0]));
234234
}
235235

236236
reader.setCursor(cursor);
@@ -280,24 +280,32 @@ private static boolean isAllowedNumber(char c) {
280280
public static abstract class Expression {
281281
public String strVal;
282282
public abstract double eval();
283+
public abstract Text getParsedTree();
283284
}
284285

285286
private static class BinaryOpExpression extends Expression {
286287

287288
private Expression left;
288289
private Expression right;
289290
private DoubleBinaryOperator operator;
291+
private String type;
290292

291-
public BinaryOpExpression(Expression left, Expression right, DoubleBinaryOperator operator) {
293+
public BinaryOpExpression(Expression left, Expression right, DoubleBinaryOperator operator, String type) {
292294
this.left = left;
293295
this.right = right;
294296
this.operator = operator;
297+
this.type = type;
295298
}
296299

297300
@Override
298301
public double eval() {
299302
return operator.applyAsDouble(left.eval(), right.eval());
300303
}
304+
305+
@Override
306+
public Text getParsedTree() {
307+
return new TranslatableText("commands.ccalc.parse.binaryOperator." + type, left.getParsedTree(), right.getParsedTree());
308+
}
301309
}
302310

303311
private static class NegateExpression extends Expression {
@@ -311,6 +319,11 @@ public NegateExpression(Expression right) {
311319
public double eval() {
312320
return -right.eval();
313321
}
322+
323+
@Override
324+
public Text getParsedTree() {
325+
return new TranslatableText("commands.ccalc.parse.negate", this.right.getParsedTree());
326+
}
314327
}
315328

316329
private static class ConstantExpression extends Expression {
@@ -321,16 +334,23 @@ private static class ConstantExpression extends Expression {
321334
"ans", () -> TempRules.calcAnswer
322335
);
323336

337+
private String type;
324338
private DoubleSupplier constant;
325339

326-
public ConstantExpression(DoubleSupplier constant) {
340+
public ConstantExpression(String type, DoubleSupplier constant) {
341+
this.type = type;
327342
this.constant = constant;
328343
}
329344

330345
@Override
331346
public double eval() {
332347
return constant.getAsDouble();
333348
}
349+
350+
@Override
351+
public Text getParsedTree() {
352+
return new TranslatableText("commands.ccalc.parse.constant", this.type);
353+
}
334354
}
335355

336356
public static class FunctionExpression extends Expression {
@@ -403,10 +423,12 @@ public boolean isAcceptableInputCount(int count) {
403423
.put("not", (UnaryFunction) val -> (double)(~((int)val)))
404424
.build();
405425

426+
private String type;
406427
private IFunction function;
407428
private Expression[] arguments;
408429

409-
public FunctionExpression(IFunction function, Expression... arguments) {
430+
public FunctionExpression(String type, IFunction function, Expression... arguments) {
431+
this.type = type;
410432
this.function = function;
411433
this.arguments = arguments;
412434
}
@@ -419,6 +441,23 @@ public double eval() {
419441
return function.eval(args);
420442
}
421443

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+
422461
private static interface IFunction {
423462
double eval(double... inputs);
424463
boolean isAcceptableInputCount(int count);
@@ -475,6 +514,11 @@ public LiteralExpression(double val) {
475514
public double eval() {
476515
return val;
477516
}
517+
518+
@Override
519+
public Text getParsedTree() {
520+
return new TranslatableText("commands.ccalc.parse.literal", this.val);
521+
}
478522
}
479523

480524
}

src/main/resources/assets/clientcommands/lang/en_us.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44

55
"commands.ccalc.expected": "Expected %s",
66
"commands.ccalc.invalidArgumentCount": "Function \"%s\" cannot take %d arguments",
7+
"commands.ccalc.parse": "Parsed expression: %s",
8+
"commands.ccalc.parse.binaryOperator.addition": "(%s + %s)",
9+
"commands.ccalc.parse.binaryOperator.division": "(%s / %s)",
10+
"commands.ccalc.parse.binaryOperator.exponentiation": "(%s ^ %s)",
11+
"commands.ccalc.parse.binaryOperator.modulo": "(%s % %s)",
12+
"commands.ccalc.parse.binaryOperator.multiplication": "(%s * %s)",
13+
"commands.ccalc.parse.binaryOperator.subtraction": "(%s - %s)",
14+
"commands.ccalc.parse.constant": "(constant '%s')",
15+
"commands.ccalc.parse.function": "(function '%s' with arguments: %s)",
16+
"commands.ccalc.parse.literal": "(literal: %s)",
17+
"commands.ccalc.parse.negate": "(negated: %s)",
718

819
"commands.ccalcstack.success": "%d %s is %d stacks and %d extra",
920
"commands.ccalcstack.success.empty": "%d items is %d stacks and %d extra",

0 commit comments

Comments
 (0)