From 0aa90327d387af705d39e2bf4fde081966a6d666 Mon Sep 17 00:00:00 2001 From: Laurent Bossavit Date: Tue, 18 Jul 2017 09:26:56 +0200 Subject: [PATCH] =?UTF-8?q?:gear:=20Interpr=C3=A8te=20avec=20des=20variabl?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/tree.test.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/tree.test.js b/test/tree.test.js index 15e5db486..552ba6a10 100644 --- a/test/tree.test.js +++ b/test/tree.test.js @@ -42,22 +42,25 @@ describe('simplified tree walks', function() { Expr.prototype.map = function(f) { return this.cata({ Num: (x) => this, // fixed - Add: (x, y) => Add(f(x), f(y)) + Add: (x, y) => Add(f(x), f(y)), + Var: (name) => this }) } // Celle-ci l'évaluation - const evaluator = (a) => { + const evaluator = state => a => { return a.cata({ Num: (x) => x, - Add: (x, y) => x + y + Add: (x, y) => x + y, + Var: (name) => state[name] }) } - let evaluate = expr => fold(evaluator, expr) + let evaluate = (expr, state={}) => fold(evaluator(state), expr) let num = x => Fx(Num(x)) let add = (x, y) => Fx(Add(x,y)) + let ref = (name) => Fx(Var(name)) it('should provide a protocol for evaluation', function() { let tree = num(45), @@ -77,6 +80,12 @@ describe('simplified tree walks', function() { expect(result).to.equal(70) }); + it('should evaluate expressions involving variables', function() { + let tree = add(num(45),ref("a")), + result = evaluate(tree,{a:25}) + expect(result).to.equal(70) + }); + /* it('should provide a protocol for missing variables', function() { let tree = Tree.Variable("a"),