-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3fe8805
Showing
10 changed files
with
788 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
npm-debug.log |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Spoon |
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
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; |
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
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(); | ||
}; |
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
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; | ||
}; |
Oops, something went wrong.