Index(); With(); Raise();

This commit is contained in:
Yuyao Huang 2024-04-23 22:10:02 +08:00
parent 48745f2ae9
commit 4cba2dfea3
5 changed files with 31 additions and 4 deletions

View File

@ -13,11 +13,12 @@ COMMENT = 2
NORMAL = 0
BREAK = 8
CONTINUE = 16
RAISE = 32
REG = lambda i: f"__REG{i}"
APPEND_SOURCE_BY_THEMSELVES = [
ast.If, ast.For
ast.If, ast.For, ast.With
]
ASSIGN_SILENT = [

View File

@ -3,4 +3,4 @@ from .statements import Pass, Assign, AnnAssign, AugAssign
from .expressions import Expr, UnaryOp, BinOp, Call, Compare, Attribute, Subscript, Slice, keyword, IfExp
from .literals import Constant, Tuple, List, Set, Dict, FormattedValue, JoinedStr
from .variables import Name
from .control_flow import If, For, Continue, Break
from .control_flow import If, For, Continue, Break, With, Raise

View File

@ -108,3 +108,25 @@ def Break(self, cmtor):
def Continue(self, cmtor):
cmtor._lines[-1] += " # True"
cmtor._stack_event = flags.CONTINUE
def With(self, cmtor):
for wi in self.items:
optional_vars = getattr(wi, "optional_vars", None)
if optional_vars is not None:
cmtor.process(wi.optional_vars)
cmtor.indent += flags.INDENT
for stmt in self.body:
if type(stmt) not in flags.APPEND_SOURCE_BY_THEMSELVES:
cmtor.append_source(cmtor.to_source(stmt))
if cmtor._stack_event == flags.NORMAL:
cmtor.process(stmt)
else:
cmtor._lines[-1] += " # skipped"
cmtor.append_source("")
cmtor.indent -= flags.INDENT
def Raise(self, cmtor):
cmtor._stack_event = flags.RAISE

View File

@ -14,7 +14,7 @@ def FunctionDef(self, cmtor):
if type(stmt) not in flags.APPEND_SOURCE_BY_THEMSELVES:
cmtor.append_source(cmtor.to_source(stmt))
if self is cmtor.root:
if self is cmtor.root and cmtor._stack_event == flags.NORMAL:
cmtor.process(stmt)
cmtor.append_source()

View File

@ -60,3 +60,7 @@ def keyword(self, cmtor):
def IfExp(self, cmtor):
cmtor.append_comment(cmtor.eval(self.test))
cmtor.append_comment(cmtor.eval(self))
def Index(self, cmtor):
cmtor.process(self.value)