debug off -> optimize on
[chr.git] / chr_swi_bootstrap.pl
blob33139e72979ffa96dde095327ee6b7a48eb162a2
1 /* $Id$
3 Part of CHR (Constraint Handling Rules)
5 Author: Tom Schrijvers
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.
32 :- module(chr,
33 [ chr_compile_step1/2 % +CHRFile, -PlFile
34 , chr_compile_step2/2 % +CHRFile, -PlFile
35 , chr_compile_step3/2 % +CHRFile, -PlFile
36 , chr_compile_step4/2 % +CHRFile, -PlFile
37 ]).
38 :- use_module(library(listing)). % portray_clause/2
39 :- include('chr_op').
41 /*******************************
42 * FILE-TO-FILE COMPILER *
43 *******************************/
45 % chr_compile(+CHRFile, -PlFile)
47 % Compile a CHR specification into a Prolog file
49 chr_compile_step1(From, To) :-
50 use_module('chr_translate_bootstrap.pl'),
51 chr_compile(From, To, informational).
52 chr_compile_step2(From, To) :-
53 use_module('chr_translate_bootstrap1.pl'),
54 chr_compile(From, To, informational).
55 chr_compile_step3(From, To) :-
56 use_module('chr_translate_bootstrap2.pl'),
57 chr_compile(From, To, informational).
58 chr_compile_step4(From, To) :-
59 use_module('chr_translate.pl'),
60 chr_compile(From, To, informational).
62 chr_compile(From, To, MsgLevel) :-
63 print_message(MsgLevel, chr(start(From))),
64 read_chr_file_to_terms(From,Declarations),
65 % read_file_to_terms(From, Declarations,
66 % [ module(chr) % get operators from here
67 % ]),
68 print_message(silent, chr(translate(From))),
69 chr_translate(Declarations, Declarations1),
70 insert_declarations(Declarations1, NewDeclarations),
71 print_message(silent, chr(write(To))),
72 writefile(To, From, NewDeclarations),
73 print_message(MsgLevel, chr(end(From, To))).
76 insert_declarations(Clauses0, Clauses) :-
77 ( Clauses0 = [:- module(M,E)|FileBody]
78 -> Clauses = [ :- module(M,E),
79 :- use_module('chr_runtime'),
80 :- style_check(-singleton),
81 :- style_check(-discontiguous)
82 | FileBody
84 ; Clauses = [ :- use_module('chr_runtime'),
85 :- style_check(-singleton),
86 :- style_check(-discontiguous)
87 | Clauses0
91 % writefile(+File, +From, +Desclarations)
93 % Write translated CHR declarations to a File.
95 writefile(File, From, Declarations) :-
96 open(File, write, Out),
97 writeheader(From, Out),
98 writecontent(Declarations, Out),
99 close(Out).
101 writecontent([], _).
102 writecontent([D|Ds], Out) :-
103 portray_clause(Out, D), % SWI-Prolog
104 writecontent(Ds, Out).
107 writeheader(File, Out) :-
108 get_time(Now),
109 convert_time(Now, Date),
110 format(Out, '/* Generated by CHR bootstrap compiler~n', []),
111 format(Out, ' From: ~w~n', [File]),
112 format(Out, ' Date: ~w~n~n', [Date]),
113 format(Out, ' DO NOT EDIT. EDIT THE CHR FILE INSTEAD~n', []),
114 format(Out, '*/~n~n', []).
117 /*******************************
118 * MESSAGES *
119 *******************************/
122 :- multifile
123 prolog:message/3.
125 prolog:message(chr(start(File))) -->
126 { file_base_name(File, Base)
128 [ 'Translating CHR file ~w'-[Base] ].
129 prolog:message(chr(end(_From, To))) -->
130 { file_base_name(To, Base)
132 [ 'Written translation to ~w'-[Base] ].
134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 read_chr_file_to_terms(Spec, Terms) :-
136 absolute_file_name(Spec, [ access(read) ],
137 Path),
138 open(Path, read, Fd, []),
139 read_chr_stream_to_terms(Fd, Terms),
140 close(Fd).
142 read_chr_stream_to_terms(Fd, Terms) :-
143 read_term(Fd, C0, [ module(chr) ]),
144 read_chr_stream_to_terms(C0, Fd, Terms).
146 read_chr_stream_to_terms(end_of_file, _, []) :- !.
147 read_chr_stream_to_terms(C, Fd, [C|T]) :-
148 ( ground(C),
149 C = (:- op(Priority,Type,Name)) ->
150 op(Priority,Type,Name)
152 true
154 read_term(Fd, C2, [module(chr)]),
155 read_chr_stream_to_terms(C2, Fd, T).