Skip to content

Commit e8f1e7e

Browse files
committed
wip: boolean multiary operations unparsing
1 parent c93a6e4 commit e8f1e7e

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

open_fortran_parser/ast_transformer.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ def _operation(self, node: ET.Element) -> typed_ast3.AST:
497497
raise NotImplementedError()
498498

499499
def _operation_multiary(
500-
self, node: ET.Element) -> t.Union[typed_ast3.BinOp, typed_ast3.Compare]:
500+
self, node: ET.Element) -> t.Union[
501+
typed_ast3.BinOp, typed_ast3.BoolOp, typed_ast3.Compare]:
501502
operators_and_operands = self.transform_all_subnodes(
502503
node, skip_empty=True, ignored={
503504
'add-operand__add-op', 'add-operand', 'mult-operand__mult-op', 'mult-operand',
@@ -508,6 +509,8 @@ def _operation_multiary(
508509
operation_type, _ = operators_and_operands[1]
509510
if operation_type is typed_ast3.BinOp:
510511
return self._operation_multiary_arithmetic(operators_and_operands)
512+
if operation_type is typed_ast3.BoolOp:
513+
return self._operation_multiary_boolean(operators_and_operands)
511514
if operation_type is typed_ast3.Compare:
512515
return self._operation_multiary_comparison(operators_and_operands)
513516
raise NotImplementedError()
@@ -541,6 +544,29 @@ def _operation_multiary_arithmetic(
541544

542545
return root_operation
543546

547+
def _operation_multiary_boolean(
548+
self, operators_and_operands: t.Sequence[t.Union[typed_ast3.AST, t.Tuple[
549+
t.Type[typed_ast3.BoolOp], t.Type[typed_ast3.AST]]]]) -> typed_ast3.BoolOp:
550+
operators_and_operands += [(None, None)]
551+
552+
root_operation = None
553+
root_operation_type = None
554+
root_operator_type = None
555+
zippped = zip(operators_and_operands[::2], operators_and_operands[1::2])
556+
for operand, (operation_type, operator_type) in zippped:
557+
if root_operation is None:
558+
root_operation_type = operation_type
559+
root_operator_type = operator_type
560+
root_operation = typed_ast3.BoolOp(
561+
op=root_operator_type(), values=[operand])
562+
continue
563+
if operation_type is not None:
564+
assert operation_type is root_operation_type, (operation_type, root_operation_type)
565+
assert operator_type is root_operator_type, (operator_type, root_operator_type)
566+
root_operation.values.append(operand)
567+
568+
return root_operation
569+
544570
def _operation_multiary_comparison(
545571
self, operators_and_operands: t.Sequence[t.Union[typed_ast3.AST, t.Tuple[
546572
t.Type[typed_ast3.Compare], t.Type[typed_ast3.AST]]]]) -> typed_ast3.Compare:
@@ -611,6 +637,8 @@ def _operator(
611637
# IsNot
612638
# In
613639
# NotIn
640+
'.and.': (typed_ast3.BoolOp, typed_ast3.And),
641+
'.or.': (typed_ast3.BoolOp, typed_ast3.Or),
614642
# unary
615643
# '+': (typed_ast3.UnaryOp, typed_ast3.UAdd),
616644
# '-': (typed_ast3.UnaryOp, typed_ast3.USub),

0 commit comments

Comments
 (0)