prebirth: Allow toplevel function applications

So we can invoke the main function for the program.

* build-aux/bootstrap/prebirth.js
  (Compiler#_cdfn): Handle non-`define' applications.
  (Compiler#assertApply): Remove function.
master
Mike Gerwitz 2017-08-28 01:21:02 -04:00
parent f23396de2e
commit f2adafb264
Signed by: mikegerwitz
GPG Key ID: 8C917B7F5DC51BA2
1 changed files with 12 additions and 29 deletions

View File

@ -319,7 +319,18 @@ class Compiler
*/ */
_cdfn( t ) _cdfn( t )
{ {
this.assertApply( t, 'define' ); // an application must be an s-expression
if ( !Array.isArray( t ) ) {
throw Error(
`\`${name}' application expected, found symbol \`${t.value}'`
);
}
// if it's not a definition, then it's a top-level application
if ( t[ 0 ].value !== 'define' )
{
return this._sexpToEs( t ) + ';';
}
// e.g. (define (foo ...) body) // e.g. (define (foo ...) body)
const [ , [ { value: name }, ...params ], ...body ] = t; const [ , [ { value: name }, ...params ], ...body ] = t;
@ -441,34 +452,6 @@ class Compiler
// final function application // final function application
return `${idfn}(${argstr})`; return `${idfn}(${argstr})`;
} }
/**
* Determine whether T is an application of a symbol NAME, or error
*
* @param {*} t hopefully a token or token list
* @param {string} name function name to assert against
*/
assertApply( t, name )
{
// an application must be an s-expression
if ( !Array.isArray( t ) ) {
throw Error(
`\`${name}' application expected, found symbol \`${t.value}'`
);
}
// if there's a match, we can stop here
if ( t[ 0 ].value === name ) {
return;
}
// otherwise, provide an informative error of what we found and what
// we should have found
throw Error(
`\`${name}' expected, found \`${t[ 0 ].value}'`
);
}
} }