Compare commits

..

15 Commits

Author SHA1 Message Date
Juraj Kirchheim
27de7bb3e7 Release 0.12.0 2016-11-05 16:28:11 +01:00
Juraj Kirchheim
371f393299 Merge branch 'master' of https://github.com/haxetink/tink_macro
# Conflicts:
#	src/tink/MacroApi.hx
2016-11-05 16:27:39 +01:00
Juraj Kirchheim
abbb9e5a4c Fixes #10. 2016-11-05 16:26:59 +01:00
back2dos
921ae162c8 Release 0.11.0 2016-10-13 09:43:55 +02:00
Juraj Kirchheim
fe779872d2 Merge pull request #9 from psmtec-dklein/fix-option
Type name tink.Option is redefined from module tink.MacroApi
2016-10-12 12:52:13 +02:00
David Klein
f8ab1c7354 tink.Option<T> is already pulled in by 'using tink.CoreApi;' 2016-10-12 11:26:06 +02:00
Juraj Kirchheim
ba5349f55f Slightly better error reporting in build cache. 2016-10-06 10:39:33 +02:00
Juraj Kirchheim
92f60cbe4f Release 0.10.0 2016-07-08 11:49:30 +02:00
Juraj Kirchheim
3b822b4cdb Merge branch 'master' of https://github.com/haxetink/tink_macro 2016-07-08 11:49:00 +02:00
Juraj Kirchheim
f2b670f9e4 Expand build cache for 2 and 3 type parameters. 2016-07-08 11:48:40 +02:00
Kevin Leung
ff491fe7e7 Add gitter link 2016-07-06 15:22:47 +08:00
Juraj Kirchheim
7e869cd9c8 Fix logic in Exprs.has 2016-06-23 05:19:53 +02:00
Juraj Kirchheim
983ffa16af Avoid stringly approach. 2016-06-10 08:20:09 +02:00
Juraj Kirchheim
4207dea754 Release 0.9.1 2016-05-20 12:41:35 +02:00
Juraj Kirchheim
3470f1cc47 Store user defined constructors. 2016-05-20 12:41:20 +02:00
9 changed files with 139 additions and 31 deletions

View File

@@ -1,4 +1,5 @@
# Tinkerbell Macro Library
[![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg?maxAge=2592000)](https://gitter.im/haxetink/public)
Explained in current marketing speak, `tink_macro` is *the* macro toolkit ;)

View File

@@ -11,8 +11,8 @@
"contributors": [
"back2dos"
],
"releasenote": "Replace MacroType with @:genericBuild for direct type converions.",
"version": "0.9.0",
"releasenote": "Improved build cache.",
"version": "0.12.0",
"url": "http://haxetink.org/tink_macro",
"dependencies": {
"tink_core": ""

View File

@@ -19,8 +19,6 @@ typedef Unops = tink.macro.Ops.Unary;
typedef MacroOutcome<D, F> = tink.core.Outcome<D, F>;
typedef MacroOutcomeTools = tink.OutcomeTools;
typedef Option<T> = haxe.ds.Option<T>;
typedef Member = tink.macro.Member;
typedef Constructor = tink.macro.Constructor;
typedef ClassBuilder = tink.macro.ClassBuilder;

View File

@@ -5,6 +5,16 @@ import haxe.macro.Expr;
import haxe.macro.Type;
import tink.macro.TypeMap;
using haxe.macro.Tools;
typedef BuildContextN = {
pos:Position,
types:Array<Type>,
usings:Array<TypePath>,
name:String,
}
typedef BuildContext = {
pos:Position,
type:Type,
@@ -12,21 +22,87 @@ typedef BuildContext = {
name:String,
}
typedef BuildContext2 = {>BuildContext,
type2:Type,
}
typedef BuildContext3 = {>BuildContext2,
type3:Type,
}
class BuildCache {
static var cache = init();
static var cache = new Map();
static function init() {
function refresh() {
cache = new Map();
return true;
static public function getType3(name, ?types, ?pos:Position, build:BuildContext3->TypeDefinition) {
if (types == null)
switch Context.getLocalType() {
case TInst(_.toString() == name => true, [t1, t2, t3]):
types = { t1: t1, t2: t2, t3: t3 };
default:
throw 'assert';
}
Context.onMacroContextReused(refresh);
refresh();
var t1 = types.t1.toComplexType(),
t2 = types.t2.toComplexType(),
t3 = types.t2.toComplexType();
return cache;
return getType(name, (macro : { t1: $t1, t2: $t2, t3: $t3 } ).toType(), pos, function (ctx) return build({
type: types.t1,
type2: types.t2,
type3: types.t3,
pos: ctx.pos,
name: ctx.name,
usings: ctx.usings
}));
}
static public function getTypeN(name, ?types, ?pos:Position, build:BuildContextN->TypeDefinition) {
if (pos == null)
pos = Context.currentPos();
if (types == null)
switch Context.getLocalType() {
case TInst(_.toString() == name => true, params):
types = params;
default:
throw 'assert';
}
var compound = ComplexType.TAnonymous([for (i in 0...types.length) {
name: 't$i',
pos: pos,
kind: FVar(types[i].toComplexType()),
}]).toType();
return getType(name, compound, pos, function (ctx) return build({
types: types,
pos: ctx.pos,
name: ctx.name,
usings: ctx.usings
}));
}
static public function getType2(name, ?types, ?pos:Position, build:BuildContext2->TypeDefinition) {
if (types == null)
switch Context.getLocalType() {
case TInst(_.toString() == name => true, [t1, t2]):
types = { t1: t1, t2: t2 };
default:
throw 'assert';
}
var t1 = types.t1.toComplexType(),
t2 = types.t2.toComplexType();
return getType(name, (macro : { t1: $t1, t2: $t2 } ).toType(), pos, function (ctx) return build({
type: types.t1,
type2: types.t2,
pos: ctx.pos,
name: ctx.name,
usings: ctx.usings
}));
}
static public function getType(name, ?type, ?pos:Position, build:BuildContext->TypeDefinition) {
@@ -38,20 +114,42 @@ class BuildCache {
switch Context.getLocalType() {
case TInst(_.toString() == name => true, [v]):
type = v;
case TInst(_.toString() == name => true, _):
Context.fatalError('type parameter expected', pos);
case TInst(_.get() => { pos: pos }, _):
Context.fatalError('Expected $name', pos);
default:
throw 'assert';
}
var forName =
switch cache[name] {
case null: cache[name] = new TypeMap();
case null: cache[name] = new Group(name);
case v: v;
}
if (!forName.exists(type)) {
var path = '$name${Lambda.count(forName)}',
usings = [];
return forName.get(type, pos, build);
}
}
private typedef Entry = {
name:String,
}
private class Group {
var name:String;
var counter = 0;
var entries = new TypeMap<Entry>();
public function new(name) {
this.name = name;
}
public function get(type:Type, pos:Position, build:BuildContext->TypeDefinition):Type {
function make(path:String) {
var usings = [];
var def = build({
pos: pos,
type: type,
@@ -60,9 +158,21 @@ class BuildCache {
});
Context.defineModule(path, [def], usings);
forName.set(type, Context.getType(path));
entries.set(type, { name: path } );
return Context.getType(path);
}
return forName.get(type);
return
switch entries.get(type) {
case null:
make('$name${counter++}');
case v:
try {
Context.getType(v.name);
}
catch (e:Dynamic) {
make(v.name);
}
}
}
}

View File

@@ -55,7 +55,7 @@ class ClassBuilder {
init();
if (constructor == null)
if (fallback != null)
new Constructor(this, fallback);
constructor = new Constructor(this, fallback);
else {
var sup = target.superClass;
while (sup != null) {

View File

@@ -96,7 +96,7 @@ class Constructor {
case Success(member): member.addMeta(':isVar');
default:
}
addStatement(macro @:pos(pos) (cast this).$name = if (true) $e else this.$name, options.prepend);
addStatement(macro @:pos(pos) (cast this).$name = if (true) $e else this.$name, options.prepend);//TODO: this seems to report type errors here rather than at the declaration position
}
else
addStatement(macro @:pos(pos) this.$name = $e, options.prepend);

View File

@@ -27,10 +27,10 @@ private class Heureka { public function new() {} }
class Exprs {
static public function has(e:Expr, condition:Expr->Bool, ?options: { ?enterFunctions: Bool }) {
var enterFunctions = options != null && options.enterFunctions;
var skipFunctions = options == null || options.enterFunctions != true;
function seek(e:Expr)
switch e {
case { expr: EFunction(_) } if (options != null || options.enterFunctions != true):
case { expr: EFunction(_) } if (skipFunctions):
case _ if (condition(e)): throw new Heureka();
default: haxe.macro.ExprTools.iter(e, seek);
}

View File

@@ -35,7 +35,7 @@ class Positions {
return errorFunc(sanitize(pos), Std.string(error));
static function contextError(pos:Position, error:String):Dynamic
return Context.error(error, pos);
return Context.fatalError(error, pos);
static function abortTypeBuild(pos:Position, error:String):Dynamic
return throw new AbortBuild(error, pos);

View File

@@ -168,12 +168,11 @@ class Types {
sub : null,
});
static function resolveDirectType()
return
switch reduce(Context.getLocalType()) {
case TInst(_, [TInst(_.get() => { kind: KExpr(e) }, _)]):
types.get(Std.parseInt(e.toString()))();
types[e.getInt().sure()]();
default:
throw 'assert';
}