From 3168a7560a4d8f1d29c8f2518d674c1f4c85ef8c Mon Sep 17 00:00:00 2001 From: Roman / Linnea Gräf Date: Fri, 21 Oct 2022 18:58:47 +0200 Subject: Fix calculator crash by using correct apis (#391) Right the wrongs --- .../moulberry/notenoughupdates/util/Calculator.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/Calculator.java b/src/main/java/io/github/moulberry/notenoughupdates/util/Calculator.java index b39a05f4..221b5023 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/Calculator.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/Calculator.java @@ -101,6 +101,10 @@ public class Calculator { token.tokenLength = 1; token.type = TokenType.BINOP; token.operatorValue = c + ""; + if (c == '*' && i + 1 < source.length() && source.charAt(i + 1) == '*') { + token.tokenLength++; + token.operatorValue = "^"; + } } else if (postops.indexOf(c) != -1) { token.tokenLength = 1; token.type = TokenType.POSTOP; @@ -162,7 +166,6 @@ public class Calculator { Deque op = new ArrayDeque<>(); List out = new ArrayList<>(); - boolean nextMultiplyShouldBePower = false; for (Token currentlyShunting : toShunt) { switch (currentlyShunting.type) { @@ -170,17 +173,6 @@ public class Calculator { out.add(currentlyShunting); break; case BINOP: - Token next = toShunt.get(toShunt.indexOf(currentlyShunting) + 1); - if (currentlyShunting.operatorValue.equals("^")) { - if (next.numericValue > 999) { - throw new CalculatorException(next.numericValue + " is too large, pick a power less than 1000", next.tokenStart, next.tokenLength); - } - } else if (next != null && currentlyShunting.operatorValue.equals("*") && next.operatorValue != null && next.operatorValue.equals("*")) { - nextMultiplyShouldBePower = true; - continue; - } else if (nextMultiplyShouldBePower && currentlyShunting.operatorValue.equals("*")) { - currentlyShunting.operatorValue = "^"; - } int p = getPrecedence(currentlyShunting); while (!op.isEmpty()) { Token l = op.peek(); -- cgit