Code: Select all
@Override
public void exitAddExpr(BasicLangParser.AddExprContext ctx) {
Util.Value left = stack.pop();
Util.Value right = stack.pop();
if (left.datatype==right.datatype) {
String result = switch (left.datatype) {
case INT, FLOAT -> String.valueOf((Double)left.getValue() + (Double)right.getValue());
case STRING, CHAR -> (String)left.getValue() + (String)right.getValue();
default -> throw new ParseCancellationException("Addition Error - Invalid Datatype ("+left.datatype.toString()+","+right.datatype.toString()+")");
};
stack.push(Util.Value.newValue(result));
} else {
throw new ParseCancellationException("Addition Error - Mismatched Datatypes ("+left.datatype.toString()+","+right.datatype.toString()+")");
}
super.exitAddExpr(ctx);
}
Bearbeiten:
Ich versuche derzeit nicht, die ParsecancellationException < /code> in meinem Code vorerst zu fangen; Ich habe diese Ausnahme einfach mit dem Debugger von Intellij und der Aktivierung von Ausnahmebestandteilen erfasst.
Ich habe die Ausnahmeverbreitung in meinem Code nicht blockiert, obwohl ich mir nicht bewusst bin, ob Antlr4 dies tut, daher meine Frage. Ausnahme, wo ich versuche, eine Zeichenfolge als Doppel zu analysieren, um festzustellen, ob sie numerisch ist.