Skip to content

Commit

Permalink
Merge pull request #5 from AlexandraGrass/code_gen
Browse files Browse the repository at this point in the history
Code gen for halt statement
  • Loading branch information
raypinto authored Jul 29, 2019
2 parents b5d32c6 + e546fa4 commit 00efb95
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
14 changes: 11 additions & 3 deletions code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
varsCreatedInsideFunction = []
otherType = ["add", "sub", "mul", "div", "if", "asgn", "let", "fun", "if","app"] #all tags for type 'Other'
opType = ["add", "sub", "mul", "div"] #binary operators
op2 = ["le", "lt", "ge", "gt", "eq"] #Comparision operators
op2 = ["le", "lt", "ge", "gt", "eq", "neq"] #Comparision operators
localAssignmentCount = 0
generatedCode = "Output:"

Expand All @@ -21,9 +21,12 @@ def parse_syntaxTree(tree): #Assume the program starts with only let, if and let
elif(tree.tag == "let"):
code_gen_for_let(tree.children)
generatedCode = generatedCode + '\n' + str(sd) + " slide " + str(let_count)
generatedCode = generatedCode + '\n' + str(sd - let_count) + " halt"
elif(tree.tag == "rec"):
code_gen_for_rec(tree.children)

elif(tree.tag in opType):
code_gen_for_op(tree.children, tree.tag)
generatedCode = generatedCode + '\n' + str(sd - let_count) + " halt"
return generatedCode

###code_generation generic call
Expand Down Expand Up @@ -122,23 +125,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 Expand Up @@ -230,6 +237,7 @@ def code_gen_for_rec(children):
sd-=1
code_generation(children[1])
generatedCode = generatedCode + '\n' + str(sd) + " slide " + str(n)
generatedCode = generatedCode + '\n' + str(sd - n) + " halt"

#find the value of n for rec definitions
def allocateLocalVars(children):
Expand Down
29 changes: 15 additions & 14 deletions output/slide_102.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ A:
2 mkbasic
2 jump D
C:
2 pushloc 2
3 getbasic
3 mark E
6 pushloc 6
7 getbasic
7 loadc 1
8 sub
7 mkbasic
7 pushglob 0
8 apply
1 pushloc 1
2 getbasic
2 mark E
5 pushloc 5
6 getbasic
6 loadc 1
7 sub
6 mkbasic
6 pushglob 0
7 apply
E:
4 mul
3 mkbasic
3 mul
2 mkbasic
D:
3 return 1
2 return 1
B:
2 rewrite 1
1 mark F
Expand All @@ -38,4 +38,5 @@ B:
5 pushloc 4
6 apply
F:
2 slide 1
2 slide 1
1 halt
3 changes: 2 additions & 1 deletion output/slide_114.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ B:
6 pushloc 4
7 apply
C:
3 slide 3
3 slide 3
0 halt
3 changes: 2 additions & 1 deletion output/slide_131.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ Output:
4 getbasic
4 add
3 mkbasic
3 slide 2
3 slide 2
1 halt
3 changes: 2 additions & 1 deletion output/slide_140.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ B:
6 pushloc 4
7 apply
C:
3 slide 2
3 slide 2
1 halt
29 changes: 15 additions & 14 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 All @@ -39,4 +39,5 @@ B:
5 pushloc 4
6 apply
F:
2 slide 1
2 slide 1
1 halt

0 comments on commit 00efb95

Please sign in to comment.