notes about snobol in python

spitpat

2009: http://www.snobol4.org/spipat/spipat-0.9.3.tar.gz

This package is in it's infancy!!

on webfaction:

    ./configure
    make

creates a:

06:03:42 spipat-0.9.3 $ file libspipat.la
libspipat.la: libtool library file, ASCII text

and there seems to be a python/setup.py file.

seems to have break(), match(), search(), etc...

on opanalysis3:

02:45:55 pkgs $ wget http://www.snobol4.org/spipat/spipat-0.9.3.tar.gz
02:47:41 pkgs $ tar xvf spipat-0.9.3.tar.gz
02:47:58 pkgs $ cd spipat-0.9.3
02:48:03 spipat-0.9.3 $ ./configure
02:48:38 spipat-0.9.3 $ make

but then:

    02:51:51 spipat-0.9.3 $ cd python/

    02:51:58 python $ python setup.py install
    running install
    running build
    running build_py
    creating build
    creating build/lib.linux-x86_64-3.6
    copying spipat.py -> build/lib.linux-x86_64-3.6
    running build_ext
    building '_spipat' extension
    creating build/temp.linux-x86_64-3.6
    gcc -pthread -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/usr/loc       al/include -I/home/perti/wendell/python3/Python-3.6.5/Include -I/home/perti/wendell/python3/Py       thon-3.6.5 -c _spipat.c -o build/temp.linux-x86_64-3.6/_spipat.o
    _spipat.c:44:20: error: spipat.h: No such file or directory

PROBABLY BAD:

02:59:06 python $ cp spipat.h /home/perti/wendell/python3/Python-3.6.5/Include

however, next error is:

_spipat.c:269: error: ‘PyBoolObject’ undeclared (first use in this function)
_spipat.c:269: error: (Each undeclared identifier is reported only once
_spipat.c:269: error: for each function it appears in.)
_spipat.c:269: error: expected expression before ‘)’ token

PROBLEM: PyBoolObject is a Python2 item and is not in Python3 !!!

AFTER lots of lookups, finally go it to compile

on webfaction:

    ./configure --prefix=/home/wendell/usr/local
    make
    make install

seems ok, proceeding (with _spipat.c as edited on opan3)

    cd python
    vim setup.py (for include and lib dirs)
    python setup.py install

good so far...

    export PYTHONPATH=/home/wendell/usr/local/lib

NOPE, but this works:

    export LD_LIBRARY_PATH=/home/wendell/usr/local/lib

however:

    02:20:52 python $ python

    Python 3.7.3 (default, Apr  4 2019, 00:30:16)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
    Type "help", "copyright", "credits" or "license" for more information.

    >>> import spipat

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/wendell/pkgs/python/spipat-0.9.3/python/spipat.py", line 112, in <module>
        from _spipat import *
    ImportError: /home/wendell/usr/local/lib/python3.7/site-packages/_spipat.cpython-37m-x86_64-linux-gnu.so: undefined symbol: PyString_FromStringAndSize

Possible: use Bytes or Unicode, not String:

choices are:

    PyUnicode_FromStringAndSize
    PyBytes_FromStringAndSize
    PyByteArray_FromStringAndSize

chose Unicode, now:

    -gnu.so: undefined symbol: PyInt_FromSize_t

after lots of edits, finally get:

    03:31:17 python $ python
    Python 3.7.3 (default, Apr  4 2019, 00:30:16)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import spipat
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/wendell/pkgs/python/spipat-0.9.3/python/spipat.py", line 112, in <module>
        from _spipat import *
    SystemError: initialization of _spipat raised unreported exception

library and 'import spipat' now succeed

proceeding with runtime errors...

Test Program:

    import spipat
    pat = spipat.Pattern('h')

    m = pat.search(b'hello')
    assert m is not None

    new = m.repl('H')
    assert new == "Hello"

some individual statements and outputs:

    pat = spipat.Pattern(b'h')

    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: expected string or callable object

So, arg checking may be incorrect on Pattern def!

now, using non-bytes (e.g. 'h') form:

    m = pat.search('hello')

    ++ Pattern_search
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: search() argument 1 must be bytes, not str

pattern check is operating for search()

ok, make that into bytes:

    >>> m = pat.search(b'hello')

    ++ Pattern_search
    ++ Pattern_search:2
    ++ Pattern_match2:a
    ++ Pattern_match2:b
    ++ Pattern_match2:c len=5
    ++ Pattern_match2:c ptrx=5739bdc0
    ++ Pattern_match2:c ptr=hello
    ++ Pattern_match2:cc
    >>> type(m)
    <class 'NoneType'>

Possible problem: search is for bytes, subject is str.

Args for PyArg_ParseTupleAndKeywords are here: https://docs.python.org/3/c-api/arg.html

analysis of initial repl.py:

Pattern_init ---

Pattern_search ---

"Requires that the Python object is a bytes object, without attempting any conversion."

"Convert a Unicode object to a C pointer to a character string. A pointer to an existing string is stored in the character pointer variable whose address you pass. The C string is NUL-terminated."

this then calls Pattern_match2 ---

PROBLEM: can't convert item to plain string!!!

grep and sed of Python-3:

     14 PyUnicode_AsASCIIString
      2 PyUnicode_AsCharmapString
      3 PyUnicode_AsDecodedObject
      3 PyUnicode_AsDecodedUnicode
      3 PyUnicode_AsEncodedObject
     24 PyUnicode_AsEncodedString
      3 PyUnicode_AsEncodedUnicode
     18 PyUnicode_AsKind
     10 PyUnicode_AsLatin1String
      2 PyUnicode_AsMBCSString
      4 PyUnicode_AsRawUnicodeEscapeString
      9 PyUnicode_AsUCS4
      3 PyUnicode_AsUCS4Copy
     20 PyUnicode_AsUnicode
     20 PyUnicode_AsUnicodeAndSize
      2 PyUnicode_AsUnicodeCopy
      4 PyUnicode_AsUnicodeEscapeString
      2 PyUnicode_AsUTF16String
      2 PyUnicode_AsUTF32String
     98 PyUnicode_AsUTF8
     37 PyUnicode_AsUTF8AndSize
     21 PyUnicode_AsUTF8String
      8 PyUnicode_AsWideChar
     20 PyUnicode_AsWideCharString

grep and sed of Python-3:

     53 PyBytes_AsString

      2 PyBytes_FormatEx
      4 PyBytes_FromBuffer
      5 PyBytes_FromFormat
      5 PyBytes_FromFormatV
      4 PyBytes_FromHex
      2 PyBytes_FromIterator
      2 PyBytes_FromList
      6 PyBytes_FromObject
      1 PyBytes_FromObject, something
      6 PyBytes_FromSize
     26 PyBytes_FromString
    273 PyBytes_FromStringAndSize
      1 PyBytes_FromStringAndSize");

an answer from SO:

from: https://stackoverflow.com/questions/22487780/what-do-i-use-instead-of-pystring-asstring-when-loading-a-python-module-in-3-3/22491037

which says: "So in the end you probably want something like:"

    if (PyUnicode_Check(result)) {

        // Convert string to bytes.
        // strdup() bytes into my_result.

        PyObject * temp_bytes = PyUnicode_AsEncodedString(result, "UTF-8", "strict");
        // Owned reference
        if (temp_bytes != NULL) {

            my_result = PyBytes_AS_STRING(temp_bytes); // Borrowed pointer
            my_result = strdup(my_result);
            Py_DECREF(temp_bytes);

        } else {
            // TODO: Handle encoding error.
        }

    } else if (PyBytes_Check(result)) {

        // strdup() bytes into my_result.

        my_result = PyBytes_AS_STRING(result); // Borrowed pointer
        my_result = strdup(my_result);

    } else {
        // Convert into your favorite string representation.
        // Convert string to bytes if it is not already.
        // strdup() bytes into my_result.

        // if you receive a non-string object, you can convert it
        // using PyObject_Repr(), PyObject_ASCII(), PyObject_Str(),
        // or PyObject_Bytes().
    }

==================================================

UNATTEMPTED packages follow

Greg Ewing's package

2007: Snobol.tar.gz: http://www.cosc.canterbury.ac.nz/greg.ewing/python/Snobol.tar.gz

is one Snobol.py file. had to change line endings:

    tr -s '\r' '\n' < Snobol.py  > Snobol_lf.py

no documentation.

SnoPy

2002: http://sourceforge.net/projects/snopy/

needs bison, swig, gnatmake, gnatbind, gnatlink to compile (webfac has bison and swig) gnatxxx seem to be Ada!

on webfaction:

    make

    snobol.y: In function ‘clear_strings’:
snobol.y:375:19: error: ‘NULL’ undeclared (first use in this function)
     if (lexp[i] = NULL)

make: *** [snobol.tab.o] Error 1

" you will need to have SWIG and gnat"

Pattern Matching

2004: http://www.wilmott.ca/python/patternmatching.html

all in Python source files

4/21/2019: converting gimpel rstory to python

    cd ~/pkgs/snobol/gimpel/SNOBOL4
    ../../csnobol/SNOBOL4/2.0/bin/snobol4.exe RSTORY.SNO
Long ago in a small village there was a little old
lady who went to a pet store and bought a cat.  On
...

TODO: get Orange book from home!