2 * Programming Language SOOPY
3 * (Simple Object Oriented Programming sYstem)
5 * Copyright (C) 2002 SUZUKI Jun
7 * URL: http://sourceforge.jp/projects/soopy/
8 * License: GPL(GNU General Public License)
11 * $Id: NameSpace.cpp,v 1.69 2004/03/27 05:08:14 randy Exp $
17 // Soopy NameSpace Class
21 vector<SpValue> Frame;
24 SpNameSpace::SpNameSpace(NSMap& m, SpValue& p)
30 SpNameSpace::SpNameSpace(SpNameSpace* ns1, SpNameSpace* ns2, SpValue& p)
33 NSMap::iterator found;
34 NSMap::iterator it = ns2->aMap.begin();
35 for(; it != ns2->aMap.end(); it++){
36 found = aMap.find(it->first);
37 if(found != aMap.end()){
40 aMap[it->first] = it->second;
45 SpNameSpace::SpNameSpace(SpValue& p)
50 SpNameSpace::~SpNameSpace()
53 *spout << ":destroy NameSpace:" << "\n";
55 // NSMap::iterator it;
56 // for(it = aMap.begin(); it != aMap.end(); it++){
61 SpValue& SpNameSpace::toString()
69 for(it = aMap.begin(); it != aMap.end(); it++){
70 SpValue temp = it->first;
71 NSKey* key = temp.asNSKey();
72 if(key->isPublic != isPublic){
73 isPublic = ! isPublic;
81 *str += key->toString();
83 *str += it->second.toString();
89 // s.setNewObject(str);
91 return SpObjectResult(str);
94 bool SpNameSpace::operator==(SpObject& obj)
96 if(SpNameSpace* p2 = dynamic_cast<SpNameSpace*>(&obj)){
97 if(size() != p2->size()){
102 for(it = aMap.begin(); it != aMap.end(); it++){
103 NSMap::iterator find;
105 find = p2->aMap.find(it->first);
106 if(find == p2->aMap.end()){ // not found
109 if(it->second != find->second){
118 bool SpNameSpace::operator<(SpObject& obj)
120 if(SpNameSpace* p2 = dynamic_cast<SpNameSpace*>(&obj)){
122 int size2 = p2->size();
124 return size1 < size2;
127 //
\82·
\82×
\82Ä
\82Ì
\83L
\81[
\82ª
\93¯
\82¶
\82©
\82Ç
\82¤
\82©
\83`
\83F
\83b
\83N
128 bool all_match = true;
130 for(it = aMap.begin(); it != aMap.end(); it++){
131 NSMap::iterator find;
132 find = p2->aMap.find(it->first);
133 if(find == p2->aMap.end()){ // not found
140 //
\82·
\82×
\82Ä
\82Ì
\83L
\81[
\82ª
\93¯
\82¶
\82Æ
\82«
\81A
\92l
\82ð
\8f\87\94Ô
\82É
\94ä
\8ar
\82·
\82é
\81B
142 for(it = aMap.begin(); it != aMap.end(); it++){
143 if(it->second != p2->aMap[it->first]){
144 return it->second < p2->aMap[it->first];
149 //
\83L
\81[
\82ª
\95Ê
\82Ì
\82Æ
\82«
\81A
\83L
\81[
\82Ì
\91å
\82«
\82³
\82ð
\94ä
\8ar
\82·
\82é
\81B
150 //
\83L
\81[
\82Ì
\8f\87\94Ô
\82É
\82Í
\88Ó
\96¡
\82ª
\82È
\82¢
\82Ì
\82Å
\81A
\82±
\82ê
\82Å
\82Í
\81A
\96{
\93\96\82Í
\95s
\8f\
\95ª
\81B
151 //
\82æ
\82Á
\82Ä
\81A
\92P
\8f\83\82È
\83l
\81[
\83\80\83X
\83y
\81[
\83X
\88È
\8aO
\82ð
\81A
\83l
\81[
\83\80\83X
\83y
\81[
\83X
\82Ì
\83L
\81[
\82Æ
\82µ
\82Ä
\8eg
\82¤
\82×
\82«
\82Å
\82Í
\82È
\82¢
\81B
154 for(it = aMap.begin(), it2 = p2->aMap.begin();
160 NSKey* key1 = temp.asNSKey();
162 NSKey* key2 = temp.asNSKey();
163 SpValue v1 = key1->val;
164 SpValue v2 = key2->val;
171 return SpObject::operator<(obj);
174 SpNameSpace& SpNameSpace::operator+=(SpNameSpace& ns)
177 for(it=ns.begin(); it != ns.end(); it++){
178 this->setValue((SpValue&)it->first, it->second, (SpNameSpace*)this);
183 bool SpNameSpace::reachable(SpNameSpace* ns)
185 if(ns == NULL) return false;
189 if(!parentNS.isNil()){
190 SpNameSpace* p = parentNS.asNameSpace();
191 return p->reachable(ns);
196 SpValue& SpNameSpace::lookup(SpValue& v, SpNameSpace* searcher)
198 NSVar* key = new NSVar(v);
200 return getValue(key, searcher, NULL, true);
201 //}catch(SpAccessException& e){
203 //}catch(SpException& e){
204 }catch(SpKeyException& e){
209 // search 1 level only
210 SpValue& SpNameSpace::lookup1(SpValue& v, SpNameSpace* searcher)
212 NSVar* key = new NSVar(v);
214 return getValue(key, searcher, NULL, false);
215 //}catch(SpAccessException& e){
217 //}catch(SpException& e){
218 }catch(SpKeyException& e){
223 SpValue& SpNameSpace::getValue(NSKey* k, SpNameSpace* searcher, SpNameSpace* cur_ns, bool search_parent)
225 //cout << "getValue key:'" << *k << "'" << endl;
235 if(it != aMap.end()){
236 SpValue temp = it->first;
237 ptr = temp.asNSKey();
240 if(searcher == NULL){
241 throw SpAccessException("can't access private feature.");
243 if(!searcher->reachable(this)){
244 throw SpAccessException("can't access private feature.");
247 if(ptr->isNSProperty()){
248 NSProperty* pro = (NSProperty*)ptr;
249 NSVar* var = new NSVar(pro->getter);
251 it = aMap.find(key2);
252 if(it == aMap.end()){ throw SpException("no getter in property"); }
254 NSKey* ptr2 = dynamic_cast<NSKey*>(temp.getObject());
255 if(ptr2->isNSFunc()){
256 SpFunc* func = it->second.asFunc();
259 // push this to currentNS
263 v = (*func)(NilObject);
270 return SpValueResult(v);
274 }else if(ptr->isNSPrimProperty()){
275 NSPrimProperty* pro = (NSPrimProperty*)ptr;
276 if(pro->getter.isFunc()){
277 SpFunc* func = pro->getter.asFunc();
280 // push this to currentNS
284 v = (*func)(NilObject);
291 return SpValueResult(v);
295 }else if(ptr->isNSFunc()){
298 SpClosure* clos = new SpClosure(it->second, ns);
299 // static SpValue result;
300 // result.setNewObject(clos);
302 return SpObjectResult(clos);
308 if(search_parent && !parentNS.isNil()){
309 SpNameSpace* ns = dynamic_cast<SpNameSpace*>(parentNS.getObject());
310 return ns->getValue(k, searcher, cur_ns);
313 throw SpKeyException("no such a key", k->toCStringWithEncoder());
316 void SpNameSpace::setValue(SpValue& key, SpValue& val, SpNameSpace* searcher, SpNameSpace* cur_ns)
324 if(it != aMap.end()){
325 old_value = it->second;
326 SpValue temp = it->first;
327 NSKey* ptr = temp.asNSKey();
330 if(searcher == NULL){
331 throw SpAccessException("can't access private feature.");
333 if(!searcher->reachable(this)){
334 throw SpAccessException("can't access private feature.");
337 if(ptr->isNSProperty()){
338 NSProperty* pro = (NSProperty*)ptr;
339 NSVar* var = new NSVar(pro->setter);
341 it = aMap.find(key2);
342 if(it == aMap.end()){ throw SpException("no setter in property"); }
344 NSKey* ptr2 = dynamic_cast<NSKey*>(temp.getObject());
345 if(ptr2->isNSFunc()){
346 SpFunc* func = it->second.asFunc();
347 // push this to currentNS
358 }else if(ptr2->isNSConst()){
359 throw SpException("setter is constant or function");
361 //aMap[pro->setter] = val;
364 }else if(ptr->isNSPrimProperty()){
365 NSPrimProperty* pro = (NSPrimProperty*)ptr;
366 if(pro->setter.isFunc()){
367 SpFunc* func = pro->setter.asFunc();
369 // push this to currentNS
380 //throw SpException("system error");
381 throw SpSystemError(__FILE__, __LINE__);
384 if(ptr->isNSConst()){
385 throw SpException("can't change constant or function");
391 if(!parentNS.isNil()){
392 SpNameSpace* ns = dynamic_cast<SpNameSpace*>(parentNS.getObject());
393 ns->setValue(key, val, searcher);
394 }else if(this == PMainNameSpace){
397 throw SpException("can't set undefined feature");
402 void SpNameSpace::intern(SpValue& key, SpValue& val)
404 NSMap::iterator it = aMap.find(key);
405 if(it != aMap.end()){
415 SpValue& NSVar::toString()
417 SpString* s = new SpString("var ");
418 *s += val.toString();
420 // v.setNewObject(s);
422 return SpObjectResult(s);
430 SpValue& NSConst::toString()
432 SpString* s1 = new SpString("const ");
434 SpValue v2 = val.toString();
435 SpString* s2 = dynamic_cast<SpString*>(v2.getObject());
437 SpString* s = new SpString(s1->toCStringWithEncoder());
439 // v.setNewObject(s);
441 return SpObjectResult(s);
449 SpValue& NSProperty::toString()
451 bool print_getter=false;
452 SpString* s = new SpString("property ");
454 SpValue v0 = NSKey::toString();
455 SpValue v1 = getter.toString();
456 SpValue v2 = setter.toString();
457 SpString* s1 = dynamic_cast<SpString*>(v1.getObject());
458 SpString* s2 = dynamic_cast<SpString*>(v2.getObject());
461 if(getter != NilObject){
466 if(setter != NilObject){
474 SpString* s3 = new SpString(s->toCStringWithEncoder());
476 // v.setNewObject(s3);
478 return SpObjectResult(s3);
486 SpValue& NSPrimProperty::toString()
488 bool print_getter=false;
489 SpString* s = new SpString("property ");
491 SpValue v0 = NSKey::toString();
492 SpValue v1 = getter.toString();
493 SpValue v2 = setter.toString();
494 SpString* s1 = dynamic_cast<SpString*>(v1.getObject());
495 SpString* s2 = dynamic_cast<SpString*>(v2.getObject());
498 if(getter != NilObject){
503 if(setter != NilObject){
511 SpString* s3 = new SpString(s->toCStringWithEncoder());
513 // v.setNewObject(s3);
515 return SpObjectResult(s3);
523 SpValue& SpNameSpace::onMessage(SpValue& rec, SpValue& msg)
525 NSVar* key = new NSVar(msg);
527 SpValue temp = getCurrentNS();
528 SpNameSpace* ns = temp.asNameSpace();
529 return getValue(key, ns);
530 // return lookup(msg);
531 }catch(SpAccessException& e){
533 }catch(SpException& e){
535 return NameSpaceMsgHandler(rec, msg);
536 }catch(SpNoMethodException& e){
546 SpValue& SpNameSpace::rename(SpValue& self, SpValue& renames)
548 if(!renames.isNameSpace()){
549 throw SpException("arg is not namespace.(ns.rename)");
551 SpNameSpace* ns1 = self.asNameSpace();
552 SpNameSpace* ns2 = renames.asNameSpace();
554 NSMap assoc = ns1->aMap;
555 NSMap::iterator it = ns2->aMap.begin();
556 for(; it != ns2->aMap.end(); it++){
557 NSMap::iterator found;
558 found = assoc.find(it->first);
559 if(found != assoc.end()){
560 SpValue val = found->second;
561 //NSKey* key = ((SpValue&)found->first).asNSKey();
562 SpValue temp = found->first;
563 NSKey* key = temp.asNSKey();
564 if(key->isNSProperty()){
565 NSProperty* p = (NSProperty*)key;
566 key = new NSProperty(it->second, p->getter, p->setter, p->isPublic);
567 }else if(key->isNSPrimProperty()){
568 NSPrimProperty* p = (NSPrimProperty*)key;
569 key = new NSProperty(it->second, p->getter, p->setter, p->isPublic);
570 }else if(key->isNSFunc()){
571 key = new NSFunc(it->second);
572 }else if(key->isNSDataType()){
573 key = new NSDataType(it->second);
574 }else if(key->isNSConst()){
575 key = new NSConst(it->second);
577 key = new NSVar(it->second);
584 // static SpValue result;
585 // result.setNewObject(new SpNameSpace(assoc, ns1->parentNS));
587 return SpObjectResult(new SpNameSpace(assoc, ns1->parentNS));
591 SpValue& SpNameSpace::make_func(SpValue& self)
593 SpNameSpace* ns = self.asNameSpace();
594 SpValue f(SpUsrFunc::fromNameSpace(ns));
595 // static SpValue result;
596 // result.setNewObject(new SpClosure(f, self));
598 return SpObjectResult(new SpClosure(f, self));
601 static SpValue PrimAppend;
603 SpValue& SpNameSpace::prim_append(SpValue& self, SpValue& key, SpValue& val)
605 SpNameSpace* ns = self.asNameSpace();
606 SpValue v1 = key.eval();
607 SpValue v2 = val.eval();
608 ns->internVar(v1, v2);
610 // static SpValue result;
611 // result.setNewObject(new SpClosureP3_1(PrimAppend, self, getCurrentNS()));
613 return SpObjectResult(new SpClosureP3_1(PrimAppend, self, getCurrentNS()));
616 SpValue& SpNameSpace::prim_keys(SpValue& self)
618 // static SpValue result;
621 SpNameSpace* ns = self.asNameSpace();
623 NSMap::iterator it = ns->aMap.begin();
624 for(; it != ns->aMap.end(); it++){
626 key = ((SpValue)it->first).asNSKey();
627 result.setNewObject(new SpCons(key->val, result));
631 return SpValueResult(result);
634 SpValue& SpNameSpace::prim_delete(SpValue& self, SpValue& key)
636 SpNameSpace* ns = self.asNameSpace();
640 NSMap::iterator found = ns->aMap.find(k);
641 if(found != ns->aMap.end()){
642 ns->aMap.erase(found);
648 SpValue& SpNameSpace::prim_lookup(SpValue& self, SpValue& key)
650 SpNameSpace* ns = self.asNameSpace();
651 SpValue v = key.eval();
652 // static SpValue result;
653 // result = ns->lookup(v);
655 return SpValueResult(ns->lookup(v));
662 SpNameSpace* PMainNameSpace;
664 SpNameSpace* PSoopyNameSpace;
666 SpNameSpace* PEnvNameSpace;
669 void SpNameSpace::init()
671 PMainNameSpace = new SpNameSpace();
672 MainNS.setNewObject(PMainNameSpace);
673 pushCurrentNS(MainNS);
675 PSoopyNameSpace = new SpNameSpace();
676 SoopyNS.setNewObject(PSoopyNameSpace);
677 PMainNameSpace->internConst(SymSoopy, SoopyNS);
679 PEnvNameSpace = new SpNameSpace();
680 EnvNS.setNewObject(PEnvNameSpace);
681 PMainNameSpace->internConst(SymEnv, EnvNS);
683 SpValue PrimRename(new SpPrim2(rename));
684 NameSpaceMsgHandler.append(SymRename, PrimRename);
686 SpValue SymEval(new SpSymbol("eval"));
687 SpValue PrimEval(new SpPrim1(make_func));
688 NameSpaceMsgHandler.append(SymEval, PrimEval);
690 PrimAppend.setNewObject(new SpPrim3(prim_append));
691 NameSpaceMsgHandler.append(SymAppend, PrimAppend);
693 SpValue PrimKeys(new SpPrim1(prim_keys));
694 NameSpaceMsgHandler.append(SymKeys, PrimKeys);
696 SpValue PrimDelete(new SpPrim2(prim_delete));
697 NameSpaceMsgHandler.append(SymDelete, PrimDelete);
699 SpValue SymLookup(new SpSymbol("lookup"));
700 SpValue PrimLookup(new SpPrim2(prim_lookup));
701 NameSpaceMsgHandler.append(SymLookup, PrimLookup);
708 SpValue& SpNameSpace::plus(SpValue& e1, SpValue& e2)
714 if(!e2.isNameSpace()){
715 throw SpException("type mismatch in +(NameSpace, ...)");
717 ns1 = dynamic_cast<SpNameSpace*>(e1.getObject());
718 ns2 = dynamic_cast<SpNameSpace*>(e2.getObject());
719 ns3 = new SpNameSpace(ns1, ns2, getCurrentNS());
721 // v.setNewObject(ns3);
723 return SpObjectResult(ns3);
728 * class MakeNameSpace
731 SpValue& MakeNameSpace::eval()
733 SpNameSpace* src = dynamic_cast<SpNameSpace*>(ns.getObject());
734 SpNameSpace* assoc = new SpNameSpace(getCurrentNS());
735 NSMap::iterator it = src->aMap.begin();
736 for(; it != src->aMap.end(); it++){
737 SpValue temp = it->first;
738 if(temp.isNSKey() && temp.asNSKey()->isNSDataType()){
739 SpDataType* dt = it->second.asDataType();
740 dt->mapping2ns(it->second, assoc);
742 SpValue v = it->second.eval();
744 assoc->intern(temp, v);
747 // static SpValue result;
748 // result.setNewObject(assoc);
750 return SpObjectResult(assoc);
753 SpValue& MakeNameSpace::toString()
757 str = new SpString();
758 *str = "MakeNameSpace(";
759 *str += ns.toString();
762 // s.setNewObject(str);
764 return SpObjectResult(str);