parser: Deal with negation more "normally", parse -a² correctly

We were treating negation in a special way in the expression parser.
As a result, `-a²` was parsed as `(-a)²` instead of `-(a²)`.

Treat negation like a normal prefix, generating an object, which makes
it possible to deal with precedence the right way.

Signed-off-by: Christophe de Dinechin <christophe@dinechin.org>
This commit is contained in:
Christophe de Dinechin 2024-08-01 13:03:15 +02:00
parent a83a6057b7
commit 83d6536626

View file

@ -73,7 +73,6 @@ object::result list::list_parse(id type,
object_g postfix = nullptr;
object_g obj = nullptr;
object_g xroot_arg = nullptr;
bool negate = false;
int precedence = p.precedence;
int lowest = precedence;
uint arity = 0;
@ -188,10 +187,13 @@ object::result list::list_parse(id type,
// Check to see if we have a sign
else if (cp == '-' || cp == '+')
{
if (cp == '-')
negate = !negate;
s = utf8_next(s);
continue;
if (cp == '+')
{
s = utf8_next(s);
continue;
}
obj = static_object(ID_neg);
length = 1;
}
}
@ -343,11 +345,6 @@ object::result list::list_parse(id type,
obj = prefix;
prefix = nullptr;
}
else if (negate)
{
obj = command::static_object(ID_neg);
negate = false;
}
else if (postfix)
{
obj = postfix;