Deal with ENew and EConst(CIdent)). Updated tests & doc.

This commit is contained in:
back2dos
2013-11-06 21:59:35 +01:00
parent aefe622434
commit 872b6be411
3 changed files with 30 additions and 2 deletions

View File

@@ -129,6 +129,7 @@ Traverse an expression and replace any *type* that looks like a type parameter f
``` ```
A `StringMap` is a natural fit here, but you can do whatever you want. A `StringMap` is a natural fit here, but you can do whatever you want.
Note that if the type for a given name is a `TPath`, it will also be substituted for class names in `new` statements and for identifiers of that name.
- `function typedMap(source:Expr, f:Expr->Array<Var>->Expr, ?ctx:Array<Var>, ?pos:Position):Expr` - `function typedMap(source:Expr, f:Expr->Array<Var>->Expr, ?ctx:Array<Var>, ?pos:Position):Expr`
Similar to transform, but handles expressions in top-down order and keeps track of variable declarations, function arguments etc. Only expressions that are not changed by the transformer function `f` are traversed further. The second argument to `f` is the current context that you can use in `typeof` to determine the type of a subexpression. Similar to transform, but handles expressions in top-down order and keeps track of variable declarations, function arguments etc. Only expressions that are not changed by the transformer function `f` are traversed further. The second argument to `f` is the current context that you can use in `typeof` to determine the type of a subexpression.
- `function bounce(f:Void->Expr, ?pos:Position):Expr` - `function bounce(f:Void->Expr, ?pos:Position):Expr`

View File

@@ -113,7 +113,22 @@ class Exprs {
static public function substParams(source:Expr, subst:ParamSubst, ?pos):Expr static public function substParams(source:Expr, subst:ParamSubst, ?pos):Expr
return crawl( return crawl(
source, source,
function (e) return e, function (e)
return switch e.expr {
case ENew({ pack: [], name: name }, args) if (subst.exists(name)):
switch subst.get(name) {
case TPath(p):
ENew(p, args).at(e.pos);
default: e;//TODO: report an error?
}
case EConst(CIdent(name)) if (subst.exists(name)):
switch subst.get(name) {
case TPath({ pack: pack, name: name }):
pack.concat([name]).drill(e.pos);
default: e;//TODO: report an error?
}
default: e;
},
function (c:ComplexType) function (c:ComplexType)
return return
switch (c) { switch (c) {

View File

@@ -46,7 +46,6 @@ class Exprs extends Base {
test(macro @yield while (_) foo, macro while (_) foo, { leaveLoops: true }); test(macro @yield while (_) foo, macro while (_) foo, { leaveLoops: true });
test(macro @yield [while (_) foo], macro [while (_) foo]); test(macro @yield [while (_) foo], macro [while (_) foo]);
} }
function testSubstitute() { function testSubstitute() {
exprEq( exprEq(
macro foo.call(arg1, arg2), macro foo.call(arg1, arg2),
@@ -67,5 +66,18 @@ class Exprs extends Base {
'C' => macro : Array<Float> 'C' => macro : Array<Float>
]) ])
); );
exprEq(
macro {
new Foo<Bar>(1, 2, 3);
Bar.foo();
},
(macro {
new X(1, 2, 3);
Y.foo();
}).substParams([
'X' => macro : Foo<Bar>,
'Y' => macro : Bar
])
);
} }
} }