Skip to content

Commit

Permalink
CSE Machine: Fix display bug when scanning out declarations (#1527)
Browse files Browse the repository at this point in the history
* Add declaration type to symbol's description

* Revert "CSE Machine: Rename to CS1101S terms (#1514)"

This reverts commit d6553ca.

* Remove some outdated lines

* Modification while running tests

* Remove package-lock.json

The project uses Yarn.

* Remove erroneous dependency changes

* Revert back to Yarn registry in lockfile

* Revert "Revert "CSE Machine: Rename to CS1101S terms (#1514)""

This reverts commit 77d8c4b.

* Pull updated test cases from master branch

* bumping version

---------

Co-authored-by: Richard Dominick <[email protected]>
Co-authored-by: henz <[email protected]>
  • Loading branch information
3 people authored Feb 5, 2024
1 parent df47748 commit ddeb915
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "js-slang",
"version": "1.0.38",
"version": "1.0.39",
"license": "Apache-2.0",
"description": "Javascript-based implementations of Source, written in Typescript",
"keywords": [
Expand Down
1 change: 0 additions & 1 deletion src/cse-machine/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = {
const environment = createBlockEnvironment(context, 'blockEnvironment')
declareFunctionsAndVariables(context, command, environment)
pushEnvironment(context, environment)

// Push block body
control.push(...handleSequence(command.body))
},
Expand Down
33 changes: 25 additions & 8 deletions src/cse-machine/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,15 @@ export const createBlockEnvironment = (
* Variables
*/

const DECLARED_BUT_NOT_YET_ASSIGNED = Symbol('Used to implement block scope')
const UNASSIGNED_CONST = Symbol('const declaration')
const UNASSIGNED_LET = Symbol('let declaration')

export function declareIdentifier(
context: Context,
name: string,
node: es.Node,
environment: Environment
environment: Environment,
constant: boolean = false
) {
if (environment.head.hasOwnProperty(name)) {
const descriptors = Object.getOwnPropertyDescriptors(environment.head)
Expand All @@ -315,7 +317,7 @@ export function declareIdentifier(
new errors.VariableRedeclaration(node, name, descriptors[name].writable)
)
}
environment.head[name] = DECLARED_BUT_NOT_YET_ASSIGNED
environment.head[name] = constant ? UNASSIGNED_CONST : UNASSIGNED_LET
return environment
}

Expand All @@ -325,7 +327,9 @@ function declareVariables(
environment: Environment
) {
for (const declaration of node.declarations) {
declareIdentifier(context, (declaration.id as es.Identifier).name, node, environment)
// Retrieve declaration type from node
const constant = node.kind === 'const'
declareIdentifier(context, (declaration.id as es.Identifier).name, node, environment, constant)
}
}

Expand All @@ -340,7 +344,14 @@ export function declareFunctionsAndVariables(
declareVariables(context, statement, environment)
break
case 'FunctionDeclaration':
declareIdentifier(context, (statement.id as es.Identifier).name, statement, environment)
// FunctionDeclaration is always of type constant
declareIdentifier(
context,
(statement.id as es.Identifier).name,
statement,
environment,
true
)
break
}
}
Expand Down Expand Up @@ -377,7 +388,7 @@ export function defineVariable(
) {
const environment = currentEnvironment(context)

if (environment.head[name] !== DECLARED_BUT_NOT_YET_ASSIGNED) {
if (environment.head[name] !== UNASSIGNED_CONST && environment.head[name] !== UNASSIGNED_LET) {
return handleRuntimeError(context, new errors.VariableRedeclaration(node, name, !constant))
}

Expand All @@ -394,7 +405,10 @@ export const getVariable = (context: Context, name: string, node: es.Identifier)
let environment: Environment | null = currentEnvironment(context)
while (environment) {
if (environment.head.hasOwnProperty(name)) {
if (environment.head[name] === DECLARED_BUT_NOT_YET_ASSIGNED) {
if (
environment.head[name] === UNASSIGNED_CONST ||
environment.head[name] === UNASSIGNED_LET
) {
return handleRuntimeError(context, new errors.UnassignedVariable(name, node))
} else {
return environment.head[name]
Expand All @@ -415,7 +429,10 @@ export const setVariable = (
let environment: Environment | null = currentEnvironment(context)
while (environment) {
if (environment.head.hasOwnProperty(name)) {
if (environment.head[name] === DECLARED_BUT_NOT_YET_ASSIGNED) {
if (
environment.head[name] === UNASSIGNED_CONST ||
environment.head[name] === UNASSIGNED_LET
) {
break
}
const descriptors = Object.getOwnPropertyDescriptors(environment.head)
Expand Down

0 comments on commit ddeb915

Please sign in to comment.