Closed ps-george closed 7 years ago
The brackets rule for functions is to ensure that this is disallowed:
exp 10
and you have to do this:
exp(10)
So only one level of brackets is needed, though more than one level is ok too (though the AST will strip it away).
In principle we could treat the functions as unary prefix operators, like !
or ~
in C, as
that would also match the way that maths is written free-hand and in many text-books
and academic papers.
What I wanted to avoid was the ambiguity of this:
exp x * 10
as it could be interpreted as:
(exp x) * 10
or:
exp (x * 10)
This could have been resolved in the grammar, but I thought it more interesting if the function production didn't just follow the pattern of all the others (though it is very similar).
The reason -exp(x+x)
is invalid is because of the -
, as there are no
unary operators in this language. So it would have to be expressed
as 0-exp(x+x)
or -1 * exp(x+x)
. Again, a deliberate decision to encourage
people to think about the difference between -
as part of a number token,
and -
as a token for binary subtraction.
Do we need two sets of brackets, and is one set of brackets invalid?
sqrt((x+c))
vssqrt(x+c)
Is the reason
-exp(x+x)
is invalid due to the-
or the fact there is only one set of brackets?