Permalink
Browse files

It totally works.

  • Loading branch information...
1 parent a34ef67 commit dc86347df4150d6fa3a59c565f78d87701e5c683 @jfhbrook committed Sep 9, 2011
Showing with 32 additions and 23 deletions.
  1. +12 −6 lib/trollscript.js
  2. +20 −17 test/test.js
View
18 lib/trollscript.js
@@ -1,10 +1,14 @@
var bf = require("bf");
-module.exports = function(tscript, cb) {
+var ts = module.exports = function(code, cb) {
+ bf(ts.translate(code), cb);
+}
+
+ts.translate = function(tscript) {
var trigraphs = [];
- (function() { for(var i = 0; i+3 < tscript.length; trigraphs.push(i+=3)); })();
- bf(trigraphs.map(function(i) {
- var trigraph = Array.prototype.slice.call(tscript, i, i+3),
+ (function() { for(var i = 0; i < tscript.length; trigraphs.push(i+=3)); })();
+ var code = trigraphs.map(function(i) {
+ var trigraph = Array.prototype.slice.call(tscript, i-3, i).join(''),
lut = {
"ooo": ">",
"ool": "<",
@@ -17,9 +21,11 @@ module.exports = function(tscript, cb) {
};
return Object.keys(lut).filter(function(k) {
- return trigraph === k;
+ return trigraph == k;
}).map(function(k) {
return lut[k];
});
- }).join(""), cb);
+ }).join("");
+
+ return code;
}
View
37 test/test.js
@@ -1,22 +1,25 @@
-var trollscript = require(__dirname + "/../lib/trollscript");
+var trollscript = require(__dirname + "/../lib/trollscript"),
+ translate = function (bf) {
+ return bf
+ .replace(/>/g, "ooo")
+ .replace(/</g, "ool")
+ .replace(/\+/g, "olo")
+ .replace(/-/g, "oll")
+ .replace(/\./g, "loo")
+ .replace(/,/g, "lol")
+ .replace(/\[/g, "llo")
+ .replace(/\]/g, "lll");
+ },
+ hiworld = '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.';
-exports["Hello world!"] = function (test) {
- var hiworld = '++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.';
- var tscript = hiworld
- .replace(/>/g, "ooo")
- .replace(/</g, "ool")
- .replace(/\+/g, "olo")
- .replace(/-/g, "oll")
- .replace(/\./g, "loo")
- .replace(/,/g, "lol")
- .replace(/\[/g, "llo")
- .replace(/\]/g, "lll");
+exports["translation works"] = function(test) {
+ test.equal(hiworld, trollscript.translate(translate(hiworld)));
+ test.done();
+}
- console.log("Here is the tscript:");
- console.log(tscript);
- trollscript(tscript, function(output) {
- console.log(output);
- test.equal(output, "Hello world!");
+exports["interpreting works"] = function(test) {
+ trollscript(translate(hiworld), function(output) {
+ test.equal(output, "Hello World!\n");
test.done();
});

0 comments on commit dc86347

Please sign in to comment.