diff --git a/hscript/Expr.hx b/hscript/Expr.hx index 0d9f4d3..1d42355 100644 --- a/hscript/Expr.hx +++ b/hscript/Expr.hx @@ -69,7 +69,7 @@ enum Expr { EReturn( ?e : Expr ); EArray( e : Expr, index : Expr ); EArrayDecl( e : Array, ?wantedType: CType ); - ENew( cl : String, params : Array ); + ENew( cl : String, params : Array, ?paramType:Array ); EThrow( e : Expr ); ETry( e : Expr, v : String, t : Null, ecatch : Expr ); EObject( fl : Array ); @@ -118,6 +118,7 @@ enum CType { CTParent( t : CType ); CTOpt( t : CType ); CTNamed( n : String, t : CType ); + CTExpr( e : Expr ); // for type parameters only } #if hscriptPos diff --git a/hscript/Interp.hx b/hscript/Interp.hx index 56217f0..56da8fb 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -1066,7 +1066,7 @@ class Interp { } else { return arr[index]; } - case ENew(cl, params): + case ENew(cl, params, _): return cnew(cl, makeArgs(params)); case EThrow(e): throw expr(e); diff --git a/hscript/Parser.hx b/hscript/Parser.hx index 58b5bb7..f9339b1 100644 --- a/hscript/Parser.hx +++ b/hscript/Parser.hx @@ -1096,12 +1096,47 @@ class Parser { mk(EReturn(e),p1,if( e == null ) tokenMax else pmax(e)); case "new": var a = []; + var params:Null> = null; a.push(getIdent()); while( true ) { var tk = token(); switch( tk ) { case TDot: a.push(getIdent()); + case TOp(op) : + if( op == '<' ) { + params = []; + while(true) { + switch(token()) { + case TConst(c): + params.push(CTExpr(mk(EConst(c)))); + case tk: + push(tk); + params.push(parseType()); + } + tk = token(); + switch(tk) { + case TComma: continue; + case TOp(op): + if( op == ">") break; + if( op.charCodeAt(0) == ">".code ) { + #if hscriptPos + tokens.add({ t : TOp(op.substr(1)), min : tokenMax - op.length - 1, max : tokenMax }); + #else + tokens.add(TOp(op.substr(1))); + #end + break; + } + default: + } + unexpected(tk); + break; + } + } + else { + unexpected(tk); + break; + } case TPOpen: break; default: @@ -1110,7 +1145,7 @@ class Parser { } } var args = parseExprList(TPClose); - mk(ENew(a.join("."), args), p1); + mk(ENew(a.join("."), args, params), p1); case "throw": var e = parseExpr(); mk(EThrow(e),p1,pmax(e)); @@ -1344,7 +1379,13 @@ class Parser { if( op == "<" ) { params = []; while( true ) { - params.push(parseType()); + switch( token() ) { + case TConst(c): + params.push(CTExpr(mk(EConst(c)))); + case tk: + push(tk); + params.push(parseType()); + } t = token(); switch( t ) { case TComma: continue; diff --git a/hscript/Printer.hx b/hscript/Printer.hx index a2d2a2d..dadbc10 100644 --- a/hscript/Printer.hx +++ b/hscript/Printer.hx @@ -98,6 +98,8 @@ class Printer { add("("); type(t); add(")"); + case CTExpr(e): + expr(e); } } @@ -291,8 +293,18 @@ class Printer { expr(e); } add("]"); - case ENew(cl, args): - add("new " + cl + "("); + case ENew(cl, args, params): + add("new " + cl); + if(params != null) { + add("<"); + var first = true; + for( p in params ) { + if( first ) first = false else add(", "); + type(p); + } + add(">"); + } + add("("); var first = true; for( e in args ) { if( first ) first = false else add(", ");