@@ -133,7 +133,9 @@ export function fromInt(n) {
133133export function toInt ( term ) {
134134 try {
135135 if ( config . numEncoding === "Church" ) {
136- return term ( x => x + 1 ) ( Primitive ( 0 ) ) ;
136+ const succ = x => x + 1 ;
137+ const result = term ( succ ) ;
138+ return result ( result === succ ? 0 : Primitive ( 0 ) ) ;
137139 } else if ( config . numEncoding === "Scott" ) {
138140 let result = 0 , evaluating = true ;
139141 while ( evaluating )
@@ -229,8 +231,13 @@ function parse(code) {
229231 function v ( i ) {
230232 const r = name ( i ) ;
231233 if ( r ) {
232- const [ j , name ] = r ;
233- return [ j , new V ( name ) ] ;
234+ const [ j , termName ] = r ;
235+ if ( termName === "_" ) {
236+ const undef = new V ( "()" ) ;
237+ undef . defName = name ( 0 ) [ 1 ] ;
238+ return [ j , undef ] ;
239+ } else
240+ return [ j , new V ( termName ) ] ;
234241 } else
235242 return null ;
236243 }
@@ -323,7 +330,10 @@ function evalLC(term) {
323330 let argEnv ;
324331 if ( arg ?. term && arg ?. env ) ( { term : arg , env : argEnv } = arg ) ; // if callback is passed another callback, or a term
325332 const termVal = new Tuple ( typeof arg === 'number' ? fromInt ( arg ) : arg , new Env ( argEnv ) ) ;
326- return runEval ( new Tuple ( term . body , new Env ( env ) . setThunk ( term . name , termVal ) ) , stack ) ;
333+ if ( term . name === "_" )
334+ return runEval ( new Tuple ( term . body , new Env ( env ) ) , stack ) ;
335+ else
336+ return runEval ( new Tuple ( term . body , new Env ( env ) . setThunk ( term . name , termVal ) ) , stack ) ;
327337 }
328338 return Object . assign ( result , { term, env} ) ;
329339 }
@@ -364,7 +374,7 @@ function evalLC(term) {
364374 env = new Env ( env ) . setThunk ( term . name , new Tuple ( lastTerm , lastEnv ) ) ;
365375 term = term . body ;
366376 } else { // Pass the function some other function.
367- term = lastTerm ( awaitArg ( term , stack , env ) ) ;
377+ term = lastTerm ( awaitArg ( term , [ ] , env ) ) ;
368378 }
369379 } else if ( term instanceof Tuple ) {
370380 // for primitives
@@ -404,9 +414,6 @@ function printStackTrace(error, term, stack) {
404414
405415 if ( config . verbosity >= "Loquacious" )
406416 console . error ( stack . slice ( stackCutoff ) . reverse ( ) . map ( v => `\twhile evaluating ${ v } ` ) . join ( '\n' ) ) ;
407-
408- if ( config . verbosity >= "Verbose" )
409- console . error ( stack . slice ( ) . reverse ( ) . map ( v => `\twhile evaluating ${ v } ` ) . join ( '\n' ) ) ;
410417}
411418
412- Object . defineProperty ( Function . prototype , "valueOf" , { value : function valueOf ( ) { return toInt ( this ) ; } } ) ;
419+ Object . defineProperty ( Function . prototype , "valueOf" , { value : function valueOf ( ) { if ( this . term ) return toInt ( this ) ; else return this ; } } ) ;
0 commit comments