-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Interpreter: Support recursive calls
It turns out python scoping is a bit weird. Top level functions are added to the `globals()` table. Function calls to self or a parent function which is not on the top level are passed as local variables. Here is an example: ```python print("s00 " + str(globals())) print("s00 " + str(locals())) s00 = 0 def scope_01(con): s01 = 1 print("s01" + str(globals())) print("s01 " + str(locals())) def scope_02(con): s02 = 2 print("s02" + str(globals())) print("s02 " + str(locals())) def scope_03(): s03 = 3 print("s03" + str(globals())) print("s03 " + str(locals())) scope_01(False) scope_02(False) if con: scope_03() if con: scope_02(True) scope_01(True) ``` This commit adds a hack that simulates the global namespace. This at least allows simple recursion.
- Loading branch information
Showing
8 changed files
with
113 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# For now separate | ||
def insert(node, item): | ||
if node.next == None: | ||
node.next = {} | ||
node.next.data = item | ||
node.next.prev = node | ||
else: | ||
insert(node.next, item) | ||
|
||
def contains(node, item): | ||
if node.data == item: | ||
return True | ||
if node.next == None: | ||
return False | ||
return find(node.next, item) | ||
|
||
def remove(node, item): | ||
if node.data == item: | ||
node.prev.next = node.next | ||
if node.next != None: | ||
node.next.prev = node.prev | ||
return True | ||
if node.next == None: | ||
return False | ||
return remove(node.next, item) | ||
|
||
def new_list(): | ||
list = {} | ||
list.head = {} | ||
return list | ||
|
||
list = new_list() | ||
|
||
# Required to not leak memory | ||
region list | ||
|
||
value = "x" | ||
insert(list.head, {}) | ||
insert(list.head, {}) | ||
insert(list.head, value) | ||
insert(list.head, {}) | ||
remove(list.head, value) | ||
|
||
drop list |