out-paccs.pl 1.82 KB
% == 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 --> [].