00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 #include <basix/mmc_glue.hpp>
00014 #include <basix/source_track.hpp>
00015 #include <basix/mmx_syntax.hpp>
00016 namespace mmx {
00017 
00018 
00019 
00020 
00021 
00022 vector<string> mmc_args;
00023 
00024 void (*compiler_initialize) (void) = NULL;
00025 void (*compiler_terminate) (void) = NULL;
00026 
00027 void
00028 mmc_initialize (int argc, char** argv) {
00029   mmc_args= fill<string> (argc);
00030   for (int i= 0; i<argc; i++)
00031     mmc_args[(nat) i]= string (argv[i]);
00032   if (compiler_initialize != NULL) compiler_initialize ();
00033 }
00034 
00035 void
00036 mmc_terminate () {
00037   if (compiler_terminate != NULL) compiler_terminate ();
00038 }
00039 
00040 void
00041 mmc_exit (const int& status) {
00042   mmc_terminate ();
00043   exit (status);
00044 }
00045 
00046 
00047 
00048 
00049 
00050 vector<string> mmc_load_directory (const string& name) {
00051   string dir_name= relative_name ("", name);
00052   vector<string> dir;
00053   if (load_directory (dir_name, dir))
00054     ERROR ("directory " * dir_name * " not found");
00055   return dir;
00056 }
00057 
00058 void
00059 mmc_save (const string& name, const string& s) {
00060   (void) save (name, s);
00061 }
00062 
00063 string
00064 mmc_load (const string& name) {
00065   string s;
00066   string file_name= name; 
00067   bool e= load (file_name, s);
00068   ASSERT (!e, "file did not load");
00069   return s;
00070 }
00071 
00072 static string
00073 strip_preamble (const string& s) {
00074   string preamble= "#!/usr/bin/env mmx-light\n";
00075   if (N(s) >= N(preamble) && s (0, N(preamble)) == preamble)
00076     return s (N(preamble)-1, N(s));
00077   return s;
00078 }
00079 
00080 generic
00081 mmc_parse (const string& file_name) {
00082   string s;
00083   if (load (file_name, s))
00084     ERROR ("file " * file_name * " not found");
00085   s= strip_preamble (s);
00086   store_file_source (file_name, s);
00087   generic r= mmx_parse (file_name, s);
00088   if (is<exception> (r)) throw as<exception> (r);
00089   return r;
00090 }
00091 
00092 generic
00093 mmc_parse (const string& s, const string& input_name) {
00094   store_file_source (input_name, s);
00095   generic r= mmx_parse (input_name, s);
00096   if (is<exception> (r)) throw as<exception> (r);
00097   return r;  
00098 }
00099 
00100 
00101 
00102 
00103 
00104 exception
00105 mmc_exception (const string& message, const generic& where) {
00106   return exception (message, where);
00107 }
00108 
00109 string
00110 as_string (const exception& e) {
00111   return source_exception (e);
00112 }
00113 
00114 generic
00115 mmc_unliteral (const generic& x) {
00116   if (is_func (x, "literal_integer", 1))
00117     return as<generic> (as_int (as_string (as<literal> (x[1]))));
00118   else if (is_func (x, "literal_floating", 1))
00119     return as<generic> (as_double (as_string (as<literal> (x[1]))));
00120   else if (is_func (x, "literal_string", 1))
00121     return as<generic> (unquote (as_string (as<literal> (x[1]))));
00122   else {
00123     mmerr << "x= " << x << "\n";
00124     ERROR ("unknown literal type");
00125   }
00126 }
00127 
00128 }