diff --git a/app/src/main/java/com/darkempire78/opencalculator/activities/MainActivity.kt b/app/src/main/java/com/darkempire78/opencalculator/activities/MainActivity.kt index ea8c0777..dabebf51 100644 --- a/app/src/main/java/com/darkempire78/opencalculator/activities/MainActivity.kt +++ b/app/src/main/java/com/darkempire78/opencalculator/activities/MainActivity.kt @@ -973,6 +973,8 @@ class MainActivity : AppCompatActivity() { val calculation = binding.input.text.toString() + Expression().addParenthesis(calculation) + if (calculation != "") { val resultString = calculationResult.toString() diff --git a/app/src/main/java/com/darkempire78/opencalculator/calculator/parser/Expression.kt b/app/src/main/java/com/darkempire78/opencalculator/calculator/parser/Expression.kt index 9694d236..04a41c57 100644 --- a/app/src/main/java/com/darkempire78/opencalculator/calculator/parser/Expression.kt +++ b/app/src/main/java/com/darkempire78/opencalculator/calculator/parser/Expression.kt @@ -102,7 +102,7 @@ class Expression { } - private fun addParenthesis(calculation: String): String { + fun addParenthesis(calculation: String): String { // Add ")" which lack var cleanCalculation = calculation var openParentheses = 0 @@ -121,6 +121,10 @@ class Expression { cleanCalculation += ')' } } + // Trigger syntax error if missing '(' + if (closeParentheses > openParentheses) { + syntax_error = true + } return cleanCalculation }