forked from montagejs/frb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile-binder.js
35 lines (28 loc) · 1.08 KB
/
compile-binder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var compileObserver = require("./compile-observer");
var Observers = require("./observers");
var Binders = require("./binders");
module.exports = compile;
function compile(syntax) {
return compile.semantics.compile(syntax);
}
compile.semantics = {
compilers: {
property: Binders.makePropertyBinder,
has: Binders.makeHasBinder,
content: Binders.makeContentBinder,
reversed: Binders.makeReversedBinder
},
compile: function (syntax) {
var compilers = this.compilers;
if (syntax.type === "equals") {
var bindLeft = this.compile(syntax.args[0]);
var observeRight = compileObserver(syntax.args[1]);
return Binders.makeEqualityBinder(bindLeft, observeRight);
} else if (compilers.hasOwnProperty(syntax.type)) {
var argObservers = syntax.args.map(compileObserver, compileObserver.semantics);
return compilers[syntax.type].apply(null, argObservers);
} else {
throw new Error("Can't compile binder for " + JSON.stringify(syntax.type));
}
}
};