Skip to content

Draft: Move multiplication of variables to the lexer

Jaycee Santos requested to merge jayceesantos/gnome-calculator:branch into master

Apparently multiplication of variables is done in lib/equation-parser.vala. There is a FIXME comment mentioning that it should be done in lib/equation-lexer.vala.

This is my attempt.

The previous implementation prioritizes multiplication of variables, which leads to misleading results for expressions such as 2xx^2. 2xx would be solved before applying the power. The misleading result would be displayed. Reimplementing variable multiplication in the lexer fixes this.

Doing so led to some changes in custom functions and exponential functions. Code had to be added to the lexer to check for the creation of long-name variables or custom functions, which somehow affected the results of complex numbers.

For example, e^iπ will no longer return -1. However, e^(i*π) returns -1. I think the former was a result of the variable multiplication in the parser.

As for custom functions, parentheses will be required when long-name variables are used as operands in the custom function definition. I have updated the example in the help page for functions.

I had to modify some unit tests to accept the new behavior.

Hopefully this is an acceptable tradeoff to avoid misleading results like in the GitLab issue below.

Closes #307

Edited by Jaycee Santos

Merge request reports