Skip to content

Instantly share code, notes, and snippets.

@vihanb
Created August 6, 2016 22:21
Show Gist options
  • Save vihanb/6056e8147ed0b7d257495fe48ba62551 to your computer and use it in GitHub Desktop.
Save vihanb/6056e8147ed0b7d257495fe48ba62551 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var cheddar = require('cheddar-lang');
var api = cheddar.stdlib;
// Enable Cheddar safe mode
global.SAFE_MODE = true;
// Print function
var printed = false;
function print(text) {
printed = true;
process.stdout.write(text);
}
// Get the default return
var res = cheddar(process.argv[2], {
PRINT: print
});
// Check if function was returned
if (res instanceof api.func) {
var args = process.argv.slice(3).map(function(arg) {
// Convert it to a Cheddar string
var str = api.init(api.string, arg);
// Attempt to convert to number
var castattempt = api.string.Cast.get("Number")(str);
if (castattempt instanceof api.number) {
return castattempt;
}
else {
return str;
}
});
// Call it with input
res = res.exec(args, null);
}
if (printed === false) {
( res.constructor.Operator || res.Operator ).get("print")(null, res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment