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

Implemented Optional Chaining Operators #81

Merged
merged 2 commits into from
Nov 26, 2024
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
23 changes: 18 additions & 5 deletions src/interpreter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1178,6 +1178,9 @@ export class Interpreter implements Expr.Visitor<BrsType>, Stmt.Visitor<BrsType>
let args = expression.args.map(this.evaluate, this);

if (!isBrsCallable(callee)) {
if (callee instanceof BrsInvalid && expression.optional) {
return callee;
}
this.addError(
new RuntimeError(RuntimeErrorDetail.NotAFunction, expression.closingParen.location)
);
Expand Down Expand Up @@ -1295,21 +1298,31 @@ export class Interpreter implements Expr.Visitor<BrsType>, Stmt.Visitor<BrsType>

let boxedSource = isBoxable(source) ? source.box() : source;
if (boxedSource instanceof BrsComponent) {
// This check is supposed to be placed below the try/catch block,
// but it's here to mimic the behavior of Roku, if they fix, we move it.
if (source instanceof BrsInvalid && expression.optional) {
return source;
}
try {
return boxedSource.getMethod(expression.name.text) || BrsInvalid.Instance;
const method = boxedSource.getMethod(expression.name.text);
if (method) {
return method;
}
} catch (err: any) {
this.addError(new BrsError(err.message, expression.name.location));
}
} else {
this.addError(
new RuntimeError(RuntimeErrorDetail.DotOnNonObject, expression.name.location)
);
}
this.addError(
new RuntimeError(RuntimeErrorDetail.DotOnNonObject, expression.name.location)
);
}

visitIndexedGet(expression: Expr.IndexedGet): BrsType {
let source = this.evaluate(expression.obj);
if (!isIterable(source)) {
if (source instanceof BrsInvalid && expression.optional) {
return source;
}
this.addError(new RuntimeError(RuntimeErrorDetail.UndimmedArray, expression.location));
}

Expand Down
2 changes: 1 addition & 1 deletion src/lexer/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export class Lexer {
break;
case "[":
advance();
addToken(Lexeme.LeftBrace);
addToken(Lexeme.LeftSquare);
break;
default:
addToken(Lexeme.Print);
Expand Down
12 changes: 9 additions & 3 deletions src/parser/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class Call extends AstNode implements Expression {
constructor(
readonly callee: Expression,
readonly closingParen: Token,
readonly args: Expression[]
readonly args: Expression[],
readonly optional: boolean = false
) {
super("Call");
}
Expand Down Expand Up @@ -94,7 +95,11 @@ export class Function extends AstNode implements Expression {
}

export class DottedGet extends AstNode implements Expression {
constructor(readonly obj: Expression, readonly name: Identifier) {
constructor(
readonly obj: Expression,
readonly name: Identifier,
readonly optional: boolean = false
) {
super("DottedGet");
}

Expand All @@ -115,7 +120,8 @@ export class IndexedGet extends AstNode implements Expression {
constructor(
readonly obj: Expression,
readonly indexes: Expression[],
readonly closingSquare: Token
readonly closingSquare: Token,
readonly optional: boolean = false
) {
super("IndexedGet");
}
Expand Down
11 changes: 6 additions & 5 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,7 @@ export class Parser {
let expr = primary();

function indexedGet() {
const optional = previous().text === "?[";
let elements: Expression[] = [];

while (match(Lexeme.Newline));
Expand All @@ -1502,11 +1503,9 @@ export class Parser {
Lexeme.RightSquare
);

expr = new Expr.IndexedGet(expr, elements, closingSquare);
expr = new Expr.IndexedGet(expr, elements, closingSquare, optional);
}

function dottedGet() {}

while (true) {
if (match(Lexeme.LeftParen)) {
expr = finishCall(expr);
Expand All @@ -1516,6 +1515,7 @@ export class Parser {
if (match(Lexeme.LeftSquare)) {
indexedGet();
} else {
const optional = previous().text === "?.";
while (match(Lexeme.Newline));

let name = consume(
Expand All @@ -1527,7 +1527,7 @@ export class Parser {
// force it into an identifier so the AST makes some sense
name.kind = Lexeme.Identifier;

expr = new Expr.DottedGet(expr, name as Identifier);
expr = new Expr.DottedGet(expr, name as Identifier, optional);
}
} else {
break;
Expand All @@ -1538,6 +1538,7 @@ export class Parser {
}

function finishCall(callee: Expression): Expression {
const optional = previous().text === "?(";
let args = [];
while (match(Lexeme.Newline));

Expand All @@ -1561,7 +1562,7 @@ export class Parser {
Lexeme.RightParen
);

return new Expr.Call(callee, closingParen, args);
return new Expr.Call(callee, closingParen, args, optional);
}

function primary(): Expression {
Expand Down
14 changes: 14 additions & 0 deletions test/e2e/Syntax.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ describe("end to end syntax", () => {
]);
});

test("optional-chaining-operators.brs", async () => {
await execute([resourceFile("optional-chaining-operators.brs")], outputStreams);

expect(allArgs(outputStreams.stdout.write).filter((arg) => arg !== "\n")).toEqual([
"invalid",
"invalid",
"invalid",
"invalid",
"invalid",
"invalid",
"invalid",
]);
});

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ sub init()
end sub

function getMessage(node as object, event as object) as string
print "*" + node.id + "* " + node.focusedChild.id.toStr() + " " + event.getData().id.toStr() + " " + node.isInFocusChain().toStr()
print "*" + node.id + "* " + node.focusedChild?.id.toStr() + " " + event.getData()?.id.toStr() + " " + node.isInFocusChain().toStr()
end function

sub onFocusTop(event as object)
Expand Down
11 changes: 11 additions & 0 deletions test/e2e/resources/optional-chaining-operators.brs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
sub Main()
thing = invalid
print thing?.property
print thing?.functionCall2?()
print thing?[0]
print thing?[0]?.property
print thing?[0]?.functionCall2?()
print thing?.functionCall?(thing?[0]?.property, thing?[0]?.functionCall2?())
node = {}
print node?.focusedChild?.id.toStr()
end sub
21 changes: 21 additions & 0 deletions test/parser/controlFlow/__snapshots__/If.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2691,6 +2691,7 @@ Array [
},
"type": "Variable",
},
"optional": false,
"type": "DottedGet",
},
"type": "DottedSet",
Expand Down Expand Up @@ -2786,6 +2787,7 @@ Array [
},
"type": "Variable",
},
"optional": false,
"type": "DottedGet",
},
"type": "DottedSet",
Expand Down Expand Up @@ -3564,8 +3566,10 @@ Array [
},
"type": "Variable",
},
"optional": false,
"type": "DottedGet",
},
"optional": false,
"type": "DottedGet",
},
],
Expand Down Expand Up @@ -3606,6 +3610,7 @@ Array [
},
"text": ")",
},
"optional": false,
"type": "Call",
},
"right": Literal {
Expand Down Expand Up @@ -3772,10 +3777,13 @@ Array [
},
"type": "Variable",
},
"optional": false,
"type": "DottedGet",
},
"optional": false,
"type": "DottedGet",
},
"optional": false,
"type": "DottedGet",
},
"closingParen": Object {
Expand All @@ -3795,6 +3803,7 @@ Array [
},
"text": ")",
},
"optional": false,
"type": "Call",
},
},
Expand Down Expand Up @@ -3862,8 +3871,10 @@ Array [
},
"type": "Variable",
},
"optional": false,
"type": "DottedGet",
},
"optional": false,
"type": "DottedGet",
},
],
Expand Down Expand Up @@ -3904,6 +3915,7 @@ Array [
},
"text": ")",
},
"optional": false,
"type": "Call",
},
"right": Literal {
Expand Down Expand Up @@ -4050,8 +4062,10 @@ Array [
},
"type": "Variable",
},
"optional": false,
"type": "DottedGet",
},
"optional": false,
"type": "DottedGet",
},
},
Expand Down Expand Up @@ -4170,8 +4184,10 @@ Array [
},
"type": "Variable",
},
"optional": false,
"type": "DottedGet",
},
"optional": false,
"type": "DottedGet",
},
],
Expand Down Expand Up @@ -4231,8 +4247,10 @@ Array [
},
"type": "Variable",
},
"optional": false,
"type": "DottedGet",
},
"optional": false,
"type": "DottedGet",
},
"closingParen": Object {
Expand All @@ -4252,6 +4270,7 @@ Array [
},
"text": ")",
},
"optional": false,
"type": "Call",
},
},
Expand Down Expand Up @@ -4422,8 +4441,10 @@ Array [
},
"type": "Variable",
},
"optional": false,
"type": "DottedGet",
},
"optional": false,
"type": "DottedGet",
},
},
Expand Down
3 changes: 3 additions & 0 deletions test/parser/expression/__snapshots__/Call.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Array [
},
"text": ")",
},
"optional": false,
"type": "Call",
},
"type": "Expression",
Expand Down Expand Up @@ -122,6 +123,7 @@ Array [
},
"text": ")",
},
"optional": false,
"type": "Call",
},
"type": "Expression",
Expand Down Expand Up @@ -533,6 +535,7 @@ Array [
},
"text": ")",
},
"optional": false,
"type": "Call",
},
"type": "Expression",
Expand Down
1 change: 1 addition & 0 deletions test/parser/expression/__snapshots__/Function.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2898,6 +2898,7 @@ Array [
},
"text": ")",
},
"optional": false,
"type": "Call",
},
"type": "Expression",
Expand Down
Loading