diff --git a/calc.cpp b/calc.cpp deleted file mode 100644 index ab15a47..0000000 --- a/calc.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/* Translation of calc.c example to C++ - (the "-goal true" in the following turns off the banner) - - swipl-ld -o calc -goal true calc.cpp calc.pl && ./calc 1 + 2 + 3 - */ - -#include -#include - -int main(int argc, char **argv) { - - PlEngine e(argv[0]); - - // combine all the arguments in a single string - std::string expression; - for (int n = 1; n < argc; n++) { - if (n != 1) { - expression.append(" "); - } - expression.append(argv[n]); - } - - // Lookup calc/1 and make the arguments and call - - PlPredicate pred("calc", 1, "user"); - PlTerm_string h0(expression); - PlQuery q(pred, PlTermv(h0)); - - return PlWrap(q.next_solution()) ? 0 : 1; -} diff --git a/calc.pl b/calc.pl deleted file mode 100644 index b5e6ca9..0000000 --- a/calc.pl +++ /dev/null @@ -1,5 +0,0 @@ -calc(Atom) :- - term_to_atom(Expr, Atom), - A is Expr, - write(A), - nl. diff --git a/pl2cpp.plx b/pl2cpp.plx index 609f640..822ed4e 100644 --- a/pl2cpp.plx +++ b/pl2cpp.plx @@ -239,11 +239,26 @@ rewritten in C++. As before, it is compiled by swipl-ld -o calc -goal true calc.cpp calc.pl \end{code} +The Prolog file is simple: + +\begin{code} +calc(String) :- + term_string(Expr, String), + A is Expr, + writeln(A). +\end{code} + +The C part of the application parses the command line options, +initialises the Prolog engine, locates the \verb$calc/1$ predicate and calls +it. The code is in \figref{calccpp}. + +\begin{figure} \begin{code} #include #include -int main(int argc, char **argv) { +int +main(int argc, char **argv) { PlEngine e(argv[0]); @@ -265,6 +280,9 @@ int main(int argc, char **argv) { return PlWrap(q.next_solution()) ? 0 : 1; } \end{code} +\caption{C++ source for the calc application} +\label{fig:calccpp} +\end{figure} \section{Sample code} \label{sec:cpp2-sample-code}