[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Gm2] Swig support in GNU Modula-2
From: |
Gaius Mulley |
Subject: |
[Gm2] Swig support in GNU Modula-2 |
Date: |
02 May 2008 20:16:03 +0100 |
User-agent: |
Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 |
Hi,
I've just implemented the -fswig option to gm2, which will
automatically generate a modulename.i file (an swig interface
file). This allows scripting languages (perl, python and tcl)
to access Modula-2 implementation modules.
Currently it is fairly basic however it appears to work with
StrLib and other procedural definition modules. It shouldn't
take much work to get most items exported by a definition module
to be automatically translated into swig format. Does anyone
on the list have much experience using swig (particularly
using typemaps?). What is missing at present is an easy (and
safe) method to create an unbounded array which can be passed
into the m2 procedure as a VAR foo: ARRAY OF type. However
we can pass strings to <foo: ARRAY OF CHAR> safely. I'm pretty
sure that by using typemaps it is possible to create a class
which will allow the user to create an object, say an ARRAY of 120
CHARs which then could be given to the <VAR foo: ARRAY OF CHAR>
safely. But if anyone knows how to do this please email me :-).
It would also be nice if the typemap also passed the high bounds
across (120) as well. Currently it does do this, but I'm fairly
sure swig does not expect the array to get written to..!
I have passed a string to a VAR foo: ARRAY OF CHAR in one of the new
examples checked in. But I'm pretty sure the way I've done it is
dangerous! Anyhow if you are interested examine the directories:
gm2/examples/swig{strlen,strlib,full-strlib}.
Here is an example session:
$ cd swig/full-strlib/
$ ls
CVS Makefile testcopy.py testequ.py testlen.py
# as you can see no .i file exists (swig interface file)
$ make
gm2 -fsources -I. -c -fPIC -g -fswig -I../../../gm2-libs \
../../../gm2-libs/StrLib.mod
Compiling: ../../../gm2-libs/StrLib.mod
Module SYSTEM : ../../../gm2-libs/SYSTEM.def
Module M2RTS : ../../../gm2-libs/M2RTS.def
Module StrLib : ../../../gm2-libs/StrLib.def
Module StrLib : ../../../gm2-libs/StrLib.mod
Module ASCII : ../../../gm2-libs/ASCII.def
Pass 2
Pass 3
Pass 4
swig -python StrLib.i
gcc -c -fPIC StrLib_wrap.c -I/usr/include/python2.4
gcc -shared StrLib.o StrLib_wrap.o -o _StrLib.so
python testlen.py
we are going to call StrLib.StrLib_StrLen("hello world") and we expect
11 in return
11
python testequ.py
we are going to call StrLib.StrLib_StrLess("abcd", "pqr") and we
expect 1 in return
1
passed
we are going to call StrLib.StrLib_StrLess("pqr", "abcd") and we
expect 0 in return
0
passed
we are going to call StrLib.StrLib_IsSubString("abcdefghijk", "fghi")
and we expect 1 in return
1
passed
python testcopy.py
5
address@hidden
# the contents of testlen.py is:
$ cat testlen.py
import StrLib
print 'we are going to call StrLib.StrLib_StrLen("hello world") and we
expect 11 in return'
print StrLib.StrLib_StrLen("hello world")
# the first 3rd of testequ.py is:
$ cat testequ.py
import StrLib
print 'we are going to call StrLib.StrLib_StrLess("abcd", "pqr") and
we expect 1 in return'
print StrLib.StrLib_StrLess("abcd", "pqr")
if StrLib.StrLib_StrLess("abcd", "pqr")==1:
print "passed"
else:
print "failed"
# testcopy.py is as follows:
$ cat testcopy.py
import StrLib
print StrLib.StrLib_StrLen("hello")
a = "1234567890" # should really find a better method
StrLib.StrLib_StrCopy("hello", a) # for creating, a, which is also
print a # compatible with ARRAY OF CHAR
regards,
Gaius
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Gm2] Swig support in GNU Modula-2,
Gaius Mulley <=