diff --git a/hscript/Bytes.hx b/hscript/Bytes.hx index 3d1a279..5b2a473 100644 --- a/hscript/Bytes.hx +++ b/hscript/Bytes.hx @@ -130,6 +130,8 @@ class Bytes { // TODO case EEnum(en): // TODO + case ECast(e, _): + // TODO case EConst(c): doEncodeConst(c); case EIdent(v): diff --git a/hscript/Expr.hx b/hscript/Expr.hx index 19c6b79..87522c0 100644 --- a/hscript/Expr.hx +++ b/hscript/Expr.hx @@ -82,6 +82,7 @@ enum Expr { EImport( c : String, ?asname:String, ?isUsing:Bool ); EClass( name:String, fields:Array, ?extend:String, interfaces:Array, ?isFinal:Bool, ?isPrivate:Bool ); EEnum( en:EnumDecl, ?isAbstract:Bool ); + ECast(e:Expr, ?t:CType); } @:structInit diff --git a/hscript/Interp.hx b/hscript/Interp.hx index 5799445..de420d9 100644 --- a/hscript/Interp.hx +++ b/hscript/Interp.hx @@ -882,6 +882,8 @@ class Interp { } variables.set(en.name, enumThingy); + case ECast(e, _): // TODO + return expr(e); case EConst(c): switch (c) { case CInt(v): return v; diff --git a/hscript/Parser.hx b/hscript/Parser.hx index d222e45..6e28b5d 100644 --- a/hscript/Parser.hx +++ b/hscript/Parser.hx @@ -1146,7 +1146,22 @@ class Parser { } mk(EEnum({ name: name, fields: fields }, false), p1); - + case "cast": + var tk = token(); + var e:Expr = null; + var t:CType = null; // null if is unsafe cast + switch(tk) { + case TPOpen: + e = parseExpr(); + ensure(TComma); + t = parseType(); + ensure(TPClose); + default: + push(tk); + e = parseExpr(); + } + + mk(ECast(e, t)); case "return": var tk = token(); push(tk); diff --git a/hscript/Printer.hx b/hscript/Printer.hx index 8039bef..deb6817 100644 --- a/hscript/Printer.hx +++ b/hscript/Printer.hx @@ -171,6 +171,16 @@ class Printer { tabs = tabs.substr(1); add("}"); + case ECast(e, t): + var safe = t != null; + add("cast "); + if(safe) add("("); + expr(e); + if(safe) { + add(", "); + addType(t); + add(")"); + } case EConst(c): switch( c ) { case CInt(i): add(i);