@@ -52,13 +52,11 @@ export function CSEResultPromise(context: Context, value: Value): Promise<Result
5252 return new Promise ( ( resolve , reject ) => {
5353 if ( value instanceof CSEBreak ) {
5454 resolve ( { status : 'suspended-cse-eval' , context } ) ;
55- } else if ( value . type === 'error' ) { // value instanceof CseError
55+ } else if ( value . type === 'error' ) {
5656 const msg = value . message ;
57- //resolve({ status: 'error', msg } as unknown as Result );
5857 const representation = new Representation ( cseFinalPrint + msg ) ;
5958 resolve ( { status : 'finished' , context, value, representation } )
6059 } else {
61- //const rep: Value = { type: "string", value: cseFinalPrint };
6260 const representation = new Representation ( value ) ;
6361 resolve ( { status : 'finished' , context, value, representation } )
6462 }
@@ -82,11 +80,8 @@ export function evaluate(code: string, program: es.Program, context: Context, op
8280 // TODO: is undefined variables check necessary for Python?
8381 // checkProgramForUndefinedVariables(program, context)
8482 } catch ( error : any ) {
85- // context.errors.push(new CseError(error.message));
8683 return { type : 'error' , message : error . message } ;
8784 }
88- // TODO: should call transformer like in js-slang
89- // seq.transform(program)
9085
9186 try {
9287 context . runtime . isRunning = true
@@ -104,9 +99,7 @@ export function evaluate(code: string, program: es.Program, context: Context, op
10499 ) ;
105100 const rep : Value = { type : "string" , value : cseFinalPrint } ;
106101 return rep ;
107- // return result;
108102 } catch ( error : any ) {
109- // context.errors.push(new CseError(error.message));
110103 return { type : 'error' , message : error . message } ;
111104 } finally {
112105 context . runtime . isRunning = false ;
@@ -131,7 +124,6 @@ function evaluateImports(program: es.Program, context: Context) {
131124 } else {
132125 throw new Error ( `Unexpected literal import: ${ spec . imported . value } ` ) ;
133126 }
134- //obj = functions[(spec.imported).name]
135127 break
136128 }
137129 case 'ImportDefaultSpecifier' : {
@@ -226,17 +218,13 @@ export function* generateCSEMachineStateStream(
226218 }
227219
228220 while ( command ) {
229- // For local debug only
230- // console.info('next command to be evaluated');
231- // console.info(command);
232- // console.info(steps);
233- // console.info(isPrelude);
234- // console.info(stepLimit);
221+
235222 // Return to capture a snapshot of the control and stash after the target step count is reached
236223 // if (!isPrelude && steps === envSteps) {
237224 // yield { stash, control, steps }
238225 // return
239226 // }
227+
240228 // Step limit reached, stop further evaluation
241229 if ( ! isPrelude && steps === stepLimit ) {
242230 handleRuntimeError ( context , new error . StepLimitExceededError ( source , command as es . Node , context ) ) ;
@@ -252,7 +240,6 @@ export function* generateCSEMachineStateStream(
252240 if ( isNode ( command ) ) {
253241 context . runtime . nodes . shift ( )
254242 context . runtime . nodes . unshift ( command )
255- //checkEditorBreakpoints(context, command)
256243 cmdEvaluators [ command . type ] ( command , context , control , stash , isPrelude )
257244 if ( context . runtime . break && context . runtime . debuggerOn ) {
258245 // TODO
@@ -267,51 +254,17 @@ export function* generateCSEMachineStateStream(
267254 cmdEvaluators [ ( command as Instr ) . instrType ] ( command , context , control , stash , isPrelude )
268255 }
269256
270- // Push undefined into the stack if both control and stash is empty
271- if ( control . isEmpty ( ) && stash . isEmpty ( ) ) {
272- //stash.push(undefined)
273- }
274257 command = control . peek ( )
275258
276259 steps += 1
277260 if ( ! isPrelude ) {
278261 context . runtime . envStepsTotal = steps
279262 }
280263
281- // printEnvironmentVariables(context.runtime.environments);
282-
283264 yield { stash, control, steps }
284265 }
285266}
286267
287- function printEnvironmentVariables ( environments : Environment [ ] ) : void {
288- console . info ( '----------------------------------------' ) ;
289- environments . forEach ( env => {
290- console . info ( `Env: ${ env . name } (ID: ${ env . id } )` ) ;
291-
292- const variables = env . head ;
293- const variableNames = Object . keys ( variables ) ;
294-
295- if ( variableNames . length > 0 ) {
296- variableNames . forEach ( varName => {
297- const descriptor = Object . getOwnPropertyDescriptor ( env . head , varName ) ;
298- if ( descriptor ) {
299- const value = descriptor . value . value ;
300- console . info ( 'value: ' , value ) ;
301- const valueStr = ( typeof value === 'object' && value !== null )
302- ? JSON . stringify ( value , null , 2 )
303- : String ( value ) ;
304- console . info ( ` ${ varName } : ${ valueStr } ` ) ;
305- } else {
306- console . info ( ` ${ varName } : None` ) ;
307- }
308- } ) ;
309- } else {
310- console . info ( ' no defined variables' ) ;
311- }
312- } ) ;
313- }
314-
315268const cmdEvaluators : { [ type : string ] : CmdEvaluator } = {
316269 /**
317270 * AST Nodes
@@ -406,7 +359,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
406359 } ,
407360
408361 IfStatement : function (
409- command : ControlItem , //es.IfStatement,
362+ command : ControlItem ,
410363 context : Context ,
411364 control : Control ,
412365 stash : Stash
@@ -415,7 +368,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
415368 } ,
416369
417370 ExpressionStatement : function (
418- command : ControlItem , //es.ExpressionStatement,
371+ command : ControlItem ,
419372 context : Context ,
420373 control : Control ,
421374 stash : Stash ,
@@ -439,7 +392,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
439392 } ,
440393
441394 FunctionDeclaration : function (
442- command : ControlItem , //es.FunctionDeclaration,
395+ command : ControlItem ,
443396 context : Context ,
444397 control : Control
445398 ) {
@@ -457,7 +410,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
457410 } ,
458411
459412 ReturnStatement : function (
460- command : ControlItem , //as es.ReturnStatement,
413+ command : ControlItem ,
461414 context : Context ,
462415 control : Control
463416 ) {
@@ -478,7 +431,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
478431 * Expressions
479432 */
480433 Literal : function (
481- command : ControlItem , //es.Literal
434+ command : ControlItem ,
482435 context : Context ,
483436 control : Control ,
484437 stash : Stash
@@ -495,7 +448,6 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
495448 value = { type : 'string' , value : literalValue } ;
496449 } else if ( typeof literalValue === 'boolean' ) {
497450 value = { type : 'bool' , value : literalValue } ;
498- //value = literalValue;
499451 } else {
500452 //handleRuntimeError(context, new CseError('Unsupported literal type'));
501453 return ;
@@ -522,14 +474,13 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
522474 }
523475 stash . push ( value ) ;
524476 } else {
525- // TODO
526- // Error
477+ // TODO: handle errors
527478 }
528479
529480 } ,
530481
531482 NoneType : function (
532- command : ControlItem , //es.Literal
483+ command : ControlItem ,
533484 context : Context ,
534485 control : Control ,
535486 stash : Stash
@@ -538,7 +489,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
538489 } ,
539490
540491 ConditionalExpression : function (
541- command : ControlItem , //es.ConditionalExpression,
492+ command : ControlItem ,
542493 context : Context ,
543494 control : Control ,
544495 stash : Stash
@@ -547,36 +498,21 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
547498 } ,
548499
549500 Identifier : function (
550- command : ControlItem , //es.Identifier,
501+ command : ControlItem ,
551502 context : Context ,
552503 control : Control ,
553504 stash : Stash
554505 ) {
555506 if ( builtInConstants . has ( ( command as es . Identifier ) . name ) ) {
556507 const builtinCons = builtInConstants . get ( ( command as es . Identifier ) . name ) ! ;
557- //try {
558508 stash . push ( builtinCons ) ;
559- // return;
560- // } catch (error) {
561- // // Error
562- // if (error instanceof Error) {
563- // throw new Error(error.message);
564- // } else {
565- // throw new Error();
566- // }
567- // // if (error instanceof RuntimeSourceError) {
568- // // throw error;
569- // // } else {
570- // // throw new RuntimeSourceError(`Error in builtin function ${funcName}: ${error}`);
571- // // }
572- // }
573509 } else {
574510 stash . push ( getVariable ( context , ( command as es . Identifier ) . name , ( command as es . Identifier ) ) ) ;
575511 }
576512 } ,
577513
578514 UnaryExpression : function (
579- command : ControlItem , //es.UnaryExpression,
515+ command : ControlItem ,
580516 context : Context ,
581517 control : Control
582518 ) {
@@ -585,19 +521,17 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
585521 } ,
586522
587523 BinaryExpression : function (
588- command : ControlItem , //es.BinaryExpression,
524+ command : ControlItem ,
589525 context : Context ,
590526 control : Control
591527 ) {
592- // currently for if statement
593-
594528 control . push ( instr . binOpInstr ( ( command as es . BinaryExpression ) . operator , command as es . Node ) ) ;
595529 control . push ( ( command as es . BinaryExpression ) . right ) ;
596530 control . push ( ( command as es . BinaryExpression ) . left ) ;
597531 } ,
598532
599533 LogicalExpression : function (
600- command : ControlItem , //es.LogicalExpression,
534+ command : ControlItem ,
601535 context : Context ,
602536 control : Control
603537 ) {
@@ -613,7 +547,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
613547 } ,
614548
615549 ArrowFunctionExpression : function (
616- command : ControlItem , //es.ArrowFunctionExpression,
550+ command : ControlItem ,
617551 context : Context ,
618552 control : Control ,
619553 stash : Stash ,
@@ -630,7 +564,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
630564 } ,
631565
632566 CallExpression : function (
633- command : ControlItem , //es.CallExpression,
567+ command : ControlItem ,
634568 context : Context ,
635569 control : Control
636570 ) {
@@ -658,7 +592,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
658592 // * Instructions
659593 // */
660594 [ InstrType . RESET ] : function (
661- command : ControlItem , //Instr,
595+ command : ControlItem ,
662596 context : Context ,
663597 control : Control ,
664598 stash : Stash
@@ -670,7 +604,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
670604 } ,
671605
672606 [ InstrType . ASSIGNMENT ] : function (
673- command : ControlItem , //AssmtInstr,
607+ command : ControlItem ,
674608 context : Context ,
675609 control : Control ,
676610 stash : Stash
@@ -696,7 +630,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
696630 } ,
697631
698632 [ InstrType . UNARY_OP ] : function (
699- command : ControlItem , //UnOpInstr,
633+ command : ControlItem ,
700634 context : Context ,
701635 control : Control ,
702636 stash : Stash
@@ -706,7 +640,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
706640 } ,
707641
708642 [ InstrType . BINARY_OP ] : function (
709- command : ControlItem , //BinOpInstr,
643+ command : ControlItem ,
710644 context : Context ,
711645 control : Control ,
712646 stash : Stash
@@ -726,7 +660,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
726660 } ,
727661
728662 [ InstrType . POP ] : function (
729- command : ControlItem , //Instr,
663+ command : ControlItem ,
730664 context : Context ,
731665 control : Control ,
732666 stash : Stash
@@ -735,7 +669,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
735669 } ,
736670
737671 [ InstrType . APPLICATION ] : function (
738- command : ControlItem , //AppInstr,
672+ command : ControlItem ,
739673 context : Context ,
740674 control : Control ,
741675 stash : Stash
@@ -749,13 +683,9 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
749683 const func : Closure = stash . pop ( ) ;
750684
751685 if ( ! ( func instanceof Closure ) ) {
752- //error
753- //handleRuntimeError(context, new errors.CallingNonFunctionValue(func, command.srcNode))
686+ // handleRuntimeError(context, new errors.CallingNonFunctionValue(func, command.srcNode))
754687 }
755688
756- // continuation in python?
757-
758- // func instanceof Closure
759689 if ( func instanceof Closure ) {
760690 // Check for number of arguments mismatch error
761691 checkNumberOfArguments ( source , command , context , func , args , ( command as AppInstr ) . srcNode )
@@ -791,11 +721,9 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
791721 } else {
792722 if ( control . peek ( ) ) {
793723 // push marker if control not empty
794- control . push ( instr . markerInstr ( ( command as AppInstr ) . srcNode ) )
724+ control . push ( instr . markerInstr ( ( command as AppInstr ) . srcNode ) ) ;
795725 }
796- control . push ( ( func as Closure ) . node . body )
797-
798- // console.info((func as Closure).node.body);
726+ control . push ( ( func as Closure ) . node . body ) ;
799727 }
800728
801729 return
@@ -807,22 +735,13 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
807735 if ( builtIns . has ( function_name ) ) {
808736 const builtinFunc = builtIns . get ( function_name ) ! ;
809737
810- //try {
811738 stash . push ( builtinFunc ( args , source , command , context ) ) ;
812739 return ;
813- // } catch (error) {
814- // // Error
815- // if (error instanceof Error) {
816- // throw new Error(error.message);
817- // } else {
818- // throw new Error();
819- // }
820- // }
821740 }
822741 } ,
823742
824743 [ InstrType . BRANCH ] : function (
825- command : ControlItem , //BranchInstr,
744+ command : ControlItem ,
826745 context : Context ,
827746 control : Control ,
828747 stash : Stash
@@ -847,7 +766,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
847766 } ,
848767
849768 [ InstrType . ENVIRONMENT ] : function (
850- command : ControlItem , //EnvInstr,
769+ command : ControlItem ,
851770 context : Context
852771 ) {
853772 while ( currentEnvironment ( context ) . id !== ( command as EnvInstr ) . env . id ) {
0 commit comments