stfc / fparser

This project maintains and develops a Fortran parser called fparser2 written purely in Python which supports Fortran 2003 and some Fortran 2008. A legacy parser fparser1 is also available but is not supported. The parsers were originally part of the f2py project by Pearu Peterson.
https://fparser.readthedocs.io
Other
61 stars 28 forks source link

Else_Stmt and Return_Stmt compare equal #400

Open reuterbal opened 1 year ago

reuterbal commented 1 year ago

I have just run into an odd situation where the following would occur:

Python 3.8.8 (default, Feb 24 2021, 16:52:30)
[GCC 8.3.1 20191121 (Red Hat 8.3.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from fparser.two import Fortran2003
>>> Fortran2003.Else_Stmt('ELSE') == Fortran2003.Return_Stmt('RETURN')
True

I have not dug any deeper into this but it seems unexpected. In my particular case this occurs with an If_Construct that looks something like this:

IF (N == 0) THEN
  VAL = Z0
  RETURN
ELSE
  VAL = Z1
  RETURN
ENDIF

From an earlier search I would have the Else_Stmt node already available and do something like if_construct_node.children.index(else_stmt_node), which I would expect to yield 3 but instead returns 2.

arporter commented 1 year ago

I think this is related to #174. Certainly, the incorrect/unexpected behaviour of index is down to that.

arporter commented 1 year ago

I can reproduce this:

>>> elsenode = Fortran2003.Else_Stmt("else")
>>> rnode = Fortran2003.Return_Stmt("return")
>>> rnode == elsenode
True

From my investigation in #174, it seems that self.items is used to compare two instances of Base and, in this case:

>>> rnode.items
(None,)
>>> elsenode.items
(None,)