⚙️ Interprète avec des variables
parent
2da3d37ae0
commit
0aa90327d3
|
@ -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"),
|
||||
|
|
Loading…
Reference in New Issue