This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/packages/pyswip/python.pl

145 lines
3.6 KiB
Perl
Raw Normal View History

2012-10-08 23:58:22 +01:00
%%% -*- Mode: Prolog; -*-
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2012-10-17 10:56:44 +01:00
% Author: Vitor Santos Costa
% E-mail: vsc@dcc.fc.up.pt
% Copyright (C): Universidade do Porto
2012-10-08 23:58:22 +01:00
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
2012-10-17 10:56:44 +01:00
% This file is part of the YAP Python Interface
2012-10-08 23:58:22 +01:00
% distributed according to Perl Artistic License
% check LICENSE file for distribution license
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/** <module> python
A C-based Prolog interface to python.
@author Vitor Santos Costa
2012-10-17 10:56:44 +01:00
@version 0:0:5, 2012/10/8
2012-10-08 23:58:22 +01:00
@license Perl Artistic License
*/
%%%
2012-10-17 10:56:44 +01:00
:- module(python, [
init_python/0,
end_python/0,
python_command/1,
2012-10-25 00:33:02 +01:00
python_assign/3,
python/2,
op(100,fy,$),
op(950,fy,:=),
op(950,yfx,:=),
(:=)/2,
(:=)/1
2012-10-17 10:56:44 +01:00
]).
2012-10-25 00:33:02 +01:00
/************************************************************************************************************
Python interface
Data types are
Python Prolog
string atoms
numbers numbers
lists lists
generic objs __pointer__(Address)
$(var) refers to the attribute __main__.var
*************************************************************************************************************/
2012-10-17 10:56:44 +01:00
:- use_module(library(shlib)).
:- use_module(library(lists)).
:- use_module(library(apply_macros)).
:- use_module(library(charsio)).
2012-10-25 00:33:02 +01:00
:- dynamic python_mref_cache/2.
:= F :- python(F,_).
V := F :- var(V), !, python(F,V).
'$'(V) := F :- atom(V), !, python(F,F1), python_assign(V, F1).
A^Key := F :- python(F,F1), python_set_item(A, Key, F1).
python_do_import(Module, MRef) :-
python_mref_cache(Module, MRef), !.
python_do_import(Module, MRef) :-
python_import(Module, MRef),
assert( python_mref_cache(Module, MRef) ).
python_eval_term(Object:len, OArg) :- !,
python_len(Object, OArg).
python_eval_term(Module:Object:len, OArg) :- !,
python_do_import(Module, MRef),
python_o(MRef, Function, ORef),
python_len(ORef, OArg).
python_eval_term(Object:dir, OArg) :- !,
python_dir(Object, OArg).
python_eval_term(Module:Object:dir, OArg) :- !,
python_do_import(Module, MRef),
python_o(MRef, Function, ORef),
python_dir(ORef, OArg).
python_eval_term(Module:Object:Field, OArg) :-
atom(Module),
atom(Object), !,
python_do_import(Module, MRef),
python_o(MRef, Function, ORef),
python_access(ORef, Field, OArg).
python_eval_term(Module:Function, OArg) :-
atom(Module), !,
python_do_import(Module, MRef),
functor(Function, F, _),
python_f(MRef, F, FRef),
python_apply(FRef, Function, OArg).
python_eval_term(Obj:Field, OArg) :-
python_access(Obj, Field, OArg).
python(Obj, Out) :-
python_eval_term(Obj, Out), !.
python(Obj, OArg) :-
python_do_is(Obj, Obj1),
python_is(Obj1, OArg).
python_do_is(A+B, NA+NB) :- !,
python_do_is(A, NA),
python_do_is(B, NB).
python_do_is(A-B, NA-NB) :- !,
python_do_is(A, NA),
python_do_is(B, NB).
python_do_is(A*B, NA*NB) :- !,
python_do_is(A, NA),
python_do_is(B, NB).
python_do_is(A/B, NA/NB) :- !,
python_do_is(A, NA),
python_do_is(B, NB).
python_do_is(A, NA) :-
python_eval_term(A, NA), !.
python_do_is(A, A).
2012-10-17 10:56:44 +01:00
2012-10-08 23:58:22 +01:00
python_command(Cmd) :-
2012-10-25 00:33:02 +01:00
python_run_command(Cmd).
2012-10-08 23:58:22 +01:00
start_python :-
use_foreign_library(foreign(python)),
2012-10-23 10:18:24 +01:00
init_python,
2012-10-25 00:33:02 +01:00
python_command('import sys').
add_cwd_to_python :-
2012-10-23 10:18:24 +01:00
unix(getcwd(Dir)),
atom_concat(['sys.path.append(\"',Dir,'\")'], Command),
python_command(Command).
2012-10-08 23:58:22 +01:00
% done
2012-10-25 00:33:02 +01:00
python_assign(Name, Exp, '$'(Name)) :-
python_assign(Name, Exp).
2012-10-08 23:58:22 +01:00
:- initialization(start_python, now).
2012-10-25 00:33:02 +01:00
:- initialization(add_cwd_to_python).