prebirth: Add Prebirth facade

This provides the complete compiler output, which is consistent with the
output of Birth.  This will be useful for browser-based bootstrapping.

* build-aux/bootstrap/prebirth.js: Extract code into `Prebirth' class and
    use the class.
  (Prebirth): Add class.
master
Mike Gerwitz 2017-10-22 01:17:16 -04:00
parent d3779c2de6
commit 2e5b536a5d
Signed by: mikegerwitz
GPG Key ID: 8C917B7F5DC51BA2
1 changed files with 42 additions and 16 deletions

View File

@ -655,33 +655,59 @@ const fnmap = {
};
/**
* Facade to make compilation as easy as `Prebirth#compile'
*/
class Prebirth
{
/**
* Parse and compile SRC.
*
* The output will be wrapped in a self-executing function to restrict
* scope, and will be prefixed with LIB (which should be libprebirth.js).
*
* @param {string} src Prebirth Lisp source to compile
* @param {string} lib libprebirth.js
*
* @return {string} compiler output with trailing newline
*/
compile( src, lib )
{
const p = new Parser();
const c = new Compiler( fnmap );
const tree = p.parseLisp( src );
// output libprebirth and compiled output, wrapped in a self-executing
// function to limit scope
return "(function(){" +
lib + '\n\n' +
c.compile( tree ) +
"})();\n";
}
}
/*
* Prebirth was originally intended to be run via the command line using
* Node.js. But it doesn't have to be. If you want, feel free to run it in
* your web browser; you'll just have to instantiate your own objects.
* your web browser; you'll just have to instantiate your own Prebirth.
*/
( function ()
{
if ( typeof process === 'undefined' )
{
return;
if ( typeof process === 'undefined' ) {
return;
}
const p = new Parser();
const c = new Compiler( fnmap );
const fs = require( 'fs' );
const src = fs.readFileSync( '/dev/stdin' ).toString();
const lib = fs.readFileSync( __dirname + '/libprebirth.js' ).toString();
const fs = require( 'fs' );
const src = fs.readFileSync( '/dev/stdin' ).toString();
const tree = p.parseLisp( src );
const lib = fs.readFileSync( __dirname + '/libprebirth.js' ).toString();
const prebirth = new Prebirth();
// output libprebirth and compiled output, wrapped in a self-executing
// function to limit scope
process.stdout.write( "(function(){" );
process.stdout.write( lib + '\n\n' );
process.stdout.write( c.compile( tree ) );
process.stdout.write( "})();\n" );
// compile and output to stdout
process.stdout.write( prebirth.compile( src, lib ) );
} )();