From 2a347e6d194f7a7b33eb0c233f88c214688e1716 Mon Sep 17 00:00:00 2001 From: Riccardo Azzolini Date: Thu, 31 Jan 2019 13:10:02 +0100 Subject: [PATCH] Allow unary operators in power exponent --- .../warppi/math/rules/dsl/frontend/Parser.java | 4 ++-- .../math/rules/dsl/frontend/ParserTest.java | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Parser.java b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Parser.java index b45a7163..98d7ffbd 100644 --- a/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Parser.java +++ b/core/src/main/java/it/cavallium/warppi/math/rules/dsl/frontend/Parser.java @@ -149,11 +149,11 @@ public class Parser { } } - // power = ( function | primary ) , [ POWER , power ] ; + // power = ( function | primary ) , [ POWER , unary ] ; private Pattern power() throws SyntaxException { Pattern pat = functionOrPrimary(); if (match(POWER) != null) { - pat = new PowerPattern(pat, power()); + pat = new PowerPattern(pat, unary()); } return pat; } diff --git a/core/src/test/java/it/cavallium/warppi/math/rules/dsl/frontend/ParserTest.java b/core/src/test/java/it/cavallium/warppi/math/rules/dsl/frontend/ParserTest.java index c2687541..9e8c1b85 100644 --- a/core/src/test/java/it/cavallium/warppi/math/rules/dsl/frontend/ParserTest.java +++ b/core/src/test/java/it/cavallium/warppi/math/rules/dsl/frontend/ParserTest.java @@ -190,13 +190,18 @@ public class ParserTest { new Token(REDUCTION, "reduction", 0), new Token(IDENTIFIER, "test", 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(TIMES, "*", 0), new Token(IDENTIFIER, "x", 0), + new Token(RIGHT_PAREN, ")", 0), new Token(ARROW, "->", 0), new Token(LEFT_BRACKET, "[", 0), new Token(IDENTIFIER, "x", 0), new Token(POWER, "^", 0), + new Token(MINUS, "-", 0), new Token(NUMBER, "2", 0), new Token(RIGHT_BRACKET, "]", 0), new Token(EOF, "", 0) @@ -206,13 +211,18 @@ public class ParserTest { final List expected = Collections.singletonList(new PatternRule( "test", RuleType.REDUCTION, - new MultiplicationPattern( - new SubFunctionPattern("x"), - new SubFunctionPattern("x") + new DivisionPattern( + new NumberPattern(new BigDecimal(1)), + new MultiplicationPattern( + new SubFunctionPattern("x"), + new SubFunctionPattern("x") + ) ), new PowerPattern( new SubFunctionPattern("x"), - new NumberPattern(new BigDecimal(2)) + new NegativePattern( + new NumberPattern(new BigDecimal(2)) + ) ) )); assertEquals(expected, parser.parse());