From 13ddf8bd025c3ff6257049cab6f3df1072ac0daf Mon Sep 17 00:00:00 2001 From: Charles Brunet Date: Fri, 25 Aug 2023 09:41:47 -0400 Subject: parser: simplify by using Unary and Binary Operator Node --- mesonbuild/mparser.py | 59 +++++++++++++++------------------------------------ 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/mesonbuild/mparser.py b/mesonbuild/mparser.py index ffe775037..00b87318c 100644 --- a/mesonbuild/mparser.py +++ b/mesonbuild/mparser.py @@ -401,7 +401,7 @@ class EmptyNode(BaseNode): pass @dataclass(unsafe_hash=True) -class OrNode(BaseNode): +class BinaryOperatorNode(BaseNode): left: BaseNode operator: SymbolNode @@ -413,53 +413,33 @@ class OrNode(BaseNode): self.operator = operator self.right = right -@dataclass(unsafe_hash=True) -class AndNode(BaseNode): - - left: BaseNode - operator: SymbolNode - right: BaseNode +class OrNode(BinaryOperatorNode): + pass - def __init__(self, left: BaseNode, operator: SymbolNode, right: BaseNode): - super().__init__(left.lineno, left.colno, left.filename) - self.left = left - self.operator = operator - self.right = right +class AndNode(BinaryOperatorNode): + pass @dataclass(unsafe_hash=True) -class ComparisonNode(BaseNode): +class ComparisonNode(BinaryOperatorNode): - left: BaseNode - operator: SymbolNode - right: BaseNode ctype: COMPARISONS def __init__(self, ctype: COMPARISONS, left: BaseNode, operator: SymbolNode, right: BaseNode): - super().__init__(left.lineno, left.colno, left.filename) - self.left = left - self.operator = operator - self.right = right + super().__init__(left, operator, right) self.ctype = ctype @dataclass(unsafe_hash=True) -class ArithmeticNode(BaseNode): +class ArithmeticNode(BinaryOperatorNode): - left: BaseNode - right: BaseNode # TODO: use a Literal for operation operation: str - operator: SymbolNode def __init__(self, operation: str, left: BaseNode, operator: SymbolNode, right: BaseNode): - super().__init__(left.lineno, left.colno, left.filename) - self.left = left - self.right = right + super().__init__(left, operator, right) self.operation = operation - self.operator = operator - @dataclass(unsafe_hash=True) -class NotNode(BaseNode): +class UnaryOperatorNode(BaseNode): operator: SymbolNode value: BaseNode @@ -469,6 +449,12 @@ class NotNode(BaseNode): self.operator = operator self.value = value +class NotNode(UnaryOperatorNode): + pass + +class UMinusNode(UnaryOperatorNode): + pass + @dataclass(unsafe_hash=True) class CodeBlockNode(BaseNode): @@ -620,18 +606,6 @@ class TestCaseClauseNode(BaseNode): self.block = block self.endtestcase = endtestcase -@dataclass(unsafe_hash=True) -class UMinusNode(BaseNode): - - operator: SymbolNode - value: BaseNode - - def __init__(self, current_location: Token, operator: SymbolNode, value: BaseNode): - super().__init__(current_location.lineno, current_location.colno, current_location.filename) - self.operator = operator - self.value = value - - @dataclass(unsafe_hash=True) class TernaryNode(BaseNode): @@ -663,6 +637,7 @@ class ParenthesizedNode(BaseNode): self.inner = inner self.rpar = rpar + if T.TYPE_CHECKING: COMPARISONS = Literal['==', '!=', '<', '<=', '>=', '>', 'in', 'notin'] -- cgit v1.2.3