From 1d70be0b5dfbcb3bb44c15730671874faccd17e8 Mon Sep 17 00:00:00 2001 From: Laurent Bossavit Date: Thu, 3 Aug 2017 14:03:04 +0200 Subject: [PATCH] =?UTF-8?q?:gear:=20Evaluer=20ET=20annoter,=20mais=20r?= =?UTF-8?q?=C3=A9cursion=20explicite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/tree.test.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/tree.test.js b/test/tree.test.js index 10a6b4917..76070e2d0 100644 --- a/test/tree.test.js +++ b/test/tree.test.js @@ -107,6 +107,45 @@ describe('simplified tree walks', function() { let add = (x, y) => Fx(Add(x,y)) let ref = (name) => Fx(Var(name)) + const ExprAnn = daggy.taggedSum('ExprAnn',{ + NumAnn: ['v', 'x'], + AddAnn: ['v', 'x', 'y'], + }) + const {NumAnn, AddAnn} = ExprAnn + + const annotate = a => { + return a.cata({ + NumAnn: (v,x) => NumAnn(x,x), + AddAnn: (v,x,y) => { + let ax = annotate(x), + ay = annotate(y), + vv = ax.val()+ay.val() + return AddAnn(vv,ax,ay) + } + }) + } + + ExprAnn.prototype.val = function() { + return this.cata({ + NumAnn: (v,x) => v, + AddAnn: (v,x,y) => v + }) + } + + it('should annotate nodes', function() { + let tree = NumAnn(null,45), + result = annotate(tree) + expect(result.val()).to.equal(45) + }); + + it('should annotate trees', function() { + let tree = AddAnn(null,NumAnn(null,25),NumAnn(null,45)), + result = annotate(tree) + expect(result.x.val()).to.equal(25) + expect(result.y.val()).to.equal(45) + expect(result.val()).to.equal(70) + }); + it('should provide a protocol for evaluation', function() { let tree = num(45), result = evaluate(tree)