Created
June 15, 2013 13:36
-
-
Save danielknell/5788155 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
Ook. Ook. Ook. Ook! Ook! Ook? Ook! Ook. Ook. Ook! Ook? Ook! |
This file contains hidden or 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
Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook? Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook? Ook! Ook! Ook? Ook! Ook? Ook. Ook! Ook. Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook? Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook? Ook! Ook! Ook? Ook! Ook? Ook. Ook. Ook. Ook! Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook. Ook! Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook. Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook? Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook? Ook! Ook! Ook? Ook! Ook? Ook. Ook! Ook. Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook? Ook? Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook? Ook! Ook! Ook? Ook! Ook? Ook. Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook? Ook. Ook! Ook. Ook. Ook. Ook. Ook. Ook. Ook. Ook! Ook. Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook. Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook! Ook. Ook. Ook? Ook. Ook? Ook. Ook. Ook! Ook. |
This file contains hidden or 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
function lexer(input) { | |
return input.toLowerCase().split(/\s+/).map(function(v) { | |
switch (v) { | |
case "ook.": return 'period'; | |
case "ook?": return 'question'; | |
case "ook!": return 'explanation'; | |
} | |
throw new Error('invalid token: ' + v); | |
}); | |
} | |
function RootNode(nodes) { | |
this.nodes = nodes; | |
} | |
RootNode.prototype.compile = function(output) { | |
output.push("function (input) {"); | |
output.push("var result = [], tape = [0], pointer = 0;"); | |
this.nodes.forEach(function(node) { | |
node.compile(output); | |
}); | |
output.push("return result.join('');"); | |
output.push("}"); | |
} | |
function MoveNode(direction) { | |
this.direction = direction; | |
} | |
MoveNode.prototype.compile = function(output) { | |
output.push("pointer" + this.direction + this.direction + ";"); | |
if ("+" === this.direction) { | |
output.push("tape[pointer] = tape[pointer] || 0;") | |
} | |
} | |
function UpdateNode(direction) { | |
this.direction = direction; | |
} | |
UpdateNode.prototype.compile = function(output) { | |
output.push("tape[pointer]" + this.direction + this.direction + ";") | |
} | |
function OutputNode() { | |
} | |
OutputNode.prototype.compile = function(output) { | |
output.push("if (0 !== tape[pointer]) {") | |
output.push("result.push(String.fromCharCode(tape[pointer]));") | |
output.push("}"); | |
} | |
function InputNode() { | |
} | |
InputNode.prototype.compile = function(output) { | |
output.push("if (input.length > 0) {") | |
output.push("tape[pointer] = input.charCodeAt(0);") | |
output.push("input = input.substr(1, input.length - 1);") | |
output.push("}") | |
output.push("else {") | |
output.push("tape[pointer] = 0") | |
output.push("}") | |
} | |
function LoopNode(nodes) { | |
this.nodes = nodes; | |
} | |
LoopNode.prototype.compile = function(output) { | |
output.push("while(tape[pointer]) {"); | |
this.nodes.forEach(function(node) { | |
node.compile(output); | |
}); | |
output.push("}"); | |
} | |
function parser(tokens) { | |
function parse(tokens) { | |
var token1, token2, result = []; | |
while(tokens.length) { | |
token1 = tokens.shift(); | |
token2 = tokens.shift(); | |
if (token2 == undefined) { | |
throw new Error('unexpected token: end of file'); | |
} | |
if (token1 == 'period' && token2 == 'question') { | |
result.push(new MoveNode('+')); | |
} | |
else if (token1 == 'question' && token2 == 'period') { | |
result.push(new MoveNode('-')); | |
} | |
else if (token1 == 'period' && token2 == 'period') { | |
result.push(new UpdateNode('+')); | |
} | |
else if (token1 == 'explanation' && token2 == 'explanation') { | |
result.push(new UpdateNode('-')); | |
} | |
else if (token1 == 'explanation' && token2 == 'period') { | |
result.push(new OutputNode()); | |
} | |
else if (token1 == 'period' && token2 == 'explanation') { | |
result.push(new InputNode()); | |
} | |
else if (token1 == 'explanation' && token2 == 'question') { | |
result.push(new LoopNode(parse(tokens))); | |
} | |
else if (token1 == 'question' && token2 == 'explanation') { | |
return result; | |
} | |
else { | |
throw new Error('unexpected token: ' + token2); | |
} | |
} | |
return result; | |
} | |
return new RootNode(parse(tokens)); | |
} | |
function compiler(node) { | |
var output = []; | |
node.compile(output); | |
return output.join("\n"); | |
} | |
var fs = require('fs') | |
; | |
require.extensions['.ook'] = function(module, filename) { | |
var content = fs.readFileSync(filename, 'utf8'); | |
module._compile('module.exports = ' + compiler(parser(lexer(content))), filename); | |
} |
This file contains hidden or 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
require('./ook'); | |
var cat = require('./cat') | |
; | |
console.log(cat('testing')); |
This file contains hidden or 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
require('./ook'); | |
var hi = require('./helloworld') | |
; | |
console.log(hi()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment