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

Add support for null coalescing (?? & ??=) #131

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions TestHScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ class TestHScript extends TestCase {
assertScript("pt2?.pt?.x", 10, vars);
}

function testNullCoalescing():Void {
var pt = {x: 10};
var vars = {
ptnull: null,
pt: pt,
pt2null: {pt: null},
pt2: {pt: pt}
}
assertScript("ptnull?.x ?? 5", 5, vars);
assertScript("pt?.x ?? 5", 10, vars);
assertScript("pt2null?.pt ?? 5", 5, vars);
assertScript("pt2null?.pt?.x ?? 5", 5, vars);
assertScript("pt2?.pt ?? 5", pt, vars);
assertScript("pt2?.pt?.x ?? 5", 10, vars);
}

function testIsOperator():Void {
var vars = {
String: String,
Expand Down
16 changes: 16 additions & 0 deletions hscript/Parser.hx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum Token {
TBrClose;
TDot;
TQuestionDot;
TQuestionQuestion;
TComma;
TSemicolon;
TBkOpen;
Expand Down Expand Up @@ -821,6 +822,18 @@ class Parser {
))
]),pmin(e1));
return parseExprNext(e);
case TQuestionQuestion:
var e2 = parseExpr();
var tmp = "__a_" + (uid++);
var e = mk(EBlock([
mk(EVar(tmp, null, e1), pmin(e1), pmax(e1)),
mk(ETernary(
mk(EBinop("==", mk(EIdent(tmp),pmin(e1),pmax(e1)), mk(EIdent("null"),pmin(e1),pmax(e1)))),
e2,
e1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be tmp to avoid code duplication?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah my bad

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also means that a relevant test is missing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just made one and after the change runs successfully, but maybe pushing it would be a lot of code just for it that now works great. I had to make a new class with a integer variable with a getter function that increases everytime you access it

))
]),pmin(e1));
return parseExprNext(e);
case TPOpen:
return parseExprNext(mk(ECall(e1,parseExprList(TPClose)),pmin(e1)));
case TBkOpen:
Expand Down Expand Up @@ -1508,6 +1521,8 @@ class Parser {
char = readChar();
if( char == ".".code )
return TQuestionDot;
else if ( char == "?".code )
return TQuestionQuestion;
this.char = char;
return TQuestion;
case ":".code: return TDoubleDot;
Expand Down Expand Up @@ -1738,6 +1753,7 @@ class Parser {
case TBrClose: "}";
case TDot: ".";
case TQuestionDot: "?.";
case TQuestionQuestion: "??";
case TComma: ",";
case TSemicolon: ";";
case TBkOpen: "[";
Expand Down
Loading