Liste des Groupes | Revenir à cl c |
On 29/03/2025 17:25, Janis Papanagnou wrote:On 26.03.2025 23:15, Keith Thompson wrote:[ dyadic and monadic minus operations, and minus sign at literals ]>
>
I don't know of any language that uses "-" for both negation (prefix,
one operand) and subtraction (infix, two operands) and treats -5
as a single token rather than a unary minus operator applied to the
constant/literal "5".
I as well don't know. It may simplify things if the '-' is detached
from a numeric positive literal, especially for numeric expressions.
>
There's certainly cases where a signed numeric token is appropriate,
thinking (for example) about a CONST declaration in Pascal, like
CONST a = -5; here you don't have expressions, just the primitive
type literals.[*]
>
The point why I think that a signed literal as entity may be useful
is that it's a static (compile time) property; -5 is in that respect
different from -x, the negation operation. A negative constant does
not "cost" a run-time operation.
>
Given what we recently discussed about a division by a constant,
where compilers (may) handle expressions by pre-calculating the
reciprocal for run-time efficiency, it's not that clear to me that
it matters either way.
>
I suppose it's a question of whether it complicates syntax definition
or parsing.
Having a single token like '-1234' is pointless. Most languages allow
you to add parentheses like '-(1234)' which must give the exact same
result.
And many also reduce expressions like '1234+1' to '12345' at compile
time. In this case you'd want '-(1234+1)' to be equivalent to '-1235'.
Or there may be a named alias 'X' for 1234, and later write '-X'.
[...]
Les messages affichés proviennent d'usenet.