Skip to content

Commit

Permalink
a bit nicer syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
lemz1 committed Oct 29, 2024
1 parent 4e9a1ce commit 25a2d7a
Showing 1 changed file with 32 additions and 70 deletions.
102 changes: 32 additions & 70 deletions source/funkin/util/macro/PolymodMacro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ class PolymodMacro
{
var negate:Bool = abstractCls.startsWith('!');
var name:String = abstractCls.replace('!', '').replace('.*', '');
if (!negate && !cls.module.startsWith(name) && cls.module + cls.name != name && cls.pack.join('.') + '.' + cls.name != name)
if (!negate && !cls.module.startsWith(name) && moduleTypePath(cls) != name && packTypePath(cls) != name)
{
continue;
}
else if (negate)
{
if (cls.module.startsWith(name) || cls.module + cls.name == name || cls.pack.join('.') + '.' + cls.name == name)
if (cls.module.startsWith(name) || moduleTypePath(cls) == name || packTypePath(cls) == name)
{
break;
}
Expand All @@ -71,7 +71,7 @@ class PolymodMacro
}
}

aliases.set('${cls.pack.join('.')}.${cls.name}', 'polymod.abstracts.${cls.pack.join('.')}.${cls.name}');
aliases.set('${packTypePath(cls)}', 'polymod.abstracts.${packTypePath(cls)}');
buildAbstract(cls);
break;
}
Expand Down Expand Up @@ -177,8 +177,6 @@ class PolymodMacro

static function buildCreateField(cls:AbstractType, field:ClassField):Field
{
var newExprStr = trialAndError(cls, field) ?? 'new ${cls.module}.${cls.name}';

var funcArgs = [];
var funcArgNames:Array<String> = [];
switch (field.type)
Expand All @@ -198,6 +196,8 @@ class PolymodMacro
throw 'how is this not a function';
}

var expr:String = '${newExpr(cls, field)}(${funcArgNames.join(', ')})';

return {
name: 'create',
access: [Access.APublic, Access.AStatic],
Expand All @@ -208,59 +208,26 @@ class PolymodMacro
expr: macro
{
@:privateAccess
return ${Context.parse(newExprStr + '(' + funcArgNames.join(', ') + ')', Context.currentPos())};
return ${Context.parse(expr, Context.currentPos())};
},
}),
pos: Context.currentPos()
};
}

static function trialAndError(cls:AbstractType, field:ClassField):Null<String>
static function newExpr(cls:AbstractType, field:ClassField):Null<String>
{
if ('${cls.module}.${cls.name}' == 'flixel.util.FlxSignal.FlxTypedSignal')
if ('${moduleTypePath(cls)}' == 'flixel.util.FlxSignal.FlxTypedSignal')
{
return 'new flixel.util.FlxSignal.FlxTypedSignal<Dynamic->Void>';
}

if (cls.params.length <= 0)
{
return null;
}

function getCombinations(num:Int):Array<Array<String>>
{
if (num == 0)
{
return [[]];
}

var combinations:Array<Array<String>> = [];
for (combination in getCombinations(num - 1))
{
combinations.push(combination.concat(['Dynamic']));
// combinations.push(combination.concat(['Dynamic->Void']));
}

return combinations;
return 'new ${moduleTypePath(cls)}';
}
var typeArgss:Array<Array<String>> = getCombinations(cls.params.length);

for (typeArgs in typeArgss)
{
// TODO: figure out a way to find out whether a typeparameter is valid or not
try
{
// var expr = Context.parse('new ${cls.module}.${cls.name}<' + typeArgs.join(', ') + '>()', Context.currentPos());
// return expr;
return 'new ${cls.module}.${cls.name} <' + typeArgs.join(', ') + '>';
}
catch (e)
{
trace(e);
}
}

return null;
return 'new ${moduleTypePath(cls)}< ${[for (_ in 0...cls.params.length) 'Dynamic'].join(', ')} >';
}

static function createFields(cls:AbstractType, field:ClassField):Array<Field>
Expand Down Expand Up @@ -326,21 +293,21 @@ class PolymodMacro
default:
}

var strExpr = Context.parse('${returnStr}${cls.module}.${cls.name}.${field.name}(${exprArgs.join(', ')})', Context.currentPos());
var expr:String = '${returnStr}${moduleTypePath(cls)}.${field.name}(${exprArgs.join(', ')})';

fields.push(
{
name: field.name,
doc: field.doc,
access: [Access.AStatic].concat(getFieldAccess(field)),
access: [Access.APublic, Access.AStatic],
kind: FieldType.FFun(
{
args: fieldArgs,
ret: returnType,
expr: macro
{
@:privateAccess
${strExpr};
${Context.parse(expr, Context.currentPos())};
},
params: []
}),
Expand All @@ -351,26 +318,26 @@ class PolymodMacro
{
name: field.name,
doc: field.doc,
access: [Access.AStatic].concat(getFieldAccess(field)),
access: [Access.APublic, Access.AStatic],
kind: FieldType.FProp('get', 'never', (macro :Dynamic), null),
pos: Context.currentPos()
});

var strExpr = Context.parse('${cls.module}.${cls.name}.${field.name}', Context.currentPos());
var expr:String = '${moduleTypePath(cls)}.${field.name}';

fields.push(
{
name: 'get_${field.name}',
doc: field.doc,
access: [Access.AStatic, field.isPublic ? Access.APublic : Access.APrivate],
access: [Access.APublic, Access.AStatic],
kind: FieldType.FFun(
{
args: [],
ret: (macro :Dynamic),
expr: macro
{
@:privateAccess
return ${strExpr};
return ${Context.parse(expr, Context.currentPos())};
},
params: []
}),
Expand All @@ -381,26 +348,26 @@ class PolymodMacro
{
name: field.name,
doc: field.doc,
access: [Access.AStatic].concat(getFieldAccess(field)),
access: [Access.APublic, Access.AStatic],
kind: FieldType.FProp('get', 'never', (macro :Dynamic), null),
pos: Context.currentPos()
});

var strExpr = Context.parse('${cls.module}.${cls.name}.${field.name}', Context.currentPos());
var expr:String = '${moduleTypePath(cls)}.${field.name}';

fields.push(
{
name: 'get_${field.name}',
doc: field.doc,
access: [Access.AStatic, field.isPublic ? Access.APublic : Access.APrivate],
access: [Access.APublic, Access.AStatic],
kind: FieldType.FFun(
{
args: [],
ret: (macro :Dynamic),
expr: macro
{
@:privateAccess
return ${strExpr};
return ${Context.parse(expr, Context.currentPos())};
},
params: []
}),
Expand All @@ -413,23 +380,18 @@ class PolymodMacro
return fields;
}

static function getFieldAccess(field:ClassField):Array<Access>
// Dynamic so any kind of type with `module` and `name` fields works
static function moduleTypePath(type:Dynamic):String
{
var access = [];
access.push(field.isPublic ? Access.APublic : Access.APrivate);
if (field.isFinal)
{
access.push(Access.AFinal);
}
if (field.isAbstract)
{
access.push(Access.AAbstract);
}
if (field.isExtern)
{
access.push(Access.AExtern);
}
return access;
var dot:String = type.module.length != 0 ? '.' : '';
return '${type.module}${dot}${type.name}';
}

// Dynamic so any kind of type with `pack` and `name` fields works
static function packTypePath(type:Dynamic):String
{
var dot:String = type.pack.length != 0 ? '.' : '';
return '${type.pack.join('.')}${dot}${type.name}';
}
#end
}

0 comments on commit 25a2d7a

Please sign in to comment.