lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Nov 27, 2018 at 7:17 AM pocomane <pocomane_7a@pocomane.com> wrote:
>
> On Tue, Nov 27, 2018 at 12:52 PM Muh Muhten <muh.muhten@gmail.com> wrote:
> > It seems to me that making this particular case work would be rather
> > involved, and essentially require negative numbers to be first-class
> > citizens in the grammar, rather than cobbled together through unary
> > minus and constant folding. I also don't see a satisfying solution
> > for, e.g. "- 9223372036854775808", "- --[[]]9223372036854775808",
> > "-(9223372036854775808)", though arguably those are *explicitly* the
> > negation of positive 9223372036854775808, which doesn't fit, and
> > really should be an integer.
>
> I am just using - 9223372036854775807-1 in my code (in test suites,  I
> prefer explicit number over math.mininteger). If it is complex to fix,
> I think it could stay as it is. Maybe just a warning in the manual
> could be useful.
>

The way Lua does it is consistent with how a number of other
programming languages handle tokenization. I know C++ does it that
way, having implemented a C++ parser once upon a time.

There's also the complication of expressions like "2-1" -- is that a
2, a -, and a 1, or is that a 2 and a -1? Obviously the intent is the
former, but any language that generates a token stream from a lexer
that it uses to subsequently construct the syntax tree is going to
have this problem.

There IS a solution available that isn't confused by your first two
examples, but it's not particularly elegant: when a unary minus
expression is parsed, and the operand is a constant, the parser folds
them together in the syntax tree instead of emitting an actual
negation operation. (Your last example isn't just "arguably" the
explicit negation, the use of parentheses makes it DEFINITELY an
explicit negation because the parentheses mean you're demanding that
the expression inside is computed BEFORE you apply the unary minus.)

/s/ Adam