basix_doc 0.1
/Users/mourrain/Devel/mmx/basix/src/math_printer.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : math_printer.cpp
00004 * DESCRIPTION: Printing math expressions for TeXmacs
00005 * COPYRIGHT  : (C) 2007  Gregoire Lecerf
00006 *******************************************************************************
00007 * This software falls under the GNU general public license and comes WITHOUT
00008 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
00009 * If you don't have this file, write to the Free Software Foundation, Inc.,
00010 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00011 ******************************************************************************/
00012 
00013 #include <basix/string.hpp>
00014 #include <basix/table.hpp>
00015 #include <basix/literal.hpp>
00016 #include <basix/compound.hpp>
00017 
00018 namespace mmx {
00019 
00020 #define SET_OPNAME(var, val) \
00021   t [literal_to_string (var)] = val;
00022 
00023 static table<string, string, exact_eq_table> 
00024 math_symbol_table () {
00025   table<string, string, exact_eq_table> t;
00026 
00027   SET_OPNAME (GEN_ACCESS, "_"); // or "%sqaccess"
00028   SET_OPNAME (GEN_AND, "/\\");
00029   SET_OPNAME (GEN_CATALAN, "%mathcatalan");
00030   SET_OPNAME (GEN_COMPOSE, "%circ");
00031   SET_OPNAME (GEN_APPEND, "%join");
00032   SET_OPNAME (GEN_DELTA, "%delta");
00033   SET_OPNAME (GEN_DERIVATIVE, "D");
00034   SET_OPNAME (GEN_DOT, "%dotaccess");
00035   SET_OPNAME (GEN_DIV, "div");
00036   SET_OPNAME (GEN_E, "%mathe");
00037   SET_OPNAME (GEN_EQUAL, "=");
00038   SET_OPNAME (GEN_EQUIV, "<=>");
00039   SET_OPNAME (GEN_EULER, "%matheuler");
00040   SET_OPNAME (GEN_FACTORIAL, "factorial");
00041   SET_OPNAME (GEN_GTR, ">");
00042   SET_OPNAME (GEN_GTREQ, ">=");
00043   SET_OPNAME (GEN_GTRGTR, ">>");
00044   SET_OPNAME (GEN_I, "%mathi");
00045   SET_OPNAME (GEN_IMPLIES, "=>");
00046   SET_OPNAME (GEN_IN, "%in");
00047   SET_OPNAME (GEN_INFINITY, "%infty");
00048   SET_OPNAME (GEN_INTO, "->");
00049   SET_OPNAME (GEN_LESS, "<");
00050   SET_OPNAME (GEN_LESSEQ, "<=");
00051   SET_OPNAME (GEN_LESSLESS, "<<");
00052   SET_OPNAME (GEN_MAPSTO, "#{:->}#");
00053   SET_OPNAME (GEN_MINUS, "-");
00054   SET_OPNAME (GEN_MOD, "mod");
00055   SET_OPNAME (GEN_NOT, "!");
00056   SET_OPNAME (GEN_OR, "\\/");
00057   SET_OPNAME (GEN_OVER, "/");
00058   SET_OPNAME (GEN_PARTIAL, "%partial");
00059   SET_OPNAME (GEN_PI, "%mathpi");
00060   SET_OPNAME (GEN_PLUS, "+&");
00061   SET_OPNAME (GEN_POWER, "^");
00062   SET_OPNAME (GEN_PRIME, "%prime");
00063   SET_OPNAME (GEN_RANGE, "%ldots"); // problem with iterator flattening
00064   SET_OPNAME (GEN_SCOPE, "%colons");
00065   SET_OPNAME (GEN_TO, "to");
00066   SET_OPNAME (GEN_DOWNTO, "downto");
00067   SET_OPNAME (GEN_ROW, "row");
00068   SET_OPNAME (GEN_SEQAND, "and");
00069   SET_OPNAME (GEN_SEQOR, "or");
00070   SET_OPNAME (GEN_SIZE, "%card");
00071   SET_OPNAME (GEN_SQTUPLE, "list");
00072   SET_OPNAME (GEN_TIMES, "*&");
00073   SET_OPNAME (GEN_TRANSTYPE, "#{:>}#");
00074   SET_OPNAME (GEN_TUPLE, "tuple");
00075   //SET_OPNAME (GEN_TYPE, "#{:}#");
00076   SET_OPNAME (GEN_UNEQUAL, "!=");
00077   SET_OPNAME (GEN_VWHERE, "|");
00078   SET_OPNAME (GEN_WHERE, "||");
00079   SET_OPNAME (GEN_XOR, "%veebar");
00080 
00081   t["alpha"] = "%alpha";
00082   t["beta"] = "%beta";
00083   t["gamma"] = "%gamma";
00084   t["delta"] = "%delta";
00085   t["epsilon"] = "%varepsilon";
00086   t["zeta"] = "%zeta";
00087   t["eta"] = "%eta";
00088   t["theta"] = "%theta";
00089   t["iota"] = "%iota";
00090   t["kappa"] = "%kappa";
00091   t["lambda"] = "%lambda";
00092   t["mu"] = "%mu";
00093   t["nu"] = "%nu";
00094   t["xi"] = "%xi";
00095   t["omicron"] = "%omicron";
00096   t["pi"] = "%pi";
00097   t["rho"] = "%rho";
00098   t["sigma"] = "%sigma";
00099   t["tau"] = "%tau";
00100   t["upsilon"] = "%upsilon";
00101   t["phi"] = "%varphi";
00102   t["chi"] = "%chi";
00103   t["psi"] = "%psi";
00104   t["omega"] = "%omega";
00105 
00106   t["Alpha"] = "%Alpha";
00107   t["Beta"] = "%Beta";
00108   t["Gamma"] = "%Gamma";
00109   t["Delta"] = "%Delta";
00110   t["Epsilon"] = "%Epsilon";
00111   t["Zeta"] = "%Zeta";
00112   t["Eta"] = "%Eta";
00113   t["Theta"] = "%Theta";
00114   t["Iota"] = "%Iota";
00115   t["Kappa"] = "%Kappa";
00116   t["Lambda"] = "%Lambda";
00117   t["Mu"] = "%Mu";
00118   t["Nu"] = "%Nu";
00119   t["Xi"] = "%Xi";
00120   t["Omicron"] = "%Omicron";
00121   t["Pi"] = "%Pi";
00122   t["Rho"] = "%Rho";
00123   t["Sigma"] = "%Sigma";
00124   t["Tau"] = "%Tau";
00125   t["Upsilon"] = "%Upsilon";
00126   t["Phi"] = "%Phi";
00127   t["Chi"] = "%Chi";
00128   t["Psi"] = "%Psi";
00129   t["Omega"] = "%Omega";
00130   return t;
00131 }
00132 
00133 static void
00134 as_math (string& out, const generic& g) {
00135   static table<string, string, exact_eq_table> t (math_symbol_table ());
00136   if (is<literal> (g)) {
00137     string tmp;
00138     string s (literal_to_string (g));
00139     if (t->get (s, tmp))
00140       out << tmp;
00141     else if (N(s) > 0 && s[0]>='0' && s[0] <= '9')
00142       out << "\"" << s << "\"";
00143     else
00144       out << s;
00145   }
00146   else if (is<compound> (g)) {
00147     nat i, n= N (g);
00148     out << "(";
00149     if (n>0) {
00150       if (exact_eq (g[0], GEN_SQTUPLE) && n>1 &&
00151           is<compound> (g[1]) && exact_eq (g[1][0], GEN_ROW))
00152         out << "matrix";
00153       else as_math (out, g[0]);
00154     }
00155     for (i=1; i<n; i++) {
00156       out << " ";
00157       as_math (out, g[i]);
00158     }
00159     out << ")";
00160   }
00161   else {
00162     generic f= as_generic (flatten (g));
00163     as_math (out, f);
00164   }
00165 }
00166 
00167 string
00168 as_math (const generic& g) {
00169   string out;
00170   as_math (out, g);
00171   return out;
00172 }
00173 
00174 } // namespace mmx
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines