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
parent
f23396de2e
commit
f2adafb264
|
@ -319,7 +319,18 @@ class Compiler
|
|||
*/
|
||||
_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)
|
||||
const [ , [ { value: name }, ...params ], ...body ] = t;
|
||||
|
@ -441,34 +452,6 @@ class Compiler
|
|||
// final function application
|
||||
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}'`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue