[pure-lang-svn] SF.net SVN: pure-lang: [298] pure/trunk
Status: Beta
Brought to you by:
agraef
From: <ag...@us...> - 2008-06-24 14:36:04
|
Revision: 298 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=298&view=rev Author: agraef Date: 2008-06-24 07:36:02 -0700 (Tue, 24 Jun 2008) Log Message: ----------- Fix some C compilation quirks and add some error reporting to the eval function. Modified Paths: -------------- pure/trunk/interpreter.cc pure/trunk/interpreter.hh pure/trunk/lib/strings.pure pure/trunk/runtime.cc pure/trunk/runtime.h Modified: pure/trunk/interpreter.cc =================================================================== --- pure/trunk/interpreter.cc 2008-06-24 12:48:18 UTC (rev 297) +++ pure/trunk/interpreter.cc 2008-06-24 14:36:02 UTC (rev 298) @@ -333,30 +333,46 @@ interpreter::error(const yy::location& l, const string& m) { nerrs++; - cout.flush(); - if (!source_s) cerr << l << ": " << m << endl; + if (source_s) { + ostringstream msg; + msg << l << ": " << m << endl; + errmsg += msg.str(); + } else { + cout.flush(); + cerr << l << ": " << m << endl; + } } void interpreter::error(const string& m) { nerrs++; - cout.flush(); - if (!source_s) cerr << m << endl; + if (source_s) { + ostringstream msg; + msg << m << endl; + errmsg += msg.str(); + } else { + cout.flush(); + cerr << m << endl; + } } void interpreter::warning(const yy::location& l, const string& m) { - cout.flush(); - if (!source_s) cerr << l << ": " << m << endl; + if (!source_s) { + cout.flush(); + cerr << l << ": " << m << endl; + } } void interpreter::warning(const string& m) { - cout.flush(); - if (!source_s) cerr << m << endl; + if (!source_s) { + cout.flush(); + cerr << m << endl; + } } // Run the interpreter on a source file, collection of source files, or on @@ -392,6 +408,7 @@ // initialize nerrs = 0; source = s; declare_op = false; + errmsg.clear(); if (check && !interactive) temp = 0; bool ok = lex_begin(); if (ok) { @@ -443,6 +460,7 @@ nerrs = 0; source = ""; declare_op = false; source_s = s.c_str(); + errmsg.clear(); bool ok = lex_begin(); if (ok) { yy::parser parser(*this); Modified: pure/trunk/interpreter.hh =================================================================== --- pure/trunk/interpreter.hh 2008-06-24 12:48:18 UTC (rev 297) +++ pure/trunk/interpreter.hh 2008-06-24 14:36:02 UTC (rev 298) @@ -262,6 +262,7 @@ // Interpreter state. For internal use only. int nerrs; // current error count + string errmsg; // last reported error (runstr) string source; // the source being parsed const char *source_s; // source pointer if input comes from a string set<string> sources; // the list of all scripts which have been loaded @@ -293,7 +294,9 @@ pure_expr *run(const list<string>& sources, bool check = true); /* This works like run() above, but takes the source directly from a - string. */ + string. No error messages will be printed, instead any errors reported + during the most recent invokation of this method are available in + errmsg. */ pure_expr *runstr(const string& source); /* Evaluate a (compile time) expression and return the (runtime expression) Modified: pure/trunk/lib/strings.pure =================================================================== --- pure/trunk/lib/strings.pure 2008-06-24 12:48:18 UTC (rev 297) +++ pure/trunk/lib/strings.pure 2008-06-24 14:36:02 UTC (rev 298) @@ -23,10 +23,14 @@ eval function does the opposite, by parsing and returning the value of an expression specified as a string in Pure syntax. (In fact, eval goes well beyond this, as it can parse and execute arbitrary Pure code. In that case - it will return the last computed expression, if any.) */ + it will return the last computed expression, if any.) Errors are reported + with the lasterr routine. This string value will be nonempty iff an error + was encountered during the most recent invokation of eval(). In that case + each reported error message is terminated with a newline character. */ extern void* str(expr*) = pure_str; extern expr* eval(char*); // IMPURE! +extern char* lasterr(); str x = cstring (pure_str x); Modified: pure/trunk/runtime.cc =================================================================== --- pure/trunk/runtime.cc 2008-06-24 12:48:18 UTC (rev 297) +++ pure/trunk/runtime.cc 2008-06-24 14:36:02 UTC (rev 298) @@ -814,7 +814,7 @@ #include <llvm/Target/TargetOptions.h> extern "C" -pure_interp *pure_create_interp(int argc, const char *argv[]) +pure_interp *pure_create_interp(int argc, char *argv[]) { // This is pretty much the same as pure.cc:main(), except that some options // are ignored and there's no user interaction. @@ -847,7 +847,7 @@ #endif // scan the command line options list<string> myargs; - for (const char **args = ++argv; *args; ++args) + for (char **args = ++argv; *args; ++args) if (*args == string("-h")) /* ignored */; else if (*args == string("-i")) @@ -1984,12 +1984,20 @@ { assert(s); interpreter& interp = *interpreter::g_interp; + interp.errmsg.clear(); pure_expr *res = interp.runstr(string(s)+";"); interp.result = 0; if (res) pure_unref_internal(res); return res; } +extern "C" +const char *lasterr() +{ + interpreter& interp = *interpreter::g_interp; + return interp.errmsg.c_str(); +} + static uint32_t mpz_hash(const mpz_t z) { uint32_t h = 0; Modified: pure/trunk/runtime.h =================================================================== --- pure/trunk/runtime.h 2008-06-24 12:48:18 UTC (rev 297) +++ pure/trunk/runtime.h 2008-06-24 14:36:02 UTC (rev 298) @@ -250,9 +250,9 @@ the library API (see below) to first unparse the expression in the source interpreter and then reparse it in the target interpreter. */ -typedef struct pure_interp; // Pure interpreter handles (opaque). +typedef struct _pure_interp pure_interp; // Pure interpreter handles (opaque). -pure_interp *pure_create_interp(int argc, const char *argv[]); +pure_interp *pure_create_interp(int argc, char *argv[]); void pure_delete_interp(pure_interp *interp); void pure_switch_interp(pure_interp *interp); @@ -444,12 +444,19 @@ /* Convert a Pure expression to a string and vice versa. Note that eval() will actually parse and execute any Pure source, so it can be used, e.g., to add new rules to the executing program at runtime. The result of eval() is the - last computed expression (NULL if none). The result of str() is a malloc'ed - string in the system encoding which must be freed by the caller. */ + last computed expression, NULL if none; in the latter case you can inspect + the result of lasterr() below to determine whether there were any errors. + The result of str() is a malloc'ed string in the system encoding which must + be freed by the caller. */ char *str(const pure_expr *x); pure_expr *eval(const char *s); +/* After an invokation of eval(), this returns error messages from the + interpreter (an empty string if none). */ + +const char *lasterr(); + /* Compute a 32 bit hash code of a Pure expression. This makes it possible to use arbitary Pure values as keys in a hash table. */ This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |