Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var ometajs_ = require("ometajs");var AbstractGrammar = ometajs_.grammars.AbstractGrammar;var BSJSParser = ometajs_.grammars.BSJSParser;var BSJSIdentity = ometajs_.grammars.BSJSIdentity;var BSJSTranslator = ometajs_.grammars.BSJSTranslator;// mini-j interpreter
// just enough j to handle simple boolean expressions
int = function (s) { return parseInt(s,10); }
ints = function (s) { return s.split(" ").map(int); }
var minij = function minij(source, opts) {AbstractGrammar.call(this, source, opts)};minij.grammarName = "minij";minij.match = AbstractGrammar.match;minij.matchAll = AbstractGrammar.matchAll;exports.minij = minij;require("util").inherits(minij, AbstractGrammar);minij.prototype["j"] = function $j() {return (this._rule("bits",false,[],null,this["bits"]))};
minij.prototype["bits"] = function $bits() {return (this._atomic(function() {var bs;return this._list(function() {return this._rule("bit",false,[],null,this["bit"]) && this._many(function() { return this._atomic(function() { return (this._many(function() { return this._atomic(function() { return this._match(" ") });}) && this._rule("bit",false,[],null,this["bit"])) });})}) && ((bs = this._getIntermediate()), true) && this._exec(ints(bs))}) || this._atomic(function() {return this._rule("bit",false,[],null,this["bit"])}))};
minij.prototype["bit"] = function $bit() {return (this._atomic(function() {return this._match("0") && this._exec(0)}) || this._atomic(function() {return this._match("1") && this._exec(1)}))};
// mini-j interpreter
// just enough j to handle simple boolean expressions
int = function (s) { return parseInt(s,10); }
ints = function (s) { return s.split(' ').map(int); }
ometa minij {
j = bits,
bits = [bit (' '+ bit)+]:bs -> ints(bs)
| bit,
bit = '0' -> 0
| '1' -> 1
}
# run this in coffeescript
assert =require 'assert'
mj = require('./minij.js').minij
cj = (j,v) -> assert.deepEqual(mj.match(j,'j'), v)
cj '0', 0
cj '1', 1
cj '1 0 1', [1,0,1]
ometajs2js < minij.ojs > minij.js && coffee test.cf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment