Skip to content

Commit

Permalink
fix(parser): Wrong negative sign precedence was causing math errors (#6
Browse files Browse the repository at this point in the history
…) (#24)
  • Loading branch information
lvcabral authored Nov 1, 2023
1 parent e6c98ae commit 98dcee2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1385,10 +1385,14 @@ export class Parser {
}

function prefixUnary(): Expression {
if (match(Lexeme.Not, Lexeme.Minus, Lexeme.Plus)) {
if (match(Lexeme.Not)) {
let operator = previous();
let right = relational();
return new Expr.Unary(operator, right);
} else if (match(Lexeme.Minus, Lexeme.Plus)) {
let operator = previous();
let right = prefixUnary();
return new Expr.Unary(operator, right);
}

return call();
Expand Down
10 changes: 10 additions & 0 deletions test/e2e/Syntax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ describe("end to end syntax", () => {
]);
});

test("negative-precedence.brs", async () => {
await execute([resourceFile("negative-precedence.brs")], outputStreams);

expect(allArgs(outputStreams.stdout.write).filter((arg) => arg !== "\n")).toEqual([
"0000",
"0",
"foo is not 1",
]);
});

test("assignment.brs", async () => {
await execute([resourceFile("assignment.brs")], outputStreams);

Expand Down
15 changes: 15 additions & 0 deletions test/e2e/resources/negative-precedence.brs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Sub Main()
x = 96
y = 56
w = 1088
h = 608
Offset(-x + 96, -y + 56, -w + 1088, -h + 608)
print -1000 +1000
foo = 5
if not foo = 1
print "foo is not 1"
end if
End Sub
Sub Offset(x, y, w, h)
print x.toStr() + y.toStr() + w.toStr() + h.toStr()
End Sub

0 comments on commit 98dcee2

Please sign in to comment.