diff --git a/bin/loopy b/bin/loopy index ed8f0ff16e4e82b02b09da126660db1baaed4035..2a657174d4e390f80331689068b8d21e8eab0de7 100644 --- a/bin/loopy +++ b/bin/loopy @@ -116,7 +116,7 @@ def main(): kernels = [kernel] - elif args.lang in ["fortran", "floopy"]: + elif args.lang in ["fortran", "floopy", "fpp"]: pre_transform_code = None if args.transform: with open(args.transform, "r") as xform_fd: @@ -132,7 +132,8 @@ def main(): + pre_transform_code) from loopy.frontend.fortran import f2loopy - kernels = f2loopy(infile_content, pre_transform_code=pre_transform_code) + kernels = f2loopy(infile_content, pre_transform_code=pre_transform_code, + use_c_preprocessor=(args.lang == "fpp")) if args.name is not None: kernels = [kernel for kernel in kernels diff --git a/examples/fortran/run-floopy.sh b/examples/fortran/run-floopy.sh index 6a718bb69ba9db8ec2a8f5cbf3b1314676d2f6ab..fcea2c8b5ed58eed8738ad263df62cdf687b3d0f 100755 --- a/examples/fortran/run-floopy.sh +++ b/examples/fortran/run-floopy.sh @@ -3,4 +3,4 @@ NAME="$1" shift -python $(which loopy) --lang=floopy "$NAME" - "$@" +python $(which loopy) --lang=fpp "$NAME" - "$@" diff --git a/loopy/frontend/fortran/__init__.py b/loopy/frontend/fortran/__init__.py index f169eeef52ef7f029dedcf9412dfa6ef828343d6..aec2d4314e1cb1fa1291b6f4d40988cb9675a3d8 100644 --- a/loopy/frontend/fortran/__init__.py +++ b/loopy/frontend/fortran/__init__.py @@ -22,9 +22,39 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. """ +from loopy.diagnostic import LoopyError + def f2loopy(source, free_form=True, strict=True, - pre_transform_code=None): + pre_transform_code=None, use_c_preprocessor=False, + file_name="<floopy code>"): + if use_c_preprocessor: + try: + import ply.lex as lex + import ply.cpp as cpp + except ImportError: + raise LoopyError("Using the C preprocessor requires PLY to be installed") + + lexer = lex.lex(cpp) + + from ply.cpp import Preprocessor + p = Preprocessor(lexer) + p.parse(source, file_name) + + tokens = [] + while True: + tok = p.token() + + if not tok: + break + + if tok.type == "CPP_COMMENT": + continue + + tokens.append(tok.value) + + source = "".join(tokens) + from fparser import api tree = api.parse(source, isfree=free_form, isstrict=strict, analyze=False, ignore_comments=False) diff --git a/requirements.txt b/requirements.txt index 708b72c8d508c979f069d41b645da18b559ecfd5..b833d6dd0d989ad3b88aedb3d09cf21766536613 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,6 @@ git+git://github.com/pyopencl/pyopencl git+git://github.com/inducer/pymbolic hg+https://bitbucket.org/inducer/f2py + +# Optional, needed for using the C preprocessor on Fortran +ply>=3.6