Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): Wrong negative sign precedence was causing math errors (#6) #24

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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