out-paccs.pl
1.82 KB
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
% == No, Emacs this is -*-Prolog-*- code, not what you thought... =============
% == Code generation for PaCCS ================================================
cg_emit(fzn(preds(PS), vars(VS), constrs(CS), G), ST) :-
cg_prefix(TEXT, PTXT),
cg_preds(PTXT, VTXT, PS, ST),
cg_vars(VTXT, CTXT, VS, ST),
cg_constrs(CTXT, GTXT, CS, ST),
cg_goal(GTXT, STXT, G, VS, ST),
cg_suffix(STXT, []),
format("%s", [TEXT]).
% == DCGs for code generation =================================================
% -- predicates ---------------------------------------------------------------
cg_preds(_PS, _ST) --> [].
% -- variables ----------------------------------------------------------------
cg_vars([V|Vs], ST) --> cg_var(V, ST), cg_vars(Vs, ST).
cg_vars([], _) --> [].
cg_var(var(N,int(LB,UB),_I,_A), _ST) -->
"fd_int ",
{ name(N, NN) }, NN,
{ format_to_codes(S, " = fd_new(%d, %d);\n", [LB, UB]) }, S.
cg_var(var(N,int,_I,_A), _ST) -->
"fd_int ",
{ name(N, NN) }, NN,
" = fd_new(1, 999);\n". % FIXME: replace with "maxint"
% -- constraints --------------------------------------------------------------
cg_constrs([C|Cs], ST) --> cg_constr(C, ST), cg_constrs(Cs, ST).
cg_constrs([], _) --> [].
cg_constr(_C, _ST) --> []. % FIXME: temporary
% -- goal ---------------------------------------------------------------------
cg_goal(satisfy, VS, ST) -->
"\n",
" if (fd_solve()) {\n",
cg_var_print(VS, ST, " "),
" }\n".
cg_var_print([], _, _) --> [].
cg_var_print([var(N,_,_,A)|Vs], ST, PFX) -->
{ member(output, A), ! },
{ format_to_codes(S, "%sfd_println(%s);\n", [PFX, N]) }, S,
cg_var_print(Vs, ST, PFX).
cg_var_print([_|Vs], ST, PFX) --> cg_var_print(Vs, ST, PFX).
% -- prefix and suffix --------------------------------------------------------
cg_prefix --> [].
cg_suffix --> [].