From 775530979eb3c6360a912132e4b744a81892badb Mon Sep 17 00:00:00 2001 From: Salvador Abreu Date: Thu, 9 Jul 2015 12:01:20 +0900 Subject: [PATCH] first stab at PaCCS code gen --- fz/out-paccs.pl | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+), 0 deletions(-) create mode 100644 fz/out-paccs.pl diff --git a/fz/out-paccs.pl b/fz/out-paccs.pl new file mode 100644 index 0000000..4af8972 --- /dev/null +++ b/fz/out-paccs.pl @@ -0,0 +1,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 --> []. -- libgit2 0.21.2