Skip to content

Instantly share code, notes, and snippets.

@skial
Created May 24, 2013 13:34
Show Gist options
  • Select an option

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

Select an option

Save skial/5643546 to your computer and use it in GitHub Desktop.
Basic experiment to add named arguments / parameters to Haxe. Using Haxe r6642
package example.namedArgs;
import haxe.macro.Type;
import haxe.macro.Expr;
import haxe.macro.Context;
using StringTools;
using uhu.macro.Jumla;
/**
* ...
* @author Skial Bainn
*/
class Macro {
public static function build() {
var cls = Context.getLocalClass().get();
var fields = Context.getBuildFields();
for (field in fields) {
switch (field.kind) {
case FFun(method):
method.expr = loop( method.expr );
case _:
}
}
return fields;
}
public static function loop(e:Expr):Expr {
var result = e;
switch (e.expr) {
case EBlock(exprs):
result = { expr:EBlock( [for (expr in exprs) loop( expr )] ), pos: e.pos };
case ECall(expr, params):
var copy = params.copy();
var matches:Array<{e:Expr, n:String, pos:Int}> = [];
for (i in 0...params.length) {
var val:Expr = params[i];
if (val.expr.getName() == 'EMeta') {
var type = expr.printExpr().find();
var arity = type.arity();
var args = type.args();
var meta:MetadataEntry = val.expr.getParameters()[0];
var name:String = meta.name.replace(':', '');
matches.push( { e: val, n: name , pos: args.indexOf( name ) } );
copy = copy.splice(0, i).concat( copy.splice(i + 1, -1) );
}
}
for (match in matches) {
if (match.pos == -1) {
trace( match );
}
while (match.pos > copy.length - 1) {
copy.push(macro null);
}
copy[match.pos] = match.e.expr.getParameters()[1];
}
result = { expr: ECall( expr, copy ), pos: e.pos };
case _:
trace( e );
}
return result;
}
}
package example.namedArgs;
/**
* ...
* @author Skial Bainn
*/
@:build( example.namedArgs.Macro.build() )
class Main {
public static function main() {
foo( true, false, true, @:g false );
}
public static function foo(a:Bool, b:Bool, c:Bool, d:Bool = false, e:Bool = true, f:Bool = false, g:Bool = true) {
trace('a = $a');
trace('b = $b');
trace('c = $c');
trace('d = $d');
trace('e = $e');
trace('f = $f');
trace('g = $g');
}
}
(function () { "use strict";
var Std = function() { }
Std.__name__ = true;
Std.string = function(s) {
return js.Boot.__string_rec(s,"");
}
var example = {}
example.namedArgs = {}
example.namedArgs.Main = function() { }
example.namedArgs.Main.__name__ = true;
example.namedArgs.Main.main = function() {
example.namedArgs.Main.foo(true,false,true,null,null,null,false);
}
example.namedArgs.Main.foo = function(a,b,c,d,e,f,g) {
if(g == null) g = true;
if(f == null) f = false;
if(e == null) e = true;
if(d == null) d = false;
console.log("a = " + Std.string(a));
console.log("b = " + Std.string(b));
console.log("c = " + Std.string(c));
console.log("d = " + Std.string(d));
console.log("e = " + Std.string(e));
console.log("f = " + Std.string(f));
console.log("g = " + Std.string(g));
}
var js = {}
js.Boot = function() { }
js.Boot.__name__ = true;
js.Boot.__string_rec = function(o,s) {
if(o == null) return "null";
if(s.length >= 5) return "<...>";
var t = typeof(o);
if(t == "function" && (o.__name__ || o.__ename__)) t = "object";
switch(t) {
case "object":
if(o instanceof Array) {
if(o.__enum__) {
if(o.length == 2) return o[0];
var str = o[0] + "(";
s += "\t";
var _g1 = 2, _g = o.length;
while(_g1 < _g) {
var i = _g1++;
if(i != 2) str += "," + js.Boot.__string_rec(o[i],s); else str += js.Boot.__string_rec(o[i],s);
}
return str + ")";
}
var l = o.length;
var i;
var str = "[";
s += "\t";
var _g = 0;
while(_g < l) {
var i1 = _g++;
str += (i1 > 0?",":"") + js.Boot.__string_rec(o[i1],s);
}
str += "]";
return str;
}
var tostr;
try {
tostr = o.toString;
} catch( e ) {
return "???";
}
if(tostr != null && tostr != Object.toString) {
var s2 = o.toString();
if(s2 != "[object Object]") return s2;
}
var k = null;
var str = "{\n";
s += "\t";
var hasp = o.hasOwnProperty != null;
for( var k in o ) { ;
if(hasp && !o.hasOwnProperty(k)) {
continue;
}
if(k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__") {
continue;
}
if(str.length != 2) str += ", \n";
str += s + k + " : " + js.Boot.__string_rec(o[k],s);
}
s = s.substring(1);
str += "\n" + s + "}";
return str;
case "function":
return "<function>";
case "string":
return o;
default:
return String(o);
}
}
String.__name__ = true;
Array.__name__ = true;
example.namedArgs.Main.main();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment