added TSwitch support
This commit is contained in:
@@ -69,7 +69,7 @@ limitations
|
||||
Compared to Haxe, limitations are :
|
||||
|
||||
- no type declarations (classes, enums, typedefs) : only expressions
|
||||
- no switch construct
|
||||
- switch construct is supported but not pattern matching (no variable capture, we use strict equality to compare case values and switch valueà
|
||||
- only one variable declaration is allowed in var
|
||||
- the parser supports optional types for var and function if allowTypes is set, but the interpreter ignore them
|
||||
- you can enable per-expression position tracking by compiling with -D hscriptPos
|
||||
|
||||
@@ -66,6 +66,7 @@ enum Expr {
|
||||
ETry( e : Expr, v : String, t : Null<CType>, ecatch : Expr );
|
||||
EObject( fl : Array<{ name : String, e : Expr }> );
|
||||
ETernary( cond : Expr, e1 : Expr, e2 : Expr );
|
||||
ESwitch( e : Expr, cases : Array<{ values : Array<Expr>, expr : Expr }>, ?defaultExpr : Expr);
|
||||
}
|
||||
|
||||
enum CType {
|
||||
|
||||
@@ -378,6 +378,23 @@ class Interp {
|
||||
return o;
|
||||
case ETernary(econd,e1,e2):
|
||||
return if( expr(econd) == true ) expr(e1) else expr(e2);
|
||||
case ESwitch(e, cases, def):
|
||||
var val : Dynamic = expr(e);
|
||||
var match = false;
|
||||
for( c in cases ) {
|
||||
for( v in c.values )
|
||||
if( expr(v) == val ) {
|
||||
match = true;
|
||||
break;
|
||||
}
|
||||
if( match ) {
|
||||
val = expr(c.expr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if( !match )
|
||||
val = def == null ? null : expr(def);
|
||||
return val;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+37
-3
@@ -58,7 +58,7 @@ class Parser {
|
||||
public var opRightAssoc : Hash<Bool>;
|
||||
public var unops : Hash<Bool>; // true if allow postfix
|
||||
#end
|
||||
|
||||
|
||||
/**
|
||||
activate JSON compatiblity
|
||||
**/
|
||||
@@ -92,7 +92,7 @@ class Parser {
|
||||
#else
|
||||
var tokens : haxe.FastList<Token>;
|
||||
#end
|
||||
|
||||
|
||||
#end
|
||||
|
||||
|
||||
@@ -556,7 +556,41 @@ class Parser {
|
||||
}
|
||||
ensure(TPClose);
|
||||
var ec = parseExpr();
|
||||
mk(ETry(e,vname,t,ec),p1,pmax(ec));
|
||||
mk(ETry(e, vname, t, ec), p1, pmax(ec));
|
||||
case "switch":
|
||||
var e = parseExpr();
|
||||
var def = null, cases = [];
|
||||
ensure(TBrOpen);
|
||||
while( true ) {
|
||||
var tk = token();
|
||||
switch( tk ) {
|
||||
case TId("case"):
|
||||
var c = { values : [], expr : null };
|
||||
cases.push(c);
|
||||
while( true ) {
|
||||
var e = parseExpr();
|
||||
c.values.push(e);
|
||||
switch( token() ) {
|
||||
case TComma:
|
||||
// next expr
|
||||
case TSemicolon:
|
||||
break;
|
||||
default:
|
||||
unexpected(tk);
|
||||
}
|
||||
}
|
||||
c.expr = parseExpr();
|
||||
case TId("default"):
|
||||
if( def != null ) unexpected(tk);
|
||||
ensure(TSemicolon);
|
||||
def = parseExpr();
|
||||
case TBrClose:
|
||||
break;
|
||||
default:
|
||||
unexpected(tk);
|
||||
}
|
||||
}
|
||||
mk(ESwitch(e, cases, def), p1, tokenMax);
|
||||
default:
|
||||
null;
|
||||
}
|
||||
|
||||
@@ -26,6 +26,13 @@ class Tools {
|
||||
case ETry(e, _, _, c): f(e); f(c);
|
||||
case EObject(fl): for( fi in fl ) f(fi.e);
|
||||
case ETernary(c, e1, e2): f(c); f(e1); f(e2);
|
||||
case ESwitch(e, cases, def):
|
||||
f(e);
|
||||
for( c in cases ) {
|
||||
for( v in c.values ) f(v);
|
||||
f(c.expr);
|
||||
}
|
||||
if( def != null ) f(def);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +59,7 @@ class Tools {
|
||||
case ETry(e, v, t, c): ETry(f(e), v, t, f(c));
|
||||
case EObject(fl): EObject([for( fi in fl ) { name : fi.name, e : f(fi.e) }]);
|
||||
case ETernary(c, e1, e2): ETernary(f(c), f(e1), f(e2));
|
||||
case ESwitch(e, cases, def): ESwitch(f(e), [for( c in cases ) { values : [for( v in c.values ) f(v)], expr : f(c.expr) } ], def == null ? null : f(def));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user