Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Sep 14, 2012
0 parents commit 3fe8805
Show file tree
Hide file tree
Showing 10 changed files with 788 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
npm-debug.log
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Spoon
10 changes: 10 additions & 0 deletions lib/spoon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var spoon = exports;

spoon.block = require('./spoon/block');
spoon.instruction = require('./spoon/instruction');
spoon.cfg = require('./spoon/cfg');
spoon.renderer = require('./spoon/renderer');

// Export API
spoon.construct = require('./spoon/api').construct;
spoon.render = require('./spoon/api').render;
16 changes: 16 additions & 0 deletions lib/spoon/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var api = exports,
spoon = require('../spoon');

api.construct = function construct(ast) {
var cfg = spoon.cfg.create();

cfg.translate(ast);

return cfg;
};

api.render = function render(cfg) {
var r = spoon.renderer.create(cfg);

return r.render();
};
74 changes: 74 additions & 0 deletions lib/spoon/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var block = exports,
spoon = require('../spoon');

function Block(cfg) {
this.cfg = cfg;
this.id = cfg.blockId++;
this.successors = [];
this.predecessors = [];
this.instructions = [];
this.root = null;
this.loop = false;

this.ended = false;
};
block.Block = Block;
block.create = function create(cfg) {
return new Block(cfg);
};

Block.prototype.toString = function toString() {
var buff = '[block ' + this.id + '' + (this.loop ? ' loop' : '') + ']\n';

buff += '# predecessors: ' + this.predecessors.map(function(b) {
return b.id
}).join(', ') + '\n';

this.instructions.forEach(function(instr) {
buff += instr.toString() + '\n';
});

buff += '# successors: ' + this.successors.map(function(b) {
return b.id
}).join(', ') + '\n';

return buff;
};

Block.prototype.add = function add(type, args) {
var instr = spoon.instruction.create(this, type, args || []);
if (this.ended) return instr;

this.instructions.push(instr);
return instr;
};

Block.prototype.end = function end() {
this.ended = true;
};

Block.prototype.addSuccessor = function addSuccessor(block) {
if (this.successors.length == 2) {
throw new Error('Block can\'t have more than 2 successors');
}
this.successors.push(block);
block.addPredecessor(this);
};

Block.prototype.addPredecessor = function addPredecessor(block) {
if (this.predecessors.length == 2) {
throw new Error('Block can\'t have more than 2 predecessors');
}
this.predecessors.push(block);
};

Block.prototype.goto = function goto(block) {
if (this.ended) return block;

this.add('goto');
this.addSuccessor(block);
this.end();

// For chaining
return block;
};
Loading

0 comments on commit 3fe8805

Please sign in to comment.