huajun07 / codesketcher

Visualise your code in action!
https://main.d1fr5et3wgts3j.amplifyapp.com/
MIT License
0 stars 0 forks source link

Fix/global variables not found #51

Closed limanjun99 closed 1 year ago

limanjun99 commented 1 year ago

This PR fixes the bug in this issue.

Previously, we were running the code with debugger.run(code, globals={}, locals={}). We should actually have been doing this: debugger.run(code, globals=empty_dict, locals=empty_dict), where empty_dict both points to the same dictionary. This is because at the module level, globals are equal to locals.

Tested by adding the following testcase:

def f1():
  a = 1
def f2():
  f1()
f2()

Other problematic code has also been tested locally in the frontend, such as this Floyd Warshall code where nV cannot be accessed:

# Floyd Warshall Algorithm in python
# The number of vertices
nV = 4

INF = 999

# Algorithm implementation
def floyd_warshall(G):
    distance = list(map(lambda i: list(map(lambda j: j, i)), G))

    # Adding vertices individually
    for k in range(nV):
        for i in range(nV):
            for j in range(nV):
                distance[i][j] = min(distance[i][j], distance[i][k] + distance[k][j])

G = [[0, 3, INF, 5],
         [2, 0, INF, 4],
         [INF, 1, 0, INF],
         [INF, INF, 2, 0]]
floyd_warshall(G)