@@ -34,8 +34,8 @@ def untag(self, v, expected_tag, ast):
3434 match v :
3535 case Tagged (val , tag ):
3636 if tag != expected_tag :
37- raise Exception ('expected tag ' + expected_tag \
38- + ', not ' + ' ' + repr (v ))
37+ raise TrappedError ('expected tag ' + expected_tag \
38+ + ', not ' + ' ' + repr (v ))
3939 return val
4040 case _:
4141 raise Exception ('expected Tagged value with ' + expected_tag \
@@ -123,34 +123,34 @@ def interp_exp(self, e, env):
123123 case _:
124124 return super ().interp_exp (e , env )
125125
126- def interp_stmts (self , ss , env ):
127- if len (ss ) == 0 :
128- return
129- match ss [0 ]:
126+ def interp_stmt (self , s , env , cont ):
127+ match s :
130128
131129 # Lif statements
132130 case If (test , body , orelse ):
133131 v = self .interp_exp (test , env )
134- match self .untag (v , 'bool' , ss [ 0 ] ):
132+ match self .untag (v , 'bool' , s ):
135133 case True :
136- return self .interp_stmts (body + ss [ 1 :] , env )
134+ return self .interp_stmts (body + cont , env )
137135 case False :
138- return self .interp_stmts (orelse + ss [ 1 :] , env )
136+ return self .interp_stmts (orelse + cont , env )
139137
140138 # Lwhile statements
141139 case While (test , body , []):
142- while self .untag (self .interp_exp (test , env ), 'bool' , ss [0 ]):
143- self .interp_stmts (body , env )
144- return self .interp_stmts (ss [1 :], env )
140+ v = self .interp_exp (test , env )
141+ if self .untag (v , 'bool' , test ):
142+ self .interp_stmts (body + [s ] + cont , env )
143+ else :
144+ return self .interp_stmts (cont , env )
145145
146146 # Ltup statements
147147 case Assign ([Subscript (tup , index )], value ):
148148 tup = self .interp_exp (tup , env )
149149 index = self .interp_exp (index , env )
150- tup_v = self .untag (tup , 'tuple' , ss [ 0 ] )
151- index_v = self .untag (index , 'int' , ss [ 0 ] )
150+ tup_v = self .untag (tup , 'tuple' , s )
151+ index_v = self .untag (index , 'int' , s )
152152 tup_v [index_v ] = self .interp_exp (value , env )
153- return self .interp_stmts (ss [ 1 :] , env )
153+ return self .interp_stmts (cont , env )
154154
155155 # override to tag the function
156156 case FunctionDef (name , params , bod , dl , returns , comment ):
@@ -159,8 +159,8 @@ def interp_stmts(self, ss, env):
159159 else :
160160 ps = [x for (x ,t ) in params ]
161161 env [name ] = self .tag (Function (name , ps , bod , env ))
162- return self .interp_stmts (ss [ 1 :] , env )
162+ return self .interp_stmts (cont , env )
163163
164164 case _:
165- return super ().interp_stmts ( ss , env )
165+ return super ().interp_stmt ( s , env , cont )
166166
0 commit comments