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

Fixed behavior of End statement to terminate the app #456

Merged
merged 1 commit into from
Jan 18, 2025
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "brs-engine",
"version": "1.8.2",
"version": "1.9.0-beta",
"title": "BrightScript Simulation Engine",
"description": "BrightScript Simulation Engine - Run Roku apps on Browsers and Node.js",
"author": "Marcelo Lv Cabral <[email protected]> (https://lvcabral.com/)",
"license": "MIT",
"copyright": "© 2019-2024, Marcelo Lv Cabral",
"copyright": "© 2019-2025, Marcelo Lv Cabral",
"homepage": "https://lvcabral.com/brs/",
"contributors": [
"Sean Barag <[email protected]>",
Expand Down
3 changes: 2 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const cecStatus: CECStatusEvent = { activeSource: true };
export const memoryInfo: MemoryInfoEvent = { usedHeapSize: 0, heapSizeLimit: 0 };
export const bscs = new Map<string, number>();
export const stats = new Map<Lexeme, number>();
export const terminateReasons = ["debug-exit", "end-statement"];

const algorithm = "aes-256-ctr";

Expand Down Expand Up @@ -882,7 +883,7 @@ async function executeApp(
await interpreter.exec(statements, sourceMap, inputParams);
} catch (err: any) {
exitReason = AppExitReason.FINISHED;
if (err.message !== "debug-exit") {
if (!terminateReasons.includes(err.message)) {
if (interpreter.options.post ?? true) {
postMessage(`error,${err.message}`);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/core/interpreter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1267,7 +1267,7 @@ export class Interpreter implements Expr.Visitor<BrsType>, Stmt.Visitor<BrsType>
});
} catch (reason: any) {
if (!(reason instanceof Stmt.BlockEnd)) {
if (reason.message === "debug-exit") {
if (core.terminateReasons.includes(reason.message)) {
throw new Error(reason.message);
} else if (reason instanceof BrsError) {
throw reason;
Expand All @@ -1277,7 +1277,7 @@ export class Interpreter implements Expr.Visitor<BrsType>, Stmt.Visitor<BrsType>
throw new Error("");
}
throw new Error(reason.message);
} else if (reason.message === "debug-exit") {
} else if (core.terminateReasons.includes(reason.message)) {
throw new Error(reason.message);
}

Expand Down
1 change: 1 addition & 0 deletions src/core/lexer/ReservedWords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const KeyWords: { [key: string]: L } = {
else: L.Else,
elseif: L.ElseIf,
"else if": L.ElseIf,
end: L.End,
endfor: L.EndFor,
"end for": L.EndFor,
endfunction: L.EndFunction,
Expand Down
14 changes: 2 additions & 12 deletions src/core/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,16 +289,6 @@ export class Parser {
};
}

/**
* A simple wrapper around `check` to make tests for a `end` identifier.
* `end` is a keyword, but not reserved, so associative arrays can have properties
* called `end`; the parser takes on this task.
* @returns `true` if the next token is an identifier with text `end`, otherwise `false`
*/
function checkEnd() {
return check(Lexeme.Identifier) && peek().text.toLowerCase() === "end";
}

function declaration(...additionalTerminators: BlockTerminator[]): Statement | undefined {
try {
let statementSeparators = [Lexeme.Colon];
Expand Down Expand Up @@ -658,7 +648,7 @@ export class Parser {
return exitFor();
}

if (checkEnd()) {
if (check(Lexeme.End)) {
return endStatement();
}

Expand Down Expand Up @@ -1335,7 +1325,7 @@ export class Parser {
function endStatement() {
let tokens = { end: advance() };

while (match(Lexeme.Newline));
while (match(Lexeme.Newline, Lexeme.Colon));

return new Stmt.End(tokens);
}
Expand Down
8 changes: 8 additions & 0 deletions test/e2e/StdLib.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,12 @@ describe("end to end standard libary", () => {
" 245",
]);
});
test("stdlib/end.brs", async () => {
await execute([resourceFile("stdlib", "end.brs")], outputStreams);

expect(allArgs(outputStreams.stdout.write).map((arg) => arg.trimEnd())).toEqual([
"test the test",
"testing end...",
]);
});
});
5 changes: 5 additions & 0 deletions test/e2e/resources/stdlib/end.brs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
sub main()
tester()
end sub
sub testEnd() : print "testing end..." : end: print "not printed" : end sub
sub tester() : print "test the test" : testEnd() : print "should not print" : end sub
5 changes: 5 additions & 0 deletions test/lexer/Lexer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ describe("lexer", () => {
]);
});

it("gives the `end` keyword its own Lexeme", () => {
let { tokens } = Lexer.scan("end");
expect(tokens.map((t) => t.kind)).toEqual([Lexeme.End, Lexeme.Eof]);
});

it("gives the `stop` keyword its own Lexeme", () => {
let { tokens } = Lexer.scan("stop");
expect(tokens.map((t) => t.kind)).toEqual([Lexeme.Stop, Lexeme.Eof]);
Expand Down
58 changes: 58 additions & 0 deletions test/parser/statement/End.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const brs = require("../../../bin/brs.node");
const { Lexeme } = brs.lexer;

const { token, identifier, EOF } = require("../ParserTests");

describe("`end` statement", () => {
beforeEach(() => {
parser = new brs.parser.Parser();
});

it("does not produce errors", () => {
let { tokens } = brs.lexer.Lexer.scan(`
sub Main()
end
end sub
`);
let { statements, errors } = parser.parse(tokens);
expect(errors.length).toEqual(0);
expect(statements).toMatchSnapshot();
});

it("is valid as a statement", () => {
let { statements, errors } = brs.parser.Parser.parse([token(Lexeme.End, "end"), EOF]);
expect(errors[0]).toBeUndefined();
expect(statements).toMatchSnapshot();
});

it("can be used as a property name on objects", () => {
let { tokens } = brs.lexer.Lexer.scan(`
sub Main()
person = {
end: true
}
end sub
`);
let { statements, errors } = parser.parse(tokens);
expect(errors.length).toEqual(0);
expect(statements).toMatchSnapshot();
});

it("is not allowed as a standalone variable", () => {
//this test depends on token locations, so use the lexer to generate those locations.
let { tokens } = brs.lexer.Lexer.scan(`
sub Main()
end = true
end sub
`);
let { statements, errors } = parser.parse(tokens);
expect(errors.length).toEqual(1);
//specifically check for the error location, because the identifier location was wrong in the past
expect(errors[0].location).toEqual({
file: "",
start: { line: 3, column: 24 },
end: { line: 3, column: 25 },
});
expect(statements).toMatchSnapshot();
});
});
43 changes: 0 additions & 43 deletions test/parser/statement/Misc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,6 @@ describe("parser", () => {
parser = new brs.parser.Parser();
});

describe("`end` keyword", () => {
it("does not produce errors", () => {
let { tokens } = brs.lexer.Lexer.scan(`
sub Main()
end
end sub
`);
let { statements, errors } = parser.parse(tokens);
expect(errors.length).toEqual(0);
expect(statements).toMatchSnapshot();
});
it("can be used as a property name on objects", () => {
let { tokens } = brs.lexer.Lexer.scan(`
sub Main()
person = {
end: true
}
end sub
`);
let { statements, errors } = parser.parse(tokens);
expect(errors.length).toEqual(0);
expect(statements).toMatchSnapshot();
});

it("is not allowed as a standalone variable", () => {
//this test depends on token locations, so use the lexer to generate those locations.
let { tokens } = brs.lexer.Lexer.scan(`
sub Main()
end = true
end sub
`);
let { statements, errors } = parser.parse(tokens);
expect(errors.length).toEqual(1);
//specifically check for the error location, because the identifier location was wrong in the past
expect(errors[0].location).toEqual({
file: "",
start: { line: 3, column: 20 },
end: { line: 3, column: 23 },
});
expect(statements).toMatchSnapshot();
});
});

it("certain reserved words are allowed as local var identifiers", () => {
let { tokens } = brs.lexer.Lexer.scan(`
sub Main()
Expand Down
1 change: 0 additions & 1 deletion test/parser/statement/Stop.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const brs = require("../../../bin/brs.node");
const { Lexeme } = brs.lexer;
const { Int32 } = brs.types;

const { token, identifier, EOF } = require("../ParserTests");

Expand Down
Loading
Loading