2015-09-24 8:19 GMT+02:00 Peter Uhnák <[email protected]>:
> > - 250 * 1.5 returns -2.25
>
> why is this valid syntax?
>
I don't know if this is valid syntax (I always wondered that there is one
place in PointTest, that is not compilable with old compiler but compiles
fine with opal).
But the error happens in
RBParser>>#parseNegatedNumber.
If a #- is recognized, it tests if a literalnumber follows (but from the
token stream, that is, the spaces are ignored).
Now the real bug is, that we do two "steps" and concstruct a new
literalvaluenode from the now following tokens.
RBParser parseExpression:'- 2' -> throws an error, because no following
tokens
RBParser parseExpression:'- 2 * 3' -> works but actually duplicates the two
last tokens "*3 *3.
and some funny other things '- 2@1' -> '-1@1'
instead, we should:
save sign and number token
do the two steps
construct the literalvaluenode fromt the two saved tokens:
parseNegatedNumber
|tokenSign tokenNumber|
(self nextToken isLiteralToken not or: [ self nextToken realValue
isNumber not ])
ifTrue: [ ^ self parserError: 'The ''-'' prefix works only for
literal numbers (use #negated instead)' ].
tokenSign := currentToken.
self step.
tokenNumber := currentToken.
self step.
^ self literalValueNodeClass
value: tokenNumber realValue negated
start: tokenSign start
stop: tokenNumber stop
source: tokenSign value, tokenNumber source.
But only if we want to support spaces between sign and number tokens.
BTW, if we use "-2" without space, parseNegatedNumber isn't even called, I
guess the scanner already creates
a single (negated)number token.
>
> On Thu, Sep 24, 2015 at 5:35 AM, Ben Coman <[email protected]> wrote:
>
>> RBParser parseExpression: '-250 * 1.5' => "RBMessageNode(-250 * 1.5)"
>> RBParser parseExpression: '- 250 * 1.5' => "RBMessageNode(*1.5 * 1.5)"
>>
>> RBParser parseExpression: '250 * -1.5' => "RBMessageNode(250 * -1.5)"
>> RBParser parseExpression: '250 * - 1.5' => MNU RBToken>>realvalue.
>>
>> RBParser parseExpression: '-250' => "RBLiteralValueNode(-250)"
>> RBParser parseExpression: '- 250' => MNU RBToken>>realvalue.
>>
>> So how significant should be the space between the negative sign and its
>> number?
>>
>> cheers -ben
>>
>> On Thu, Sep 24, 2015 at 6:34 AM, Clément Bera <[email protected]>
>> wrote:
>> > RBParser parseExpression: '- 250 * 1.5' => Answers
>> RBMessageNode(*1.5 *
>> > 1.5)
>> >
>> > Second expression looks correct.
>> >
>> > Hence RBParser bug.
>> >
>> > 2015-09-23 23:42 GMT+02:00 [email protected] <[email protected]>:
>> >>
>> >> debug it on the first one gives "MNU: receiver of
>> "doSemanticAnalyisIn:"
>> >> is nil.
>> >>
>> >> Weird.
>> >>
>> >> On Wed, Sep 23, 2015 at 8:40 AM, stepharo <[email protected]> wrote:
>> >>>
>> >>> Hi guys
>> >>>
>> >>> I think that this is quite bad that
>> >>> - 250 * 1.5 returns -2.25
>> >>>
>> >>> while
>> >>>
>> >>> -250 * 1.5 return 375
>> >>>
>> >>> stef
>> >>>
>> >>
>> >
>>
>>
>