Skeleton of Generable build macro

This commit is contained in:
2022-03-30 15:09:10 -06:00
parent c6f70b46ea
commit 95e12f62e1
4 changed files with 101 additions and 1 deletions

View File

@@ -0,0 +1,70 @@
package prokgen;
import haxe.macro.Context;
import haxe.macro.Expr;
class Generable {
public static macro function build():Array<Field> {
var fields = Context.getBuildFields();
var instanceFields:Map<String, ComplexType> = [];
var generatorFields:Map<ComplexType,String> = [];
var hasGenScore = false;
for (field in fields) {
switch (field) {
// TODO find all the fields that are generators
case {
name: name,
doc: _,
access: access,
kind: FVar(type, _),
pos: _,
meta: _
} if (access.indexOf(AStatic) != -1):
// TODO find all fields that are non-generator instance variables
case {
name: name,
doc: _,
access: access,
kind: FVar(type, _),
pos: _,
meta: _
} if (access.indexOf(AStatic) == -1):
// TODO make sure genScore() is defined and returns Float
case {
name: "genScore",
doc: _,
access: access,
kind: FFun({
// TODO ret needs to be float or Tink_macro needs to identify the expression as returning Float
ret: _,
args: [],
expr: _
}),
pos: _,
meta: _
}:
hasGenScore = true;
default:
}
}
var generateField = {
name: "generate",
access: [AStatic, APublic],
kind: FFun({
ret: null,
// TODO accept a ProkRandom arg
args: [],
expr: macro return 0
}),
pos: Context.currentPos()
};
fields.push(generateField);
return fields;
}
}

View File

@@ -4,6 +4,7 @@ import kiss.Kiss;
import kiss.Prelude;
import prokgen.ProkRandom;
import prokgen.generators.*;
import prokgen.examples.*;
@:build(kiss.Kiss.build())
class Main {}

View File

@@ -10,4 +10,5 @@
:Array<Dynamic> cList (for i (range generators.length) (.combine (nth generators i) (nth aList i) (nth bList i)))]
~aList
~bList
~cList))
~cList)
~(HighNumbers.generate))

View File

@@ -0,0 +1,28 @@
package prokgen.examples;
import prokgen.generators.*;
@:build(prokgen.Generable.build())
class HighNumbers {
var i:Int;
var f:Float;
var iList:Array<Int>;
var fList:Array<Float>;
static var iGen:IntGen = new IntGen(-1000, 1000);
static var fGen:FloatGen = new FloatGen(-1000, 100);
static var iListGen:ArrayGen<Int> = new ArrayGen<Int>(new IntGen(-1000, 1000), 0, 10);
static var fListGen:ArrayGen<Float> = new ArrayGen<Float>(new FloatGen(-1000, 1000), 0, 10);
function genScore() {
var sum:Float = i + f;
for (i in iList) {
sum += i;
}
for (f in fList) {
sum += f;
}
return sum;
}
}