gh-113710: Add types to the interpreter DSL (#113711)

Co-authored-by: Jules <57632293+JuliaPoo@users.noreply.github.com>
Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
This commit is contained in:
Ken Jin 2024-01-13 01:30:27 +08:00 committed by GitHub
parent 79970792fd
commit ac92527c08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 227 additions and 138 deletions

View file

@ -75,6 +75,11 @@ class StackEffect(Node):
size: str = "" # Optional `[size]`
# Note: size cannot be combined with type or cond
# Optional `(type, refinement)`
type_prop: None | tuple[str, None | str] = field(
default_factory=lambda: None, init=True, compare=False, hash=False
)
def __repr__(self) -> str:
items = [self.name, self.type, self.cond, self.size]
while items and items[-1] == "":
@ -138,11 +143,13 @@ class Family(Node):
@dataclass
class Pseudo(Node):
name: str
flags: list[str] # instr flags to set on the pseudo instruction
targets: list[str] # opcodes this can be replaced by
flags: list[str] # instr flags to set on the pseudo instruction
targets: list[str] # opcodes this can be replaced by
AstNode = InstDef | Macro | Pseudo | Family
class Parser(PLexer):
@contextual
def definition(self) -> AstNode | None:
@ -253,14 +260,25 @@ class Parser(PLexer):
@contextual
def stack_effect(self) -> StackEffect | None:
# IDENTIFIER [':' IDENTIFIER [TIMES]] ['if' '(' expression ')']
# IDENTIFIER [':' [IDENTIFIER [TIMES]] ['&' '(' IDENTIFIER ['+' IDENTIFIER] ')']] ['if' '(' expression ')']
# | IDENTIFIER '[' expression ']'
if tkn := self.expect(lx.IDENTIFIER):
type_text = ""
type_prop = None
if self.expect(lx.COLON):
type_text = self.require(lx.IDENTIFIER).text.strip()
if self.expect(lx.TIMES):
type_text += " *"
if i := self.expect(lx.IDENTIFIER):
type_text = i.text.strip()
if self.expect(lx.TIMES):
type_text += " *"
if self.expect(lx.AND):
consumed_bracket = self.expect(lx.LPAREN) is not None
type_prop_text = self.require(lx.IDENTIFIER).text.strip()
refinement = None
if self.expect(lx.PLUS):
refinement = self.require(lx.IDENTIFIER).text.strip()
type_prop = (type_prop_text, refinement)
if consumed_bracket:
self.require(lx.RPAREN)
cond_text = ""
if self.expect(lx.IF):
self.require(lx.LPAREN)
@ -277,7 +295,7 @@ class Parser(PLexer):
self.require(lx.RBRACKET)
type_text = "PyObject **"
size_text = size.text.strip()
return StackEffect(tkn.text, type_text, cond_text, size_text)
return StackEffect(tkn.text, type_text, cond_text, size_text, type_prop)
return None
@contextual
@ -364,7 +382,9 @@ class Parser(PLexer):
if self.expect(lx.COMMA):
if not (size := self.expect(lx.IDENTIFIER)):
if not (size := self.expect(lx.NUMBER)):
raise self.make_syntax_error("Expected identifier or number")
raise self.make_syntax_error(
"Expected identifier or number"
)
if self.expect(lx.RPAREN):
if self.expect(lx.EQUALS):
if not self.expect(lx.LBRACE):