From c3cecad4725101c1b548217430bd7e3f0c310b1a Mon Sep 17 00:00:00 2001 From: Juraj Kirchheim Date: Tue, 19 Apr 2016 12:52:45 +0200 Subject: [PATCH] Add TypeMap with test. --- README.md | 6 +++++- src/tink/macro/TypeMap.hx | 32 ++++++++++++++++++++++++++++++++ tests/Run.hx | 1 + tests/TypeMapTest.hx | 30 ++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/tink/macro/TypeMap.hx create mode 100644 tests/TypeMapTest.hx diff --git a/README.md b/README.md index da3e793..7c8ebf3 100644 --- a/README.md +++ b/README.md @@ -408,4 +408,8 @@ Here, `Value` will just use a plain expression, whereas `Arg` and `OptArg` will ### Expression Level Transformation -Because the state of a constructor is rather delicate, the API prohibits you to just mess around with the whole constructor body at an expression level. For that to happen, you can register `onGenerate` hooks. These will be called when the corresponding `ClassBuilder` does its export. The hooks are cleared after the export. \ No newline at end of file +Because the state of a constructor is rather delicate, the API prohibits you to just mess around with the whole constructor body at an expression level. For that to happen, you can register `onGenerate` hooks. These will be called when the corresponding `ClassBuilder` does its export. The hooks are cleared after the export. + +# TypeMap + +You can find a type map, i.e. a map where the keys are `haxe.macro.Type`, in `tink.macro.TypeMap`. It's pretty much an ordinary map. Currently, it relies rather strongly on [`haxe.macro.TypeTools.toString()`](http://api.haxe.org/haxe/macro/TypeTools.html#toString) and it remains to be determined whether that is a reliable choice. Please report any issues you might face. \ No newline at end of file diff --git a/src/tink/macro/TypeMap.hx b/src/tink/macro/TypeMap.hx new file mode 100644 index 0000000..f9b4e79 --- /dev/null +++ b/src/tink/macro/TypeMap.hx @@ -0,0 +1,32 @@ +package tink.macro; + +import haxe.Constraints.IMap; +import haxe.ds.BalancedTree; +import haxe.macro.Context; +import haxe.macro.Type; + +using haxe.macro.Tools; +using tink.MacroApi; + +class TypeMap extends BalancedTree implements IMap { + var follow:Bool; + + public function new(?noFollow:Bool) { + this.follow = noFollow != true; + super(); + } + + 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 v: v; + } + } + +} \ No newline at end of file diff --git a/tests/Run.hx b/tests/Run.hx index 6439761..519789e 100644 --- a/tests/Run.hx +++ b/tests/Run.hx @@ -11,6 +11,7 @@ class Run { new Exprs(), new Types(), new Positions(), + new TypeMapTest(), ]; #end macro static function test() { diff --git a/tests/TypeMapTest.hx b/tests/TypeMapTest.hx new file mode 100644 index 0000000..14166d6 --- /dev/null +++ b/tests/TypeMapTest.hx @@ -0,0 +1,30 @@ +package; + +import haxe.unit.TestCase; + +import tink.macro.TypeMap; +using haxe.macro.Context; + +class TypeMapTest extends TestCase { + + function testMap() { + var t = new TypeMap(); + var t1 = (macro [{ foo: [{ bar: '5' }]}]).typeof(); + var t2 = (macro [{ foo: [{ bar: 5 }]}]).typeof(); + + t.set(t1, 0); + assertEquals(Lambda.count(t), 1); + t.set(t2, 1); + assertEquals(Lambda.count(t), 2); + t.set(t1, 2); + assertEquals(Lambda.count(t), 2); + t.set(t2, 3); + assertEquals(Lambda.count(t), 2); + + assertEquals(t.get(t1), 2); + assertEquals(t.get(t2), 3); + + assertTrue(true); + } + +} \ No newline at end of file