Refactor haxe.maco.Function generation out of FieldForms

This commit is contained in:
2020-11-25 13:58:49 -07:00
parent 4faa4cf831
commit 90c7ba68da
2 changed files with 41 additions and 35 deletions

View File

@@ -85,41 +85,7 @@ class FieldForms {
return {
name: name,
access: access,
// TODO type parameter declarations
kind: FFun({
args: switch (args[1].def) {
case ListExp(funcArgs):
[
// TODO optional arguments, default values
for (funcArg in funcArgs)
{
name: switch (funcArg.def) {
case Symbol(name) | TypedExp(_, {pos: _, def: Symbol(name)}):
name;
default:
throw CompileError.fromExp(funcArg, 'function argument should be a symbol or typed symbol');
},
type: switch (funcArg.def) {
case TypedExp(type, _):
Helpers.parseComplexType(type, funcArg);
default: null;
}
}
];
case CallExp(_, _):
throw CompileError.fromExp(args[1], 'expected an argument list. Change the parens () to brackets []');
default:
throw CompileError.fromExp(args[1], 'expected an argument list');
},
ret: switch (args[0].def) {
case TypedExp(type, _): Helpers.parseComplexType(type, args[0]);
default: null;
},
expr: {
pos: Context.currentPos(),
expr: EReturn(convert(CallExp(Symbol("begin").withPos(args[2].pos), args.slice(2)).withPos(args[2].pos)))
}
}),
kind: FFun(Helpers.makeFunction(args[0], args[1], args.slice(2), convert)),
pos: Context.currentPos()
};
}

View File

@@ -4,7 +4,9 @@ import haxe.macro.Expr;
import haxe.macro.Context;
import kiss.Reader;
import kiss.CompileError;
import kiss.Types;
using kiss.Reader;
using StringTools;
class Helpers {
@@ -50,4 +52,42 @@ class Helpers {
public static function parseComplexType(path:String, from:ReaderExp):ComplexType {
return TPath(parseTypePath(path, from));
}
// TODO generic type parameter declarations
public static function makeFunction(?name:ReaderExp, argList:ReaderExp, body:Array<ReaderExp>, convert:ExprConversion):Function {
return {
ret: if (name != null) switch (name.def) {
case TypedExp(type, _): Helpers.parseComplexType(type, name);
default: null;
} else null,
args: switch (argList.def) {
case ListExp(funcArgs):
[
// TODO optional arguments, default values, rest arguments
for (funcArg in funcArgs)
{
name: switch (funcArg.def) {
case Symbol(name) | TypedExp(_, {pos: _, def: Symbol(name)}):
name;
default:
throw CompileError.fromExp(funcArg, 'function argument should be a symbol or typed symbol');
},
type: switch (funcArg.def) {
case TypedExp(type, _):
Helpers.parseComplexType(type, funcArg);
default: null;
}
}
];
case CallExp(_, _):
throw CompileError.fromExp(argList, 'expected an argument list. Change the parens () to brackets []');
default:
throw CompileError.fromExp(argList, 'expected an argument list');
},
expr: {
pos: Context.currentPos(),
expr: EReturn(convert(CallExp(Symbol("begin").withPos(body[0].pos), body).withPos(body[0].pos)))
}
}
}
}