@@ -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 ()
0 commit comments