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
parent
d3779c2de6
commit
2e5b536a5d
|
@ -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 ) );
|
||||
} )();
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue