Yuyao Huang 49651168e3 If()
2024-04-20 02:01:26 +08:00

51 lines
1.5 KiB
Python

import ast
from .. import flags
from ..utils import to_source, APPEND_SOURCE_BY_THEMSELVES
ELIF = 2
PASS = 4
def If(self, cmtor, state=0):
if state & PASS:
test = False
test_comment = "skipped"
else:
test = cmtor.eval(self.test, format=False)
test_comment = test
if test:
state = state | PASS
if state & ELIF:
cmtor.append_source(f"elif {to_source(self.test)}: # {test_comment}")
else:
cmtor.append_source(f"if {to_source(self.test)}: # {test_comment}")
cmtor.indent += flags.INDENT
for stmt in self.body:
if type(stmt) not in APPEND_SOURCE_BY_THEMSELVES:
cmtor.append_source(to_source(stmt))
if test:
cmtor.process(stmt)
cmtor.append_source()
cmtor.indent -= flags.INDENT
if self.orelse:
if type(self.orelse[0]) == ast.If:
cmtor.process(self.orelse[0], state=state | ELIF)
else:
test = not (state & PASS)
test_comment = True if test else "skipped"
cmtor.append_source(f"else: # {test_comment}")
cmtor.indent += flags.INDENT
for stmt in self.orelse:
if type(stmt) not in APPEND_SOURCE_BY_THEMSELVES:
cmtor.append_source(to_source(stmt))
if test:
cmtor.process(stmt)
cmtor.append_source()
cmtor.indent -= flags.INDENT