From bce2a8460e22e85b7a9d38f2c8d40da475e1983b Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Mon, 20 Jul 2020 20:40:03 +0700 Subject: [PATCH 1/9] Add reference ANTLR grammar, implement missing rules and operations in parser, also add support for symbols in ASM --- kmath-ast/reference/ArithmeticsEvaluator.g4 | 59 +++++++++++++++ .../kotlin/scientifik/kmath/ast/MST.kt | 38 +++++----- .../kotlin/scientifik/kmath/ast/parser.kt | 71 ++++++++++++------- .../kotlin/scientifik/kmath/asm/asm.kt | 15 +++- .../kmath/asm/internal/AsmBuilder.kt | 2 +- .../kotlin/scietifik/kmath/ast/ParserTest.kt | 35 +++++++++ 6 files changed, 173 insertions(+), 47 deletions(-) create mode 100644 kmath-ast/reference/ArithmeticsEvaluator.g4 diff --git a/kmath-ast/reference/ArithmeticsEvaluator.g4 b/kmath-ast/reference/ArithmeticsEvaluator.g4 new file mode 100644 index 000000000..684a9de44 --- /dev/null +++ b/kmath-ast/reference/ArithmeticsEvaluator.g4 @@ -0,0 +1,59 @@ +grammar ArithmeticsEvaluator; + +fragment DIGIT: '0'..'9'; +fragment LETTER: 'a'..'z'; +fragment UNDERSCORE: '_'; + +ID: (LETTER | UNDERSCORE) (LETTER | UNDERSCORE | DIGIT)*; +NUM: (DIGIT | '.')+ ([eE] MINUS? DIGIT+)?; +MUL: '*'; +DIV: '/'; +PLUS: '+'; +MINUS: '-'; +POW: '^'; +COMMA: ','; +LPAR: '('; +RPAR: ')'; +WS: [ \n\t\r]+ -> skip; + +number + : NUM + ; + +singular + : ID + ; + +unaryFunction + : ID LPAR subSumChain RPAR + ; + +binaryFunction + : ID LPAR subSumChain COMMA subSumChain RPAR + ; + +term + : number + | singular + | unaryFunction + | binaryFunction + | MINUS term + | LPAR subSumChain RPAR + ; + +powChain + : term (POW term)* + ; + + +divMulChain + : powChain ((PLUS | MINUS) powChain)* + ; + +subSumChain + : divMulChain ((DIV | MUL) divMulChain)* + ; + +rootParser + : subSumChain EOF + ; diff --git a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt index 142d27f93..e87df882f 100644 --- a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt +++ b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt @@ -41,27 +41,25 @@ sealed class MST { //TODO add a function with named arguments -fun Algebra.evaluate(node: MST): T { - return when (node) { - is MST.Numeric -> (this as? NumericAlgebra)?.number(node.value) - ?: error("Numeric nodes are not supported by $this") - is MST.Symbolic -> symbol(node.value) - is MST.Unary -> unaryOperation(node.operation, evaluate(node.value)) - is MST.Binary -> when { - this !is NumericAlgebra -> binaryOperation(node.operation, evaluate(node.left), evaluate(node.right)) - node.left is MST.Numeric && node.right is MST.Numeric -> { - val number = RealField.binaryOperation( - node.operation, - node.left.value.toDouble(), - node.right.value.toDouble() - ) - number(number) - } - node.left is MST.Numeric -> leftSideNumberOperation(node.operation, node.left.value, evaluate(node.right)) - node.right is MST.Numeric -> rightSideNumberOperation(node.operation, evaluate(node.left), node.right.value) - else -> binaryOperation(node.operation, evaluate(node.left), evaluate(node.right)) +fun Algebra.evaluate(node: MST): T = when (node) { + is MST.Numeric -> (this as? NumericAlgebra)?.number(node.value) + ?: error("Numeric nodes are not supported by $this") + is MST.Symbolic -> symbol(node.value) + is MST.Unary -> unaryOperation(node.operation, evaluate(node.value)) + is MST.Binary -> when { + this !is NumericAlgebra -> binaryOperation(node.operation, evaluate(node.left), evaluate(node.right)) + node.left is MST.Numeric && node.right is MST.Numeric -> { + val number = RealField.binaryOperation( + node.operation, + node.left.value.toDouble(), + node.right.value.toDouble() + ) + number(number) } + node.left is MST.Numeric -> leftSideNumberOperation(node.operation, node.left.value, evaluate(node.right)) + node.right is MST.Numeric -> rightSideNumberOperation(node.operation, evaluate(node.left), node.right.value) + else -> binaryOperation(node.operation, evaluate(node.left), evaluate(node.right)) } } -fun MST.compile(algebra: Algebra): T = algebra.evaluate(this) \ No newline at end of file +fun MST.compile(algebra: Algebra): T = algebra.evaluate(this) diff --git a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt index 30a92c5ae..ae0b6656d 100644 --- a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt +++ b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt @@ -5,6 +5,8 @@ import com.github.h0tk3y.betterParse.grammar.Grammar import com.github.h0tk3y.betterParse.grammar.parseToEnd import com.github.h0tk3y.betterParse.grammar.parser import com.github.h0tk3y.betterParse.grammar.tryParseToEnd +import com.github.h0tk3y.betterParse.lexer.Token +import com.github.h0tk3y.betterParse.lexer.TokenMatch import com.github.h0tk3y.betterParse.parser.ParseResult import com.github.h0tk3y.betterParse.parser.Parser import scientifik.kmath.operations.FieldOperations @@ -15,45 +17,66 @@ import scientifik.kmath.operations.SpaceOperations /** * TODO move to common */ -private object ArithmeticsEvaluator : Grammar() { - val num by token("-?[\\d.]+(?:[eE]-?\\d+)?".toRegex()) - val lpar by token("\\(".toRegex()) - val rpar by token("\\)".toRegex()) - val mul by token("\\*".toRegex()) - val pow by token("\\^".toRegex()) - val div by token("/".toRegex()) - val minus by token("-".toRegex()) - val plus by token("\\+".toRegex()) - val ws by token("\\s+".toRegex(), ignore = true) +object ArithmeticsEvaluator : Grammar() { + private val num: Token by token("[\\d.]+(?:[eE]-?\\d+)?".toRegex()) + private val id: Token by token("[a-z_][\\da-z_]*".toRegex()) + private val lpar: Token by token("\\(".toRegex()) + private val rpar: Token by token("\\)".toRegex()) + private val comma: Token by token(",".toRegex()) + private val mul: Token by token("\\*".toRegex()) + private val pow: Token by token("\\^".toRegex()) + private val div: Token by token("/".toRegex()) + private val minus: Token by token("-".toRegex()) + private val plus: Token by token("\\+".toRegex()) + private val ws: Token by token("\\s+".toRegex(), ignore = true) - val number: Parser by num use { MST.Numeric(text.toDouble()) } + private val number: Parser by num use { MST.Numeric(text.toDouble()) } + private val singular: Parser by id use { MST.Symbolic(text) } - val term: Parser by number or - (skip(minus) and parser(this::term) map { MST.Unary(SpaceOperations.MINUS_OPERATION, it) }) or - (skip(lpar) and parser(this::rootParser) and skip(rpar)) + private val unaryFunction: Parser by (id and skip(lpar) and parser(::subSumChain) and skip(rpar)) + .map { (id, term) -> MST.Unary(id.text, term) } - val powChain by leftAssociative(term, pow) { a, _, b -> + private val binaryFunction: Parser by id + .and(skip(lpar)) + .and(parser(::subSumChain)) + .and(skip(comma)) + .and(parser(::subSumChain)) + .and(skip(rpar)) + .map { (id, left, right) -> MST.Binary(id.text, left, right) } + + private val term: Parser by number + .or(binaryFunction) + .or(unaryFunction) + .or(singular) + .or(skip(minus) and parser(::term) map { MST.Unary(SpaceOperations.MINUS_OPERATION, it) }) + .or(skip(lpar) and parser(::subSumChain) and skip(rpar)) + + private val powChain: Parser by leftAssociative(term = term, operator = pow) { a, _, b -> MST.Binary(PowerOperations.POW_OPERATION, a, b) } - val divMulChain: Parser by leftAssociative(powChain, div or mul use { type }) { a, op, b -> - if (op == div) { + private val divMulChain: Parser by leftAssociative( + term = powChain, + operator = div or mul use TokenMatch::type + ) { a, op, b -> + if (op == div) MST.Binary(FieldOperations.DIV_OPERATION, a, b) - } else { + else MST.Binary(RingOperations.TIMES_OPERATION, a, b) - } } - val subSumChain: Parser by leftAssociative(divMulChain, plus or minus use { type }) { a, op, b -> - if (op == plus) { + private val subSumChain: Parser by leftAssociative( + term = divMulChain, + operator = plus or minus use TokenMatch::type + ) { a, op, b -> + if (op == plus) MST.Binary(SpaceOperations.PLUS_OPERATION, a, b) - } else { + else MST.Binary(SpaceOperations.MINUS_OPERATION, a, b) - } } override val rootParser: Parser by subSumChain } fun String.tryParseMath(): ParseResult = ArithmeticsEvaluator.tryParseToEnd(this) -fun String.parseMath(): MST = ArithmeticsEvaluator.parseToEnd(this) \ No newline at end of file +fun String.parseMath(): MST = ArithmeticsEvaluator.parseToEnd(this) diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt index f0022a43a..ee0ea15ff 100644 --- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/asm.kt @@ -8,7 +8,6 @@ import scientifik.kmath.ast.MST import scientifik.kmath.ast.MstExpression import scientifik.kmath.expressions.Expression import scientifik.kmath.operations.Algebra -import scientifik.kmath.operations.NumericAlgebra import kotlin.reflect.KClass /** @@ -17,7 +16,19 @@ import kotlin.reflect.KClass fun MST.compileWith(type: KClass, algebra: Algebra): Expression { fun AsmBuilder.visit(node: MST) { when (node) { - is MST.Symbolic -> loadVariable(node.value) + is MST.Symbolic -> { + val symbol = try { + algebra.symbol(node.value) + } catch (ignored: Throwable) { + null + } + + if (symbol != null) + loadTConstant(symbol) + else + loadVariable(node.value) + } + is MST.Numeric -> loadNumeric(node.value) is MST.Unary -> buildAlgebraOperationCall( diff --git a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt index 5531fd5dc..a947b3478 100644 --- a/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt +++ b/kmath-ast/src/jvmMain/kotlin/scientifik/kmath/asm/internal/AsmBuilder.kt @@ -288,7 +288,7 @@ internal class AsmBuilder internal constructor( /** * Loads a [T] constant from [constants]. */ - private fun loadTConstant(value: T) { + internal fun loadTConstant(value: T) { if (classOfT in INLINABLE_NUMBERS) { val expectedType = expectationStack.pop() val mustBeBoxed = expectedType.sort == Type.OBJECT diff --git a/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserTest.kt b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserTest.kt index 5394a4b00..9179c3428 100644 --- a/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserTest.kt +++ b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserTest.kt @@ -4,8 +4,10 @@ import scientifik.kmath.ast.evaluate import scientifik.kmath.ast.mstInField import scientifik.kmath.ast.parseMath import scientifik.kmath.expressions.invoke +import scientifik.kmath.operations.Algebra import scientifik.kmath.operations.Complex import scientifik.kmath.operations.ComplexField +import scientifik.kmath.operations.RealField import kotlin.test.Test import kotlin.test.assertEquals @@ -22,4 +24,37 @@ internal class ParserTest { val res = ComplexField.mstInField { number(2) + number(2) * (number(2) + number(2)) }() assertEquals(Complex(10.0, 0.0), res) } + + @Test + fun `evaluate MST with singular`() { + val mst = "i".parseMath() + val res = ComplexField.evaluate(mst) + assertEquals(ComplexField.i, res) + } + + + @Test + fun `evaluate MST with unary function`() { + val mst = "sin(0)".parseMath() + val res = RealField.evaluate(mst) + assertEquals(0.0, res) + } + + @Test + fun `evaluate MST with binary function`() { + val magicalAlgebra = object : Algebra { + override fun symbol(value: String): String = value + + override fun unaryOperation(operation: String, arg: String): String = throw NotImplementedError() + + override fun binaryOperation(operation: String, left: String, right: String): String = when (operation) { + "magic" -> "$left ★ $right" + else -> throw NotImplementedError() + } + } + + val mst = "magic(a, b)".parseMath() + val res = magicalAlgebra.evaluate(mst) + assertEquals("a ★ b", res) + } } -- 2.34.1 From 2864b2d12e4ad7100b2978a5f4378fd4cb5b846b Mon Sep 17 00:00:00 2001 From: Iaroslav Postovalov Date: Sun, 26 Jul 2020 11:17:47 +0700 Subject: [PATCH 2/9] Move https://dl.bintray.com/hotkeytlt/maven to another RepositoryHandler --- build.gradle.kts | 1 + kmath-ast/build.gradle.kts | 12 ++---------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 052b457c5..8a2ba3617 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,6 +11,7 @@ allprojects { repositories { jcenter() maven("https://dl.bintray.com/kotlin/kotlinx") + maven("https://dl.bintray.com/hotkeytlt/maven") } group = "scientifik" diff --git a/kmath-ast/build.gradle.kts b/kmath-ast/build.gradle.kts index 511571fc9..4f67d57eb 100644 --- a/kmath-ast/build.gradle.kts +++ b/kmath-ast/build.gradle.kts @@ -1,10 +1,4 @@ -plugins { - id("scientifik.mpp") -} - -repositories { - maven("https://dl.bintray.com/hotkeytlt/maven") -} +plugins { id("scientifik.mpp") } kotlin.sourceSets { // all { @@ -30,8 +24,6 @@ kotlin.sourceSets { } jsMain { - dependencies { - implementation("com.github.h0tk3y.betterParse:better-parse-js:0.4.0-alpha-3") - } + dependencies { implementation("com.github.h0tk3y.betterParse:better-parse-js:0.4.0-alpha-3") } } } \ No newline at end of file -- 2.34.1 From 45087f8b2de6b615ea627400b8dd12edc8e0918d Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Mon, 27 Jul 2020 15:37:18 +0700 Subject: [PATCH 3/9] Add capital latin letters support --- kmath-ast/reference/ArithmeticsEvaluator.g4 | 3 ++- kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/kmath-ast/reference/ArithmeticsEvaluator.g4 b/kmath-ast/reference/ArithmeticsEvaluator.g4 index 684a9de44..137d42860 100644 --- a/kmath-ast/reference/ArithmeticsEvaluator.g4 +++ b/kmath-ast/reference/ArithmeticsEvaluator.g4 @@ -2,9 +2,10 @@ grammar ArithmeticsEvaluator; fragment DIGIT: '0'..'9'; fragment LETTER: 'a'..'z'; +fragment CAPITAL_LETTER: 'A'..'Z' fragment UNDERSCORE: '_'; -ID: (LETTER | UNDERSCORE) (LETTER | UNDERSCORE | DIGIT)*; +ID: (LETTER | UNDERSCORE | CAPITAL_LETTER) (LETTER | UNDERSCORE | DIGIT | CAPITAL_LETTER)*; NUM: (DIGIT | '.')+ ([eE] MINUS? DIGIT+)?; MUL: '*'; DIV: '/'; diff --git a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt index ae0b6656d..27f133d6a 100644 --- a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt +++ b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt @@ -19,7 +19,7 @@ import scientifik.kmath.operations.SpaceOperations */ object ArithmeticsEvaluator : Grammar() { private val num: Token by token("[\\d.]+(?:[eE]-?\\d+)?".toRegex()) - private val id: Token by token("[a-z_][\\da-z_]*".toRegex()) + private val id: Token by token("[a-z_A-Z][\\da-z_A-Z]*".toRegex()) private val lpar: Token by token("\\(".toRegex()) private val rpar: Token by token("\\)".toRegex()) private val comma: Token by token(",".toRegex()) -- 2.34.1 From a5499260fae7e412da6ca6fcd9769382d7d9357f Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Mon, 27 Jul 2020 15:45:05 +0700 Subject: [PATCH 4/9] Update num rule to match 1e+1 like numbers --- kmath-ast/reference/ArithmeticsEvaluator.g4 | 8 ++++---- .../src/commonMain/kotlin/scientifik/kmath/ast/parser.kt | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/kmath-ast/reference/ArithmeticsEvaluator.g4 b/kmath-ast/reference/ArithmeticsEvaluator.g4 index 137d42860..ba8166bc8 100644 --- a/kmath-ast/reference/ArithmeticsEvaluator.g4 +++ b/kmath-ast/reference/ArithmeticsEvaluator.g4 @@ -2,11 +2,11 @@ grammar ArithmeticsEvaluator; fragment DIGIT: '0'..'9'; fragment LETTER: 'a'..'z'; -fragment CAPITAL_LETTER: 'A'..'Z' +fragment CAPITAL_LETTER: 'A'..'Z'; fragment UNDERSCORE: '_'; ID: (LETTER | UNDERSCORE | CAPITAL_LETTER) (LETTER | UNDERSCORE | DIGIT | CAPITAL_LETTER)*; -NUM: (DIGIT | '.')+ ([eE] MINUS? DIGIT+)?; +NUM: (DIGIT | '.')+ ([eE] (MINUS? | PLUS?) DIGIT+)?; MUL: '*'; DIV: '/'; PLUS: '+'; @@ -17,7 +17,7 @@ LPAR: '('; RPAR: ')'; WS: [ \n\t\r]+ -> skip; -number +num : NUM ; @@ -34,7 +34,7 @@ binaryFunction ; term - : number + : num | singular | unaryFunction | binaryFunction diff --git a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt index 27f133d6a..a0b729ffe 100644 --- a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt +++ b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt @@ -18,7 +18,7 @@ import scientifik.kmath.operations.SpaceOperations * TODO move to common */ object ArithmeticsEvaluator : Grammar() { - private val num: Token by token("[\\d.]+(?:[eE]-?\\d+)?".toRegex()) + private val num: Token by token("[\\d.]+(?:[eE][-+]?\\d+)?".toRegex()) private val id: Token by token("[a-z_A-Z][\\da-z_A-Z]*".toRegex()) private val lpar: Token by token("\\(".toRegex()) private val rpar: Token by token("\\)".toRegex()) -- 2.34.1 From f8383deb708e0b88a89ff76ddb0021c1facf5464 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Mon, 27 Jul 2020 15:49:36 +0700 Subject: [PATCH 5/9] Fix operators in reference --- kmath-ast/reference/ArithmeticsEvaluator.g4 | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/kmath-ast/reference/ArithmeticsEvaluator.g4 b/kmath-ast/reference/ArithmeticsEvaluator.g4 index ba8166bc8..9835ec7f7 100644 --- a/kmath-ast/reference/ArithmeticsEvaluator.g4 +++ b/kmath-ast/reference/ArithmeticsEvaluator.g4 @@ -46,13 +46,12 @@ powChain : term (POW term)* ; - divMulChain - : powChain ((PLUS | MINUS) powChain)* + : powChain ((DIV | MUL) powChain)* ; subSumChain - : divMulChain ((DIV | MUL) divMulChain)* + : divMulChain ((PLUS | MINUS) divMulChain)* ; rootParser -- 2.34.1 From 1ebd3626969e07b3ee16fe4f77a03b2315563fcd Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Mon, 27 Jul 2020 15:58:09 +0700 Subject: [PATCH 6/9] Update num token in reference --- kmath-ast/reference/ArithmeticsEvaluator.g4 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kmath-ast/reference/ArithmeticsEvaluator.g4 b/kmath-ast/reference/ArithmeticsEvaluator.g4 index 9835ec7f7..dc47b23fb 100644 --- a/kmath-ast/reference/ArithmeticsEvaluator.g4 +++ b/kmath-ast/reference/ArithmeticsEvaluator.g4 @@ -6,7 +6,7 @@ fragment CAPITAL_LETTER: 'A'..'Z'; fragment UNDERSCORE: '_'; ID: (LETTER | UNDERSCORE | CAPITAL_LETTER) (LETTER | UNDERSCORE | DIGIT | CAPITAL_LETTER)*; -NUM: (DIGIT | '.')+ ([eE] (MINUS? | PLUS?) DIGIT+)?; +NUM: (DIGIT | '.')+ ([eE] [-+]? DIGIT+)?; MUL: '*'; DIV: '/'; PLUS: '+'; -- 2.34.1 From c64a89c6b6296517d7c7b9690e7ae319ae54fcd8 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Mon, 27 Jul 2020 19:27:59 +0700 Subject: [PATCH 7/9] Implement power as binary operation with unchecked cast, add tests on parser precedence --- .../kotlin/scientifik/kmath/ast/MST.kt | 8 ++--- .../kmath/ast/ParserPrecedenceTest.kt | 36 +++++++++++++++++++ .../kmath/operations/NumberAlgebra.kt | 11 ++++-- 3 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserPrecedenceTest.kt diff --git a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt index e87df882f..1127c18aa 100644 --- a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt +++ b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt @@ -2,7 +2,6 @@ package scientifik.kmath.ast import scientifik.kmath.operations.Algebra import scientifik.kmath.operations.NumericAlgebra -import scientifik.kmath.operations.RealField /** * A Mathematical Syntax Tree node for mathematical expressions @@ -49,12 +48,11 @@ fun Algebra.evaluate(node: MST): T = when (node) { is MST.Binary -> when { this !is NumericAlgebra -> binaryOperation(node.operation, evaluate(node.left), evaluate(node.right)) node.left is MST.Numeric && node.right is MST.Numeric -> { - val number = RealField.binaryOperation( + binaryOperation( node.operation, - node.left.value.toDouble(), - node.right.value.toDouble() + number(node.left.value), + number(node.right.value) ) - number(number) } node.left is MST.Numeric -> leftSideNumberOperation(node.operation, node.left.value, evaluate(node.right)) node.right is MST.Numeric -> rightSideNumberOperation(node.operation, evaluate(node.left), node.right.value) diff --git a/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserPrecedenceTest.kt b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserPrecedenceTest.kt new file mode 100644 index 000000000..9bdbb12c9 --- /dev/null +++ b/kmath-ast/src/jvmTest/kotlin/scietifik/kmath/ast/ParserPrecedenceTest.kt @@ -0,0 +1,36 @@ +package scietifik.kmath.ast + +import scientifik.kmath.ast.evaluate +import scientifik.kmath.ast.parseMath +import scientifik.kmath.operations.Field +import scientifik.kmath.operations.RealField +import kotlin.test.Test +import kotlin.test.assertEquals + +internal class ParserPrecedenceTest { + private val f: Field = RealField + + @Test + fun test1(): Unit = assertEquals(6.0, f.evaluate("2*2+2".parseMath())) + + @Test + fun test2(): Unit = assertEquals(6.0, f.evaluate("2+2*2".parseMath())) + + @Test + fun test3(): Unit = assertEquals(10.0, f.evaluate("2^3+2".parseMath())) + + @Test + fun test4(): Unit = assertEquals(10.0, f.evaluate("2+2^3".parseMath())) + + @Test + fun test5(): Unit = assertEquals(16.0, f.evaluate("2^3*2".parseMath())) + + @Test + fun test6(): Unit = assertEquals(16.0, f.evaluate("2*2^3".parseMath())) + + @Test + fun test7(): Unit = assertEquals(18.0, f.evaluate("2+2^3*2".parseMath())) + + @Test + fun test8(): Unit = assertEquals(18.0, f.evaluate("2*2^3+2".parseMath())) +} diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt index 953c5a112..ca5fa68f0 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt @@ -34,6 +34,13 @@ interface ExtendedField : ExtendedFieldOperations, Field { } } +interface NumberExtendedField : ExtendedField { + override fun binaryOperation(operation: String, left: T, right: T): T = when (operation) { + PowerOperations.POW_OPERATION -> power(left, right as Number) + else -> super.binaryOperation(operation, left, right) + } +} + /** * Real field element wrapping double. * @@ -53,7 +60,7 @@ inline class Real(val value: Double) : FieldElement { * A field for double without boxing. Does not produce appropriate field element */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") -object RealField : ExtendedField, Norm { +object RealField : NumberExtendedField, Norm { override val zero: Double = 0.0 override inline fun add(a: Double, b: Double) = a + b override inline fun multiply(a: Double, b: Double) = a * b @@ -88,7 +95,7 @@ object RealField : ExtendedField, Norm { } @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") -object FloatField : ExtendedField, Norm { +object FloatField : NumberExtendedField, Norm { override val zero: Float = 0f override inline fun add(a: Float, b: Float) = a + b override inline fun multiply(a: Float, b: Float) = a * b -- 2.34.1 From 0995dca8b857d07d5739dc34e8b75e8df78703f6 Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Mon, 27 Jul 2020 20:46:57 +0700 Subject: [PATCH 8/9] Delete unchecked cast, revert evaluate changes, add RealField handling "pow" operation --- .../kotlin/scientifik/kmath/ast/MST.kt | 11 ++++++++--- .../kmath/operations/NumberAlgebra.kt | 17 ++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt index 1127c18aa..46558cbfb 100644 --- a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt +++ b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/MST.kt @@ -2,6 +2,7 @@ package scientifik.kmath.ast import scientifik.kmath.operations.Algebra import scientifik.kmath.operations.NumericAlgebra +import scientifik.kmath.operations.RealField /** * A Mathematical Syntax Tree node for mathematical expressions @@ -47,13 +48,17 @@ fun Algebra.evaluate(node: MST): T = when (node) { is MST.Unary -> unaryOperation(node.operation, evaluate(node.value)) is MST.Binary -> when { this !is NumericAlgebra -> binaryOperation(node.operation, evaluate(node.left), evaluate(node.right)) + node.left is MST.Numeric && node.right is MST.Numeric -> { - binaryOperation( + val number = RealField.binaryOperation( node.operation, - number(node.left.value), - number(node.right.value) + node.left.value.toDouble(), + node.right.value.toDouble() ) + + number(number) } + node.left is MST.Numeric -> leftSideNumberOperation(node.operation, node.left.value, evaluate(node.right)) node.right is MST.Numeric -> rightSideNumberOperation(node.operation, evaluate(node.left), node.right.value) else -> binaryOperation(node.operation, evaluate(node.left), evaluate(node.right)) diff --git a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt index ca5fa68f0..2c57d0098 100644 --- a/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt +++ b/kmath-core/src/commonMain/kotlin/scientifik/kmath/operations/NumberAlgebra.kt @@ -1,5 +1,6 @@ package scientifik.kmath.operations +import scientifik.kmath.operations.RealField.pow import kotlin.math.abs import kotlin.math.pow as kpow @@ -34,13 +35,6 @@ interface ExtendedField : ExtendedFieldOperations, Field { } } -interface NumberExtendedField : ExtendedField { - override fun binaryOperation(operation: String, left: T, right: T): T = when (operation) { - PowerOperations.POW_OPERATION -> power(left, right as Number) - else -> super.binaryOperation(operation, left, right) - } -} - /** * Real field element wrapping double. * @@ -60,7 +54,7 @@ inline class Real(val value: Double) : FieldElement { * A field for double without boxing. Does not produce appropriate field element */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") -object RealField : NumberExtendedField, Norm { +object RealField : ExtendedField, Norm { override val zero: Double = 0.0 override inline fun add(a: Double, b: Double) = a + b override inline fun multiply(a: Double, b: Double) = a * b @@ -92,10 +86,15 @@ object RealField : NumberExtendedField, Norm { override inline fun Double.times(b: Double) = this * b override inline fun Double.div(b: Double) = this / b + + override fun binaryOperation(operation: String, left: Double, right: Double): Double = when (operation) { + PowerOperations.POW_OPERATION -> left pow right + else -> super.binaryOperation(operation, left, right) + } } @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") -object FloatField : NumberExtendedField, Norm { +object FloatField : ExtendedField, Norm { override val zero: Float = 0f override inline fun add(a: Float, b: Float) = a + b override inline fun multiply(a: Float, b: Float) = a * b -- 2.34.1 From 11c98d6acfaf955f632e24d9ab77a54d002aefec Mon Sep 17 00:00:00 2001 From: Iaroslav Date: Mon, 27 Jul 2020 23:06:28 +0700 Subject: [PATCH 9/9] Upgrade better-parse to 0.4.0 --- kmath-ast/build.gradle.kts | 8 +----- .../kotlin/scientifik/kmath/ast/parser.kt | 26 ++++++++++--------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/kmath-ast/build.gradle.kts b/kmath-ast/build.gradle.kts index 4f67d57eb..d13a7712d 100644 --- a/kmath-ast/build.gradle.kts +++ b/kmath-ast/build.gradle.kts @@ -9,21 +9,15 @@ kotlin.sourceSets { commonMain { dependencies { api(project(":kmath-core")) - implementation("com.github.h0tk3y.betterParse:better-parse-multiplatform:0.4.0-alpha-3") - implementation("com.github.h0tk3y.betterParse:better-parse-multiplatform-metadata:0.4.0-alpha-3") + implementation("com.github.h0tk3y.betterParse:better-parse:0.4.0") } } jvmMain { dependencies { - implementation("com.github.h0tk3y.betterParse:better-parse-jvm:0.4.0-alpha-3") implementation("org.ow2.asm:asm:8.0.1") implementation("org.ow2.asm:asm-commons:8.0.1") implementation(kotlin("reflect")) } } - - jsMain { - dependencies { implementation("com.github.h0tk3y.betterParse:better-parse-js:0.4.0-alpha-3") } - } } \ No newline at end of file diff --git a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt index a0b729ffe..11797d79f 100644 --- a/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt +++ b/kmath-ast/src/commonMain/kotlin/scientifik/kmath/ast/parser.kt @@ -7,6 +7,7 @@ import com.github.h0tk3y.betterParse.grammar.parser import com.github.h0tk3y.betterParse.grammar.tryParseToEnd import com.github.h0tk3y.betterParse.lexer.Token import com.github.h0tk3y.betterParse.lexer.TokenMatch +import com.github.h0tk3y.betterParse.lexer.regexToken import com.github.h0tk3y.betterParse.parser.ParseResult import com.github.h0tk3y.betterParse.parser.Parser import scientifik.kmath.operations.FieldOperations @@ -15,20 +16,21 @@ import scientifik.kmath.operations.RingOperations import scientifik.kmath.operations.SpaceOperations /** - * TODO move to common + * TODO move to core */ object ArithmeticsEvaluator : Grammar() { - private val num: Token by token("[\\d.]+(?:[eE][-+]?\\d+)?".toRegex()) - private val id: Token by token("[a-z_A-Z][\\da-z_A-Z]*".toRegex()) - private val lpar: Token by token("\\(".toRegex()) - private val rpar: Token by token("\\)".toRegex()) - private val comma: Token by token(",".toRegex()) - private val mul: Token by token("\\*".toRegex()) - private val pow: Token by token("\\^".toRegex()) - private val div: Token by token("/".toRegex()) - private val minus: Token by token("-".toRegex()) - private val plus: Token by token("\\+".toRegex()) - private val ws: Token by token("\\s+".toRegex(), ignore = true) + // TODO replace with "...".toRegex() when better-parse 0.4.1 is released + private val num: Token by regexToken("[\\d.]+(?:[eE][-+]?\\d+)?") + private val id: Token by regexToken("[a-z_A-Z][\\da-z_A-Z]*") + private val lpar: Token by regexToken("\\(") + private val rpar: Token by regexToken("\\)") + private val comma: Token by regexToken(",") + private val mul: Token by regexToken("\\*") + private val pow: Token by regexToken("\\^") + private val div: Token by regexToken("/") + private val minus: Token by regexToken("-") + private val plus: Token by regexToken("\\+") + private val ws: Token by regexToken("\\s+", ignore = true) private val number: Parser by num use { MST.Numeric(text.toDouble()) } private val singular: Parser by id use { MST.Symbolic(text) } -- 2.34.1