Skip to content

Commit 8017f50

Browse files
committed
added allowNew + ENew typing
1 parent c0ff8b8 commit 8017f50

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

hscript/Checker.hx

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ class Checker {
412412
public var allowGlobalsDefine : Bool;
413413
public var allowUntypedMeta : Bool;
414414
public var allowPrivateAccess : Bool;
415+
public var allowNew : Bool;
415416

416417
public function new( ?types ) {
417418
if( types == null ) types = new CheckerTypes();
@@ -1346,6 +1347,21 @@ class Checker {
13461347
return null;
13471348
}
13481349

1350+
function unifyCallParams( args : Array<{ name : String, opt : Bool, t : TType }>, params : Array<Expr>, pos : Expr ) {
1351+
for( i in 0...params.length ) {
1352+
var a = args[i];
1353+
if( a == null ) {
1354+
error("Too many arguments", params[i]);
1355+
break;
1356+
}
1357+
var t = typeExpr(params[i], a == null ? Value : WithType(a.t));
1358+
unify(t, a.t, params[i]);
1359+
}
1360+
for( i in params.length...args.length )
1361+
if( !args[i].opt )
1362+
error("Missing argument "+args[i].name+":"+typeStr(args[i].t), pos);
1363+
}
1364+
13491365
function typeExpr( expr : Expr, withType : WithType ) : TType {
13501366
if( expr == null && isCompletion )
13511367
return switch( withType ) {
@@ -1407,18 +1423,7 @@ class Checker {
14071423
callExpr = prev;
14081424
switch( follow(ft) ) {
14091425
case TFun(args, ret):
1410-
for( i in 0...params.length ) {
1411-
var a = args[i];
1412-
if( a == null ) {
1413-
error("Too many arguments", params[i]);
1414-
break;
1415-
}
1416-
var t = typeExpr(params[i], a == null ? Value : WithType(a.t));
1417-
unify(t, a.t, params[i]);
1418-
}
1419-
for( i in params.length...args.length )
1420-
if( !args[i].opt )
1421-
error("Missing argument "+args[i].name+":"+typeStr(args[i].t), expr);
1426+
unifyCallParams(args, params, expr);
14221427
return ret;
14231428
case TDynamic:
14241429
for( p in params ) typeExpr(p,Value);
@@ -1741,6 +1746,24 @@ class Checker {
17411746
var et = typeExpr(e, Value);
17421747
return t == null ? makeMono() : makeType(t,expr);
17431748
case ENew(cl, params):
1749+
if( !allowNew ) error("'new' is not allowed", expr);
1750+
var t = types.resolve(cl);
1751+
if( t == null ) error("Unknown class "+cl, expr);
1752+
switch( t ) {
1753+
case TInst(c,_) if( c.constructor != null ):
1754+
switch( c.constructor.t ) {
1755+
case TFun(args, _):
1756+
var ms = [for( c in c.params ) makeMono()];
1757+
var mf = [for( c in c.constructor.params ) makeMono()];
1758+
var args = [for( a in args ) { name : a.name, opt : a.opt, t : apply(apply(a.t,c.params,ms),c.constructor.params,mf) }];
1759+
unifyCallParams(args, params, expr);
1760+
return TInst(c, ms);
1761+
default:
1762+
throw "assert";
1763+
}
1764+
default:
1765+
error(typeStr(t)+" cannot be constructed", expr);
1766+
}
17441767
}
17451768
error("Don't know how to type "+edef(expr).getName(), expr);
17461769
return TDynamic;

0 commit comments

Comments
 (0)