import { Node , ESTree } from "../../Node" ; import { BaseClosure , } from '../../Closure' ; import { Messages } from '../../Message' ; import { Interpreter } from '../../../interpreter/main' ; import { EmptyStatementReturn } from '../../Symbols' ; import { Return , BreakLabel , ContinueLabel } from '../../TokenClass' ; import { Break , Continue } from '../../Symbols' ; import Scope , { createScope } from "../../Scope" ; export function programHandler ( this , node ) { this . blockDeclareStart ( ) ; let stmtClosures = ( node . body as Node [ ] ) . map ( ( stmt ) => { return this . createClosure ( stmt ) ; } ) ; let functionDecl = ( node . body as Node [ ] ) . map ( ( _ , index ) => ( { type : _ . type , index } ) ) . filter ( _ => _ . type == 'FunctionDeclaration' ) ; let lexDeclared = this . blockDeclareEnd ( ) ; return ( ) => { let result = EmptyStatementReturn ; let prevScope ; let newScope ; if ( lexDeclared ) { newScope = createScope ( this . getCurrentScope ( ) , `BScope` , "block" ) ; newScope . lexDeclared = lexDeclared ; prevScope = this . entryBlockScope ( newScope ) ; } functionDecl . forEach ( _ => { stmtClosures [ _ . index ] ( ) ; stmtClosures [ _ . index ] = null ; } ) ; stmtClosures = stmtClosures . filter ( _ => _ ) ; for ( let i = 0 ; i < stmtClosures . length ; i ++ ) { const stmtClosure = stmtClosures [ i ] ; const ret = this . setValue ( stmtClosure ( ) ) ; if ( ret === EmptyStatementReturn ) continue ; result = ret ; if ( result instanceof Return || result instanceof BreakLabel || result instanceof ContinueLabel || result === Break || result === Continue ) { break ; } } if ( lexDeclared ) { this . setCurrentScope ( prevScope ! ) ; } return result ; } ; }