Skip to content

Commit a5e731b

Browse files
committed
Address review comments
1 parent f787fe0 commit a5e731b

File tree

2 files changed

+17
-27
lines changed

2 files changed

+17
-27
lines changed

Tools/cases_generator/generators_common.py

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -466,23 +466,13 @@ def _emit_stmt(
466466
storage: Storage,
467467
inst: Instruction | None,
468468
) -> tuple[bool, Token | None, Storage]:
469-
if isinstance(stmt, SimpleStmt):
470-
return self._emit_simple(stmt, uop, storage, inst)
471-
elif isinstance(stmt, BlockStmt):
472-
return self._emit_block(stmt, uop, storage, inst)
473-
elif isinstance(stmt, IfStmt):
474-
return self._emit_if(stmt, uop, storage, inst)
475-
elif isinstance(stmt, ForStmt):
476-
return self._emit_for(stmt, uop, storage, inst)
477-
elif isinstance(stmt, WhileStmt):
478-
return self._emit_while(stmt, uop, storage, inst)
479-
elif isinstance(stmt, MacroIfStmt):
480-
return self._emit_macro_if(stmt, uop, storage, inst)
481-
else:
482-
raise NotImplementedError("Unexpected statement")
483-
469+
method_name = "emit_" + stmt.__class__.__name__
470+
method = getattr(self, method_name, None)
471+
if method is None:
472+
raise NotImplementedError
473+
return method(stmt, uop, storage, inst) # type: ignore
484474

485-
def _emit_simple(
475+
def emit_SimpleStmt(
486476
self,
487477
stmt: SimpleStmt,
488478
uop: CodeSection,
@@ -536,7 +526,7 @@ def _emit_simple(
536526
raise analysis_error(ex.args[0], tkn) #from None
537527

538528

539-
def _emit_macro_if(
529+
def emit_MacroIfStmt(
540530
self,
541531
stmt: MacroIfStmt,
542532
uop: CodeSection,
@@ -568,7 +558,7 @@ def _emit_macro_if(
568558
return reachable, None, storage
569559

570560

571-
def _emit_if(
561+
def emit_IfStmt(
572562
self,
573563
stmt: IfStmt,
574564
uop: CodeSection,
@@ -610,7 +600,7 @@ def _emit_if(
610600
assert rbrace is not None
611601
raise analysis_error(ex.args[0], rbrace) from None
612602

613-
def _emit_block(
603+
def emit_BlockStmt(
614604
self,
615605
stmt: BlockStmt,
616606
uop: CodeSection,
@@ -635,7 +625,7 @@ def _emit_block(
635625
tkn = stmt.close
636626
raise analysis_error(ex.args[0], tkn) from None
637627

638-
def _emit_for(
628+
def emit_ForStmt(
639629
self,
640630
stmt: ForStmt,
641631
uop: CodeSection,
@@ -648,7 +638,7 @@ def _emit_for(
648638
self.out.emit(tkn)
649639
return self._emit_stmt(stmt.body, uop, storage, inst)
650640

651-
def _emit_while(
641+
def emit_WhileStmt(
652642
self,
653643
stmt: WhileStmt,
654644
uop: CodeSection,
@@ -670,7 +660,7 @@ def emit_tokens(
670660
emit_braces: bool = True
671661
) -> Storage:
672662
self.out.start_line()
673-
reachable, tkn, storage = self._emit_block(code.body, code, storage, inst, emit_braces)
663+
reachable, tkn, storage = self.emit_BlockStmt(code.body, code, storage, inst, emit_braces)
674664
assert tkn is not None
675665
try:
676666
if reachable:

Tools/cases_generator/parsing.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Parser for bytecodes.inst."""
22

33
from dataclasses import dataclass, field
4-
from typing import NamedTuple, Callable, TypeVar, Literal, cast, Optional, Iterator
4+
from typing import NamedTuple, Callable, TypeVar, Literal, cast, Iterator
55
from io import StringIO
66

77
import lexer as lx
@@ -81,13 +81,13 @@ def __repr__(self) -> str:
8181
return io.getvalue()
8282

8383
def print(self, out:CWriter) -> None:
84-
raise NotImplementedError()
84+
raise NotImplementedError
8585

8686
def accept(self, visitor: Visitor) -> None:
87-
raise NotImplementedError()
87+
raise NotImplementedError
8888

8989
def tokens(self) -> Iterator[lx.Token]:
90-
raise NotImplementedError()
90+
raise NotImplementedError
9191

9292

9393
@dataclass
@@ -96,7 +96,7 @@ class IfStmt(Stmt):
9696
condition: list[lx.Token]
9797
body: Stmt
9898
else_: lx.Token | None
99-
else_body: Optional[Stmt]
99+
else_body: Stmt | None
100100

101101
def print(self, out:CWriter) -> None:
102102
out.emit(self.if_)

0 commit comments

Comments
 (0)