[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Can LPeg do basic arithmetic?
- From: Sean Conner <sean@...>
- Date: Tue, 5 Jul 2016 21:35:29 -0400
It was thus said that the Great Soni L. once stated:
> On 05/07/16 09:37 PM, Sean Conner wrote:
> >It was thus said that the Great Soni L. once stated:
> >>The goal is to never run Lua code for it.
So we're back to the above:
lpeg.P"0+0" * lpeg.Cc(0)
+ lpeg.P"0+1" * lpeg.Cc(1)
...
although if you want to deal with whitespace:
plus = lpeg.S" \t\v\r\n"^0
math = lpeg.P"0" * plus * lpeg.P"0" * lpeg.Cc(0)
+ lpeg.P"1" * plus * lpeg.P"1" * lpeg.Cc(1)
...
That's the best (enumerate all possible inputs) short of unary
non-negative "numbers". And if you are going *that* route---the ultimate
golfed fizz-buzz:
https://codon.com/programming-with-nothing
(and even then, he cheats by avoiding actual I/O)
> >>Perl regex can run Perl code,
> >>but the linked math-with-regex thingy doesn't use that and that's the
> >>whole point. If someone ports LPeg to a different programming language,
> >>a LPeg that calls Lua code will stop working.
> >
> > That was never specified as a limititation in your initial post.
>
> No, but it is mentioned in the linked stackoverflow question. You should
> read the full context before replying.
Touche'
> >>Also, if this was a codegolf or programming puzzle and you weren't
> >>allowed to use the language's built-in addition subtraction etc, you
> >>could do it with regex instead.
> > If this was a codegolf or programming puzzle and you weren't allowed to
> >use the languages's built-in addition subtraction etc, you wouldn't be
> >*ABLE* to do this using LPeg. Period. End of story. That '*' in the
> >above example? That's Lua multiplication operator.
>
> You're mistaking "operation" and "operator". In other languages, "+" is
> often overloaded for concatenation as well as addition, and in such
> challenges you're (usually) still allowed to use "+" for concatenation.
Then what would you call the '*' operator? Leslie?
-spc