Skip to content

Commit

Permalink
CommonJS
Browse files Browse the repository at this point in the history
  • Loading branch information
octachrome committed Aug 1, 2014
1 parent 31cbe25 commit 663e7b3
Show file tree
Hide file tree
Showing 18 changed files with 1,306 additions and 1,126 deletions.
140 changes: 77 additions & 63 deletions js/core.js
Original file line number Diff line number Diff line change
@@ -1,84 +1,98 @@
function eq(a, b) {
return a === b;
if (typeof exports === 'undefined') {
var exports = window.core = {};
}

function ne(a, b) {
return a !== b;
}

function gt(a, b) {
return a > b;
}
(function (exports) {
function eq(a, b) {
return a === b;
}

function lt(a, b) {
return a < b;
}
function ne(a, b) {
return a !== b;
}

function ge(a, b) {
return a >= b;
}
function gt(a, b) {
return a > b;
}

function le(a, b) {
return a <= b;
}
function lt(a, b) {
return a < b;
}

function add(a, b) {
return a + b;
}
function ge(a, b) {
return a >= b;
}

function sub(a, b) {
return a + b;
}
function le(a, b) {
return a <= b;
}

function mul(a, b) {
return a * b;
}
function add(a, b) {
return a + b;
}

function div(a, b) {
return a / b;
}
function sub(a, b) {
return a + b;
}

function inv(f) {
return function (x) {
return !f(x);
function mul(a, b) {
return a * b;
}
}

function isWhitespace(c) {
return c[0] === ' ' || c[0] === '\t' || c[0] === '\n';
}
function div(a, b) {
return a / b;
}

function isNum(c) {
return c[0] >= '0' && c[0] <= '9';
}
function inv(f) {
return function (x) {
return !f(x);
}
}

function isNumOrDot(c) {
return c[0] >= '0' && c[0] <= '9' || c[0] === '.';
}
function isWhitespace(c) {
return c[0] === ' ' || c[0] === '\t' || c[0] === '\n';
}

function isAlpha(c) {
return c[0] >= 'a' && c[0] <= 'z' || c[0] >= 'A' && c[0] <= 'Z';
}
function isNum(c) {
return c[0] >= '0' && c[0] <= '9';
}

function isIdChar(c) {
return isAlpha(c) || isNum(c) || c === '_';
}
function isNumOrDot(c) {
return c[0] >= '0' && c[0] <= '9' || c[0] === '.';
}

function partial(fn, others) {
var bound = Array.prototype.slice.call(arguments, 1);
return function () {
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, bound.concat(args));
function isAlpha(c) {
return c[0] >= 'a' && c[0] <= 'z' || c[0] >= 'A' && c[0] <= 'Z';
}
}

function nth(n, list) {
if (list === empty()) {
throw new Error('Index out of bounds in nth');
function isIdChar(c) {
return isAlpha(c) || isNum(c) || c === '_';
}
if (n == 0) {
return head(list);
} else {
return nth(n - 1, tail(list));

function partial(fn, others) {
var bound = Array.prototype.slice.call(arguments, 1);
return function () {
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, bound.concat(args));
}
}
}

exports.eq = eq;
exports.ne = ne;
exports.gt = gt;
exports.lt = lt;
exports.ge = ge;
exports.le = le;
exports.add = add;
exports.sub = sub;
exports.mul = mul;
exports.div = div;
exports.inv = inv;
exports.isWhitespace = isWhitespace;
exports.isNum = isNum;
exports.isNumOrDot = isNumOrDot;
exports.isAlpha = isAlpha;
exports.isIdChar = isIdChar;
exports.partial = partial;

})(exports);
87 changes: 48 additions & 39 deletions js/lex.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,51 @@
var Tokens = {
MIN: 'minimise',
MAX: 'maximise',
SUBJECT_TO: 'subject to',
BOUNDS: 'bounds',
FREE: 'free',
END: 'end',
PLUS: '+',
MINUS: '-',
COLON: ':',
LE: '<=',
GE: '>=',
EQ: '='
};
if (typeof exports === 'undefined') {
var exports = window.lex = {};
}

function clex(charList) {
if (charList === empty()) {
return empty();
}
for (var k in Tokens) {
var token = Tokens[k];
var s = toString(take(token.length, charList));
if (s === token) {
return cons(token, clex(drop(token.length, charList)));
(function (exports) {
var c = require('./core');
var l = require('./list');

var Tokens = exports.Tokens = {
MIN: 'minimise',
MAX: 'maximise',
SUBJECT_TO: 'subject to',
BOUNDS: 'bounds',
FREE: 'free',
END: 'end',
PLUS: '+',
MINUS: '-',
COLON: ':',
LE: '<=',
GE: '>=',
EQ: '='
};

exports.clex = function clex(charList) {
if (charList === l.empty()) {
return l.empty();
}
for (var k in Tokens) {
var token = Tokens[k];
var s = l.toString(l.take(token.length, charList));
if (s === token) {
return l.cons(token, clex(l.drop(token.length, charList)));
}
}
var h = l.head(charList);
if (c.isWhitespace(h)) {
return clex(l.tail(charList));
}
if (c.isNum(h)) {
var n = l.takeWhile(c.isNumOrDot, charList);
var rest = l.dropWhile(c.isNumOrDot, charList);
return l.cons(l.toString(n), clex(rest));
}
if (c.isAlpha(h)) {
var n = l.takeWhile(c.isIdChar, charList);
var rest = l.dropWhile(c.isIdChar, charList);
return l.cons(l.toString(n), clex(rest));
}
return l.cons(h, clex(l.tail(charList)));
}
var h = head(charList);
if (isWhitespace(h)) {
return clex(tail(charList));
}
if (isNum(h)) {
var n = takeWhile(isNumOrDot, charList);
var rest = dropWhile(isNumOrDot, charList);
return cons(toString(n), clex(rest));
}
if (isAlpha(h)) {
var n = takeWhile(isIdChar, charList);
var rest = dropWhile(isIdChar, charList);
return cons(toString(n), clex(rest));
}
return cons(h, clex(tail(charList)));
}
})(exports);
Loading

0 comments on commit 663e7b3

Please sign in to comment.