Skip to content

Instantly share code, notes, and snippets.

@IanSSenne
Created May 16, 2023 08:07
Show Gist options
  • Save IanSSenne/f6c64553af22de16847050950e3cdc70 to your computer and use it in GitHub Desktop.
Save IanSSenne/f6c64553af22de16847050950e3cdc70 to your computer and use it in GitHub Desktop.

what is this?

this is a haxe macro function that will show expr information via Context.info.

image

image

package;
import haxe.macro.ComplexTypeTools;
import haxe.Json;
import haxe.EnumTools;
import haxe.macro.ExprTools;
import haxe.macro.Expr;
import haxe.macro.Context;
class ExprUtils {
private static function s(v:Any):String {
return Json.stringify(v);
}
public static macro function inspect(e:Expr) {
function itterate(e:Expr, depth:Int) {
ExprTools.iter(e, (e2) -> {
var params:Array<String> = [];
switch (e2.expr) {
case EConst(c):
params.push('EConst(${switch (c) {
case CInt(v, s):
"CInt(" + v + (s == null ? ", " + s : "") + ")";
case CFloat(v):
"CFloat(" + v + ")";
case CString(v,kind):
"CString(" + s(v) + (kind == null ? "" : ", "+kind.getName())+ ")";
case CIdent(v):
"CIdent(" + v + ")";
case CRegexp(v, opt):
"CRegexp(" + s(v) + "," + s(v) + ")";
}})');
case EBinop(op, _, _):
params.push('EBinop(' + op.getName() + ',_,_)');
case EUnop(op, _):
params.push('EUnop(' + op.getName() + ',_)');
case EField(_, field, kind):
params.push('EField(_,${s(field)}${kind == null ? "" : ", " + kind.getName()})');
case EObjectDecl(fields):
params.push('EObjectDecl([${fields.map((f) -> s({
field: f.field,
quotes: f.quotes,
})).join(", ")}])');
case ENew(t, _):
params.push('ENew(' + s({
name: t.name,
params: t.params == null ? null : t.params.map(f -> switch (f) {
case TPType(t):
"TPType(" + ComplexTypeTools.toString(t) + ")";
case TPExpr(e):
"TPExpr(_)";
})
}) + ',_)');
case EUnop(op, postFix, _):
params.push('EUnop(' + op.getName() + ', ' + postFix + ', _)');
case EFunction(kind, f):
params.push('EFunction(' + kind.getName() + ',_)');
case EWhile(_, _, normalWhile):
params.push('EWhile(_,_,${normalWhile})');
case ETry(_, catches):
params.push('ETry(_,${catches.map((c) -> '{"name":${s(c.name)}${c.type == null ? "" : ", \"type\":" + ComplexTypeTools.toString(c.type)}}')})');
case ECast(_, t):
params.push('ECast(_,${t == null ? "null" : ComplexTypeTools.toString(t)})');
case EDisplay(_, displayKind):
params.push('EDisplay(_,${displayKind.getName()})');
case ECheckType(_, t):
params.push('ECheckType(_,${ComplexTypeTools.toString(t)})');
case EMeta(s2, _):
params.push('EMeta(${s({name:s2.name})},_)');
case EIs(_, t):
params.push('EIs(_,${ComplexTypeTools.toString(t)})');
case _:
var x = e2.expr.getParameters();
var s = e2.expr.getName();
if (x.length > 0) {
params.push(s + '(' + x.map((p) -> "_").join(", ") + ')');
} else {
params.push(s);
}
}
Context.info(depth + ":" + params.join(", "), e2.pos);
itterate(e2, depth + 1);
});
}
itterate(e, 0);
return e;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment