Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
4c523e02a5 | ||
![]() |
f39fc3b296 | ||
![]() |
7a3914094d | ||
![]() |
a597d7fa50 | ||
![]() |
3d5626d66f | ||
![]() |
88a1ffba0b | ||
![]() |
8f81959da9 | ||
![]() |
b20986ecd0 | ||
![]() |
e55b72b697 | ||
![]() |
240ace34ec | ||
![]() |
d851094aa3 | ||
![]() |
ea16f79090 | ||
![]() |
aad311f524 | ||
![]() |
f0d5d80266 | ||
![]() |
38aa0f4dc4 | ||
![]() |
f4fb7d63c3 | ||
![]() |
241919549c |
@@ -64,6 +64,8 @@ Rejects an expression and displays a generic or custom error message
|
||||
Converts an expression into the corresponding Haxe source code
|
||||
- `log(e:Expr, ?pos:Position):Expr`
|
||||
Traces the string representation of an expression and returns it.
|
||||
- `concat(e1:Expr, e2:Expr):Expr`
|
||||
Concats two expressions into a block. If either sub-expression is a block itself, it gets flattened into the resulting block.
|
||||
|
||||
#### Extracting Constants
|
||||
|
||||
|
@@ -11,8 +11,8 @@
|
||||
"contributors": [
|
||||
"back2dos"
|
||||
],
|
||||
"releasenote": "Improved build cache.",
|
||||
"version": "0.12.0",
|
||||
"releasenote": "Better converion for TMono",
|
||||
"version": "0.12.3",
|
||||
"url": "http://haxetink.org/tink_macro",
|
||||
"dependencies": {
|
||||
"tink_core": ""
|
||||
|
@@ -167,12 +167,7 @@ private class Group {
|
||||
case null:
|
||||
make('$name${counter++}');
|
||||
case v:
|
||||
try {
|
||||
Context.getType(v.name);
|
||||
}
|
||||
catch (e:Dynamic) {
|
||||
make(v.name);
|
||||
}
|
||||
Context.getType(v.name);
|
||||
}
|
||||
}
|
||||
}
|
@@ -388,6 +388,17 @@ class Exprs {
|
||||
case EFunction(_, f): Success(f);
|
||||
default: e.pos.makeFailure(NOT_A_FUNCTION);
|
||||
}
|
||||
|
||||
static public function concat(e:Expr, with:Expr, ?pos) {
|
||||
if(pos == null) pos = e.pos;
|
||||
return
|
||||
switch [e.expr, with.expr] {
|
||||
case [EBlock(e1), EBlock(e2)]: EBlock(e1.concat(e2)).at(pos);
|
||||
case [EBlock(e1), e2]: EBlock(e1.concat([with])).at(pos);
|
||||
case [e1, EBlock(e2)]: EBlock([e].concat(e2)).at(pos);
|
||||
default: EBlock([e, with]).at(pos);
|
||||
}
|
||||
}
|
||||
|
||||
static inline var NOT_AN_INT = "integer constant expected";
|
||||
static inline var NOT_AN_IDENT = "identifier expected";
|
||||
|
105
src/tink/macro/Sisyphus.hx
Normal file
105
src/tink/macro/Sisyphus.hx
Normal 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)) ],
|
||||
}
|
||||
}
|
||||
}
|
@@ -17,14 +17,15 @@ class TypeMap<V> extends BalancedTree<Type, V> implements IMap<Type, V> {
|
||||
}
|
||||
|
||||
override function compare(k1:Type, k2:Type):Int {
|
||||
|
||||
if (follow) {
|
||||
k1 = k1.reduce();
|
||||
k2 = k2.reduce();
|
||||
}
|
||||
//trace(k1.toString());
|
||||
//trace(k2.toString());
|
||||
|
||||
return switch k1.getIndex() - k2.getIndex() {
|
||||
case 0: Reflect.compare(k1.toString(), k2.toString());//TODO: this may be rather expensive and not very reliable
|
||||
case 0:
|
||||
Reflect.compare(k1.toString(), k2.toString());//much to my surprise, this actually seems to work (at least with 3.4)
|
||||
case v: v;
|
||||
}
|
||||
}
|
||||
|
@@ -15,10 +15,6 @@ using tink.CoreApi;
|
||||
class Types {
|
||||
static var types = new Map<Int,Void->Type>();
|
||||
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)
|
||||
return
|
||||
if (reduced)
|
||||
@@ -152,7 +148,7 @@ class Types {
|
||||
|
||||
static public function toComplex(type:Type, ?options:{ ?direct: Bool }):ComplexType {
|
||||
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;
|
||||
if (ret == null)
|
||||
ret = lazyComplex(function () return type);
|
||||
@@ -168,11 +164,11 @@ class Types {
|
||||
sub : null,
|
||||
});
|
||||
|
||||
static function resolveDirectType()
|
||||
static function resolveDirectType()
|
||||
return
|
||||
switch reduce(Context.getLocalType()) {
|
||||
case TInst(_, [TInst(_.get() => { kind: KExpr(e) }, _)]):
|
||||
types[e.getInt().sure()]();
|
||||
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:
|
||||
throw 'assert';
|
||||
}
|
||||
|
@@ -80,4 +80,11 @@ class Exprs extends Base {
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
function testConcat() {
|
||||
exprEq(macro {a; b;}, (macro a).concat(macro b));
|
||||
exprEq(macro {a; b; c;}, (macro {a; b;}).concat(macro c));
|
||||
exprEq(macro {a; b; c;}, (macro a).concat(macro {b; c;}));
|
||||
exprEq(macro {a; b; c; d;}, (macro {a; b;}).concat(macro {c; d;}));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user