magic-lang / rock

ooc compiler written in ooc
http://ooc-lang.org/
MIT License
14 stars 4 forks source link

correct version parse #42

Closed horasal closed 8 years ago

horasal commented 8 years ago

Description

This pull request makes the negative version parses correctl. in original rock, the following

version(!a && !b)

will be parsed as

version(!(a && !b))

This pull request fixes it to

version((!a) && (!b))

Implementation Details

No change in the ooc-side (expect re-generatation of the parser). In nagaqueen, the old source looks like:

VersionName = - < [a-zA-Z0-9_]+ > { tokenPos; $$=nq_onVersionName(core->this, yytext) }
VersionNegation = - '!' { tokenPos; } - spec:VersionSpec { $$=nq_onVersionNegation(core->this, spec) }

where VersionSpec is the block contains things like ( VersionSpec ), VersionSpec && VersionSpec and VersionSpec || VersionSpec. However, for version(!a && !b), because the matching is greedy, once we enter the rule VersionNegation, the VersionSpec is matched to the end. Let's consider what ! should affect, it is easy to know that the !block should only contain:

So the rules should be:

VersionNegation = (- '!' { tokenPos; } - spec:VersionName { $$=nq_onVersionNegation(core->this, spec) }) |
                   (- '!' { tokenPos; } - '(' - spec:VersionSpec - ')' { $$=nq_onVersionNegation(core->this, spec) })
horasal commented 8 years ago

One more thing, the test provided by bounty repo says:

Compiling with the following flags should yield the following sums:
None        2+16+64     = 82        (Currently: -910)
-DA         4+32+64     = 100       (Currently: 44)
-DB         32+64       = 96        (Currently: -878)
-DC         2+32+64     = 98        (Currently: -942)
-DA -DB     1+32+64     = 97        (Currently: 51)
-DA -DB -DC 1+32        = 33        (Currently: 35)

However, the ounput of -DA should be 108 because:

A && !B = 4
!B && A = 8
A || B || C = 32
A || !B || !C = 64

4 + 8 + 32 + 64 = 108
horasal commented 8 years ago

@fredrikbryntesson sent you a mail

marcusnaslund commented 8 years ago

@zhaihj is right, it should be 108 and not 100. My bad.

thomasfanell commented 8 years ago

Great job @zhaihj ping @fredrikbryntesson

thomasfanell commented 8 years ago

Merging via #49 (or really, via nagaqueen later today)