Implicit Multiplication
Equals recognizes implicit multiplication. For example, we can write 3(4)
and understand that the answer should be 12
.
A token with respect to the following is (according to the “New Oxford American Dictionary 3rd”) “an individual occurrence of a symbol or string.”
We only need two tokens in order to recognize that two things are being implicitly multiplied:
Current Token | |||||
---|---|---|---|---|---|
Number | Operator | Variable | Function | ||
Previous Token |
Number | Yes | Maybe | Yes | Yes |
Operator | Maybe | Maybe | Maybe | Maybe | |
Variable | Yes | Maybe | Yes | Yes | |
Function | No | Maybe | No | No |
Handling Operators
Handling implicit multiplication when operator tokens are involved is tricky. Under normal circumstances, an operator followed by an operator should not result in implicit multiplication. For example, if you have 1 + + 3
, this should be 1 + 3
(the second +
being the unary positive, and is thus discarded). It would be incorrect to insert a multiplier between the two +
signs.
However, under other circumstances, you do want to insert a multipler. The common case of “(2)(3)
” should result in a multiplier being inserted in between the inner two parentheses (which are both considered operators by the tokenizer).
Thus, a multiplier is inserted in conjunction with operators if:
- the previous token is a number, a variable, or a left associative unary operator (factorial, closing parenthesis, etc.) and
- the current token is a number, a variable, or a right associative unary operator (logical not, opening parenthesis, etc.)
Functions and Implicit multiplication
A multiplier token is never inserted after a function token, because the rules of argumentless functions make it impossible for a function token to be followed by anything other than an opening parenthesis (an operator).