babel-plugin-chr
v1.0.0
Published
Babel plugin that precompiles CHR.js source code
Downloads
12
Maintainers
Readme
babel-plugin-chr
Babel plugin that precompiles CHR.js source code.
Install
npm install babel-plugin-chr
Usage
Run:
babel --plugins chr script.js
Or add the plugin to your .babelrc
configuration:
{
"plugins": [ "chr" ]
}
Example
The plugin will compile the following code:
var CHR = require('chr')
var chr = new CHR()
chr('some @ a ==> b')
into:
var _Runtime = require('chr/runtime');
var chr = {
Store: new _Runtime.Store(),
History: new _Runtime.History(),
Constraints: {},
Replacements: []
};
chr = (function (chr) {
chr.a = _Runtime.Helper.dynamicCaller('a');
chr.Constraints['a/0'] = [];
chr.b = _Runtime.Helper.dynamicCaller('b');
chr.Constraints['b/0'] = [];
chr.Constraints['a/0'][0] = function (constraint) {
var self = this;
var ids = [constraint.id];
if (ids.every(function (id) {
return self.Store.alive(id);
})) {
if (_Runtime.Helper.allDifferent(ids)) {
if (self.History.notIn('some', ids)) {
self.History.add('some', ids);
self.b();
}
}
}
};
return chr;
})(chr);
It supports the normal CHR.js
syntax as specified here: fnogatz/CHR.js