diff --git a/README.md b/README.md index 31460f2..775a133 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,16 @@ Gir supports following optimizations: * Add offsets to commands that modify tape, to reduce moving tape head * Turn multiply loops into one command +Examples +-------- +`ircbot.js` is an implementation of an IRC bot's brainfuck interpreter +function. Smaller examples are available in `api.md` + TODO ---- ### gir.html * Implement a UI ### General -* Make Gir use modules * Get this on NPM? * Move into subdirectories? diff --git a/gir.html b/gir.html index 56dd7f6..3c6f99a 100644 --- a/gir.html +++ b/gir.html @@ -3,8 +3,23 @@ Gir testbench + - + +

Open the JavaScript console

diff --git a/gir.js b/gir.js index ad309ea..c88d94a 100644 --- a/gir.js +++ b/gir.js @@ -947,3 +947,10 @@ function decodeUTF8(encoded) { function compile(program, enableExtensions = true) { return optimize(parse(program, enableExtensions)); } + +exports.compile = compile; +exports.prettifyIR = prettifyIR; +exports.newVM = newVM; +exports.runVM = runVM; +exports.encodeUTF8 = encodeUTF8; +exports.decodeUTF8 = decodeUTF8; diff --git a/example.js b/ircbot.js similarity index 87% rename from example.js rename to ircbot.js index ad8439e..212ad32 100644 --- a/example.js +++ b/ircbot.js @@ -1,5 +1,7 @@ 'use strict'; +const gir = require('./gir.js'); + class IntParseError extends Error {} const programCacheSize = 16; @@ -15,7 +17,7 @@ function cachedCompile(program, enableExtensions = true) { programCache.delete(program); } - let compiled = compile(program, enableExtensions); + let compiled = gir.compile(program, enableExtensions); programCache.set(program, {compiled: compiled, extensions: enableExtensions}); @@ -58,10 +60,10 @@ function cachedCompile(program, enableExtensions = true) { // (string, string, int) → string function ircbotRun(program, input, maxCycles = 400000) { let compiled = cachedCompile(program); - let vm = newVM(compiled, encodeUTF8(input)); + let vm = gir.newVM(compiled, gir.encodeUTF8(input)); - let result = runVM(vm, maxCycles); - let output = decodeUTF8(result.state.output); + let result = gir.runVM(vm, maxCycles); + let output = gir.decodeUTF8(result.state.output); // Replace all characters < 0x20 except for IRC formatting codes // with their graphical representations at U+24xx @@ -157,9 +159,32 @@ function ircbotRun(program, input, maxCycles = 400000) { // If there was a problem with parsing an int, throw an Error if(result.intParseFailed) { - let context = decodeUTF8(result.state.input).slice(0, 3); + let context = gir.decodeUTF8(result.state.input).slice(0, 3); throw new IntParseError(`';': couldn't read number (near '${context})'`); } return output; } + +exports.ircbotRun = ircbotRun; + +function main() { + const readline = require('readline'); + + const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout, + prompt: 'program!input> ' + }); + rl.prompt(); + + rl.on('line', line => { + let [program, input] = line.split('!'); + console.log(ircbotRun(program, input)); + rl.prompt(); + }); +} + +if(require.main === module) { + main(); +}