diff --git a/data/evaluatto/src/main/java/io/github/sadellie/evaluatto/Tokenizer.kt b/data/evaluatto/src/main/java/io/github/sadellie/evaluatto/Tokenizer.kt index e3a74bc5..9c45dc4d 100644 --- a/data/evaluatto/src/main/java/io/github/sadellie/evaluatto/Tokenizer.kt +++ b/data/evaluatto/src/main/java/io/github/sadellie/evaluatto/Tokenizer.kt @@ -25,7 +25,7 @@ sealed class TokenizerException(override val message: String) : Exception(messag } class Tokenizer(private val streamOfTokens: String) { - // Do this in init? + // Don't create object at all? fun tokenize(): List { var cursor = 0 val tokens: MutableList = mutableListOf() @@ -76,6 +76,13 @@ class Tokenizer(private val streamOfTokens: String) { .missingClosingBrackets() .missingMultiply() .unpackAlPercents() + // input like 80%80% should be treated as 80%-80%. + // After unpacking we get (80/100)(80/100), the multiply is missing + // No, we can't unpack before fixing missing multiply. + // Ideally we we need to add missing multiply for 80%80% + // In that case unpackAlPercents gets input with all operators 80%*80% in this case + // Can't be done right now since missingMultiply checks for tokens in front only + .missingMultiply() } private fun List.missingClosingBrackets(): List { @@ -174,7 +181,7 @@ class Tokenizer(private val streamOfTokens: String) { // Add "/ 100" and other stuff mutList.addAll( - percentIndex+1, + percentIndex + 1, listOf( Token.Operator.divide, "100", @@ -192,7 +199,7 @@ class Tokenizer(private val streamOfTokens: String) { private fun List.getNumberOrExpressionBefore(pos: Int): List { val digits = Token.Digit.all.map { it[0] } - val tokenInFront = this[pos-1] + val tokenInFront = this[pos - 1] // Just number if (tokenInFront.all { it in digits }) return listOf(tokenInFront) @@ -234,5 +241,4 @@ class Tokenizer(private val streamOfTokens: String) { return this.subList(cursor, pos) } - } diff --git a/data/evaluatto/src/test/java/io/github/sadellie/evaluatto/FixLexiconTest.kt b/data/evaluatto/src/test/java/io/github/sadellie/evaluatto/FixLexiconTest.kt index d0d9d4fe..4a3f3ffa 100644 --- a/data/evaluatto/src/test/java/io/github/sadellie/evaluatto/FixLexiconTest.kt +++ b/data/evaluatto/src/test/java/io/github/sadellie/evaluatto/FixLexiconTest.kt @@ -118,5 +118,7 @@ class FixLexiconTest { assertLex( "((90÷100)÷(90÷100))+((90÷100)−(90÷100×((90÷100))))", "(90%÷90%)+(90%−90%)" ) + + assertLex("(80÷100)×(80÷100)", "80%80%") } }