• Intrinsic Python from COBOL

    From bwtiffin@1:2320/100 to comp.lang.cobol on Sat Apr 22 23:17:11 2017
    From Newsgroup: comp.lang.cobol

    Just added the initial pre-alpha cut of a python(script [,arg,...]) Intrinsic Function to GnuCOBOL.
    It's in the gnu-cobol-builtin-script branch of the GnuCOBOL source tree at https://sourceforge.net/p/open-cobol/code/HEAD/tree/branches/gnu-cobol-builtin-script/
    ./configure now supports --with-python
    It's an early cut, and python3.5m is temporarily hard coded in the autoconf build scripts. That will change soon. Anyone keen to try this may need to get
    their hands dirty and modify a little bit of the configure.ac file
    # Optional Python
    AC_ARG_WITH([python],
    [AS_HELP_STRING([--with-python],
    [(GnuCOBOL) Allow embedded Python])],
    [ if test "$with_python" = "yes"; then
    AC_CHECK_HEADERS([python3.5m/Python.h], [],
    .AC_MSG_ERROR([Python.h is required for embedded Python]))
    AC_CHECK_LIB([python3.5m], [Py_Initialize],
    .[AC_DEFINE([WITH_PYTHON], [1])
    CFLAGS="$CFLAGS -I/usr/include/python3.5m"
    .LIBCOB_LIBS="$LIBCOB_LIBS -lpython3.5m"],
    .AC_MSG_ERROR([libpython3.5m is required for embedded Python]), [])
    fi ],
    [])
    Change the python3.5m occurrences and run autoreconf. Soon that will be simplified to ./configure --with-python=version
    Early samples:
    *>-<*
    *> Author: Brian Tiffin
    *> Dedicated to the public domain
    *>
    *> Date started: April 2017
    *> Modified: 2017-04-06/08:35-0400 btiffin
    *>+<*
    *>
    *> withpython.cob, embedded Python intrinsic
    *> Tectonics: cobc -xj withpython.cob
    *>
    >>SOURCE FORMAT IS FREE
    identification division.
    program-id. withpython.
    REPLACE ==newline== BY ==& x'0a' &==.
    environment division.
    configuration section.
    repository.
    function all intrinsic.
    data division.
    working-storage section.
    01 answer pic x(80).
    procedure division.
    sample-main.
    move python(
    "from time import time,ctime" newline
    "print('Python: Today is', ctime(time()))" newline
    " " newline
    "def func(arg):" newline
    " return [ctime(time()), arg/2, arg, arg*2]" newline
    " " newline
    "result = func(42)") to answer
    display "COBOL: " trim(answer)
    goback.
    end program withpython.
    That displays
    prompt$ cobc -xj withpython.cob
    Python: Today is Sun Apr 23 01:49:07 2017
    COBOL: ['Sun Apr 23 01:49:07 2017', 21.0, 42, 84]
    The data returned from the intrinsic is pulled from a Python variable called "result". Another hard coded alpha feature that will change soon. The intrinsic will eventually return the generic 'last result' just like the Python
    console, but for now it relies on a specific variable in the Python global dictionary, result.
    The data can be pretty much any Python type and will be returned in the same form as what is normally shown by the Python console (stringified repr format).
    That example above is list data, sent to GnuCOBOL as character data in Python display form.
    A sample with numeric data and a Python structure (in display form)
    identification division.
    program-id. numbers.
    environment division.
    configuration section.
    repository.
    function all intrinsic.
    data division.
    working-storage section.
    01 result pic s9(9).
    procedure division.
    display ":" python("print(6 * 7 * -1)") ":"

    move python("result = 6 * 7 * -1") to result
    display ":" result ":"
    display ":" python("result = {'value': 6 * 7 * -1}") ":"
    goback.
    end program numbers.
    That returns an empty field (python just does a print), an actual number in COBOL s9(9) form and then a python dictionary (dict) type (in character data form).
    -42
    ::
    :-000000042:
    :{'value': -42}:
    Details of this (optional) compiler extension will be discussed on SourceForge at
    https://sourceforge.net/p/open-cobol/discussion/contrib/thread/bce95c2f/
    There will likely be an Intrinsic Python Tips and Tricks thread within the main
    GnuCOBOL discussion area as well.
    This in pre-alpha and the build is not as smooth as it could (and will) be. And there are likely issues not yet discovered that will need addressing. Intrepid explorers welcome, suggestions and complaints appreciated.
    Cheers, have good,
    Brian

    SEEN-BY: 154/30 2320/100 0 1 227/0