Skip to content

Commit eaa71ce

Browse files
CSE Machine: Fixed bug in for loops (#1734)
* Fixed bug in for loops * comment removed * Fix bugs in for loops and block exec * Fix bugs in for loops and block exec * Fix bugs in for loops and block exec --------- Co-authored-by: alsonleej <alsonleej@gmail.com>
1 parent 8ead9f2 commit eaa71ce

File tree

3 files changed

+6
-8
lines changed

3 files changed

+6
-8
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,5 +130,6 @@
130130
}
131131
]
132132
]
133-
}
133+
},
134+
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
134135
}

src/cse-machine/__tests__/__snapshots__/cse-machine-runtime-context.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ exports[`Avoid unnescessary environment instruction 265`] = `5`;
532532

533533
exports[`Avoid unnescessary environment instruction 266`] = `4`;
534534

535-
exports[`Avoid unnescessary environment instruction 267`] = `4`;
535+
exports[`Avoid unnescessary environment instruction 267`] = `5`;
536536

537537
exports[`Avoid unnescessary environment instruction 268`] = `4`;
538538

src/cse-machine/interpreter.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,8 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
499499
}
500500

501501
if (command.body.length == 1) {
502-
// If program only consists of one statement, evaluate it immediately
503-
const next = command.body[0]
504-
cmdEvaluators[next.type](next, context, control, stash, isPrelude)
502+
// If program only consists of one statement, unwrap outer block
503+
control.push(...handleSequence(command.body))
505504
} else {
506505
// Push block body as statement sequence
507506
const seq: StatementSequence = ast.statementSequence(command.body, command.loc)
@@ -584,14 +583,12 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
584583
// Refer to Source §3 specifications https://docs.sourceacademy.org/source_3.pdf
585584
if (init.type === 'VariableDeclaration' && init.kind === 'let') {
586585
const id = init.declarations[0].id as es.Identifier
587-
const valueExpression = init.declarations[0].init!
588-
589586
control.push(
590587
ast.blockStatement(
591588
[
592589
init,
593590
ast.forStatement(
594-
ast.assignmentExpression(id, valueExpression, command.loc),
591+
ast.assignmentExpression(id, ast.identifier(id.name, command.loc), command.loc),
595592
test,
596593
update,
597594
ast.blockStatement(

0 commit comments

Comments
 (0)