Skip to content

Commit

Permalink
CSE Machine: Fixed bug in for loops (#1734)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
chuckyang123 and alsonleej authored Feb 18, 2025
1 parent 8ead9f2 commit eaa71ce
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,6 @@
}
]
]
}
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ exports[`Avoid unnescessary environment instruction 265`] = `5`;

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

exports[`Avoid unnescessary environment instruction 267`] = `4`;
exports[`Avoid unnescessary environment instruction 267`] = `5`;

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

Expand Down
9 changes: 3 additions & 6 deletions src/cse-machine/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,9 +499,8 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
}

if (command.body.length == 1) {
// If program only consists of one statement, evaluate it immediately
const next = command.body[0]
cmdEvaluators[next.type](next, context, control, stash, isPrelude)
// If program only consists of one statement, unwrap outer block
control.push(...handleSequence(command.body))
} else {
// Push block body as statement sequence
const seq: StatementSequence = ast.statementSequence(command.body, command.loc)
Expand Down Expand Up @@ -584,14 +583,12 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
// Refer to Source §3 specifications https://docs.sourceacademy.org/source_3.pdf
if (init.type === 'VariableDeclaration' && init.kind === 'let') {
const id = init.declarations[0].id as es.Identifier
const valueExpression = init.declarations[0].init!

control.push(
ast.blockStatement(
[
init,
ast.forStatement(
ast.assignmentExpression(id, valueExpression, command.loc),
ast.assignmentExpression(id, ast.identifier(id.name, command.loc), command.loc),
test,
update,
ast.blockStatement(
Expand Down

0 comments on commit eaa71ce

Please sign in to comment.