Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Added support for bases other than decimal #2

Open
wants to merge 4 commits into from

2 participants

@mappum

We all know that real men code in binary, and hexidecimal makes for smaller file sizes. I added a base option to compile and run, and implemented it with a command line switch in cmd.js.

For example, now you can do number-script -b 2, then input 11000000111001 in interactive mode, or read a file with number-script -b 16 hex.number.

Vox Industires and others added some commits
@substack
Owner

Don't ever add node_modules to .gitignore, it messes up some forms of deployment: http://www.mikealrogers.com/posts/nodemodules-in-git.html

@mappum

Oh, TIL. Removed.

@substack
Owner

A problem is that bignum can't convert to arbitrary bases in .toString() so decompiling won't work. I pushed a fork using bigint, but that requires libgmp.

@mappum

Because of that, I did not include the base specification in decompile, only run and compile.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 8 deletions.
  1. +2 −0  README.markdown
  2. +8 −3 bin/cmd.js
  3. +2 −1  bin/usage.txt
  4. +4 −4 index.js
View
2  README.markdown
@@ -118,6 +118,8 @@ Usage: number-script [options] path/to/script.number
-o, --output set a file to output to ("-" goes to stdout)
-v, --version display NumberScript version
-h, --help display this message
+
+ -b, --base specifies the base to use for the code, defaults to 10
```
View
11 bin/cmd.js
@@ -16,6 +16,11 @@ if (argv.v || argv.version) {
}
var outfile = argv.o || argv.output;
+var base;
+if (argv.b || argv.base) {
+ base = argv.b || argv.base;
+}
+
if (argv.d || argv.decompile) {
var file = argv.d || argv.decompile;
readFile(file, function (err, src) {
@@ -35,7 +40,7 @@ if (argv.c || argv.compile) {
else number.compile(src, function (err, c) {
if (err) console.error(err)
else writeFile(outfile, c)
- });
+ }, base);
});
return;
}
@@ -56,7 +61,7 @@ if (argv.r || argv.run || argv._[0]) {
if (err) console.error(err)
else number.run(src, ctx, function (err) {
if (err) console.error(err)
- });
+ }, base);
});
return;
}
@@ -69,7 +74,7 @@ if (true || argv.i || argv.interactive) {
};
repl.start('> ', null, function (cmd, _, _, cb) {
var n = cmd.replace(/^\(|\)$/g, '');
- number.run(n, cb);
+ number.run(n, cb, null, base);
});
}
View
3  bin/usage.txt
@@ -7,4 +7,5 @@ Usage: number-script [options] path/to/script.number
-o, --output set a file to output to ("-" goes to stdout)
-v, --version display NumberScript version
-h, --help display this message
-
+
+ -b, --base specifies the base to use for the code, defaults to 10
View
8 index.js
@@ -6,7 +6,7 @@ var vm = require('vm');
exports = module.exports = run;
exports.run = run;
-function run (n, ctx, cb) {
+function run (n, ctx, cb, base) {
if (typeof ctx === 'function') {
cb = ctx;
ctx = {};
@@ -18,11 +18,11 @@ function run (n, ctx, cb) {
}
catch (err) { return cb(err) };
cb(null, res);
- });
+ }, base);
}
exports.compile = compile;
-function compile (n, cb) {
+function compile (n, cb, base) {
var js;
if (Buffer.isBuffer(n)) {
var xs = [];
@@ -35,7 +35,7 @@ function compile (n, cb) {
js = String(bignum.fromBuffer(buf).toBuffer());
}
else if (typeof n === 'string') {
- js = String(bignum(n.replace(/\s+/g, '')).toBuffer());
+ js = String(bignum(n.replace(/\s+/g, ''), base).toBuffer());
}
else if (n && typeof n === 'object' && n.toBuffer) {
js = String(n.toBuffer());
Something went wrong with that request. Please try again.