From f626891e75f4a2d3f4222ff1e770f8035717b2eb Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Thu, 31 Aug 2017 00:46:23 -0400 Subject: [PATCH] prebirth: More substantial libprebirth This is a decent start. * build-aux/bootstrap/libprebirth.js: Add a lot of stuff. Go look for yourself. --- build-aux/bootstrap/libprebirth.js | 77 ++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/build-aux/bootstrap/libprebirth.js b/build-aux/bootstrap/libprebirth.js index 1e8e2d9..e566635 100644 --- a/build-aux/bootstrap/libprebirth.js +++ b/build-aux/bootstrap/libprebirth.js @@ -32,20 +32,79 @@ * removed as they are rewritten in Prebirth Lisp. * * By convention, everything is prefixed with `js:' in Prebirth (not `es:', - * because we're using JavaScript-specific features). Since - * non-alphanumeric and non-underscore characters are converted into `$', - * and all functions are prefixed with '$$' for global uniqueness, this - * results in a `$$js$' prefix in all of the functions below. + * because we're using JavaScript-specific features). For character + * transformation rules, see `Compiler#_idFromName' in `prebirth.js'. */ +const $h$t = true; +const $h$f = false; -/** - * Log to the console - */ -function $$js$console() +const argToArr = args => Array.prototype.slice.call( args ); + +function $$list() { return argToArr( arguments ); } + +const _error = str => { - console.log.apply( console, arguments ); + throw Error( str ); +} + +const _assertPair = xs => +{ + _assertList( xs ); + + if ( xs.length === 0 ) { + throw TypeError( "expecting pair" ); + } + + return true; +} + +const _assertList = xs => +{ + if ( !Array.isArray( xs ) ) { + throw TypeError( "expecting list" ); + } + + return true; +} + +// only false (#f in Scheme) is non-true +const _truep = x => x !== false; + +// ignore obj for now +const $$error = ( msg, obj ) => _error( msg ); + +const $$cons = ( item, list ) => _assertList( list ) && [ item ].concat( list ) +const $$car = xs => _assertPair( xs ) && xs[ 0 ]; +const $$cdr = xs => _assertPair( xs ) && xs.slice( 1 ); + +const $$list$7$ = xs => Array.isArray( xs ); +const $$pair$7$ = xs => Array.isArray( xs ) && ( xs.length > 0 ); + + +// R7RS string +const $$substring = ( s, start, end ) => s.substring( start, end ); +const $$string$_$length = s => s.length; +const $$string$q$$7$ = ( x, y ) => x === y; +const $$string$_$ref = ( s, i ) => s[ i ] + || _error( `value out of range: ${i}`); + +// R7RS math +function $$$p$() +{ + return argToArr( arguments ).reduce( ( ( x, y ) => x + y ), 0 ); } +// Node.js stuff +const fs = require( 'fs' ); + +// stdin->string +const $$js$stdin$_$$g$string = () => + fs.readFileSync( '/dev/stdin' ).toString(); + +const $$js$regexp = ( s, opts ) => new RegExp( s, opts ); +const $$js$match = ( r, s ) => s.match( r ) || false; + + /** =============== end of libprebirth =============== **/