Pārlūkot izejas kodu

added macro based .add() with parameters detection

ncannasse 7 gadi atpakaļ
vecāks
revīzija
8a3768a1d2
1 mainītis faili ar 32 papildinājumiem un 1 dzēšanām
  1. 32 1
      h2d/Console.hx

+ 32 - 1
h2d/Console.hx

@@ -16,8 +16,9 @@ typedef ConsoleArgDesc = {
 	?opt : Bool,
 }
 
-class Console extends h2d.Object {
+class Console #if !macro extends h2d.Object #end {
 
+	#if !macro
 	public static var HIDE_LOG_TIMEOUT = 3.;
 
 	var width : Int;
@@ -66,6 +67,34 @@ class Console extends h2d.Object {
 		commands.set(name, { help : help == null ? "" : help, args:args, callb:callb } );
 	}
 
+	#end
+
+	public macro function add( ethis, name, callb ) {
+		var args = [];
+		var et = haxe.macro.Context.typeExpr(callb);
+		switch( haxe.macro.Context.follow(et.t) ) {
+		case TFun(fargs, _):
+			for( a in fargs ) {
+				var t = haxe.macro.Context.followWithAbstracts(a.t);
+				var tstr = haxe.macro.TypeTools.toString(t);
+				var tval = switch( tstr ) {
+				case "Int": AInt;
+				case "Float": AFloat;
+				case "String": AString;
+				case "Bool": ABool;
+				default: haxe.macro.Context.error("Unsupported parameter type "+tstr+" for argument "+a.name, callb.pos);
+				}
+				var tname = ""+tval;
+				args.push(macro { name : $v{a.name}, t : h2d.Console.ConsoleArg.$tname, opt : $v{a.opt} });
+			}
+		default:
+			haxe.macro.Context.error(haxe.macro.TypeTools.toString(et.t)+" should be a function", callb.pos);
+		}
+		return macro $ethis.addCommand($name,null,$a{args},$callb);
+	}
+
+	#if !macro
+
 	public function addAlias( name, command ) {
 		aliases.set(name, command);
 	}
@@ -357,4 +386,6 @@ class Console extends h2d.Object {
 		super.sync(ctx);
 	}
 
+	#end
+
 }