Skip to content
Open
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: 6 additions & 0 deletions TestHScript.hx
Original file line numberDiff line numberDiff line change
Expand Up@@ -154,6 +154,7 @@ class TestHScript extends TestCase{
assertScript("ptnull?.x", null, vars);
assertScript("ptnull?.pt.x", null, vars);
assertScript("ptnull?.call()", null, vars);
assertScript("ptnull?.pt.call()", null, vars);
assertScript("pt?.x", 10, vars);
assertScript("pt?.call()", 11, vars);
assertScript("pt2null?.pt", null, vars);
Expand All@@ -162,6 +163,11 @@ class TestHScript extends TestCase{
assertScript("pt2?.pt", pt, vars);
assertScript("pt2?.pt?.x", 10, vars);
assertScript("pt2?.pt?.call()", 11, vars);
assertScript("ptnull ?? 12", 12, vars);
assertScript("pt2null.pt ?? 12", 12, vars);
assertScript("ptnull?.x ?? 12", 12, vars);
assertScript("pt?.x ?? 12", 10, vars);
assertScript("pt.y ?? 12", 12, vars);
}

function testIsOperator():Void{
Expand Down
2 changes: 1 addition & 1 deletion hscript/JsInterp.hx
Original file line numberDiff line numberDiff line change
Expand Up@@ -222,7 +222,7 @@ class JsInterp extends Interp{
return '$$i.get(${exprValue(e)},"$f")'
case EBinop(op, e1, e2):
switch( op ){
case "+","-","*","/","%","&","|","^",">>","<<",">>>","==","!=",">=","<=",">","<":
case "+","-","*","/","%","&","|","^",">>","<<",">>>","==","!=",">=","<=",">","<","??":
return '${exprOp(e1)} $op ${exprOp(e2)}'
case "||","&&":
return '(${exprCond(e1)} $op ${exprCond(e2)})'
Expand Down
25 changes: 10 additions & 15 deletions hscript/Parser.hx
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,7 +33,6 @@ enum Token{
TBrClose;
TDot;
TQuestionDot;
TQuestionDouble;
TComma;
TSemicolon;
TBkOpen;
Expand DownExpand Up@@ -837,10 +836,10 @@ class Parser{
}
}

function parseExprNext( e1 : Expr ){
function parseExprNext( e1 : Expr, ?noOp = false ){
var tk = token();
switch( tk ){
case TOp(op):
case TOp(op) if( !noOp ):

if( op == "->" ){
// single arg reinterpretation of `f -> e` , `(f) -> e` and `(f:T) -> e`
Expand All@@ -864,15 +863,15 @@ class Parser{
return parseExprNext(mk(EUnop(op,false,e1),pmin(e1)));
}
return makeBinop(op,e1,parseExpr());
case TId(op) if( opPriority.exists(op) ):
case TId(op) if( !noOp && opPriority.exists(op) ):
return parseExprNext(makeBinop(op,e1,parseExpr()));
case TDot:
var field = getIdent();
return parseExprNext(mk(EField(e1,field),pmin(e1)));
return parseExprNext(mk(EField(e1,field),pmin(e1)), noOp);
case TQuestionDot:
var tmp = "__a_" + (uid++);
push(TDot);
var e2 = parseExprNext(mk(EIdent(tmp),pmin(e1),pmax(e1)));
var e2 = parseExprNext(mk(EIdent(tmp),pmin(e1),pmax(e1)), true);
var e = mk(EBlock([
mk(EVar(tmp, null, e1), pmin(e1), pmax(e1)),
mk(ETernary(
Expand All@@ -881,21 +880,18 @@ class Parser{
e2
))
]),pmin(e1));
return e;
return parseExprNext(e, noOp);
case TPOpen:
return parseExprNext(mk(ECall(e1,parseExprList(TPClose)),pmin(e1)));
return parseExprNext(mk(ECall(e1,parseExprList(TPClose)),pmin(e1)), noOp);
case TBkOpen:
var e2 = parseExpr();
ensure(TBkClose);
return parseExprNext(mk(EArray(e1,e2),pmin(e1)));
case TQuestion:
return parseExprNext(mk(EArray(e1,e2),pmin(e1)), noOp);
case TQuestion if( !noOp ):
var e2 = parseExpr();
ensure(TDoubleDot);
var e3 = parseExpr();
return mk(ETernary(e1,e2,e3),pmin(e1),pmax(e3));
case TQuestionDouble:
var e2 = parseExpr();
return makeBinop("??",e1,e2);
default:
push(tk);
return e1;
Expand DownExpand Up@@ -1573,7 +1569,7 @@ class Parser{
if( char == ".".code )
return TQuestionDot;
if( char == "?".code )
return TQuestionDouble;
return TOp("??");
this.char = char;
return TQuestion;
case ":".code: return TDoubleDot;
Expand DownExpand Up@@ -1803,7 +1799,6 @@ class Parser{
case TBrClose: "}"
case TDot: "."
case TQuestionDot: "?."
case TQuestionDouble: "??"
case TComma: ","
case TSemicolon: ""
case TBkOpen: "["
Expand Down