RockstarLang/rockstar

List Arithmetic grammar error

Andreal2000 opened this issue · 0 comments

I discovered that there is a bug in the grammar for the list arithmetic in rockstar.peg.
This bug allow the parser to return at the interpreter code that can't be evaluated.

I tried run this code on the online interpreter at codewithrockstar.com

let var be 1, 2 is greater than 3, 4
Shout var

the output was: Error: Sorry - I don't know how to evaluate this: [{"number":1},{"number":2}]

This means that the parser consider this code right.

I started looking in rockstar.peg and i think i found the problem in this block of code:

arithmetic = first:product rest:((add / subtract) product)+
{ return rest.reduce(function(memo, curr) {
return { binary: { op: curr[0], lhs: memo, rhs: curr[1]} };
}, first); }
/ product
product = first:simple_expression rest:((multiply / divide) expression_list)+
{ return rest.reduce(function(memo, curr) {
return { binary: { op: curr[0], lhs: memo, rhs: curr[1]} };
}, first); }
/ expression_list
/ simple_expression

I think that expression_list at line 286 is the error and should be moved at line 276, heres the code that i think will solve the problem

arithmetic         = first:product rest:((add / subtract) (product / expression_list))+
                { return rest.reduce(function(memo, curr) {
                      return { binary: { op: curr[0], lhs: memo, rhs: curr[1]} };
                }, first); }
            / product


product     = first:simple_expression rest:((multiply / divide) expression_list)+
                { return rest.reduce(function(memo, curr) {
                    return { binary: { op: curr[0], lhs: memo, rhs: curr[1]} };
                }, first); }
            / simple_expression

In this way in the cited code below arithmetic can be product but can't be expression_list

comparison = lhs:arithmetic c:comparator rhs:comparison