[pure-lang-svn] SF.net SVN: pure-lang:[719] pure/trunk
Status: Beta
Brought to you by:
agraef
From: <ag...@us...> - 2008-09-05 13:40:37
|
Revision: 719 http://pure-lang.svn.sourceforge.net/pure-lang/?rev=719&view=rev Author: agraef Date: 2008-09-05 13:40:46 +0000 (Fri, 05 Sep 2008) Log Message: ----------- User can now override print representations of expressions at runtime via the __show__ function. Modified Paths: -------------- pure/trunk/ChangeLog pure/trunk/printer.cc pure/trunk/symtable.cc pure/trunk/symtable.hh Modified: pure/trunk/ChangeLog =================================================================== --- pure/trunk/ChangeLog 2008-09-05 12:11:50 UTC (rev 718) +++ pure/trunk/ChangeLog 2008-09-05 13:40:46 UTC (rev 719) @@ -1,5 +1,9 @@ 2008-09-05 Albert Graef <Dr....@t-...> + * printer.cc (operator << (ostream& os, const pure_expr *x)): + Experimental support for calling a user-defined __show__ function + to override print representations of expressions at runtime. + * configure.ac, runtime.cc (pure_sys_vars): Add configure checks for POSIX/ISOC99 complex types. (Requires reconfigure.) Modified: pure/trunk/printer.cc =================================================================== --- pure/trunk/printer.cc 2008-09-05 12:11:50 UTC (rev 718) +++ pure/trunk/printer.cc 2008-09-05 13:40:46 UTC (rev 719) @@ -622,6 +622,28 @@ return os << p.x; } +static inline bool pstr(ostream& os, pure_expr *x) +{ + interpreter& interp = *interpreter::g_interp; + int32_t f = interp.symtab.__show__sym; + if (f > 0 && interp.globenv.find(f) != interp.globenv.end()) { + assert(x->refc > 0); + pure_expr *y = pure_app(pure_symbol(f), x); + assert(y); + if (y->tag == EXPR::STR) { + char *s = fromutf8(y->data.s); + pure_freenew(y); + if (s) { + os << s; free(s); + return true; + } else + return false; + } else + return false; + } else + return false; +} + ostream& operator << (ostream& os, const pure_expr *x) { char test; @@ -631,6 +653,7 @@ throw err("stack overflow in printer"); char buf[64]; assert(x); + if (pstr(os, (pure_expr*)x)) return os; //os << "{" << x->refc << "}"; switch (x->tag) { case EXPR::INT: Modified: pure/trunk/symtable.cc =================================================================== --- pure/trunk/symtable.cc 2008-09-05 12:11:50 UTC (rev 718) +++ pure/trunk/symtable.cc 2008-09-05 13:40:46 UTC (rev 719) @@ -2,7 +2,7 @@ #include "symtable.hh" #include <assert.h> -symtable::symtable() : fno(0), rtab(1024) +symtable::symtable() : fno(0), rtab(1024), __show__sym(0) { // enter any predefined symbols here, e.g.: //sym("-", 6, infixl); @@ -72,6 +72,7 @@ _sym = symbol(s, fno, modno); //cout << "new symbol " << _sym.f << ": " << _sym.s << endl; rtab[fno] = &_sym; + if (__show__sym == 0 && s == "__show__") __show__sym = fno; } return _sym; } @@ -88,6 +89,7 @@ _sym = symbol(s, fno, prec, fix, modno); //cout << "new symbol " << _sym.f << ": " << _sym.s << endl; rtab[fno] = &_sym; + if (__show__sym == 0 && s == "__show__") __show__sym = fno; } return _sym; } @@ -125,6 +127,7 @@ _sym = symbol(s, fno, prec, fix, modno); //cout << "new symbol " << _sym.f << ": " << _sym.s << endl; rtab[fno] = &_sym; + if (__show__sym == 0 && s == "__show__") __show__sym = fno; } return _sym; } Modified: pure/trunk/symtable.hh =================================================================== --- pure/trunk/symtable.hh 2008-09-05 12:11:50 UTC (rev 718) +++ pure/trunk/symtable.hh 2008-09-05 13:40:46 UTC (rev 719) @@ -63,6 +63,7 @@ // get a symbol by its number symbol& sym(int32_t f); // retrieve various builtin symbols (create when necessary) + int32_t __show__sym; // This is cached here to improve performance. symbol& nil_sym(); symbol& cons_sym(); symbol& void_sym(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |