Skip to content

Commit ae36bb9

Browse files
committed
Migrated environment, closure && added functionDef, Lambda (Refs #51)[12]
* New Files: py_closure.ts * Added necessary parts for functionDef and Lambda such as explicit and implicit return, reset
1 parent 37944cb commit ae36bb9

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/cse-machine/py_interpreter.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,19 @@ const pyCmdEvaluators: { [type: string]: CmdEvaluator } = {
355355
pyDefineVariable(context, functionDefNode.name.lexeme, closure);
356356
},
357357

358+
'Lambda': (code, command, context, control, stash, isPrelude) => {
359+
const lambdaNode = command as ExprNS.Lambda;
360+
361+
//create closure, capturing current environment
362+
const closure = PyClosure.makeFromLambda(
363+
lambdaNode,
364+
currentEnvironment(context),
365+
context
366+
);
367+
// lambda is expression, just push value onto stash
368+
stash.push(closure);
369+
},
370+
358371
/**
359372
* Only handles explicit return for now
360373
* To handle implicit return None next
@@ -463,7 +476,11 @@ const pyCmdEvaluators: { [type: string]: CmdEvaluator } = {
463476

464477
// push reset and implicit return for cleanup at end of function
465478
control.push(instrCreator.resetInstr(instr.srcNode));
466-
control.push(instrCreator.endOfFunctionBodyInstr(instr.srcNode));
479+
480+
// Only push endOfFunctionBodyInstr for functionDef
481+
if (closure.node.constructor.name === 'FunctionDef') {
482+
control.push(instrCreator.endOfFunctionBodyInstr(instr.srcNode));
483+
}
467484

468485
// create new function environment
469486
const newEnv = createEnvironment(context, closure, args, instr.srcNode as ExprNS.Call);

0 commit comments

Comments
 (0)