Permalink
Browse files

initial thing that works

  • Loading branch information...
0 parents commit cc4723ed0ac0c4388e838ecfb1229122547e2a41 @substack committed Jun 22, 2012
Showing with 218 additions and 0 deletions.
  1. +18 −0 LICENSE
  2. +93 −0 bin/cmd.js
  3. +10 −0 bin/usage.txt
  4. +54 −0 index.js
  5. +43 −0 package.json
18 LICENSE
@@ -0,0 +1,18 @@
+This software is released under the MIT license:
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
93 bin/cmd.js
@@ -0,0 +1,93 @@
+#!/usr/bin/env node
+var fs = require('fs');
+var path = require('path');
+var argv = require('optimist').argv;
+var number = require('../');
+
+if (argv.h || argv.help) {
+ return fs.createReadStream(__dirname + '/usage.txt')
+ .pipe(process.stdout)
+ ;
+}
+if (argv.v || argv.version) {
+ var v = require('../package.json').version;
+ return console.log('NumberScript version ' + v);
+}
+var outfile = argv.o || argv.output || '-';
+
+if (argv.d || argv.decompile) {
+ var file = argv.c || argv.compile || '-';
+ readFile(file, function (err, src) {
+ if (err) console.error(err)
+ else number.decompile(src, function (err, n) {
+ if (err) console.error(err)
+ else writeFile(outfile, n)
+ });
+ });
+ return;
+}
+
+if (argv.i || argv.interactive) {
+ var ctx = {
+ require : require,
+ console : console,
+ process : process,
+ };
+ repl.start(function (n, cb) {
+ number.run(n, cb);
+ });
+}
+
+if (argv.c || argv.compile) {
+ var file = argv.c || argv.compile || '-';
+ readFile(file, function (err, src) {
+ if (err) console.error(err)
+ else number.compile(src, function (err, c) {
+ if (err) console.error(err)
+ else writeFile(outfile, c)
+ });
+ });
+ return;
+}
+
+if (true || argv.r || argv.run) {
+ var file = argv.r || argv.run || argv._[0] || '-';
+ var ctx = {
+ require : require,
+ console : console,
+ process : process,
+ __filename : file,
+ __dirname : file === '-' ? process.cwd() : path.dirname(file)
+ };
+ readFile(file, function (err, src) {
+ if (err) console.error(err)
+ else number.run(src, ctx, function (err) {
+ if (err) console.error(err)
+ });
+ });
+ return;
+}
+
+function readFile (file, cb) {
+ if (file === '-') {
+ var data = '';
+ process.stdin.on('data', function (buf) { data += buf });
+ process.stdin.on('end', function () {
+ cb(null, data);
+ });
+ process.stdin.resume();
+ }
+ else {
+ fs.readFile(file, function (err, src) {
+ if (err) cb(err)
+ else cb(null, src)
+ });
+ }
+}
+
+function writeFile (file, src) {
+ if (file === '-') {
+ process.stdout.write(src);
+ }
+ else fs.writeFile(file, src);
+}
10 bin/usage.txt
@@ -0,0 +1,10 @@
+
+Usage: number-script [options] path/to/script.number
+
+ -c, --compile compile to JavaScript
+ -d, --decompile decompile JavaScript back to NumberScript
+ -i, --interactive run an interactive NumberScript REPL
+ -o, --output set a file to output to ("-" goes to stdout)
+ -v, --version display NumberScript version
+ -h, --help display this message
+
54 index.js
@@ -0,0 +1,54 @@
+var bignum = require('bignum');
+var vm = require('vm');
+
+exports = module.exports = run;
+exports.run = run;
+function run (n, ctx, cb) {
+ if (typeof ctx === 'function') {
+ cb = ctx;
+ ctx = {};
+ }
+ compile(n, function (err, src) {
+ if (err) return cb(err)
+ try {
+ var res = vm.runInNewContext(src, ctx);
+ }
+ catch (err) { return cb(err) };
+ cb(null, res);
+ });
+}
+
+exports.compile = compile;
+function compile (n, cb) {
+ var js;
+ if (Buffer.isBuffer(n)) {
+ var xs = [];
+ for (var i = 0; i < n.length; i++) {
+ if (/\S/.test(String.fromCharCode(n[i]))) {
+ xs.push(n[i]);
+ }
+ }
+ var buf = new Buffer(xs);
+ js = String(bignum.fromBuffer(buf).toBuffer());
+ }
+ else if (typeof n === 'string') {
+ js = String(bignum(n.replace(/\s+/g, '')).toBuffer());
+ }
+ else if (n && typeof n === 'object' && n.toBuffer) {
+ js = String(n.toBuffer());
+ }
+ try {
+ vm.runInNewContext('(function () { ' + js + ' })');
+ }
+ catch (e) {
+ if (cb) cb('Invalid number.')
+ return;
+ }
+ cb(null, js);
+}
+
+exports.decompile = decompile;
+function decompile (src, cb) {
+ var buf = Buffer.isBuffer(src) ? buf : new Buffer(src);
+ cb(null, String(bignum.fromBuffer(buf)));
+}
43 package.json
@@ -0,0 +1,43 @@
+{
+ "name" : "number-script",
+ "version" : "0.0.0",
+ "description" : "Ordinal JavaScript",
+ "main" : "index.js",
+ "bin" : {
+ "number-script" : "bin/cmd.js"
+ },
+ "directories" : {
+ "example" : "example",
+ "test" : "test"
+ },
+ "dependencies" : {
+ "optimist" : "~0.3.4",
+ "bignum" : "~0.4.1"
+ },
+ "devDependencies" : {
+ "tap" : "~0.2.5"
+ },
+ "scripts" : {
+ "test" : "tap test/*.js"
+ },
+ "repository" : {
+ "type" : "git",
+ "url" : "git://github.com/substack/number-script.git"
+ },
+ "homepage" : "https://github.com/substack/number-script",
+ "keywords" : [
+ "javascript",
+ "language",
+ "numberscript",
+ "compiler",
+ "ordinal",
+ "integral"
+ ],
+ "author" : {
+ "name" : "James Halliday",
+ "email" : "mail@substack.net",
+ "url" : "http://substack.net"
+ },
+ "license" : "MIT",
+ "engine" : { "node" : ">=0.6" }
+}

0 comments on commit cc4723e

Please sign in to comment.