Skip to content

Commit f4cfe6e

Browse files
author
allendowney
committed
Fixing Lumpy arrows
1 parent 2c79df3 commit f4cfe6e

5 files changed

Lines changed: 96 additions & 31 deletions

File tree

‎python2/Lumpy.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ def update(self):
891891
self.item.coords([self.key.pos(), self.val.pos()])
892892

893893

894-
class ParentArrow(Thing):
894+
class ParentArrow(Arrow):
895895
"""Represents an inheritance arrow.
896896
897897
Shows an is-a relationship between classes in a class diagram.
@@ -927,7 +927,7 @@ def draw(self, diag):
927927
canvas.lower(self.item)
928928

929929

930-
class ContainsArrow(Thing):
930+
class ContainsArrow(Arrow):
931931
"""Represents a contains arrow.
932932
933933
Shows a has-a relationship between classes in a class diagram.

‎python2/Makefile‎

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ CellWorld_test.py lumpy_example3.py Sync_test.py World.py\
2121
Gui.py Lumpy.py TurmiteWorld.py World_test.py\
2222
danger.gif words.txt TurtleWorld_test.py
2323

24-
24+
FLAGS = -wn -x import
2525

2626
python3:
2727
cp $(FILES) ../python3
2828
cp sync_code/*.py ../python3/sync_code
29-
cd ../python3; 2to3 -wn Gui.py
30-
cd ../python3; 2to3 -wn World.py
31-
cd ../python3; 2to3 -wn TurtleWorld.py
32-
cd ../python3; 2to3 -wn CellWorld.py
33-
cd ../python3; 2to3 -wn TurmiteWorld.py
34-
cd ../python3; 2to3 -wn AmoebaWorld.py
35-
cd ../python3; 2to3 -wn Lumpy.py
36-
cd ../python3; 2to3 -wn Sync.py
37-
38-
cd ../python3; 2to3 -wn Gui_test.py
39-
cd ../python3; 2to3 -wn World_test.py
40-
cd ../python3; 2to3 -wn TurtleWorld_test.py
41-
cd ../python3; 2to3 -wn CellWorld_test.py
42-
cd ../python3; 2to3 -wn TurmiteWorld_test.py
43-
cd ../python3; 2to3 -wn AmoebaWorld_test.py
44-
cd ../python3; 2to3 -wn Lumpy_test.py
45-
cd ../python3; 2to3 -wn Sync_test.py
29+
cd ../python3; 2to3 $(FLAGS) Gui.py
30+
cd ../python3; 2to3 $(FLAGS) World.py
31+
cd ../python3; 2to3 $(FLAGS) TurtleWorld.py
32+
cd ../python3; 2to3 $(FLAGS) CellWorld.py
33+
cd ../python3; 2to3 $(FLAGS) TurmiteWorld.py
34+
cd ../python3; 2to3 $(FLAGS) AmoebaWorld.py
35+
cd ../python3; 2to3 $(FLAGS) Lumpy.py
36+
cd ../python3; 2to3 $(FLAGS) Sync.py
37+
38+
cd ../python3; 2to3 $(FLAGS) Gui_test.py
39+
cd ../python3; 2to3 $(FLAGS) World_test.py
40+
cd ../python3; 2to3 $(FLAGS) TurtleWorld_test.py
41+
cd ../python3; 2to3 $(FLAGS) CellWorld_test.py
42+
cd ../python3; 2to3 $(FLAGS) TurmiteWorld_test.py
43+
cd ../python3; 2to3 $(FLAGS) AmoebaWorld_test.py
44+
cd ../python3; 2to3 $(FLAGS) Lumpy_test.py
45+
cd ../python3; 2to3 $(FLAGS) Sync_test.py
4646

4747
clean:
4848
rm *~ *.pyc

‎python2/Sync.py‎

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ def __init__(self, column, name=None):
530530
self.column = column
531531
self.sync = column.p
532532
self.name, self.color = self.sync.get_name(name)
533+
self.flag_map = {}
533534
self.sync.register(self)
534535
self.start()
535536

@@ -586,7 +587,17 @@ def skip_body(self):
586587
body_indent = self.count_spaces(source)
587588

588589
indent = body_indent - head_indent
589-
print indent
590+
591+
if indent <= 0:
592+
raise SyntaxError('Body of if statement must be indented.')
593+
594+
while 1:
595+
self.next_row()
596+
source = self.row.get()
597+
line_indent = self.count_spaces(source)
598+
if line_indent <= head_indent:
599+
break
600+
590601

591602
def count_spaces(self, source):
592603
"""Returns the number of leading spaces after expanding tabs."""
@@ -662,9 +673,12 @@ def exec_line(self, source, sync):
662673
exec code in sync.globals, sync.locals
663674
return True
664675
except SyntaxError as error:
665-
# this had better be an if statement
666-
flag = self.handle_conditional(source, sync)
667-
return flag
676+
# check whether it's a conditional statement
677+
if s.startswith('if') or s.startswith('else'):
678+
flag = self.handle_conditional(source, sync)
679+
return flag
680+
else:
681+
raise error
668682

669683
def handle_conditional(self, source, sync):
670684
"""Evaluates the condition part of an if statement.
@@ -678,14 +692,28 @@ def handle_conditional(self, source, sync):
678692
evaluating the condition; otherwise raises a SyntaxError
679693
"""
680694
s = source.strip()
681-
if not (s.startswith('if') and s.endswith(':')):
682-
raise SyntaxError('Python blocks must be if statements')
695+
if not s.endswith(':'):
696+
raise SyntaxError('Header must end with :')
697+
698+
if s.startswith('if'):
699+
# evaluate the condition
700+
condition = source[2:-1].strip()
701+
flag = eval(condition, sync.globals, sync.locals)
683702

684-
# pull out the condition
685-
condition = source[2:-1].strip()
686-
flag = eval(condition, sync.globals, sync.locals)
703+
# store the flag
704+
indent = self.count_spaces(source)
705+
self.flag_map[indent] = flag
687706

688-
return flag
707+
return flag
708+
709+
else:
710+
# see whether the condition was true
711+
indent = self.count_spaces(source)
712+
try:
713+
flag = self.flag_map[indent]
714+
return not flag
715+
except KeyError:
716+
raise SyntaxError('else does not match if')
689717

690718
def step_loop(self, event=None):
691719
self.step()

‎python2/Sync_test.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class Tests(unittest.TestCase):
1313

14-
def test_sync_mutex(self):
14+
def xtest_sync_mutex(self):
1515
sync = Sync.Sync('mutex.py')
1616

1717
threads = sync.get_threads()
@@ -56,6 +56,24 @@ def test_sync_conditional(self):
5656
source = threadA.step()
5757
self.assertEqual(source, 'if counter == 1:')
5858

59+
source = threadA.step()
60+
self.assertEqual(source, 'pass')
61+
62+
source = threadA.step()
63+
source = threadA.step()
64+
self.assertEqual(source, 'else:')
65+
66+
source = threadA.step()
67+
self.assertEqual(source, ' print False')
68+
69+
source = threadA.step()
70+
source = threadA.step()
71+
self.assertEqual(source, ' print True')
72+
73+
source = threadA.step()
74+
source = threadA.step()
75+
self.assertEqual(source, 'pass')
76+
5977

6078
if __name__ == '__main__':
6179
unittest.main()

‎python2/sync_code/conditional.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,22 @@
88

99
if counter == 1:
1010
print False
11+
pass
12+
13+
if counter == counter+1:
14+
pass
15+
pass
16+
else:
17+
print False
18+
19+
20+
if True:
21+
print True
22+
else:
23+
print False
24+
25+
pass
26+
27+
28+
29+

0 commit comments

Comments
 (0)