From 8181c8d94e8f21bd2349236dc1299d60f34a15d8 Mon Sep 17 00:00:00 2001 From: Raymond Pinto Date: Sun, 28 Jul 2019 18:05:26 +0200 Subject: [PATCH] Fixed the if else jump sd issues --- code_generator.py | 6 +++++- output/slide_159.txt | 26 +++++++++++++------------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/code_generator.py b/code_generator.py index e4f2870..217a6f7 100644 --- a/code_generator.py +++ b/code_generator.py @@ -122,9 +122,10 @@ 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 @@ -132,13 +133,16 @@ def code_gen_for_if(children): 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): diff --git a/output/slide_159.txt b/output/slide_159.txt index 795bbc2..2d5e3bf 100644 --- a/output/slide_159.txt +++ b/output/slide_159.txt @@ -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