1 /**********************************************************************
2
3 This file is part of the Quantum Computation Language QCL.
4
5 (c) Copyright by Bernhard Oemer <oemer@tph.tuwien.ac.at>, 1998
6
7 This program comes without any warranty; without even the implied
8 warranty of merchantability or fitness for any particular purpose.
9
10 This program is free software under the terms of the
11 GNU General Public Licence (GPL) version 2 or higher
12
13 ************************************************************************/
14
15
16 #pragma implementation
17
18 #include "symbols.h"
19 #include "syntax.h"
20
21 #define IND() if(ind!=NOINDENT) \
22 { int i; for(i=0;i<ind;i++) ostr+= " "; }
23 #define NL() (ind==NOINDENT ? string(" ") : string("\n"))
24 #define INC() (ind!=NOINDENT ? ind+1 : ind)
25
26
deltree(entry * t)27 void SymTab::deltree(entry *t) {
28 if(t->pred) deltree(t->pred);
29 if(t->succ) deltree(t->succ);
30 if(t->pval) qcl_delete(t->pval);
31 if(global && t->pdef) qcl_delete(t->pdef);
32 qcl_delete(t);
33 }
34
putentry(entry * t,entry * e)35 int SymTab::putentry(entry *t,entry *e) {
36 if(e->pdef->id()<t->pdef->id()) {
37 if(t->pred) return putentry(t->pred,e);
38 t->pred=e;
39 return 0;
40 };
41 if(e->pdef->id()>t->pdef->id()) {
42 if(t->succ) return putentry(t->succ,e);
43 t->succ=e;
44 return 0;
45 };
46 return 1;
47 }
48
getentry(entry * t,const tId & id) const49 SymTab::entry * SymTab::getentry(entry *t,const tId& id) const {
50 if(id==t->pdef->id()) return t;
51 if(t->pred && id<t->pdef->id()) return getentry(t->pred,id);
52 if(t->succ && id>t->pdef->id()) return getentry(t->succ,id);
53 return 0;
54 }
55
put(sDef * d,const tValue & v)56 int SymTab::put(sDef *d,const tValue& v) {
57 entry *e;
58
59 if(!d->isDef()) return 1;
60 e=new entry;
61 e->pdef=d;
62 e->pval= d->isValueDef() ? new tValue(v) : 0;
63 e->pred=e->succ=0;
64 if(root) return putentry(root,e);
65 root=e;
66 return 0;
67 }
68
getVal(const tId & id,ObjType * o) const69 tValue SymTab::getVal(const tId& id,ObjType *o) const {
70 entry *e;
71
72 if(!root) return tValue();
73 e=getentry(root,id);
74 if(!e) return tValue();
75 if(!e->pdef->isValueDef()) return tValue();
76 if(o) *o=e->pdef->object();
77 return *(e->pval);
78 }
79
getDef(const tId & id) const80 sDef *SymTab::getDef(const tId& id) const {
81 entry *e;
82
83 if(!root) return 0;
84 e=getentry(root,id);
85 if(!e) return 0;
86 return e->pdef;
87 }
88
getRef(const tId & id,ObjType * o) const89 tValue *SymTab::getRef(const tId& id,ObjType *o) const {
90 entry *e;
91
92 if(!root) return 0;
93 e=getentry(root,id);
94 if(!e) return 0;
95 if(!e->pdef->isValueDef()) return 0;
96 if(o) *o=e->pdef->object();
97 return e->pval;
98 }
99
recstr(entry * e,int ind)100 string SymTab::recstr(entry *e,int ind) {
101 string ostr;
102 if (!e) return "";
103 if(e->pred) ostr+=recstr(e->pred,ind);
104 IND();
105 ostr+=e->pdef->declstr()+";";
106 if(ind!=NOINDENT || e->succ) ostr+=NL();
107 if(e->succ) ostr+=recstr(e->succ,ind);
108 return ostr;
109 }
110
prtstr(int ind)111 string SymTab::prtstr(int ind) {
112 string ostr;
113
114 if(ind==NOINDENT) {
115 return "{"+recstr(root,ind)+"}";
116 } else {
117 return recstr(root,ind);
118 }
119 }
120
put(sDef * d,const tValue & v)121 int SymTabComb::put(sDef *d,const tValue& v) {
122 return tab1->put(d,v);
123 }
124
getVal(const tId & id,ObjType * o) const125 tValue SymTabComb::getVal(const tId& id,ObjType *o) const {
126 tValue v;
127
128 v=tab1->getVal(id,o);
129 if(v.isDefined()) return v;
130 return tab2->getVal(id,o);
131 }
132
getDef(const tId & id) const133 sDef *SymTabComb::getDef(const tId& id) const {
134 sDef *d;
135
136 d=tab1->getDef(id);
137 if(d) return d;
138 return tab2->getDef(id);
139 }
140
getRef(const tId & id,ObjType * o) const141 tValue *SymTabComb::getRef(const tId& id,ObjType *o) const {
142 tValue *v;
143
144 v=tab1->getRef(id,o);
145 if(v) return v;
146 return tab2->getRef(id,o);
147 }
148
prtstr(int ind)149 string SymTabComb::prtstr(int ind) {
150 string ostr;
151
152 if(ind==NOINDENT) {
153 ostr="{";
154 if(tab1) ostr+=tab1->prtstr(ind);
155 ostr+=",";
156 if(tab2) ostr+=tab2->prtstr(ind);
157 ostr+="}";
158 } else {
159 if(tab2) ostr+=tab2->prtstr(ind);
160 if(tab1) ostr+=tab1->prtstr(ind);
161 };
162 return ostr;
163 }
164
165 SymTable *glsym; // global SymTable
166
167
168
169
170