Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var falafel = require('falafel')
- , assert = require('assert')
- // Preparations
- Number.prototype['+'] = function(other) { return this + other }
- Number.prototype['-'] = function(other) { return this - other }
- Number.prototype['*'] = function(other) { return this * other }
- Number.prototype['/'] = function(other) { return this / other }
- String.prototype['+'] = function(other) { return this + other }
- function wrapFunc(fn) {
- assert.equal(typeof fn, 'function', "Passed non-function to wrapFunc!")
- var processed = process("(" + fn + ")")
- , newParts = explodeFunction(processed.toString())
- return Function(newParts.args, newParts.body)
- function process(body) {
- return falafel(body, function(node) {
- if (node.type == 'BinaryExpression') {
- node.update( wrap("%s", node.left.source())
- + wrap("['%s']", node.operator)
- + wrap("(%s)", node.right.source()))
- }
- })
- function wrap(format, other) {
- return format.replace(/%s/g, other)
- }
- }
- function explodeFunction(str) {
- if (str[0] == '(' && str.slice(-1) == ')') {
- // Unwrap parenthesized function: ((((x)))) --> x
- return explodeFunction(str.slice(1, -1))
- }
- var match = str.match(/^function\s*(\w*)\s*\(([^)]*)\)\s*{([\s\S]*)}$/)
- assert.ok(match, "Couldn't explode function!")
- var name = match[1]
- , args = match[2]
- , body = match[3]
- return { name:name, args:args, body:body }
- }
- }
Add Comment
Please, Sign In to add comment