Expose a binding leak across tests

Two integer interpreters support Num, Var, Add, and Let. Literals, lookups, and addition behave identically in both. Each Let(name, initializer, body) first evaluates its initializer in the incoming environment.


The reference interpreter then copies that environment, installs the binding in the copy, and evaluates the body in the copy. The flawed interpreter instead overwrites the binding directly in the incoming dictionary, evaluates the body there, and never restores the previous binding.


Each interpreter starts with its own dictionary containing x = 2. Run tests P, Q, and R in that order. Within each interpreter, reuse the same dictionary across all three tests.


Python
# P
Let("x", Num(5), Num(1))

# Q
Add(Var("x"), Num(4))

# R
Let("x", Add(Var("x"), Num(1)),
    Add(Var("x"), Num(2)))


Complete the table with the dimensionless integer returned by each test. Enter exact integers.

TestReference resultFlawed result
P
Q
R

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Programming Language Design and Construction Questions: