3 Part of CHR
(Constraint Handling Rules
)
6 E
-mail
: Tom
.Schrijvers
@cs.kuleuven
.ac
.be
7 WWW
: http
://www
.swi
-prolog
.org
8 Copyright
(C
): 2003-2004, K
.U
. Leuven
10 This program is free software
; you can redistribute it
and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation
; either version
2
13 of the License
, or (at your option
) any later version
.
15 This program is distributed
in the hope that it will be useful
,
16 but WITHOUT ANY WARRANTY
; without even the implied warranty of
17 MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE
. See the
18 GNU General Public License
for more details
.
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library
; if not, write to the Free Software
22 Foundation
, Inc
., 59 Temple Place
, Suite
330, Boston
, MA
02111-1307 USA
24 As a special exception
, if you
link this library with other files
,
25 compiled with a Free Software compiler
, to produce an executable
, this
26 library does
not by itself cause the resulting executable to be covered
27 by the GNU General Public License
. This exception does
not however
28 invalidate any other reasons why the executable file might be covered by
29 the GNU General Public License
.
33 [ chr_compile_step1
/2 % +CHRFile
, -PlFile
34 , chr_compile_step2
/2 % +CHRFile
, -PlFile
35 , chr_compile_step3
/2 % +CHRFile
, -PlFile
37 :- use_module
(library
(listing
)). % portray_clause
/2
40 /*******************************
41 * FILE
-TO
-FILE COMPILER
*
42 *******************************/
44 % chr_compile
(+CHRFile
, -PlFile
)
46 % Compile a CHR specification into a Prolog file
48 chr_compile_step1
(From
, To
) :-
49 use_module
('chr_translate_bootstrap.pl'),
50 chr_compile
(From
, To
, informational
).
51 chr_compile_step2
(From
, To
) :-
52 use_module
('chr_translate_bootstrap1.pl'),
53 chr_compile
(From
, To
, informational
).
54 chr_compile_step3
(From
, To
) :-
55 use_module
('chr_translate.pl'),
56 chr_compile
(From
, To
, informational
).
58 chr_compile
(From
, To
, MsgLevel
) :-
59 print_message
(MsgLevel
, chr(start
(From
))),
60 read_chr_file_to_terms
(From
,Declarations
),
61 % read_file_to_terms
(From
, Declarations
,
62 % [ module
(chr) % get operators from here
64 print_message
(silent
, chr(translate
(From
))),
65 chr_translate
(Declarations
, Declarations1
),
66 insert_declarations
(Declarations1
, NewDeclarations
),
67 print_message
(silent
, chr(write(To
))),
68 writefile
(To
, From
, NewDeclarations
),
69 print_message
(MsgLevel
, chr(end
(From
, To
))).
72 insert_declarations
(Clauses0
, Clauses
) :-
73 ( Clauses0
= [:- module
(M
,E
)|FileBody
]
74 -> Clauses
= [ :- module
(M
,E
),
75 :- use_module
('chr_runtime'),
76 :- style_check
(-singleton
),
77 :- style_check
(-discontiguous
)
80 ; Clauses
= [ :- use_module
('chr_runtime'),
81 :- style_check
(-singleton
),
82 :- style_check
(-discontiguous
)
87 % writefile
(+File
, +From
, +Desclarations
)
89 % Write translated CHR declarations to a File
.
91 writefile
(File
, From
, Declarations
) :-
92 open(File
, write, Out
),
93 writeheader
(From
, Out
),
94 writecontent
(Declarations
, Out
),
98 writecontent
([D
|Ds
], Out
) :-
99 portray_clause
(Out
, D
), % SWI
-Prolog
100 writecontent
(Ds
, Out
).
103 writeheader
(File
, Out
) :-
105 convert_time
(Now
, Date
),
106 format
(Out
, '/* Generated by CHR bootstrap compiler~n', []),
107 format
(Out
, ' From: ~w~n', [File
]),
108 format
(Out
, ' Date: ~w~n~n', [Date
]),
109 format
(Out
, ' DO NOT EDIT. EDIT THE CHR FILE INSTEAD~n', []),
110 format
(Out
, '*/~n~n', []).
113 /*******************************
115 *******************************/
121 prolog
:message
(chr(start
(File
))) -->
122 { file_base_name
(File
, Base
)
124 [ 'Translating CHR file ~w'-[Base
] ].
125 prolog
:message
(chr(end
(_From
, To
))) -->
126 { file_base_name
(To
, Base
)
128 [ 'Written translation to ~w'-[Base
] ].
130 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131 read_chr_file_to_terms
(Spec
, Terms
) :-
132 absolute_file_name
(Spec
, [ access
(read) ],
134 open(Path
, read, Fd
, []),
135 read_chr_stream_to_terms
(Fd
, Terms
),
138 read_chr_stream_to_terms
(Fd
, Terms
) :-
139 read_term
(Fd
, C0
, [ module
(chr) ]),
140 read_chr_stream_to_terms
(C0
, Fd
, Terms
).
142 read_chr_stream_to_terms
(end_of_file
, _
, []) :- !.
143 read_chr_stream_to_terms
(C
, Fd
, [C
|T
]) :-
145 C
= (:- op
(Priority
,Type
,Name
)) ->
146 op
(Priority
,Type
,Name
)
150 read_term
(Fd
, C2
, [module
(chr)]),
151 read_chr_stream_to_terms
(C2
, Fd
, T
).