Compare commits

..

38 Commits

Author SHA1 Message Date
Juraj Kirchheim
209847e5ea Release 0.14.2 2017-09-11 10:36:42 +02:00
Juraj Kirchheim
c94e4a0337 Add way to get constructor args. 2017-09-11 10:35:54 +02:00
Juraj Kirchheim
cc009b2026 Release 0.14.1 2017-08-28 15:02:56 +02:00
Juraj Kirchheim
0a36ee8e2b Merge pull request #16 from ibilon/haxe4
Support for haxe 4 EIn change to EBinop(OpIn)
2017-08-18 16:00:37 +02:00
Juraj Kirchheim
5879773566 Fix typo. 2017-08-18 15:41:31 +02:00
Juraj Kirchheim
58262a1810 Use reification to abstract over Haxe version specific differences. 2017-08-18 15:38:58 +02:00
Valentin Lemière
9ceadeb88f Support for haxe 4 EIn change to EBinop(OpIn) 2017-08-18 15:13:28 +02:00
Juraj Kirchheim
15d75965ad Release 0.14.0 2017-06-05 10:13:08 +02:00
Juraj Kirchheim
c7634e0a29 Just use Error.rethrow 2017-06-01 13:14:39 +02:00
Juraj Kirchheim
49e48bc801 Merge pull request #15 from Simn/eval
don't use neko.Lib.rethrow when not in neko
2017-06-01 13:06:27 +02:00
Simon Krajewski
c150b84f18 don't use neko.Lib.rethrow when not in neko 2017-06-01 13:01:52 +02:00
Kevin Leung
04f86ce341 official gitter badge [ci skip] 2017-05-26 07:59:52 +08:00
Kevin Leung
45380371d8 Add badge [ci skip] 2017-05-19 17:54:35 +08:00
Juraj Kirchheim
d94d946301 Update README.md 2017-05-18 13:43:49 +02:00
Juraj Kirchheim
56b7caaf01 Release 0.13.5 2017-05-06 12:42:01 +02:00
Juraj Kirchheim
73e9da8435 Fix 3.2.1. 2017-04-13 21:07:44 +02:00
Juraj Kirchheim
bf23337923 Release 0.13.4 2017-04-13 21:04:00 +02:00
Juraj Kirchheim
02bd3bf69d Add Types.getMeta. 2017-04-13 21:02:58 +02:00
Juraj Kirchheim
82a3417b97 Gitignore. 2017-04-13 20:58:16 +02:00
Juraj Kirchheim
1f986c3732 VSCode setup. 2017-04-13 20:57:29 +02:00
Kevin Leung
20e998fd12 Release 0.13.3 2017-03-20 20:01:41 +08:00
Kevin Leung
0f9c0d1e97 what the haxe? 2017-03-20 19:49:21 +08:00
Kevin Leung
2ae13d534e my turn 2017-03-20 19:41:16 +08:00
back2dos
65d341f1e1 "push and read travis log" is the best kind of debugging ... 2017-03-20 11:50:32 +01:00
back2dos
68fa50c7c3 Let's see if we can fix that. 2017-03-20 11:32:50 +01:00
back2dos
32aa2d6570 Bump version. 2017-03-20 11:20:59 +01:00
back2dos
d81642e733 Use travix release. 2017-03-20 11:19:49 +01:00
Juraj Kirchheim
d5f4987ddb Release 0.13.1 2017-03-16 16:31:14 +01:00
Juraj Kirchheim
671f1bc995 Merge pull request #11 from haxetink/kevinresol-patch-1
Add TypeMap alias
2017-03-16 16:02:34 +01:00
back2dos
b1012937fc Release 0.13.0 2017-03-06 17:58:24 +01:00
back2dos
bd4b8c436f Build cache works better with compiler server. 2017-03-06 17:58:07 +01:00
back2dos
dbebd0d70d Allow querying whether a type exists. 2017-03-06 17:57:30 +01:00
Juraj Kirchheim
4c523e02a5 Release 0.12.3 2017-03-06 14:31:20 +01:00
Juraj Kirchheim
f39fc3b296 Try to save TMono. 2017-03-06 09:16:31 +01:00
Kevin Leung
e06c40325b Add TypeMap alias 2017-03-04 22:33:13 +08:00
back2dos
7a3914094d Release 0.12.2 2017-03-01 18:26:05 +01:00
back2dos
a597d7fa50 Workaround for https://github.com/HaxeFoundation/haxe/issues/6063 2017-03-01 18:25:21 +01:00
back2dos
3d5626d66f Give up on trying to work around cache problems. 2017-03-01 18:17:42 +01:00
14 changed files with 247 additions and 103 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/bin

View File

@@ -2,18 +2,26 @@ sudo: required
dist: trusty dist: trusty
language: haxe language: haxe
haxe: haxe:
- "3.2.1" - 3.2.1
- development - development
matrix: matrix:
allow_failures: allow_failures:
- haxe: development - haxe: development
install: install:
- haxelib install travix - haxelib install travix
- haxelib run travix install - haxelib run travix install
script: script:
- haxelib run travix node - haxelib run travix node
env:
secure: T4SCtY5qmEsK1ARWPevJmqLm23tv4CobLrbPOQV3FsoQno7FCP1S/+9GmuoJKzeTjWMzdTeDsp8TVwZ6AyGjvhl2nZNjhU+QTsir4tfbYYRyvsz/QK6pveFbPQVv7OsnnaB4wbZtqGZ8mzFeQf7Ol4tsNe7iUFJb/iVc+4/lUxo=
deploy:
provider: script
script: haxe && haxelib run travix release
on:
tags: true

5
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,5 @@
{
"haxe.displayConfigurations": [
["tests.hxml"]
]
}

6
.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,6 @@
{
"version": "0.1.0",
"command": "haxelib",
"args": ["run", "travix", "node"],
"problemMatcher": "$haxe"
}

View File

@@ -1,5 +1,7 @@
# Tinkerbell Macro Library # Tinkerbell Macro Library
[![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg?maxAge=2592000)](https://gitter.im/haxetink/public)
[![Build Status](https://travis-ci.org/haxetink/tink_macro.svg?branch=master)](https://travis-ci.org/haxetink/tink_macro)
[![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/haxetink/public)
Explained in current marketing speak, `tink_macro` is *the* macro toolkit ;) Explained in current marketing speak, `tink_macro` is *the* macro toolkit ;)

1
bin/.gitignore vendored
View File

@@ -1 +0,0 @@
*.n

View File

@@ -11,8 +11,8 @@
"contributors": [ "contributors": [
"back2dos" "back2dos"
], ],
"releasenote": "Several minor improvements.", "releasenote": "Add way to get constructor args.",
"version": "0.12.1", "version": "0.14.2",
"url": "http://haxetink.org/tink_macro", "url": "http://haxetink.org/tink_macro",
"dependencies": { "dependencies": {
"tink_core": "" "tink_core": ""

View File

@@ -14,6 +14,7 @@ typedef Bouncer = tink.macro.Bouncer;
typedef Types = tink.macro.Types; typedef Types = tink.macro.Types;
typedef Binops = tink.macro.Ops.Binary; typedef Binops = tink.macro.Ops.Binary;
typedef Unops = tink.macro.Ops.Unary; typedef Unops = tink.macro.Ops.Unary;
typedef TypeMap<T> = tink.macro.TypeMap<T>;
//TODO: consider adding stuff from haxe.macro.Expr here //TODO: consider adding stuff from haxe.macro.Expr here
typedef MacroOutcome<D, F> = tink.core.Outcome<D, F>; typedef MacroOutcome<D, F> = tink.core.Outcome<D, F>;
@@ -35,4 +36,4 @@ class MacroApi {
static public function pos() static public function pos()
return haxe.macro.Context.currentPos(); return haxe.macro.Context.currentPos();
} }

View File

@@ -5,6 +5,7 @@ import haxe.macro.Expr;
import haxe.macro.Type; import haxe.macro.Type;
import tink.macro.TypeMap; import tink.macro.TypeMap;
using tink.MacroApi;
using haxe.macro.Tools; using haxe.macro.Tools;
typedef BuildContextN = { typedef BuildContextN = {
@@ -104,7 +105,7 @@ class BuildCache {
usings: ctx.usings usings: ctx.usings
})); }));
} }
static public function getType(name, ?type, ?pos:Position, build:BuildContext->TypeDefinition) { static public function getType(name, ?type, ?pos:Position, build:BuildContext->TypeDefinition) {
if (pos == null) if (pos == null)
@@ -156,32 +157,28 @@ private class Group {
usings: usings, usings: usings,
name: path.split('.').pop() name: path.split('.').pop()
}); });
Context.defineModule(path, [def], usings);
entries.set(type, { name: path } ); entries.set(type, { name: path } );
Context.defineModule(path, [def], usings);
return Context.getType(path); return Context.getType(path);
} }
function doMake()
while (true)
switch '$name${counter++}' {
case _.definedType() => Some(_):
case v:
return make(v);
}
return return
switch entries.get(type) { switch entries.get(type) {
case null: case null:
var ret = null; doMake();
while (ret == null) {
try {
Context.getType('$name$counter');
}
catch (e:Dynamic) {
ret = make('$name$counter');
}
counter++;
}
ret;
case v: case v:
try { switch v.name.definedType() {
Context.getType(v.name); case Some(v): v;
} default: doMake();
catch (e:Dynamic) {
make(v.name);
} }
} }
} }

View File

@@ -9,51 +9,51 @@ using tink.MacroApi;
using Lambda; using Lambda;
class ClassBuilder { class ClassBuilder {
var memberList:Array<Member>; var memberList:Array<Member>;
var macros:Map<String,Field>; var macros:Map<String,Field>;
var constructor:Null<Constructor>; var constructor:Null<Constructor>;
public var target(default, null):ClassType; public var target(default, null):ClassType;
var superFields:Map<String,Bool>; var superFields:Map<String,Bool>;
var initializeFrom:Array<Field>; var initializeFrom:Array<Field>;
public function new(?target, ?fields) { public function new(?target, ?fields) {
if (target == null) if (target == null)
target = Context.getLocalClass().get(); target = Context.getLocalClass().get();
if (fields == null) if (fields == null)
fields = Context.getBuildFields(); fields = Context.getBuildFields();
this.initializeFrom = fields; this.initializeFrom = fields;
this.target = target; this.target = target;
} }
function init() { function init() {
if (initializeFrom == null) return; if (initializeFrom == null) return;
var fields = initializeFrom; var fields = initializeFrom;
initializeFrom = null; initializeFrom = null;
this.memberList = []; this.memberList = [];
this.macros = new Map(); this.macros = new Map();
for (field in fields) for (field in fields)
if (field.access.has(AMacro)) if (field.access.has(AMacro))
macros.set(field.name, field) macros.set(field.name, field)
else if (field.name == 'new') { else if (field.name == 'new') {
var m:Member = field; var m:Member = field;
this.constructor = new Constructor(this, m.getFunction().sure(), m.isPublic, m.pos, field.meta); this.constructor = new Constructor(this, m.getFunction().sure(), m.isPublic, m.pos, field.meta);
} }
else else
doAddMember(field); doAddMember(field);
} }
public function getConstructor(?fallback:Function):Constructor { public function getConstructor(?fallback:Function):Constructor {
init(); init();
if (constructor == null) if (constructor == null)
if (fallback != null) if (fallback != null)
constructor = new Constructor(this, fallback); constructor = new Constructor(this, fallback);
else { else {
@@ -64,18 +64,18 @@ class ClassBuilder {
try { try {
var ctor = cl.constructor.get(); var ctor = cl.constructor.get();
var func = Context.getTypedExpr(ctor.expr()).getFunction().sure(); var func = Context.getTypedExpr(ctor.expr()).getFunction().sure();
for (arg in func.args) //this is to deal with type parameter substitutions for (arg in func.args) //this is to deal with type parameter substitutions
arg.type = null; arg.type = null;
func.expr = "super".resolve().call(func.getArgIdents()); func.expr = "super".resolve().call(func.getArgIdents());
constructor = new Constructor(this, func); constructor = new Constructor(this, func);
if (ctor.isPublic) if (ctor.isPublic)
constructor.publish(); constructor.publish();
} }
catch (e:Dynamic) {//fails for unknown reason catch (e:Dynamic) {//fails for unknown reason
if (e == 'assert') if (e == 'assert')
neko.Lib.rethrow(e); tink.core.Error.rethrow(e);
constructor = new Constructor(this, null); constructor = new Constructor(this, null);
} }
break; break;
@@ -85,15 +85,15 @@ class ClassBuilder {
if (constructor == null) if (constructor == null)
constructor = new Constructor(this, null); constructor = new Constructor(this, null);
} }
return constructor; return constructor;
} }
public function hasConstructor():Bool { public function hasConstructor():Bool {
init(); init();
return this.constructor != null; return this.constructor != null;
} }
public function export(?verbose):Array<Field> { public function export(?verbose):Array<Field> {
if (initializeFrom != null) return null; if (initializeFrom != null) return null;
var ret = (constructor == null || target.isInterface) ? [] : [constructor.toHaxe()]; var ret = (constructor == null || target.isInterface) ? [] : [constructor.toHaxe()];
@@ -108,24 +108,24 @@ class ClassBuilder {
} }
for (m in macros) for (m in macros)
ret.push(m); ret.push(m);
if (verbose) if (verbose)
for (field in ret) for (field in ret)
Context.warning(new Printer().printField(field), field.pos); Context.warning(new Printer().printField(field), field.pos);
return ret; return ret;
} }
public function iterator():Iterator<Member> { public function iterator():Iterator<Member> {
init(); init();
return this.memberList.copy().iterator(); return this.memberList.copy().iterator();
} }
public function hasOwnMember(name:String):Bool { public function hasOwnMember(name:String):Bool {
init(); init();
return return
macros.exists(name) || memberByName(name).isSuccess(); macros.exists(name) || memberByName(name).isSuccess();
} }
public function hasSuperField(name:String):Bool { public function hasSuperField(name:String):Bool {
if (superFields == null) { if (superFields == null) {
superFields = new Map(); superFields = new Map();
@@ -139,51 +139,51 @@ class ClassBuilder {
} }
return superFields.get(name); return superFields.get(name);
} }
public function memberByName(name:String, ?pos:Position) { public function memberByName(name:String, ?pos:Position) {
init(); init();
for (m in memberList) for (m in memberList)
if (m.name == name) if (m.name == name)
return Success(m); return Success(m);
return pos.makeFailure('unknown member $name'); return pos.makeFailure('unknown member $name');
} }
public function removeMember(member:Member):Bool { public function removeMember(member:Member):Bool {
init(); init();
return return
memberList.remove(member); memberList.remove(member);
} }
public function hasMember(name:String):Bool public function hasMember(name:String):Bool
return hasOwnMember(name) || hasSuperField(name); return hasOwnMember(name) || hasSuperField(name);
function doAddMember(m:Member, ?front:Bool = false):Member { function doAddMember(m:Member, ?front:Bool = false):Member {
init(); init();
if (m.name == 'new') if (m.name == 'new')
throw 'Constructor must not be registered as ordinary member'; throw 'Constructor must not be registered as ordinary member';
//if (hasOwnMember(m.name)) //if (hasOwnMember(m.name))
//m.pos.error('duplicate member declaration ' + m.name); //m.pos.error('duplicate member declaration ' + m.name);
if (front) if (front)
memberList.unshift(m); memberList.unshift(m);
else else
memberList.push(m); memberList.push(m);
return m;
}
public function addMember(m:Member, ?front:Bool = false):Member {
doAddMember(m, front);
if (!m.isStatic && hasSuperField(m.name))
m.overrides = true;
return m; return m;
} }
public function addMember(m:Member, ?front:Bool = false):Member {
doAddMember(m, front);
if (!m.isStatic && hasSuperField(m.name))
m.overrides = true;
return m;
}
static public function run(plugins:Array<ClassBuilder->Void>, ?verbose) { static public function run(plugins:Array<ClassBuilder->Void>, ?verbose) {
var builder = new ClassBuilder(); var builder = new ClassBuilder();
for (p in plugins) for (p in plugins)

View File

@@ -62,6 +62,10 @@ class Constructor {
default: [].toBlock(); default: [].toBlock();
} }
} }
public function getArgList():Array<FunctionArg>
return beforeArgs.concat(args).concat(afterArgs);
public function addStatement(e:Expr, ?prepend) public function addStatement(e:Expr, ?prepend)
if (prepend) if (prepend)
this.nuStatements.unshift(e) this.nuStatements.unshift(e)

View File

@@ -241,7 +241,7 @@ class Exprs {
} }
static public inline function iterate(target:Expr, body:Expr, ?loopVar:String = 'i', ?pos:Position) static public inline function iterate(target:Expr, body:Expr, ?loopVar:String = 'i', ?pos:Position)
return EFor(EIn(loopVar.resolve(pos), target).at(pos), body).at(pos); return macro @:pos(pos.sanitize()) for ($i{loopVar} in $target) $body;
static public function toFields(object:Dynamic<Expr>, ?pos:Position) static public function toFields(object:Dynamic<Expr>, ?pos:Position)
return EObjectDecl([for (field in Reflect.fields(object)) return EObjectDecl([for (field in Reflect.fields(object))

105
src/tink/macro/Sisyphus.hx Normal file
View File

@@ -0,0 +1,105 @@
package tink.macro;
import haxe.macro.Expr;
import haxe.macro.Type;
class Sisyphus {
static function nullable(complexType : ComplexType) : ComplexType return macro : Null<$complexType>;
static function toField(cf : ClassField) : Field return {
function varAccessToString(va : VarAccess, getOrSet : String) : String return {
switch (va) {
case AccNormal: "default";
case AccNo: "null";
case AccNever: "never";
case AccResolve: throw "Invalid TAnonymous";
case AccCall: getOrSet;
case AccInline: "default";
case AccRequire(_, _): "default";
}
}
if (cf.params.length == 0) {
name: cf.name,
doc: cf.doc,
access: cf.isPublic ? [ APublic ] : [ APrivate ],
kind: switch([ cf.kind, cf.type ]) {
case [ FVar(read, write), ret ]:
FProp(
varAccessToString(read, "get"),
varAccessToString(write, "set"),
toComplexType(ret),
null);
case [ FMethod(_), TFun(args, ret) ]:
FFun({
args: [
for (a in args) {
name: a.name,
opt: a.opt,
type: toComplexType(a.t),
}
],
ret: toComplexType(ret),
expr: null,
});
default:
throw "Invalid TAnonymous";
},
pos: cf.pos,
meta: cf.meta.get(),
} else {
throw "Invalid TAnonymous";
}
}
public static function toComplexType(type : Null<Type>) : Null<ComplexType> return
switch (type) {
case null:
null;
case TMono(_.get() => t):
t == null ? Types.toComplex(type, { direct: true }) : toComplexType(t);
case TEnum(_.get() => baseType, params):
TPath(toTypePath(baseType, params));
case TInst(_.get() => classType, params):
switch (classType.kind) {
case KTypeParameter(_):
TPath({
name: classType.name,
pack: [],
});
default:
TPath(toTypePath(classType, params));
}
case TType(_.get() => baseType, params):
TPath(toTypePath(baseType, params));
case TFun(args, ret):
TFunction(
[ for (a in args) a.opt ? nullable(toComplexType(a.t)) : toComplexType(a.t) ],
toComplexType(ret));
case TAnonymous(_.get() => { fields: fields }):
TAnonymous([ for (cf in fields) toField(cf) ]);
case TDynamic(t):
if (t == null) {
macro : Dynamic;
} else {
var ct = toComplexType(t);
macro : Dynamic<$ct>;
}
case TLazy(f):
toComplexType(f());
case TAbstract(_.get() => baseType, params):
TPath(toTypePath(baseType, params));
default:
throw "Invalid type";
}
static function toTypePath(baseType : BaseType, params : Array<Type>) : TypePath return {
var module = baseType.module;
{
pack: baseType.pack,
name: module.substring(module.lastIndexOf(".") + 1),
sub: baseType.name,
params: [ for (t in params) TPType(toComplexType(t)) ],
}
}
}

View File

@@ -13,12 +13,18 @@ using tink.macro.Functions;
using tink.CoreApi; using tink.CoreApi;
class Types { class Types {
static public function definedType(typeName:String)
return
try {
Some(Context.getType(typeName));
}
catch (e:Dynamic)
if (Std.string(e) == 'Type not found \'$typeName\'') None;
else tink.core.Error.rethrow(e);
static var types = new Map<Int,Void->Type>(); static var types = new Map<Int,Void->Type>();
static var idCounter = 0; static var idCounter = 0;
//@:noUsing macro static public function getType(id:Int):Type
//return types.get(id)();
static public function getID(t:Type, ?reduced = true) static public function getID(t:Type, ?reduced = true)
return return
if (reduced) if (reduced)
@@ -43,6 +49,16 @@ class Types {
throw 'not implemented'; throw 'not implemented';
} }
static public function getMeta(type:Type)
return switch type {
case TInst(_.get().meta => m, _): [m];
case TEnum(_.get().meta => m, _): [m];
case TAbstract(_.get().meta => m, _): [m];
case TType(_.get() => t, _): [t.meta].concat(getMeta(t.type));
case TLazy(f): getMeta(f());
default: [];
}
static function getDeclaredFields(t:ClassType, out:Array<ClassField>, marker:Map<String,Bool>) { static function getDeclaredFields(t:ClassType, out:Array<ClassField>, marker:Map<String,Bool>) {
for (field in t.fields.get()) for (field in t.fields.get())
if (!marker.exists(field.name)) { if (!marker.exists(field.name)) {
@@ -152,7 +168,7 @@ class Types {
static public function toComplex(type:Type, ?options:{ ?direct: Bool }):ComplexType { static public function toComplex(type:Type, ?options:{ ?direct: Bool }):ComplexType {
var ret = var ret =
if (options == null || options.direct != true) haxe.macro.TypeTools.toComplexType(type); if (options == null || options.direct != true) tink.macro.Sisyphus.toComplexType(type);
else null; else null;
if (ret == null) if (ret == null)
ret = lazyComplex(function () return type); ret = lazyComplex(function () return type);
@@ -167,14 +183,14 @@ class Types {
params : [TPExpr(register(f).toExpr())], params : [TPExpr(register(f).toExpr())],
sub : null, sub : null,
}); });
static function resolveDirectType() static function resolveDirectType()
return return
switch reduce(Context.getLocalType()) { switch reduce(Context.getLocalType()) {
case TInst(_, [TInst(_.get() => { kind: KExpr(e) }, _)]): case TInst(_, [TInst(_.get() => { kind: KExpr(e) }, _)]):
types[e.getInt().sure()]();//When using compiler server, this call throws on occasion, in which case modifying this file (to update mtime and invalidate the cache) will solve the problem types[e.getInt().sure()]();//When using compiler server, this call throws on occasion, in which case modifying this file (to update mtime and invalidate the cache) will solve the problem
default: default:
throw 'assert'; throw 'assert';
} }
} }