Skip to content

Commit

Permalink
Fixes issue 363
Browse files Browse the repository at this point in the history
Fist step of fix for nested block, our insert blocks algorithm was not entirely correct: jumping back two indents was not properly handled
  • Loading branch information
Felienne committed May 7, 2021
1 parent fb87d9e commit 9863a51
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 52 deletions.
20 changes: 17 additions & 3 deletions hedy.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,15 +947,28 @@ def preprocess_blocks(code):
lines = code.split("\n")
current_indent = 0
previous_block = 0
indent_size = None #we don't fix indent size but the first encounter sets it
for line in lines:
leading_spaces = find_indent_length(line)

#set indent size for program
if indent_size == None and leading_spaces > 0:
indent_size = leading_spaces

if leading_spaces > previous_block:
current_indent += 1
previous_block = leading_spaces
elif leading_spaces < previous_block:
#we springen 'terug' dus er moet een block in!
processed_code.append('end-block')
current_indent -=1
# we springen 'terug' dus er moeten end-blocken in
# bij meerdere terugsprongen sluiten we ook meerdere blokken

difference = (previous_block - leading_spaces)

number_of_indents = difference // indent_size
for i in range(number_of_indents):
processed_code.append('end-block')

current_indent = leading_spaces // indent_size
previous_block = leading_spaces

#if indent remains the same, do nothing, just add line
Expand All @@ -975,6 +988,7 @@ def transpile_inner(input_string, level):

if level >= 8:
input_string = preprocess_blocks(input_string)
print(input_string)

try:
program_root = parser.parse(input_string+ '\n').children[0] # getting rid of the root could also be done in the transformer would be nicer
Expand Down
43 changes: 21 additions & 22 deletions tests/tests_level_08.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ def test_if_under_else_in_for(self):
for i in range 0 to 10
antwoord is ask Wat is 5*5
if antwoord is 24
print 'Dat is fout!'
print 'Dat is fout!'
else
print 'Dat is goed!'
print 'Dat is goed!'
if antwoord is 25
i is 10""")
i is 10""")

expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
Expand All @@ -180,27 +180,26 @@ def test_if_under_else_in_for(self):
result = hedy.transpile(code, 8)

self.assertEqual(expected, result)
# fails, bugreport
# def test_for_ifbug(self):
# code = textwrap.dedent("""\
# for i in range 0 to 10
# antwoord is ask Wat is 5*5
# if antwoord is 24
# print 'fout'
# print 'klaar met for loop'""")
#
# expected = textwrap.dedent("""\
# for i in range(int(0), int(10)+1):
# antwoord = input('Wat is 5*5')
# if str(antwoord) == str('24'):
# print('fout')
# print('klaar met for loop')""")
#
# result = hedy.transpile(code, 8)
#
# self.assertEqual(expected, result)

#fails, issue 363
def test_for_ifbug(self):
code = textwrap.dedent("""\
for i in range 0 to 10
antwoord is ask Wat is 5*5
if antwoord is 24
print 'fout'
print 'klaar met for loop'""")

expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
antwoord = input('Wat is 5*5')
if str(antwoord) == str('24'):
print('fout')
print('klaar met for loop')""")

result = hedy.transpile(code, 8)

self.assertEqual(expected, result)


#programs with issues to see if we catch them properly
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_level_09.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ def test_if_under_else_in_for(self):
for i in range 0 to 10:
antwoord is ask Wat is 5*5
if antwoord is 24:
print 'Dat is fout!'
print 'Dat is fout!'
else:
print 'Dat is goed!'
print 'Dat is goed!'
if antwoord is 25:
i is 10""")
i is 10""")

expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_level_10.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,11 @@ def test_if_under_else_in_for(self):
for i in range 0 to 10:
antwoord is ask Wat is 5*5
if antwoord is 24:
print 'Dat is fout!'
print 'Dat is fout!'
else:
print 'Dat is goed!'
print 'Dat is goed!'
if antwoord is 25:
i is 10""")
i is 10""")

expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_level_11.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,11 @@ def test_if_under_else_in_for(self):
for i in range(0, 10):
antwoord is input('Wat is 5*5')
if antwoord is 24:
print('Dat is fout!')
print('Dat is fout!')
else:
print('Dat is goed!')
print('Dat is goed!')
if antwoord is 25:
i is 10""")
i is 10""")

expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_level_12.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ def test_if_under_else_in_for(self):
for i in range(0, 10):
antwoord is input('Wat is 5*5')
if antwoord is 24:
print('Dat is fout!')
print('Dat is fout!')
else:
print('Dat is goed!')
print('Dat is goed!')
if antwoord is 25:
i is 10""")
i is 10""")

expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_level_13.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ def test_if_under_else_in_for(self):
for i in range(0, 10):
antwoord is input('Wat is 5*5')
if antwoord is 24:
print('Dat is fout!')
print('Dat is fout!')
else:
print('Dat is goed!')
print('Dat is goed!')
if antwoord is 25:
i is 10""")
i is 10""")

expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_level_14.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ def test_if_under_else_in_for(self):
for i in range(0, 10):
antwoord is input('Wat is 5*5')
if antwoord is 24:
print('Dat is fout!')
print('Dat is fout!')
else:
print('Dat is goed!')
print('Dat is goed!')
if antwoord is 25:
i is 10""")
i is 10""")

expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_level_15.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ def test_if_under_else_in_for(self):
for i in range(0, 10):
antwoord is input('Wat is 5*5')
if antwoord is 24:
print('Dat is fout!')
print('Dat is fout!')
else:
print('Dat is goed!')
print('Dat is goed!')
if antwoord is 25:
i is 10""")
i is 10""")

expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_level_16.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ def test_if_under_else_in_for(self):
for i in range(0, 10):
antwoord is input('Wat is 5*5')
if antwoord is 24:
print('Dat is fout!')
print('Dat is fout!')
else:
print('Dat is goed!')
print('Dat is goed!')
if antwoord is 25:
i is 10""")
i is 10""")

expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
Expand Down
6 changes: 3 additions & 3 deletions tests/tests_level_17.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ def test_if_under_else_in_for(self):
for i in range(0, 10):
antwoord is input('Wat is 5*5')
if antwoord is 24:
print('Dat is fout!')
print('Dat is fout!')
else:
print('Dat is goed!')
print('Dat is goed!')
if antwoord is 25:
i is 10""")
i is 10""")

expected = textwrap.dedent("""\
for i in range(int(0), int(10)+1):
Expand Down

0 comments on commit 9863a51

Please sign in to comment.