Skip to content

Commit

Permalink
Added the halt statement
Browse files Browse the repository at this point in the history
  • Loading branch information
raypinto committed Jul 29, 2019
1 parent 8181c8d commit e546fa4
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 20 deletions.
8 changes: 6 additions & 2 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 @@ -234,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
3 changes: 2 additions & 1 deletion output/slide_159.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ B:
5 pushloc 4
6 apply
F:
2 slide 1
2 slide 1
1 halt

0 comments on commit e546fa4

Please sign in to comment.