Skeleton of Generable build macro
This commit is contained in:
70
projects/prokgen/src/prokgen/Generable.hx
Normal file
70
projects/prokgen/src/prokgen/Generable.hx
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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 {}
|
||||
|
||||
@@ -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))
|
||||
|
||||
28
projects/prokgen/src/prokgen/examples/HighNumbers.hx
Normal file
28
projects/prokgen/src/prokgen/examples/HighNumbers.hx
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user