From 2e5b536a5d00b75a4d0b12f666125175c946cbca Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Sun, 22 Oct 2017 01:17:16 -0400 Subject: [PATCH] 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. --- build-aux/bootstrap/prebirth.js | 58 ++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/build-aux/bootstrap/prebirth.js b/build-aux/bootstrap/prebirth.js index a77a6c0..150b7aa 100644 --- a/build-aux/bootstrap/prebirth.js +++ b/build-aux/bootstrap/prebirth.js @@ -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 ) ); } )();