[pure-lang-svn] SF.net SVN: pure-lang:[620] pure/trunk
Status: Beta
Brought to you by:
agraef
|
From: <ag...@us...> - 2008-08-27 00:32:20
|
Revision: 620
http://pure-lang.svn.sourceforge.net/pure-lang/?rev=620&view=rev
Author: agraef
Date: 2008-08-27 00:32:31 +0000 (Wed, 27 Aug 2008)
Log Message:
-----------
Add support for private symbols to symbol table.
Modified Paths:
--------------
pure/trunk/symtable.cc
pure/trunk/symtable.hh
Modified: pure/trunk/symtable.cc
===================================================================
--- pure/trunk/symtable.cc 2008-08-26 22:37:56 UTC (rev 619)
+++ pure/trunk/symtable.cc 2008-08-27 00:32:31 UTC (rev 620)
@@ -42,42 +42,88 @@
segfault_sym();
}
-symbol* symtable::lookup(const string& s)
+symbol* symtable::lookup(const string& s, int32_t modno)
{
- map<string, symbol>::iterator it = tab.find(s);
- if (it == tab.end())
+ sym_map& m = tab[modno];
+ sym_map::iterator it = m.find(s);
+ if (it == m.end() && modno >= 0) {
+ m = tab[-1];
+ it = m.find(s);
+ }
+ if (it == m.end())
return 0;
else
return &it->second;
}
-symbol& symtable::sym(const string& s)
+symbol& symtable::sym(const string& s, int32_t modno)
{
- symbol& _sym = tab[s];
+ symbol* _symp = lookup(s, modno);
+ if (_symp) modno = _symp->modno;
+ symbol& _sym = tab[modno][s];
if (_sym.f == 0) {
if ((uint32_t)++fno > rtab.capacity())
rtab.reserve(rtab.capacity()+1024);
- _sym = symbol(s, fno);
+ _sym = symbol(s, fno, modno);
//cout << "new symbol " << _sym.f << ": " << _sym.s << endl;
rtab[fno] = &_sym;
}
return _sym;
}
-symbol& symtable::sym(const string& s, prec_t prec, fix_t fix)
+symbol& symtable::sym(const string& s, prec_t prec, fix_t fix, int32_t modno)
{
assert(prec <= 10);
- symbol& _sym = tab[s];
+ symbol* _symp = lookup(s, modno);
+ if (_symp) modno = _symp->modno;
+ symbol& _sym = tab[modno][s];
if (_sym.f == 0) {
if ((uint32_t)++fno > rtab.capacity())
rtab.reserve(rtab.capacity()+1024);
- _sym = symbol(s, fno, prec, fix);
+ _sym = symbol(s, fno, prec, fix, modno);
//cout << "new symbol " << _sym.f << ": " << _sym.s << endl;
rtab[fno] = &_sym;
}
return _sym;
}
+symbol* symtable::xlookup(const string& s, int32_t modno)
+{
+ sym_map& m = tab[modno];
+ sym_map::iterator it = m.find(s);
+ if (it == m.end())
+ return 0;
+ else
+ return &it->second;
+}
+
+symbol& symtable::xsym(const string& s, int32_t modno)
+{
+ symbol& _sym = tab[modno][s];
+ if (_sym.f == 0) {
+ if ((uint32_t)++fno > rtab.capacity())
+ rtab.reserve(rtab.capacity()+1024);
+ _sym = symbol(s, fno, modno);
+ //cout << "new symbol " << _sym.f << ": " << _sym.s << endl;
+ rtab[fno] = &_sym;
+ }
+ return _sym;
+}
+
+symbol& symtable::xsym(const string& s, prec_t prec, fix_t fix, int32_t modno)
+{
+ assert(prec <= 10);
+ symbol& _sym = tab[modno][s];
+ if (_sym.f == 0) {
+ if ((uint32_t)++fno > rtab.capacity())
+ rtab.reserve(rtab.capacity()+1024);
+ _sym = symbol(s, fno, prec, fix, modno);
+ //cout << "new symbol " << _sym.f << ": " << _sym.s << endl;
+ rtab[fno] = &_sym;
+ }
+ return _sym;
+}
+
symbol& symtable::sym(int32_t f)
{
assert(f > 0 && (uint32_t)f < rtab.size());
Modified: pure/trunk/symtable.hh
===================================================================
--- pure/trunk/symtable.hh 2008-08-26 22:37:56 UTC (rev 619)
+++ pure/trunk/symtable.hh 2008-08-27 00:32:31 UTC (rev 620)
@@ -20,19 +20,24 @@
string s; // print name
prec_t prec; // precedence level
fix_t fix; // fixity
+ int32_t modno; // module key for private symbol, -1 for global symbol
symbol() : // constructor for dummy entries
- f(0), s(""), prec(10), fix(infix) { }
- symbol(const string& _s, int _f) :
- f(_f), s(_s), prec(10), fix(infix) { x = expr(f); }
- symbol(const string& _s, int _f, prec_t _prec, fix_t _fix) :
- f(_f), s(_s), prec(_prec), fix(_fix) { x = expr(f); }
+ f(0), s(""), prec(10), fix(infix), modno(-1) { }
+ symbol(const string& _s, int _f, int32_t _modno = -1) :
+ f(_f), s(_s), prec(10), fix(infix), modno(_modno) { x = expr(f); }
+ symbol(const string& _s, int _f, prec_t _prec, fix_t _fix,
+ int32_t _modno = -1) :
+ f(_f), s(_s), prec(_prec), fix(_fix), modno(_modno) { x = expr(f); }
};
/* Symbol table. */
+typedef map<string, symbol> sym_map;
+typedef map<int32_t, sym_map> sym_tab;
+
class symtable {
int32_t fno;
- map<string, symbol> tab;
+ sym_tab tab;
vector<symbol*> rtab;
public:
symtable();
@@ -42,11 +47,21 @@
// get current number of symbols in table (symbols are always numbered
// consecutively from 1 to nsyms())
int32_t nsyms() { return fno; }
- // look up an existing symbol (return 0 if not in table)
- symbol* lookup(const string& s);
+ /* The following routines first search for a symbol in the given module,
+ failing that they will also search for a global symbol. (If modno==-1
+ then only global symbols will be searched.) */
+ // look up an existing symbol in given module (return 0 if not in table)
+ symbol* lookup(const string& s, int32_t modno = -1);
// get a symbol by its name (create if necessary)
- symbol& sym(const string& s);
- symbol& sym(const string& s, prec_t prec, fix_t fix);
+ symbol& sym(const string& s, int32_t modno = -1);
+ symbol& sym(const string& s, prec_t prec, fix_t fix, int32_t modno = -1);
+ /* These work like the above, but will only return exact matches in the
+ given module. */
+ symbol* xlookup(const string& s, int32_t modno = -1);
+ symbol& xsym(const string& s, int32_t modno = -1);
+ symbol& xsym(const string& s, prec_t prec, fix_t fix, int32_t modno = -1);
+ // get a symbol by its number
+ symbol& sym(int32_t f);
// retrieve various builtin symbols (create when necessary)
symbol& nil_sym();
symbol& cons_sym();
@@ -80,8 +95,6 @@
symbol& failed_cond_sym() { return sym("failed_cond"); }
symbol& signal_sym() { return sym("signal"); }
symbol& segfault_sym() { return sym("stack_fault"); }
- // get a symbol by its number
- symbol& sym(int32_t f);
};
#endif // ! SYMTABLE_HH
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|