Skip to content

Commit

Permalink
Fixed the if else jump sd issues
Browse files Browse the repository at this point in the history
  • Loading branch information
raypinto committed Jul 28, 2019
1 parent b5d32c6 commit 8181c8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
6 changes: 5 additions & 1 deletion code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,27 @@ def code_gen_for_op(children, type): #The children of operators can be var arg o

###code_generation for if
def code_gen_for_if(children):
global jumpId, generatedCode
global jumpId, generatedCode, sd
code_generation(children[0], False, True)
elseJumpId = jumpId
elseSD = sd
generatedCode = generatedCode + '\n' + str(sd) + " jumpz " + chr(elseJumpId)
jumpId+= 1

#then part of if can be basic, var or arg type
code_generation(children[1], True, False)

postElseJumpId = jumpId
postElseSD = sd
generatedCode = generatedCode + '\n' + str(sd) + " jump " + chr(postElseJumpId)
generatedCode = generatedCode + '\n' + chr(elseJumpId) + ":"
sd = elseSD
jumpId+= 1

#else part of if can be either basic or application type
code_generation(children[2])
generatedCode = generatedCode + '\n' + chr(postElseJumpId) + ":"
sd = postElseSD

###code_generation for function
def code_gen_for_fun(children):
Expand Down
26 changes: 13 additions & 13 deletions output/slide_159.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ A:
1 pushloc 1
2 jump D
C:
2 mark E
5 pushloc 6
1 mark E
4 pushloc 5
5 getbasic
5 loadc 1
6 sub
5 mkbasic
5 pushloc 5
6 getbasic
6 loadc 1
7 sub
6 mkbasic
6 pushloc 6
6 pushloc 7
7 getbasic
7 pushloc 8
8 getbasic
8 mul
7 mkbasic
7 pushglob 0
8 apply
7 mul
6 mkbasic
6 pushglob 0
7 apply
E:
D:
3 return 2
2 return 2
B:
2 rewrite 1
1 mark F
Expand Down

0 comments on commit 8181c8d

Please sign in to comment.