Skip to content

Instantly share code, notes, and snippets.

@skial
Last active December 16, 2015 06:59
Show Gist options
  • Select an option

  • Save skial/5395970 to your computer and use it in GitHub Desktop.

Select an option

Save skial/5395970 to your computer and use it in GitHub Desktop.
package ;
class A {
public function new() {
}
public function some() {
}
public function thing() {
}
}
-D debug
-D no-copt
-cp src
-main Main
-js bin/example.methodCascades.js
package ;
@:build( MyMacro.build() )
class Main {
public static function main() {
var a = new A();
_.some();
_.thing();
_.
}
}
package example.methodCascades;
import haxe.macro.Compiler;
import haxe.macro.Type;
import haxe.macro.Expr;
import haxe.macro.Context;
/**
* ...
* @author Skial Bainn
*/
class MyMacro {
public static function build() {
var fields = Context.getBuildFields();
for ( field in fields ) {
switch ( field.kind ) {
case FFun( f ):
f.expr = find( f.expr );
case _:
}
}
return fields;
}
private static var prev:Var = null;
private static function find(e:Expr):Expr {
var result = e;
switch ( e.expr ) {
case EBlock( exprs ):
var new_exprs = [];
for ( expr in exprs ) {
new_exprs.push( find( expr ) );
}
result = { expr:EBlock( new_exprs ), pos:e.pos };
case ECall( expr, params ):
result = { expr:ECall( find( expr ), params ), pos:e.pos };
case EField( expr, field ):
result = { expr:EField( find( expr ), field ), pos:e.pos };
case EConst( constant ):
var value = null;
switch ( constant ) {
case CIdent( s ):
value = s;
case _:
value = null;
}
if ( value != '_' ) {
result = { expr:EConst( constant ), pos:e.pos };
} else {
result = { expr:EConst( CIdent( prev.name ) ), pos:e.pos };
}
case EVars( vars ):
prev = vars[0];
result = { expr:EVars( vars ), pos:e.pos };
case EDisplay(e1, isCall):
// Thanks @simn - https://github.com/HaxeFoundation/haxe/issues/1704
result = { expr: EDisplay(find(e1),isCall), pos: e.pos };
case _:
trace( e );
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment