1- import { AstNode , EmptyFileSystem , interruptAndCheck , LangiumDocument , MaybePromise , OperationCancelled } from "langium" ;
1+ import { AstNode , EmptyFileSystem , interruptAndCheck , LangiumDocument , MaybePromise } from "langium" ;
22import { BinaryExpression , Expression , isBinaryExpression , isBooleanExpression , isClass , isExpression , isExpressionBlock , isForStatement , isFunctionDeclaration , isIfStatement , isMemberCall , isNilExpression , isNumberExpression , isParameter , isPrintStatement , isReturnStatement , isStringExpression , isUnaryExpression , isVariableDeclaration , isWhileStatement , LoxElement , LoxProgram , MemberCall } from "../language-server/generated/ast" ;
33import { createLoxServices } from "../language-server/lox-module" ;
44import { v4 } from 'uuid' ;
@@ -7,7 +7,6 @@ import { CancellationToken, CancellationTokenSource } from "vscode-languageserve
77
88export interface InterpreterContext {
99 log : ( value : unknown ) => MaybePromise < void > ,
10- onTimeout ?: ( ) => void ,
1110 onStart ?: ( ) => void ,
1211}
1312
@@ -33,7 +32,6 @@ interface RunnerContext {
3332 cancellationToken : CancellationToken ,
3433 timeout : NodeJS . Timeout ,
3534 log : ( value : unknown ) => MaybePromise < void > ,
36- onTimeout ?: ( ) => void ,
3735 onStart ?: ( ) => void ,
3836}
3937
@@ -102,6 +100,7 @@ export async function runProgram(program: LoxProgram, outerContext: InterpreterC
102100 const cancellationToken = cancellationTokenSource . token ;
103101
104102 const timeout = setTimeout ( async ( ) => {
103+ console . log ( 'Interpreter timeout' ) ;
105104 cancellationTokenSource . cancel ( ) ;
106105 } , TIMEOUT_MS ) ;
107106
@@ -110,7 +109,6 @@ export async function runProgram(program: LoxProgram, outerContext: InterpreterC
110109 cancellationToken,
111110 timeout,
112111 log : outerContext . log ,
113- onTimeout : outerContext . onTimeout ,
114112 onStart : outerContext . onStart ,
115113 } ;
116114
@@ -122,10 +120,7 @@ export async function runProgram(program: LoxProgram, outerContext: InterpreterC
122120 }
123121
124122 for ( const statement of program . elements ) {
125- if ( await checkCancellationToken ( context ) ) {
126- end = true ;
127- break ;
128- }
123+ await interruptAndCheck ( context . cancellationToken ) ;
129124
130125 if ( ! isClass ( statement ) && ! isFunctionDeclaration ( statement ) ) {
131126 await runLoxElement ( statement , context , ( ) => { end = true } ) ;
@@ -141,9 +136,7 @@ export async function runProgram(program: LoxProgram, outerContext: InterpreterC
141136}
142137
143138async function runLoxElement ( element : LoxElement , context : RunnerContext , returnFn : ReturnFunction ) : Promise < void > {
144- if ( await checkCancellationToken ( context ) ) {
145- return ;
146- }
139+ await interruptAndCheck ( context . cancellationToken ) ;
147140
148141 if ( isExpressionBlock ( element ) ) {
149142 await interruptAndCheck ( CancellationToken . None ) ;
@@ -202,9 +195,8 @@ async function runLoxElement(element: LoxElement, context: RunnerContext, return
202195}
203196
204197async function runExpression ( expression : Expression , context : RunnerContext ) : Promise < unknown > {
205- if ( await checkCancellationToken ( context ) ) {
206- return ;
207- }
198+ await interruptAndCheck ( context . cancellationToken ) ;
199+
208200
209201 if ( isBinaryExpression ( expression ) ) {
210202 const { left, right, operator } = expression ;
@@ -286,9 +278,7 @@ async function setExpressionValue(left: Expression, right: unknown, context: Run
286278}
287279
288280async function runMemberCall ( memberCall : MemberCall , context : RunnerContext ) : Promise < unknown > {
289- if ( await checkCancellationToken ( context ) ) {
290- return ;
291- }
281+ await interruptAndCheck ( context . cancellationToken ) ;
292282
293283 let previous : unknown = undefined ;
294284 if ( memberCall . previous ) {
@@ -363,21 +353,6 @@ function applyOperator(node: BinaryExpression, operator: string, left: unknown,
363353 }
364354}
365355
366- async function checkCancellationToken ( context : RunnerContext ) : Promise < boolean > {
367- try {
368- await interruptAndCheck ( context . cancellationToken ) ;
369- }
370- catch ( e ) {
371- if ( e === OperationCancelled ) {
372- if ( context . onTimeout ) {
373- context . onTimeout ( ) ;
374- }
375- return true ;
376- }
377- throw e ;
378- }
379- return false ;
380- }
381356
382357function isNumber ( value : unknown ) : value is number {
383358 return typeof value === 'number' ;
0 commit comments