Allow unary operators in power exponent

This commit is contained in:
Riccardo Azzolini 2019-01-31 13:10:02 +01:00
parent ebbf8b013a
commit 2a347e6d19
2 changed files with 16 additions and 6 deletions

View File

@ -149,11 +149,11 @@ public class Parser {
} }
} }
// power = ( function | primary ) , [ POWER , power ] ; // power = ( function | primary ) , [ POWER , unary ] ;
private Pattern power() throws SyntaxException { private Pattern power() throws SyntaxException {
Pattern pat = functionOrPrimary(); Pattern pat = functionOrPrimary();
if (match(POWER) != null) { if (match(POWER) != null) {
pat = new PowerPattern(pat, power()); pat = new PowerPattern(pat, unary());
} }
return pat; return pat;
} }

View File

@ -190,13 +190,18 @@ public class ParserTest {
new Token(REDUCTION, "reduction", 0), new Token(REDUCTION, "reduction", 0),
new Token(IDENTIFIER, "test", 0), new Token(IDENTIFIER, "test", 0),
new Token(COLON, ":", 0), new Token(COLON, ":", 0),
new Token(NUMBER, "1", 0),
new Token(DIVIDE, "/", 0),
new Token(LEFT_PAREN, "(", 0),
new Token(IDENTIFIER, "x", 0), new Token(IDENTIFIER, "x", 0),
new Token(TIMES, "*", 0), new Token(TIMES, "*", 0),
new Token(IDENTIFIER, "x", 0), new Token(IDENTIFIER, "x", 0),
new Token(RIGHT_PAREN, ")", 0),
new Token(ARROW, "->", 0), new Token(ARROW, "->", 0),
new Token(LEFT_BRACKET, "[", 0), new Token(LEFT_BRACKET, "[", 0),
new Token(IDENTIFIER, "x", 0), new Token(IDENTIFIER, "x", 0),
new Token(POWER, "^", 0), new Token(POWER, "^", 0),
new Token(MINUS, "-", 0),
new Token(NUMBER, "2", 0), new Token(NUMBER, "2", 0),
new Token(RIGHT_BRACKET, "]", 0), new Token(RIGHT_BRACKET, "]", 0),
new Token(EOF, "", 0) new Token(EOF, "", 0)
@ -206,13 +211,18 @@ public class ParserTest {
final List<PatternRule> expected = Collections.singletonList(new PatternRule( final List<PatternRule> expected = Collections.singletonList(new PatternRule(
"test", "test",
RuleType.REDUCTION, RuleType.REDUCTION,
new MultiplicationPattern( new DivisionPattern(
new SubFunctionPattern("x"), new NumberPattern(new BigDecimal(1)),
new SubFunctionPattern("x") new MultiplicationPattern(
new SubFunctionPattern("x"),
new SubFunctionPattern("x")
)
), ),
new PowerPattern( new PowerPattern(
new SubFunctionPattern("x"), new SubFunctionPattern("x"),
new NumberPattern(new BigDecimal(2)) new NegativePattern(
new NumberPattern(new BigDecimal(2))
)
) )
)); ));
assertEquals(expected, parser.parse()); assertEquals(expected, parser.parse());