Skip to content

Instantly share code, notes, and snippets.

@Sunjammer
Last active August 29, 2015 14:17
Show Gist options
  • Save Sunjammer/39a4a0bf4237b38e2025 to your computer and use it in GitHub Desktop.
Save Sunjammer/39a4a0bf4237b38e2025 to your computer and use it in GitHub Desktop.
Shitty haxe short lambdas
package;
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Expr.Field;
import haxe.macro.Expr.FieldType;
import haxe.macro.Expr.Function;
import haxe.macro.MacroStringTools;
using StringTools;
/**
* ...
* @author Andreas Rønning
*/
class Shortish
{
static function unrollExpr(expr:Expr) {
switch(expr.expr) {
case EField(e, s):
unrollExpr(e);
case EFunction(name, func):
unrollExpr(func.expr);
case EBlock(exprs):
for (e in exprs) {
unrollExpr(e);
}
case ECall(e, p):
for (param in p) {
unrollExpr(param);
}
unrollExpr(e);
case EVars(vars):
for (v in vars) {
unrollExpr(v.expr);
}
case EMeta(s, e):
switch(s.name.toLowerCase()) {
case "f":
blam(e, expr);
default:
unrollExpr(e);
}
default:
}
}
static function create(s:String):ExprDef {
var func = s.split("=>");
if (func.length != 2) { } //Some error
func[0] = func[0].replace("(", "").replace(")", "").replace(" ","");
var args:Array<FunctionArg> = [];
var argstrs = func[0].split(",");
while (argstrs.length > 0) {
args.push( { name:argstrs.shift(), type:null } );
}
var body = Context.parse("return " + func[1], Context.currentPos());
var f:Function = {
args : args,
ret : null,
expr : body
}
return EFunction(null, f);
}
static function blam(expr:Expr, toReplace:Expr) {
switch(expr.expr) {
case EConst(c):
switch(c) {
case CString(s):
toReplace.expr = create(s);
default:
}
default:
}
}
macro public static function build():Array<Field> {
var fields = Context.getBuildFields();
for (fld in fields) {
switch(fld.kind) {
case FFun(func):
unrollExpr(func.expr);
default : continue;
}
}
return fields;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment