| 155 | |
| 156 | |
| 157 | class WhileGuard(BlockGuard): |
| 158 | def __init__(self, while_op): |
| 159 | if not isinstance(while_op, While): |
| 160 | raise TypeError("WhileGuard takes a while op") |
| 161 | if not in_pir_mode(): |
| 162 | super().__init__(while_op.helper.main_program) |
| 163 | self.while_op = while_op |
| 164 | |
| 165 | def __enter__(self): |
| 166 | if in_pir_mode(): |
| 167 | self.block = build_while_op(self.while_op.cond_var, []).body() |
| 168 | return self.block.__enter__() |
| 169 | self.while_op.status = While.IN_WHILE_BLOCK |
| 170 | return super().__enter__() |
| 171 | |
| 172 | def __exit__(self, exc_type, exc_val, exc_tb): |
| 173 | if in_pir_mode(): |
| 174 | cf_yield([self.while_op.cond_var]) |
| 175 | return self.block.__exit__(exc_type, exc_val, exc_tb) |
| 176 | if exc_type is not None: |
| 177 | return False |
| 178 | self.while_op.status = While.AFTER_WHILE_BLOCK |
| 179 | self.while_op._complete() |
| 180 | return super().__exit__(exc_type, exc_val, exc_tb) |
| 181 | |
| 182 | |
| 183 | class If: |