Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

initial thing that works

  • Loading branch information...
commit cc4723ed0ac0c4388e838ecfb1229122547e2a41 0 parents
James Halliday authored
18 LICENSE
... ... @@ -0,0 +1,18 @@
  1 +This software is released under the MIT license:
  2 +
  3 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  4 +this software and associated documentation files (the "Software"), to deal in
  5 +the Software without restriction, including without limitation the rights to
  6 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  7 +the Software, and to permit persons to whom the Software is furnished to do so,
  8 +subject to the following conditions:
  9 +
  10 +The above copyright notice and this permission notice shall be included in all
  11 +copies or substantial portions of the Software.
  12 +
  13 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  15 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  16 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  17 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
93 bin/cmd.js
... ... @@ -0,0 +1,93 @@
  1 +#!/usr/bin/env node
  2 +var fs = require('fs');
  3 +var path = require('path');
  4 +var argv = require('optimist').argv;
  5 +var number = require('../');
  6 +
  7 +if (argv.h || argv.help) {
  8 + return fs.createReadStream(__dirname + '/usage.txt')
  9 + .pipe(process.stdout)
  10 + ;
  11 +}
  12 +if (argv.v || argv.version) {
  13 + var v = require('../package.json').version;
  14 + return console.log('NumberScript version ' + v);
  15 +}
  16 +var outfile = argv.o || argv.output || '-';
  17 +
  18 +if (argv.d || argv.decompile) {
  19 + var file = argv.c || argv.compile || '-';
  20 + readFile(file, function (err, src) {
  21 + if (err) console.error(err)
  22 + else number.decompile(src, function (err, n) {
  23 + if (err) console.error(err)
  24 + else writeFile(outfile, n)
  25 + });
  26 + });
  27 + return;
  28 +}
  29 +
  30 +if (argv.i || argv.interactive) {
  31 + var ctx = {
  32 + require : require,
  33 + console : console,
  34 + process : process,
  35 + };
  36 + repl.start(function (n, cb) {
  37 + number.run(n, cb);
  38 + });
  39 +}
  40 +
  41 +if (argv.c || argv.compile) {
  42 + var file = argv.c || argv.compile || '-';
  43 + readFile(file, function (err, src) {
  44 + if (err) console.error(err)
  45 + else number.compile(src, function (err, c) {
  46 + if (err) console.error(err)
  47 + else writeFile(outfile, c)
  48 + });
  49 + });
  50 + return;
  51 +}
  52 +
  53 +if (true || argv.r || argv.run) {
  54 + var file = argv.r || argv.run || argv._[0] || '-';
  55 + var ctx = {
  56 + require : require,
  57 + console : console,
  58 + process : process,
  59 + __filename : file,
  60 + __dirname : file === '-' ? process.cwd() : path.dirname(file)
  61 + };
  62 + readFile(file, function (err, src) {
  63 + if (err) console.error(err)
  64 + else number.run(src, ctx, function (err) {
  65 + if (err) console.error(err)
  66 + });
  67 + });
  68 + return;
  69 +}
  70 +
  71 +function readFile (file, cb) {
  72 + if (file === '-') {
  73 + var data = '';
  74 + process.stdin.on('data', function (buf) { data += buf });
  75 + process.stdin.on('end', function () {
  76 + cb(null, data);
  77 + });
  78 + process.stdin.resume();
  79 + }
  80 + else {
  81 + fs.readFile(file, function (err, src) {
  82 + if (err) cb(err)
  83 + else cb(null, src)
  84 + });
  85 + }
  86 +}
  87 +
  88 +function writeFile (file, src) {
  89 + if (file === '-') {
  90 + process.stdout.write(src);
  91 + }
  92 + else fs.writeFile(file, src);
  93 +}
10 bin/usage.txt
... ... @@ -0,0 +1,10 @@
  1 +
  2 +Usage: number-script [options] path/to/script.number
  3 +
  4 + -c, --compile compile to JavaScript
  5 + -d, --decompile decompile JavaScript back to NumberScript
  6 + -i, --interactive run an interactive NumberScript REPL
  7 + -o, --output set a file to output to ("-" goes to stdout)
  8 + -v, --version display NumberScript version
  9 + -h, --help display this message
  10 +
54 index.js
... ... @@ -0,0 +1,54 @@
  1 +var bignum = require('bignum');
  2 +var vm = require('vm');
  3 +
  4 +exports = module.exports = run;
  5 +exports.run = run;
  6 +function run (n, ctx, cb) {
  7 + if (typeof ctx === 'function') {
  8 + cb = ctx;
  9 + ctx = {};
  10 + }
  11 + compile(n, function (err, src) {
  12 + if (err) return cb(err)
  13 + try {
  14 + var res = vm.runInNewContext(src, ctx);
  15 + }
  16 + catch (err) { return cb(err) };
  17 + cb(null, res);
  18 + });
  19 +}
  20 +
  21 +exports.compile = compile;
  22 +function compile (n, cb) {
  23 + var js;
  24 + if (Buffer.isBuffer(n)) {
  25 + var xs = [];
  26 + for (var i = 0; i < n.length; i++) {
  27 + if (/\S/.test(String.fromCharCode(n[i]))) {
  28 + xs.push(n[i]);
  29 + }
  30 + }
  31 + var buf = new Buffer(xs);
  32 + js = String(bignum.fromBuffer(buf).toBuffer());
  33 + }
  34 + else if (typeof n === 'string') {
  35 + js = String(bignum(n.replace(/\s+/g, '')).toBuffer());
  36 + }
  37 + else if (n && typeof n === 'object' && n.toBuffer) {
  38 + js = String(n.toBuffer());
  39 + }
  40 + try {
  41 + vm.runInNewContext('(function () { ' + js + ' })');
  42 + }
  43 + catch (e) {
  44 + if (cb) cb('Invalid number.')
  45 + return;
  46 + }
  47 + cb(null, js);
  48 +}
  49 +
  50 +exports.decompile = decompile;
  51 +function decompile (src, cb) {
  52 + var buf = Buffer.isBuffer(src) ? buf : new Buffer(src);
  53 + cb(null, String(bignum.fromBuffer(buf)));
  54 +}
43 package.json
... ... @@ -0,0 +1,43 @@
  1 +{
  2 + "name" : "number-script",
  3 + "version" : "0.0.0",
  4 + "description" : "Ordinal JavaScript",
  5 + "main" : "index.js",
  6 + "bin" : {
  7 + "number-script" : "bin/cmd.js"
  8 + },
  9 + "directories" : {
  10 + "example" : "example",
  11 + "test" : "test"
  12 + },
  13 + "dependencies" : {
  14 + "optimist" : "~0.3.4",
  15 + "bignum" : "~0.4.1"
  16 + },
  17 + "devDependencies" : {
  18 + "tap" : "~0.2.5"
  19 + },
  20 + "scripts" : {
  21 + "test" : "tap test/*.js"
  22 + },
  23 + "repository" : {
  24 + "type" : "git",
  25 + "url" : "git://github.com/substack/number-script.git"
  26 + },
  27 + "homepage" : "https://github.com/substack/number-script",
  28 + "keywords" : [
  29 + "javascript",
  30 + "language",
  31 + "numberscript",
  32 + "compiler",
  33 + "ordinal",
  34 + "integral"
  35 + ],
  36 + "author" : {
  37 + "name" : "James Halliday",
  38 + "email" : "mail@substack.net",
  39 + "url" : "http://substack.net"
  40 + },
  41 + "license" : "MIT",
  42 + "engine" : { "node" : ">=0.6" }
  43 +}

0 comments on commit cc4723e

Please sign in to comment.
Something went wrong with that request. Please try again.