home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / modprolg / mod-prol.lha / Prolog / modlib / src / $io.P < prev    next >
Encoding:
Text File  |  1992-06-03  |  7.7 KB  |  228 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* io.P */
  25.  
  26. $io_export([$write/1,$writeq/1,$display/1,$print/1,$print_al/2,$print_ar/2,
  27.         $errmsg/1]).
  28.  
  29. % $io_use : $bio, $bmeta
  30.  
  31. $write(T) :- $write(T,999).
  32.  
  33. $write(T,_) :- var(T), !, $writename(T).
  34. $write([],_) :- !, $writename([]).
  35. $write([X|Y],_) :- !, $put(0'[), $write(X,999), $writetail(Y).
  36. $write(T,Prec) :- $structure(T), !, $functor0(T, P), $arity(T, N), 
  37.   (N=:=1 ->
  38.     ($read_curr_op(Opprec,fx,P) ->
  39.     Nprec is Opprec-1,$writepreop(P,T,Prec,Opprec,Nprec);
  40.      $read_curr_op(Opprec,fy,P) ->
  41.     $writepreop(P,T,Prec,Opprec,Opprec);
  42.      $read_curr_op(Opprec,xf,P) ->
  43.     Nprec is Opprec-1,$writepostop(P,T,Prec,Opprec,Nprec);
  44.      $read_curr_op(Opprec,yf,P) ->
  45.     $writepostop(P,T,Prec,Opprec,Opprec);
  46.     $writestr(P,N,T)
  47.     );
  48.    N=:=2 ->
  49.     ($read_curr_op(Opprec,xfx,P) ->
  50.     Nprec is Opprec-1,$writebinop(P,T,Prec,Opprec,Nprec,Nprec);
  51.      $read_curr_op(Opprec,xfy,P) ->
  52.     Nprec is Opprec-1,$writebinop(P,T,Prec,Opprec,Nprec,Opprec);
  53.      $read_curr_op(Opprec,yfx,P) ->
  54.     Nprec is Opprec-1,$writebinop(P,T,Prec,Opprec,Opprec,Nprec);
  55.     $writestr(P,N,T)
  56.     );
  57.    $writestr(P,N,T)
  58.   ).
  59.  
  60. $write(T,_) :- $write_path_name(T).
  61.  
  62. $writestr(P,N,T) :-
  63.         $write_path_name(P), $put(0'(), arg(1, T, X), $write(X,999),
  64.         $writearg(T, N, 1), $put(0')).
  65.  
  66. $writebinop(Op,Term,Oldp,Curp,Newlp,Newrp) :- 
  67.     arg(1,Term,Arg1),
  68.     arg(2,Term,Arg2),
  69.     (Curp > Oldp -> 
  70.         $put(0'(),
  71.         $write(Arg1,Newlp),$tab(1),$write_path_name(Op),
  72.         $tab(1),$write(Arg2,Newrp),
  73.         $put(0'))
  74.           ;
  75.         $write(Arg1,Newlp),$tab(1),$write_path_name(Op),
  76.         $tab(1),$write(Arg2,Newrp)
  77.     ).
  78.  
  79.  
  80. $writepreop(Op,Term,Oldp,Curp,Newp) :- 
  81.     arg(1,Term,Arg),
  82.     (Curp > Oldp -> 
  83.         $put(0'(),
  84.         $write_path_name(Op),$tab(1),$write(Arg,Newp),
  85.         $put(0'))
  86.           ;
  87.         $write_path_name(Op),$tab(1),$write(Arg,Newp)
  88.     ).
  89.  
  90. $writepostop(Op,Term,Oldp,Curp,Newp) :- 
  91.     arg(1,Term,Arg),
  92.     (Curp > Oldp -> 
  93.         $put(0'(),
  94.         $write(Arg,Newp),$tab(1),$write_path_name(Op),
  95.         $put(0'))
  96.           ;
  97.         $write(Arg,Newp),$tab(1),$write_path_name(Op)
  98.     ).
  99.  
  100. $writearg(T, N, N) :- !.
  101. $writearg(T, N, M) :- L is M + 1, $put(0',), arg(L, T, X),
  102.         $write(X,999), $writearg(T, N, L).
  103.  
  104. $writetail(X) :- var(X), ! , $put(0'|), $writename(X), $put(0']).
  105. $writetail([X|Y]) :- !, $put(0',), $write(X,999), $writetail(Y).
  106. $writetail([]) :- !, $put(0']).
  107. $writetail(X) :- $put(0'|), $write(X,999), $put(0']).
  108.  
  109.  
  110. $writeq(T) :- var(T), !, $writeqname(T).
  111. $writeq((X,Y)) :- !, $put(40), $writeq(X), $put(44), $writeqcommatail(Y).
  112.     $writeqcommatail(X) :- var(X), ! , $writeq_path_name(X), $put(41).
  113.     $writeqcommatail((X,Y)) :- !, $writeq(X), $put(44), $writeqcommatail(Y).
  114.     $writeqcommatail(X) :- $writeq(X), $put(41).
  115. $writeq([]) :- !, $writeqname([]).
  116. $writeq([X|Y]) :- !, $put(91), $writeq(X), $writeqtail(Y).
  117.     $writeqtail(X) :- var(X), ! , $put(124),
  118.             $writeq_path_name(X), $put(93).
  119.     $writeqtail([X|Y]) :- !, $put(44),
  120.                            $writeq(X), $writeqtail(Y).
  121.     $writeqtail([]) :- !, $put(93).
  122.     $writeqtail(X) :- $put(124),
  123.             $writeq(X), $put(93).
  124. $writeq(T) :- $structure(T), !, $functor0(T, P), $arity(T, N), 
  125.         $writeq_path_name(P), $put(40), arg(1, T, X), $writeq(X),
  126.         $writeqarg(T, N, 1), $put(41).
  127.     $writeqarg(T, N, N) :- !.
  128.     $writeqarg(T, N, M) :- L is M + 1, $put(44), arg(L, T, X),
  129.         $writeq(X), $writeqarg(T, N, L).
  130. $writeq((':-')) :- !, $put(40), $writeqname((':-')), $put(41).
  131. $writeq(T) :- $writeq_path_name(T).
  132.  
  133. $display(X) :- $telling(OStr), $tell(user), $write(X), $told, $tell(OStr).
  134.  
  135. $print(X) :-
  136.        $write(X).
  137.  
  138. $print_al(N,A0) :-
  139.     integer(N) ->
  140.     (($dismantle_name(A0,A,_) -> true ; A = A0),
  141.      $conlength(A,ALen),
  142.      (ALen >= N -> NTab is 0 ; NTab is N - ALen),
  143.      $print(A),
  144.      $tab(NTab)
  145.     ) ;
  146.     $errmsg('*** illegal input to print_al ***').
  147.  
  148. $print_ar(N,A0) :-
  149.     integer(N) ->
  150.     (($dismantle_name(A0,A,_) -> true ; A = A0),
  151.      $conlength(A,ALen),
  152.      (ALen >= N -> NTab is 0 ; NTab is N - ALen),
  153.      $tab(NTab),
  154.      $print(A)
  155.     ) ;
  156.     $errmsg('*** illegal input to print_ar ***').
  157.  
  158. $errmsg(X) :-
  159.      $telling(CurrOut), $tell(stderr), $write(X), $nl, $told, $tell(CurrOut).
  160.  
  161. % $write_path_name/1 module names are printed instead of the tag.
  162. % If tag does not correspond to any top level structure, search for the
  163. % name of a substructure. If you cannot find the structure tag anywhere, give
  164. % up, and simply print the tag.
  165.  
  166. $write_path_name(Arg) :-
  167.     ( $atom(Arg) ; $structure(Arg) ),
  168.     $functor0(Arg, X),
  169.         $dismantle_name(X,Y,Tag),
  170.         ( Tag == perv -> $writename(X) ;
  171.             ( Tag == 0 -> $writename(Y) ;
  172.                 ( $symtype($module_structure(_,_,_,_,_), Type),
  173.           Type > 0,
  174.           ( $module_structure(Name,Tag,_,_,_) ->
  175.                         $writename_path(Name,Y) ;
  176.                         ( $module_structure(Name,_,Substrs,_,_),
  177.                           $memberchk(Str ---> Tag, Substrs),
  178.                           $writename_path(Name:Str,Y) ) ) ) ) ), !.
  179.  
  180. $write_path_name(X) :- $writename(X).
  181.  
  182. % $writename_path/2
  183.  
  184. $writename_path(Path, Name) :-
  185.         Path = root:X -> $writename_path(X, Name) ;
  186.                 ( Path = X:Y ->
  187.                         ( $writename(X),
  188.                           $writename(':'),
  189.                           $writename_path(Y,Name) ) ;
  190.                         ( $writename(Path),
  191.                           $writename(':'),
  192.                           $writename(Name) ) ).
  193.  
  194. % $writeq_path_name/1 module names are printed instead of the tag.
  195. % If tag does not correspond to any top level structure, search for the
  196. % name of a substructure. If you cannot find the structure tag anywhere, give
  197. % up, and simply print the tag.
  198.  
  199. $writeq_path_name(Arg) :-
  200.     ( $atom(Arg) ; $structure(Arg) ),
  201.     $functor0(Arg, X),
  202.         $dismantle_name(X,Y,Tag),
  203.         ( Tag == perv -> $writeqname(X) ;
  204.             ( Tag == 0 -> $writeqname(Y) ;
  205.                 ( $symtype($module_structure(_,_,_,_,_), Type),
  206.           Type > 0,
  207.           ( $module_structure(Name,Tag,_,_,_) ->
  208.                         $writeqname_path(Name,Y) ;
  209.                         ( $module_structure(Name,_,Substrs,_,_),
  210.                           $memberchk(Str ---> Tag, Substrs),
  211.                           $writeqname_path(Name:Str,Y) ) ) ) ) ), !.
  212.  
  213. $writeq_path_name(X) :- $writeqname(X).
  214.  
  215. % $writeqname_path/2
  216.  
  217. $writeqname_path(Path, Name) :-
  218.         Path = root:X -> $writeqname_path(X, Name) ;
  219.                 ( Path = X:Y ->
  220.                         ( $writeqname(X),
  221.                           $writename(':'),
  222.                           $writeqname_path(Y,Name) ) ;
  223.                         ( $writeqname(Path),
  224.                           $writename(':'),
  225.                           $writeqname(Name) ) ).
  226.  
  227. /* ------------------------------ $io.P ------------------------------ */
  228.