Permalink
Browse files

basic index, seems to work on the repl

  • Loading branch information...
0 parents commit bc66c379111b8dcf29bdd9b8bfc2c368adc60e21 @substack committed Jul 18, 2011
Showing with 75 additions and 0 deletions.
  1. +75 −0 index.js
@@ -0,0 +1,75 @@
+var burrito = require('burrito');
+var vm = require('vm');
+var EventEmitter = require('events').EventEmitter;
+
+module.exports = function (src) {
+ var b = new Bunker();
+ if (src) b.include(src);
+ return b;
+};
+
+function Bunker () {
+ this.sources = [];
+ this.nodes = [];
+
+ this.names = {
+ call : burrito.generateName(6),
+ stat : burrito.generateName(6)
+ };
+}
+
+Bunker.prototype = new EventEmitter;
+
+Bunker.prototype.include = function (src) {
+ this.sources.push(src);
+ this.source = null;
+ return this;
+};
+
+Bunker.prototype.compile = function () {
+ var src = this.sources.join('\n');
+ var nodes = this.nodes;
+ var names = this.names;
+
+ return burrito(src, function (node) {
+ if (node.name === 'call') {
+ var i = nodes.length;
+ nodes.push(node);
+ node.wrap(names.call + '(' + i + ')(%s)');
+ }
+ else if (node.name === 'stat' || node.name === 'throw') {
+ var i = nodes.length;
+ nodes.push(node);
+ node.wrap('{' + names.stat + '(' + i + ');%s}');
+ }
+ });
+};
+
+Bunker.prototype.run = function (context) {
+ if (!context) context = {};
+
+ var self = this;
+ var stack = [];
+ var src = self.compile();
+
+ context[self.names.call] = function (i) {
+ var node = self.nodes[i];
+ stack.unshift(node);
+ self.emit('call', node, stack);
+
+ return function (expr) {
+ stack.shift();
+ return expr;
+ };
+ };
+
+ context[self.names.stat] = function (i) {
+ var node = self.nodes[i];
+ self.emit('stat', node, stack);
+ };
+
+ console.log(src);
+ vm.runInNewContext(src, context);
+
+ return self;
+};

0 comments on commit bc66c37

Please sign in to comment.