Use different multiplication signs in JSCL

This commit is contained in:
serso 2016-01-24 21:21:41 +01:00
parent 0e4960dc4e
commit 27ee71a179
3 changed files with 42 additions and 12 deletions

View File

@ -10,23 +10,19 @@ import javax.annotation.Nonnull;
* Date: 10/27/11 * Date: 10/27/11
* Time: 2:45 PM * Time: 2:45 PM
*/ */
class MultiplyOrDivideFactor implements Parser<Generic> { class DivideFactor implements Parser<Generic> {
public static final Parser<Generic> multiply = new MultiplyOrDivideFactor(true); public static final Parser<Generic> parser = new DivideFactor();
public static final Parser<Generic> divide = new MultiplyOrDivideFactor(false); private DivideFactor() {
boolean multiplication;
private MultiplyOrDivideFactor(boolean multiplication) {
this.multiplication = multiplication;
} }
public Generic parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException { public Generic parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException {
int pos0 = p.position.intValue(); final int pos0 = p.position.intValue();
ParserUtils.skipWhitespaces(p); ParserUtils.skipWhitespaces(p);
if (p.position.intValue() < p.expression.length() && p.expression.charAt(p.position.intValue()) == (multiplication ? '*' : '/')) { final int pos1 = p.position.intValue();
if (pos1 < p.expression.length() && p.expression.charAt(pos1) == '/') {
p.position.increment(); p.position.increment();
} else { } else {
ParserUtils.throwParseException(p, pos0, Messages.msg_10, '*', '/'); ParserUtils.throwParseException(p, pos0, Messages.msg_10, '*', '/');

View File

@ -0,0 +1,34 @@
package jscl.text;
import jscl.math.Generic;
import jscl.text.msg.Messages;
import javax.annotation.Nonnull;
/**
* User: serso
* Date: 10/27/11
* Time: 2:45 PM
*/
class MultiplyFactor implements Parser<Generic> {
public static final Parser<Generic> parser = new MultiplyFactor();
private static boolean isMultiplication(char c) {
return c == '*' || c == '×' || c == '∙';
}
public Generic parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException {
final int pos0 = p.position.intValue();
ParserUtils.skipWhitespaces(p);
final int pos1 = p.position.intValue();
if (pos1 < p.expression.length() && isMultiplication(p.expression.charAt(pos1))) {
p.position.increment();
} else {
ParserUtils.throwParseException(p, pos0, Messages.msg_10, '*', '/');
}
return ParserUtils.parseWithRollback(Factor.parser, pos0, previousSumElement, p);
}
}

View File

@ -27,12 +27,12 @@ class TermParser implements Parser<Generic> {
while (true) { while (true) {
try { try {
Generic b = MultiplyOrDivideFactor.multiply.parse(p, null); Generic b = MultiplyFactor.parser.parse(p, null);
result = result.multiply(s); result = result.multiply(s);
s = b; s = b;
} catch (ParseException e) { } catch (ParseException e) {
try { try {
Generic b = MultiplyOrDivideFactor.divide.parse(p, null); Generic b = DivideFactor.parser.parse(p, null);
if (s.compareTo(JsclInteger.valueOf(1)) == 0) if (s.compareTo(JsclInteger.valueOf(1)) == 0)
s = new Inverse(GenericVariable.content(b, true)).expressionValue(); s = new Inverse(GenericVariable.content(b, true)).expressionValue();
else else