Raw File
webppl
#!/usr/bin/env node
'use strict';

var fs = require('fs');
var path = require('path');
var webppl = require('./src/main');
var pkg = require('./src/pkg');
var util = require('./src/util');
var pkginfo = require('./src/pkginfo');
var git = require('./src/git');
var parseArgs = require('minimist');
var _ = require('underscore');

function printWebPPLValue(x) {
  if (isErp(x)) {
    x.print();
  } else {
    console.log(x);
  }
};

function topK(s,x) {
  console.log('\n* Program return value:\n');
  printWebPPLValue(x);
};

var cliRunner = util.trampolineRunners.cli;

function run(code, packages, verbose, programFile) {
  packages.forEach(function(pkg) {
    if (pkg.js) { global[pkg.js.identifier] = require(pkg.js.path); }
    pkg.headers.forEach(webppl.requireHeader);
  });

  webppl.run(code, topK, {
    bundles: webppl.parsePackageCode(packages, verbose),
    filename: programFile,
    verbose: verbose
  });
}

var lines = function(ar) {
  return ar.join('\n')
}

function compile(code, packages, verbose, programFile, outputFile) {
  var compiledCode = 'var webppl = require("' + require.resolve('./src/main') + '");\n';
  packages.forEach(function(pkg) {
    if (pkg.js) { compiledCode += 'var ' + pkg.js.identifier + ' = require("' + pkg.js.path + '");\n'; }
    pkg.headers.forEach(function(header) {
      compiledCode += 'webppl.requireHeader("' + header + '");\n';
    });
  });

  var compileOptions = {
    bundles: webppl.parsePackageCode(packages, verbose),
    filename: programFile,
    verbose: verbose
  };

  var compiledBody = webppl.compile(code, compileOptions);

  compiledCode += lines([
    'var __runner__ = ' + cliRunner.toString() + ';',
    printWebPPLValue.toString() + ';',
    topK.toString() + ';',
    'var main = ' + compiledBody + '\n',
    "main(__runner__)({}, topK, '');"
  ]);

  // Write Javascript code to file
  fs.writeFile(
      outputFile,
      compiledCode,
      function(err) {
        if (err) {
          console.log(err);
        } else {
          console.log('Wrote webppl code to', outputFile);
        }
      });
}

function main() {

  var argv = parseArgs(process.argv.slice(2));

  // Print version if requested
  if (argv.version) {
    var dir = __dirname;
    console.log(_.filter([git.branch(dir), pkginfo.version(), dir]).join(' '));
    return;
  }

  // Return if no file argument given
  if (argv._[0] === undefined) {
    console.log('No program argument given! Run webppl as follows:');
    console.log('webppl program.wppl [--compile] [--out compiled.js] ' +
                '[--require path] [--random-seed int]');
    return;
  }

  // Load webppl code
  var programFile = argv._[0];
  console.log('Processing', programFile);

  var code = fs.readFileSync(programFile, 'utf8');
  var processCode = argv.compile ? compile : run;
  var outputFile = argv.out ? argv.out : 'tmp.js';

  var packagePaths = [
    path.join(path.dirname(programFile), 'node_modules'),
    pkg.globalPkgDir()
  ];

  var packages = util.asArray(argv.require).map(function(name_or_path) {
    return pkg.load(pkg.read(name_or_path, packagePaths, argv.verbose));
  });

  var seed = argv['random-seed'];
  if (seed !== undefined) {
    util.assertValidRandomSeed(seed);
    util.seedRNG(seed);
  }

  processCode(code, packages, argv.verbose, programFile, outputFile);
}

main();
back to top