fun with python

This commit is contained in:
Vítor Santos Costa
2012-10-08 23:58:22 +01:00
parent c4da6a9c68
commit ecf5ac655f
5 changed files with 438 additions and 155 deletions

View File

@@ -1,45 +1,100 @@
#
# default base directory for YAP installation
#
#
ROOTDIR = @prefix@
#
# where the binary should be
#
BINDIR = $(ROOTDIR)/bin
#
# where YAP should look for binary libraries
#
LIBDIR=@libdir@
YAPLIBDIR=@libdir@/Yap
#
# where YAP should look for architecture-independent Prolog libraries
#
SHAREDIR=$(ROOTDIR)/share
#
#
# You shouldn't need to change what follows.
#
INSTALL=@INSTALL@
INSTALL_DATA=@INSTALL_DATA@
INSTALL_PROGRAM=@INSTALL_PROGRAM@
srcdir=@srcdir@
YAP_EXTRAS=@YAP_EXTRAS@
################################################################
# YAP/SWI-Prolog R interface package
# Author: Nicos Angelopoulos
# Copyright: Perl License
################################################################
install:
(cd $(srcdir); python setup.py install)
PACKAGE=pyswip
DOC=pyswip
PKGCFLAGS=@PYTHON_INCLUDES@
include ../Makefile.defs
OBJS= python.o
SOLIBS= python.@SO@
LIBPL= python.pl
all: @PYTHON_TARGET@
pyswip: $(SOLIBS)
dummy::
python.@SO@: $(OBJS)
$(LD) $(LDSOFLAGS) -o $@ $(OBJS) $(LIBS) @PYTHON_LIBS@ $(LIBPLSO)
install: install-@PYTHON_TARGET@
install-dummy::
install-pyswip: $(SOLIBS) $(addprefix $(srcdir)/, $(LIBPL))
mkdir -p $(DESTDIR)$(SOLIBDIR)
rm -f $(DESTDIR)$(SOLIBDIR)/python.@SO@
$(INSTALL_PROGRAM) $(SOLIBS) $(SOLIBDIR)
mkdir -p $(DESTDIR)$(PLLIBDIR)
for f in $(LIBPL); do \
$(INSTALL_DATA) $(srcdir)/$$f $(DESTDIR)$(PLLIBDIR); \
done
$(MKINDEX)
install-python:
(cd $(srcdir); python setup.py install)
ln-install::
$(MAKE) INSTALL_DATA="../ln-install" INSTALL_PROGRAM="../ln-install" install
rpm-install: install
html-install::
mkdir -p $(DESTDIR)$(PKGDOC)
$(INSTALL) -m 644 $(DOC).html $(DESTDIR)$(PKGDOC)
pdf-install::
mkdir -p $(DESTDIR)$(PKGDOC)
$(INSTALL) -m 644 $(DOC).pdf $(DESTDIR)$(PKGDOC)
uninstall::
(cd $(SOLIBDIR) && rm -f $(TARGETS))
(cd $(PLBASE)/library && rm -f $(LIBPL))
$(MKINDEX)
################################################################
# Documentation
################################################################
TEXEXTRA= libpython.tex
$(TEX): $(TEXEXTRA)
libpython.tex: python.pl
$(PLTOTEX) --section 'library(python)' --out=$@
################################################################
# Testing
################################################################
check::
(cd $(srcdir) && $(PL) -q -f test_python.pl -g test_python,halt -t 'halt(1)' )
(cd $(srcdir)/examples; python create_term.py)
(cd $(srcdir)/examples; python knowledgebase.py)
(cd $(srcdir)/examples; python knowledgebase.py)
(cd $(srcdir)/examples; python register_foreign.py)
(cd $(srcdir)/examples; python register_foreign_simple.py)
(cd $(srcdir)/examples/coins; python coins.py)
(cd $(srcdir)/examples/draughts; python puzzle1.py)
(cd $(srcdir)/examples/hanoi; python hanoi.py)
(cd $(srcdir)/examples/sendmoremoney; python money.py)
(cd $(srcdir)/examples/sudoku; python sudoku.py)
(cd $(srcdir)/examples/sudoku; python sudoku_daily.py)
################################################################
# Clean
################################################################
clean:
rm -f $(SOLIBS) *~ *.o *% a.out core config.log
distclean: clean
rm -f $(TARGETS) config.cache config.h config.status Makefile
rm -f $(DOC).aux $(DOC).log $(DOC).out $(DOC).toc
rm -rf html
rm -rf autom4te.cache
test:
(cd $(srcdir)/examples; python create_term.py)
(cd $(srcdir)/examples; python knowledgebase.py)
(cd $(srcdir)/examples; python knowledgebase.py)
(cd $(srcdir)/examples; python register_foreign.py)
(cd $(srcdir)/examples; python register_foreign_simple.py)
(cd $(srcdir)/examples/coins; python coins.py)
(cd $(srcdir)/examples/draughts; python puzzle1.py)
(cd $(srcdir)/examples/hanoi; python hanoi.py)
(cd $(srcdir)/examples/sendmoremoney; python money.py)
(cd $(srcdir)/examples/sudoku; python sudoku.py)
(cd $(srcdir)/examples/sudoku; python sudoku_daily.py)

48
packages/pyswip/python.c Normal file
View File

@@ -0,0 +1,48 @@
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <Python.h>
#include <assert.h>
static foreign_t
init_python(void)
{
Py_Initialize();
return TRUE;
}
static foreign_t
end_python(void)
{
Py_Finalize();
return TRUE;
}
static foreign_t
python_run_command(term_t cmd)
{
char *s;
size_t len;
if ( PL_get_nchars(cmd, &len, &s, CVT_ALL|CVT_EXCEPTION) ) {
PyRun_SimpleString(s);
return TRUE;
}
return FALSE;
}
install_t install_python(void);
install_t
install_python(void)
{ // FUNCTOR_dot2 = PL_new_functor(PL_new_atom("."), 2);
// FUNCTOR_equal2 = PL_new_functor(PL_new_atom("="), 2);
// FUNCTOR_boolop1 = PL_new_functor(PL_new_atom("@"), 1);
// ATOM_true = PL_new_atom("true");
// ATOM_false = PL_new_atom("false");
PL_register_foreign("init_python", 0, init_python, 0);
PL_register_foreign("end_python", 0, end_python, 0);
PL_register_foreign("python_run_command", 1, python_run_command, 0);
}

47
packages/pyswip/python.pl Normal file
View File

@@ -0,0 +1,47 @@
%%% -*- Mode: Prolog; -*-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Nicos Angelopoulos, Vitor Santos Costa, Jan Wielemaker
% E-mail: Nicos Angelopoulos <nicos@gmx.co.uk>
% Copyright (C): Nicos Angelopoulos, Universidade do Porto, VU University Amsterdam
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This file is part of r..eal
% distributed according to Perl Artistic License
% check LICENSE file for distribution license
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
:- module(python, [
init_python/0,
end_python/0,
python_command/1
]).
:- use_module(library(shlib)).
:- use_module(library(lists)).
:- use_module(library(apply_macros)).
:- use_module(library(charsio)).
/** <module> python
A C-based Prolog interface to python.
@author Vitor Santos Costa
@version 0:0:5, 2012/09/12
@license Perl Artistic License
*/
%%%
python_command(Cmd) :-
python_run_command(Cmd).
start_python :-
use_foreign_library(foreign(python)),
init_python.
% done
:- initialization(start_python, now).