Skip to content

Commit

Permalink
Fix number parsing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiyotoko committed Nov 18, 2023
1 parent 84b893a commit 4c807be
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/scvis/parser/AccessException.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AccessException(String message, int errorCode) {
checkForRange(300, 399);
}

public AccessException(ClassCastException exception) {
public AccessException(Exception exception) {
super(exception.getMessage(), 300);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/scvis/parser/Callable.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
import java.util.List;

public interface Callable {
Object call(List<Object> args) throws ClassCastException;
Object call(List<Object> args) throws AccessException;
}
41 changes: 31 additions & 10 deletions src/main/java/org/scvis/parser/NameSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

public class NameSpace {

Expand All @@ -40,22 +41,42 @@ public class NameSpace {

static {
// From java.lang.Math
BUILD_INS.declare("sin", (Callable) args -> Math.sin((double) args.get(0)));
BUILD_INS.declare("cos", (Callable) args -> Math.cos((double) args.get(0)));
BUILD_INS.declare("tan", (Callable) args -> Math.tan((double) args.get(0)));
BUILD_INS.declare("asin", (Callable) args -> Math.asin((double) args.get(0)));
BUILD_INS.declare("acos", (Callable) args -> Math.acos((double) args.get(0)));
BUILD_INS.declare("atan", (Callable) args -> Math.atan((double) args.get(0)));
BUILD_INS.declare("sqrt", (Callable) args -> Math.sqrt((double) args.get(0)));
BUILD_INS.declare("abs", (Callable) args -> Math.abs((double) args.get(0)));
BUILD_INS.declare("signum", (Callable) args -> Math.signum((double) args.get(0)));
BUILD_INS.declare("hypot", (Callable) args -> Math.hypot((double) args.get(0), (double) args.get(0)));
BUILD_INS.declare("sin", (Callable) args -> Math.sin(num(args, 0).doubleValue()));
BUILD_INS.declare("cos", (Callable) args -> Math.cos(num(args, 0).doubleValue()));
BUILD_INS.declare("tan", (Callable) args -> Math.tan(num(args, 0).doubleValue()));
BUILD_INS.declare("asin", (Callable) args -> Math.asin(num(args, 0).doubleValue()));
BUILD_INS.declare("acos", (Callable) args -> Math.acos(num(args, 0).doubleValue()));
BUILD_INS.declare("atan", (Callable) args -> Math.atan(num(args, 0).doubleValue()));
BUILD_INS.declare("sqrt", (Callable) args -> Math.sqrt(num(args, 0).doubleValue()));
BUILD_INS.declare("abs", (Callable) args -> Math.abs(num(args, 0).doubleValue()));
BUILD_INS.declare("signum", (Callable) args -> Math.signum(num(args, 0).doubleValue()));
BUILD_INS.declare("hypot", (Callable) args -> Math.hypot(num(args, 0).doubleValue(), num(args, 1).doubleValue()));

BUILD_INS.declare("pi", Math.PI);
BUILD_INS.declare("e", Math.E);
BUILD_INS.declare("tau", TAU);
}

@SuppressWarnings("unchecked")
private static <T> T obj(List<Object> args, int index) throws AccessException {
if (index >= args.size())
throw new AccessException("Argument " + index + " is missing", 330);
try {
return (T) args.get(index);
} catch (ClassCastException e) {
throw new AccessException(e);
}
}

private static Number num(List<Object> args, int index) throws AccessException {
if (index >= args.size())
throw new AccessException("Argument " + index + " is missing", 330);
Object num = args.get(index);
if (!(num instanceof Number))
throw new AccessException("Argument " + index + " is not an instance of number", 340);
return (Number) num;
}

private static class Undeclared {
private static final Object UNDECLARED = new Undeclared();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/scvis/parser/TokenEvaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ private void eliminate(Operator operator) throws EvaluationException, AccessExce
int index = tokens.indexOf(operator);
if (index == -1)
throw new EvaluationException("Operator must be present in tokens", 200);
if (index == tokens.size() - 1)
throw new EvaluationException("A right value must exist", 230);
if (index == 0) {
Object right = tokens.get(1);
if (operator instanceof Sign) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/scvis/parser/TokenParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class TokenParser {
public static final @Nonnull Map<Character, Operator> CHARACTER_OPERATOR_MAP = Map.of(
'+', BinaryOperator.OperatorAndSign.ADD, '-', BinaryOperator.OperatorAndSign.SUB, '*', BinaryOperator.MUL,
'/', BinaryOperator.DIV, '%', BinaryOperator.MOD, '^', BinaryOperator.POW,
',', Operator.SEPARATOR, ';', Operator.SEPARATOR, '=', BinaryOperator.EQU);
',', Operator.SEPARATOR, ';', Operator.SEPARATOR);

private int pos;

Expand Down Expand Up @@ -238,15 +238,15 @@ private Object parseNumber(@Nonnull char[] chars) throws ParsingException {
build = build * 10.0 + Character.digit(c, 10);
} else if (c == '.') {
if (real > -1)
throw new ParsingException("Could not parse number by " + c, 190);
throw new ParsingException("A number can not have two dots", 190);
real = pos;
} else {
pos--;
break;
}
}
if (real > -1)
build /= Math.pow(10.0, pos - real - 1);
build /= Math.pow(10.0, pos - real);
return build;
}

Expand Down

0 comments on commit 4c807be

Please sign in to comment.