1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
% -----------------------------------------------------------------------------
% (C) Altran Praxis Limited
% -----------------------------------------------------------------------------
%
% The SPARK toolset is free software; you can redistribute it and/or modify it
% under terms of the GNU General Public License as published by the Free
% Software Foundation; either version 3, or (at your option) any later
% version. The SPARK toolset is distributed in the hope that it will be
% useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
% Public License for more details. You should have received a copy of the GNU
% General Public License distributed with the SPARK toolset; see file
% COPYING3. If not, go to http://www.gnu.org/licenses for a complete copy of
% the license.
%
% =============================================================================
%###############################################################################
% PURPOSE
%-------------------------------------------------------------------------------
% Clauses to control formatting of vc output. These must appear in the user
% module.
%###############################################################################
%###############################################################################
% DEPENDENCIES
%###############################################################################
%###############################################################################
% TYPES
%###############################################################################
%###############################################################################
% DATA
%###############################################################################
/* Portray a BINARY operator */
portray(Term):-
nonvar(Term),
functor(Term, Name_Atom, 2),
arg(1,Term, A),
arg(2,Term, B),
/* Find a BINARY operator that matches */
current_op(Pred,F,Name_Atom),
(
F = xfx
;
F = yfx
;
F = xfy
),
/* Find the precedences of sub-terms A and B */
term_pri(A, AP),
term_pri(B, BP),
(
/* If A's principal functor is lower precedence than that of Term, then */
/* we need to parathesize A */
/* Additionally, for xfx operators, we parenthesise if A's functor is */
/* equal in precedence to that of Term */
(
F = xfx,
AP >= Pred
;
F \= xfx,
AP > Pred
),
write('('),
write_term(A, [priority(Pred), portrayed(true), portray(true), numbervars(true)]),
write(')')
;
(
F = xfx,
AP < Pred
;
F \= xfx,
AP =< Pred
),
write_term(A, [priority(Pred), portrayed(true), portray(true), numbervars(true)])
),
write(' '),
write(Name_Atom),
write(' '),
(
/* If B's principal functor is lower or equal precedence than that of Term, then */
/* we need to parathesize B */
/* This is to re-produce the POPLOG behaviour where multi-term expressions */
/* with equal-precedence operators are printed with the RHS parenthesised */
/* For example, we want "A + (B + C)" NOT "A + B + C" */
BP >= Pred,
write('('),
write_term(B, [priority(Pred), portrayed(true), portray(true), numbervars(true)]),
write(')')
;
BP < Pred,
write_term(B, [priority(Pred), portrayed(true), portray(true), numbervars(true)])
),
!.
/* Portray an UNARY operator of type fx or fy */
portray(Term):-
nonvar(Term),
functor(Term, Name_Atom, 1),
arg(1,Term, A),
/* Find a UNARY operator that matches */
(
current_op(Pred,fx,Name_Atom)
;
current_op(Pred,fy,Name_Atom)
),
/* Find the precedences of sub-term A */
term_pri(A, AP),
write(Name_Atom),
write(' '),
(
/* If A's principal functor is lower precedence than that of Term, then */
/* we need to parathesize A */
AP > Pred,
write('('),
write_term(A, [priority(Pred), portrayed(true), portray(true), numbervars(true)]),
write(')')
;
AP =< Pred,
write_term(A, [priority(Pred), portrayed(true), portray(true), numbervars(true)])
),
!.
/* Portray an UNARY operator of type xf or yf */
portray(Term):-
nonvar(Term),
functor(Term, Name_Atom, 1),
arg(1,Term, A),
/* Find a UNARY operator that matches */
(
current_op(Pred,xf,Name_Atom)
;
current_op(Pred,yf,Name_Atom)
),
/* Find the precedences of sub-term B */
term_pri(A, AP),
(
/* If A's principal functor is lower precedence than that of Term, then */
/* we need to parathesize A */
AP > Pred,
write('('),
write_term(A, [priority(Pred), portrayed(true), portray(true), numbervars(true)]),
write(')')
;
AP =< Pred,
write_term(A, [priority(Pred), portrayed(true), portray(true), numbervars(true)])
),
write(' '),
write(Name_Atom),
!.
/* Portray clauses for built-in PROLOG operators */
portray(List) :-
(List = [] ; List = [_|_]), !,
print_list(List).
/* Catch all - functor F, which is not an operator, with non-zero */
/* number of arguments - print as F(Args) with commas */
/* between the arguments. */
portray(X) :-
X =.. [F|Args],
atomic(F),
Args \== [],
!,
write(F),
write('('),
print_list1(Args),
write(')'),
!.
/* print_list/1 - prints a list enclosed in [ ] with */
/* comma and space between each element */
print_list(List) :-
write('['),
print_list1(List),
write(']').
print_list1([X]) :- !,
print(X).
print_list1([X|Xs]) :- !,
print(X),
write(', '),
print_list1(Xs).
print_list1([]).
/* term_pri/2 - returns the precedence of the principal functor of Term */
term_pri(Term, Prio) :-
/* Careful here to only look for BINARY operators */
nonvar(Term),
functor(Term, Name_Atom, 2),
(
current_op(Prio, xfx, Name_Atom)
;
current_op(Prio, yfx, Name_Atom)
;
current_op(Prio, xfy, Name_Atom)
).
term_pri(Term, Prio) :-
/* Careful here to only look for UNARY operators */
nonvar(Term),
functor(Term, Name_Atom, 1),
(
current_op(Prio, fx, Name_Atom)
;
current_op(Prio, fy, Name_Atom)
;
current_op(Prio, xf, Name_Atom)
;
current_op(Prio, yf, Name_Atom)
).
/* We don't want to parenthesise atoms, literals and so on, so we pretend */
/* that these have a very high precedence */
term_pri(_Term, Prio) :-
Prio = 1,
!.
%###############################################################################
% END-OF-FILE
|