Commit 775530979eb3c6360a912132e4b744a81892badb

Authored by Salvador Abreu
1 parent da6d2c72
Exists in master

first stab at PaCCS code gen

Showing 1 changed file with 61 additions and 0 deletions   Show diff stats
fz/out-paccs.pl 0 → 100644
@@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
  1 +% == No, Emacs this is -*-Prolog-*- code, not what you thought... =============
  2 +
  3 +% == Code generation for PaCCS ================================================
  4 +
  5 +cg_emit(fzn(preds(PS), vars(VS), constrs(CS), G), ST) :-
  6 + cg_prefix(TEXT, PTXT),
  7 + cg_preds(PTXT, VTXT, PS, ST),
  8 + cg_vars(VTXT, CTXT, VS, ST),
  9 + cg_constrs(CTXT, GTXT, CS, ST),
  10 + cg_goal(GTXT, STXT, G, VS, ST),
  11 + cg_suffix(STXT, []),
  12 + format("%s", [TEXT]).
  13 +
  14 +% == DCGs for code generation =================================================
  15 +
  16 +% -- predicates ---------------------------------------------------------------
  17 +
  18 +cg_preds(_PS, _ST) --> [].
  19 +
  20 +% -- variables ----------------------------------------------------------------
  21 +
  22 +cg_vars([V|Vs], ST) --> cg_var(V, ST), cg_vars(Vs, ST).
  23 +cg_vars([], _) --> [].
  24 +
  25 +cg_var(var(N,int(LB,UB),_I,_A), _ST) -->
  26 + "fd_int ",
  27 + { name(N, NN) }, NN,
  28 + { format_to_codes(S, " = fd_new(%d, %d);\n", [LB, UB]) }, S.
  29 +cg_var(var(N,int,_I,_A), _ST) -->
  30 + "fd_int ",
  31 + { name(N, NN) }, NN,
  32 + " = fd_new(1, 999);\n". % FIXME: replace with "maxint"
  33 +
  34 +
  35 +% -- constraints --------------------------------------------------------------
  36 +
  37 +cg_constrs([C|Cs], ST) --> cg_constr(C, ST), cg_constrs(Cs, ST).
  38 +cg_constrs([], _) --> [].
  39 +
  40 +cg_constr(_C, _ST) --> []. % FIXME: temporary
  41 +
  42 +% -- goal ---------------------------------------------------------------------
  43 +
  44 +cg_goal(satisfy, VS, ST) -->
  45 + "\n",
  46 + " if (fd_solve()) {\n",
  47 + cg_var_print(VS, ST, " "),
  48 + " }\n".
  49 +
  50 +cg_var_print([], _, _) --> [].
  51 +cg_var_print([var(N,_,_,A)|Vs], ST, PFX) -->
  52 + { member(output, A), ! },
  53 + { format_to_codes(S, "%sfd_println(%s);\n", [PFX, N]) }, S,
  54 + cg_var_print(Vs, ST, PFX).
  55 +cg_var_print([_|Vs], ST, PFX) --> cg_var_print(Vs, ST, PFX).
  56 +
  57 +
  58 +% -- prefix and suffix --------------------------------------------------------
  59 +
  60 +cg_prefix --> [].
  61 +cg_suffix --> [].