mirror of
https://github.com/leporello-js/leporello-js
synced 2026-01-13 21:14:28 -08:00
fix let var assignments
This commit is contained in:
17
src/eval.js
17
src/eval.js
@@ -833,17 +833,14 @@ const apply_assignments = (node, assignments) => {
|
|||||||
.flat()
|
.flat()
|
||||||
.map(c => c.index)
|
.map(c => c.index)
|
||||||
|
|
||||||
// Scope we return to parent block
|
const parent_assignments = Object.entries(assignments).filter(([index, v]) =>
|
||||||
const scope = Object.fromEntries(
|
|
||||||
Object
|
|
||||||
.entries(assignments)
|
|
||||||
.filter(([index, v]) =>
|
|
||||||
let_ids.find(i => i.toString() == index) == null
|
let_ids.find(i => i.toString() == index) == null
|
||||||
)
|
)
|
||||||
.map(([k, {name, value}]) => [name, value])
|
// Scope we return to parent block
|
||||||
|
const scope = Object.fromEntries(
|
||||||
|
parent_assignments.map(([k, {name, value}]) => [name, value])
|
||||||
)
|
)
|
||||||
|
return {node, scope, assignments: Object.fromEntries(parent_assignments)}
|
||||||
return {node, scope}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const eval_decl_pair = (s, scope, calls, context, is_assignment) => {
|
const eval_decl_pair = (s, scope, calls, context, is_assignment) => {
|
||||||
@@ -993,14 +990,14 @@ const eval_statement = (s, scope, calls, context) => {
|
|||||||
},
|
},
|
||||||
{ok: true, returned: false, children: [], scope: initial_scope, calls, assignments: {}}
|
{ok: true, returned: false, children: [], scope: initial_scope, calls, assignments: {}}
|
||||||
)
|
)
|
||||||
const {node: next_node, scope: next_scope} =
|
const {node: next_node, scope: next_scope, assignments: parent_assignments} =
|
||||||
apply_assignments({...s, children: children, result: {ok}}, assignments)
|
apply_assignments({...s, children: children, result: {ok}}, assignments)
|
||||||
return {
|
return {
|
||||||
ok,
|
ok,
|
||||||
node: next_node,
|
node: next_node,
|
||||||
scope: {...scope, ...next_scope},
|
scope: {...scope, ...next_scope},
|
||||||
returned,
|
returned,
|
||||||
assignments,
|
parent_assignments,
|
||||||
calls: next_calls,
|
calls: next_calls,
|
||||||
}
|
}
|
||||||
} else if(['let', 'const', 'assignment'].includes(s.type)) {
|
} else if(['let', 'const', 'assignment'].includes(s.type)) {
|
||||||
|
|||||||
19
test/test.js
19
test/test.js
@@ -1555,6 +1555,25 @@ export const tests = [
|
|||||||
)
|
)
|
||||||
}),
|
}),
|
||||||
|
|
||||||
|
test('block scoping shadow bug', () => {
|
||||||
|
assert_code_evals_to(
|
||||||
|
`
|
||||||
|
let y = 3
|
||||||
|
if(true) {
|
||||||
|
let y
|
||||||
|
y = 1
|
||||||
|
if(true) {
|
||||||
|
let y
|
||||||
|
y = 2
|
||||||
|
}
|
||||||
|
y
|
||||||
|
}
|
||||||
|
y
|
||||||
|
`,
|
||||||
|
3
|
||||||
|
)
|
||||||
|
}),
|
||||||
|
|
||||||
test('step_into', () => {
|
test('step_into', () => {
|
||||||
const code = `
|
const code = `
|
||||||
const x = () => 1;
|
const x = () => 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user