Created
January 7, 2015 12:36
-
-
Save tangentstorm/dc4936e49009d145db67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)}))}; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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