Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #33205
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: -2 ** 2 |
| Date | 2017-05-05 08:17 +0200 |
| Organization | PointedEars Software (PES) |
| Message-ID | <1634774.atdPhlSkOF@PointedEars.de> (permalink) |
| References | <exponentiation-20170504145841@ram.dialup.fu-berlin.de> |
Stefan Ram wrote:
> In a recent Firefox:
>
> -2 ** 2
>
> SyntaxError: unparenthesized unary expression can't appear on
> the left-hand side of '**'
>
> . Can't seem to find the corresponding rule in a recent copy
> of ECMAScript (12.6), though.
Next time, *cite* your sources, OK?
<http://www.ecma-international.org/ecma-262/7.0/#sec-exp-operator>
| 12.6 Exponentiation Operator
|
| Syntax
|
| ExponentiationExpression[Yield] :
| UnaryExpression[?Yield]
| UpdateExpression[?Yield] ** ExponentiationExpression[?Yield]
(via /Statement/ → /ExpressionStatement/ → /Expression/ →
/AssignmentExpression/ → … → /MultiplicativeExpression/)
*The longest match wins*, so check the second (longer) alternative first:
“-2” must be producible by UpdateExpression. Check by looking at the
possible productions for that:
| 12.4 Update Expressions
|
| Syntax
|
| UpdateExpression :
| LeftHandSideExpression[?Yield]
| LeftHandSideExpression[?Yield][no LineTerminator here]++
| LeftHandSideExpression[?Yield][no LineTerminator here]--
| ++ UnaryExpression[?Yield]
| -- UnaryExpression[?Yield]
Conclusion: “-2” cannot be produced by the /UpdateExpression/ production’s
results.
The only remaining alternative is /UnaryExpression/:
| 12.5 Unary Operators
|
| Syntax
|
| UnaryExpression :
| UpdateExpression[?Yield]
| delete UnaryExpression
| void UnaryExpression
| typeof UnaryExpression
| + UnaryExpression
| - UnaryExpression
| ~ UnaryExpression
| ! UnaryExpression
And therein lies the problem.
“-2” cannot be produced by /UpdateExpression/, so it must be producible by
“- UnaryExpression” for “-2 ** 2” to be syntactically valid.
But “2” cannot be produced by /UnaryExpression/, because it can neither be
produced by /UpdateExpression/ nor any other /UnaryExpression/ production
result.
Conclusion: “-2 ** 2” is *syntactically invalid*.
[This has nothing to do with operator precedence.]
Finding out why “(-2) ** 2” is valid syntax, and works as intended, is left
as an exercise to the reader.
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next | Find similar | Unroll thread
Re: -2 ** 2 Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2017-05-05 08:17 +0200
csiph-web