PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/p3/symtable2.c

https://bitbucket.org/mathioudcsd/compiler_all_phases
C | 513 lines | 330 code | 87 blank | 96 comment | 35 complexity | aeee3da0f61814d2cd84077981776f96 MD5 | raw file
  1. //test
  2. #include "symtable.h"
  3. #include "tmpVars.h"
  4. unsigned programVarOffset = 0;
  5. unsigned functionLocalOffset = 0;
  6. unsigned formalArgOffset = 0;
  7. unsigned scopeSpaceCounter =1;
  8. unsigned int HashFunction(const char* x){
  9. unsigned int i=0;
  10. unsigned int hashNum=0;
  11. return hashNum=(hashNum*314+(unsigned int)x[0])%HASHSIZE;
  12. }
  13. SymbolTable* CreateTable(){
  14. int i;
  15. SymbolTable* new;
  16. new=(SymbolTable*)malloc(sizeof(SymbolTable));
  17. if(new==NULL){
  18. fprintf(stderr, "Malloc Error: new Symbol table\n");
  19. return NULL;
  20. }
  21. new->scopeHead=(ScopeList*) malloc(sizeof(ScopeList) );
  22. new->scopeHead->symTableNode = NULL;
  23. new->scopeHead->next=NULL;
  24. for(i=0;i<HASHSIZE;i++)
  25. new->hashTable[i]=NULL;
  26. return new;
  27. }
  28. int addScope(ScopeList* scopeList, unsigned int scope){
  29. ScopeList* scopePtr=scopeList;
  30. unsigned int hop=0;
  31. //uparxei
  32. if(scopePtr!= NULL && scope==0){
  33. return 1;
  34. }
  35. while(scopePtr->next != NULL ){
  36. if(hop == scope){
  37. return 1;
  38. }
  39. hop++;
  40. scopePtr = scopePtr->next;
  41. }
  42. ScopeList* newscope= (ScopeList*) malloc(sizeof(ScopeList) );
  43. newscope->symTableNode= NULL;
  44. newscope->next= NULL;
  45. scopePtr->next=newscope;
  46. return 0;
  47. }
  48. SymbolTableEntry* CreateNode(const char* name, unsigned int scope, unsigned int line, enum SymbolTableType type){
  49. char* nameBuff;
  50. SymbolTableEntry* buff = (SymbolTableEntry*)malloc(sizeof(SymbolTableEntry));
  51. if(buff==NULL){
  52. fprintf(stderr, "Malloc Error: new Node creation\n");
  53. return NULL;
  54. }
  55. nameBuff=(char*)malloc(strlen(name));
  56. nameBuff=strdup(name);
  57. buff->name=nameBuff;
  58. buff->scope=scope;
  59. buff->line=line;
  60. buff->isActive=1;
  61. buff->type=type;
  62. buff->nextInTable=NULL;
  63. buff->nextInScope=NULL;
  64. return buff;
  65. }
  66. int Insert(SymbolTable* tbl, ScopeList* scopeList, SymbolTableEntry* newNode){
  67. unsigned int hash;
  68. unsigned int scope;
  69. unsigned int i=0;
  70. ScopeList* scopePtr=scopeList;
  71. SymbolTableEntry* symEntry;
  72. SymbolTableEntry* buffer;
  73. SymbolTableEntry* curr;
  74. hash=HashFunction(newNode->name);
  75. scope=newNode->scope;
  76. if(tbl->hashTable[hash]==NULL){
  77. tbl->hashTable[hash]=newNode;
  78. tbl->hashTable[hash]->nextInTable=NULL;
  79. }
  80. else{
  81. curr=tbl->hashTable[hash];
  82. while(curr->nextInTable!=NULL){
  83. curr=curr->nextInTable;
  84. }
  85. curr->nextInTable=newNode;
  86. curr=newNode;
  87. curr->nextInTable=NULL;
  88. }
  89. addScope(scopeList, scope);
  90. /* add sto scopeList */
  91. scopePtr = scopeList;
  92. while(i<scope){
  93. scopePtr=scopePtr->next;
  94. i++;
  95. }
  96. if(scopePtr->symTableNode==NULL){
  97. scopePtr->symTableNode=newNode;
  98. scopePtr->next=NULL;
  99. }
  100. else{
  101. symEntry=scopePtr->symTableNode;
  102. while(symEntry->nextInScope!=NULL){
  103. symEntry=symEntry->nextInScope;
  104. }
  105. symEntry->nextInScope=newNode;
  106. }
  107. return 0;
  108. }
  109. void Hide(SymbolTable* tbl, unsigned int scope){
  110. ScopeList* slist= tbl->scopeHead;
  111. unsigned int num= 0;
  112. while(num<scope){
  113. if(slist->next==NULL){
  114. fprintf(stderr, "Hide Error: scope does not exist\n");
  115. return;
  116. }
  117. slist= slist->next;
  118. num++;
  119. }
  120. SymbolTableEntry * buffer= slist->symTableNode;
  121. if(num>0){
  122. while(buffer){
  123. if(buffer->isActive==1){
  124. if(buffer->type<3)
  125. if(strncmp(buffer->name, "_", 1)==0){
  126. fprintf(stdout, "De-Activating _t var : %s\n",buffer->name);
  127. removeFromActiveTempList_n(buffer->name);
  128. }
  129. else
  130. fprintf(stdout, "De-Activating variable : %s\n",buffer->name);
  131. else
  132. fprintf(stdout, "De-Activating function : %s\n",buffer->name);
  133. }
  134. buffer->isActive= 0;
  135. buffer= buffer->nextInScope;
  136. }
  137. }
  138. return;
  139. }
  140. SymbolTableEntry* Lookup(SymbolTable* tbl, const char* name){
  141. unsigned int key= HashFunction(name);
  142. SymbolTableEntry * buffer= tbl->hashTable[key];
  143. while(buffer){
  144. if( strcmp(buffer->name, name )==0 )
  145. return buffer;
  146. buffer= buffer->nextInTable;
  147. }
  148. return NULL;
  149. }
  150. SymbolTableEntry* ScopeLookup(SymbolTable* tbl, const char* name, unsigned int scope){
  151. ScopeList* slist= tbl->scopeHead;
  152. unsigned int num= 0;
  153. while(num<scope){
  154. if(slist->next==NULL){
  155. fprintf(stderr, "ScopeLookup Error: scope does not exist\n");
  156. return NULL;
  157. }
  158. slist= slist->next;
  159. num++;
  160. }
  161. SymbolTableEntry * buffer= slist->symTableNode;
  162. while(buffer){
  163. if( strcmp(buffer->name, name )==0 ){
  164. if(buffer->isActive)
  165. return buffer;
  166. }
  167. buffer= buffer->nextInScope;
  168. }
  169. return NULL;
  170. }
  171. int ScopeLookup2(SymbolTable* tbl, const char* name, unsigned int scope){
  172. ScopeList* slist= tbl->scopeHead;
  173. unsigned int num= 0;
  174. while(num<scope){
  175. if(slist->next==NULL){
  176. return 0;
  177. }
  178. slist= slist->next;
  179. num++;
  180. }
  181. SymbolTableEntry * buffer= slist->symTableNode;
  182. while(buffer){
  183. if( strcmp(buffer->name, name )==0 ){
  184. if(buffer->isActive)
  185. return 1;
  186. }
  187. buffer= buffer->nextInScope;
  188. }
  189. return 0;
  190. }
  191. void printTable(SymbolTable* s){
  192. int i = 0;
  193. SymbolTableEntry* ptr=s->hashTable[0];
  194. int type;
  195. fprintf(stdout, "===================================\n" );
  196. fprintf(stdout, "LINE \t SCOPE \t TYPE \t\t\t NAME\n");
  197. fprintf(stdout, "______________________________________________________\n");
  198. while(i<HASHSIZE){
  199. ptr=s->hashTable[i];
  200. while(ptr!=NULL){
  201. type=(int)ptr->type;
  202. switch (ptr->type){
  203. case 0:
  204. fprintf(stdout, "%d \t (%d) \t GLOBAL VARIABLE \t %s\n", ptr->line, ptr->scope, ptr->name);
  205. break;
  206. case 1:
  207. fprintf(stdout, "%d \t (%d) \t LOCAL VARIABLE \t %s\n", ptr->line, ptr->scope, ptr->name);
  208. break;
  209. case 2:
  210. fprintf(stdout, "%d \t (%d) \t FORMAL VARIABLE \t %s\n", ptr->line, ptr->scope, ptr->name);
  211. break;
  212. case 3:
  213. fprintf(stdout, "%d \t (%d) \t USERFUNC \t\t %s\n", ptr->line, ptr->scope, ptr->name);
  214. break;
  215. case 4:
  216. //fprintf(stdout, "%d \t (%d) \t LIBFUNC \t\t %s\n", ptr->line, ptr->scope, ptr->name);
  217. break;
  218. }
  219. ptr=ptr->nextInTable;
  220. }
  221. i++;
  222. }
  223. fprintf(stdout, "______________________________________________________\n");
  224. }
  225. void printScopeTable(ScopeList* scopeList){
  226. ScopeList* scopePtr =scopeList;
  227. SymbolTableEntry* ptr=scopePtr->symTableNode;
  228. int type;
  229. while(scopePtr!=NULL){
  230. while(ptr!=NULL){
  231. fprintf(stdout, "%d ", ptr->scope);
  232. fprintf(stdout, "%s \n",ptr->name);
  233. ptr=ptr->nextInScope;
  234. }
  235. scopePtr=scopePtr->next;
  236. if(scopePtr==NULL)
  237. break;
  238. ptr=scopePtr->symTableNode;
  239. }
  240. }
  241. // Arguments* CreateArgsList(Arguments* args,const char* a){
  242. // Arguments* head = args;
  243. // if (head==NULL){
  244. // Arguments *node = (Arguments*) malloc(sizeof(Arguments));
  245. // node -> name = a;
  246. // node -> next = NULL;
  247. // head = node;
  248. // return head;
  249. // }
  250. // else{
  251. // Arguments *temp = head;
  252. // while(temp -> next!=NULL){
  253. // temp = temp -> next;
  254. // }
  255. // Arguments *node = (Arguments*) malloc(sizeof(Arguments));
  256. // node ->name = a;
  257. // node -> next = NULL;
  258. // temp -> next = node;
  259. // return head;
  260. // }
  261. // }
  262. void reverse(char* s)
  263. {
  264. int i, j;
  265. char c;
  266. for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
  267. c = s[i];
  268. s[i] = s[j];
  269. s[j] = c;
  270. }
  271. }
  272. void itoa(int n, char* s)
  273. {
  274. int i, sign;
  275. if ((sign = n) < 0) /* record sign */
  276. n = -n; /* make n positive */
  277. i = 0;
  278. do { /* generate digits in reverse order */
  279. s[i++] = n % 10 + '0'; /* get next digit */
  280. } while ((n /= 10) > 0); /* delete it */
  281. if (sign < 0)
  282. s[i++] = '-';
  283. s[i] = '\0';
  284. reverse(s);
  285. }
  286. int isLibFunc(SymbolTable* s,const char* text){
  287. ScopeList* slist= s->scopeHead;
  288. unsigned int num= 0;
  289. SymbolTableEntry * buffer= slist->symTableNode;
  290. while(num<12){
  291. if( strcmp(buffer->name, text )==0 ){
  292. return 1;
  293. }
  294. num++;
  295. buffer= buffer->nextInScope;
  296. }
  297. return 0;
  298. }
  299. void report_error(int lineno, char* text){
  300. fprintf(stderr,"\x1b[31mERROR line(%d): \'%s\' shadows a library function\n\x1b[0m", lineno, text);
  301. }
  302. scopespace_t currScopeSpace(void){
  303. if(scopeSpaceCounter == 1)
  304. return programvar;
  305. else if(scopeSpaceCounter%2 == 0)
  306. return formalarg;
  307. else
  308. return functionLocal;
  309. }
  310. unsigned currScopeOffset(void){
  311. switch(currScopeSpace()){
  312. case programvar:
  313. return programVarOffset;
  314. case functionLocal:
  315. return functionLocalOffset;
  316. case formalarg:
  317. return formalArgOffset;
  318. default:
  319. assert(0);
  320. }
  321. }
  322. void incCurrScopeOffset(void){
  323. switch(currScopeSpace()){
  324. case programvar:
  325. ++programVarOffset;
  326. break;
  327. case functionLocal:
  328. ++functionLocalOffset;
  329. break;
  330. case formalarg:
  331. ++formalArgOffset;
  332. break;
  333. default:
  334. assert(0);
  335. }
  336. }
  337. void enterScopeSpace(void){
  338. ++scopeSpaceCounter;
  339. }
  340. void exitScopeSpace(void){
  341. assert(scopeSpaceCounter>1);
  342. --scopeSpaceCounter;
  343. }
  344. void reset_formalArgsOffset(void){
  345. formalArgOffset = 0;
  346. }
  347. void reset_functionLocalOffset(void){
  348. functionLocalOffset = 0;
  349. }
  350. unsigned get_functionLocalOffset(void){
  351. return functionLocalOffset;
  352. }
  353. void set_functionLocalOffset(unsigned offset){
  354. functionLocalOffset = offset;
  355. }
  356. /*
  357. int main(void){
  358. int i=0;
  359. SymbolTable* table = CreateTable();
  360. SymbolTableEntry* a = CreateNode("ohh", 0, 22, 2, NULL);
  361. Insert(table, table->scopeHead, a);
  362. a = CreateNode("darling", 1, 20, 2, NULL);
  363. Insert(table, table->scopeHead, a);
  364. a = CreateNode("what", 2, 20, 2, NULL);
  365. Insert(table, table->scopeHead, a);
  366. addScope(table->scopeHead, 3);
  367. a = CreateNode("have", 4, 20, 2, NULL);
  368. Insert(table, table->scopeHead, a);
  369. printTable(table);
  370. return 1;
  371. }
  372. /*
  373. a = CreateNode("i", 4, 20, 2, NULL);
  374. Insert(table, table->scopeHead, a);
  375. a = CreateNode("done", 0, 20, 2, NULL);
  376. Insert(table, table->scopeHead, a);
  377. a = CreateNode("done", 4, 20, 2, NULL);
  378. Insert(table, table->scopeHead, a);
  379. a = CreateNode("done", 5, 20, 2, NULL);
  380. Insert(table, table->scopeHead, a);
  381. a = CreateNode("done", 6, 20, 2, NULL);
  382. Insert(table, table->scopeHead, a);
  383. a = CreateNode("done", 7, 20, 2, NULL);
  384. Insert(table, table->scopeHead, a);
  385. printScopeTable(table->scopeHead);
  386. printTable(table);
  387. //----------LOOKUPS tests---------------------*/
  388. /* if(ScopeLookup(table, "page", 1))
  389. fprintf(stdout, "page found in scope 1\n");
  390. else
  391. fprintf(stdout, "page NOT found in scope 1\n");
  392. if(ScopeLookup(table, "page", 30))
  393. fprintf(stdout, "page found in scope 30\n");
  394. else
  395. fprintf(stdout, "page NOT found in scope 30\n");
  396. if(ScopeLookup(table, "darling",1))
  397. fprintf(stdout, "darling found\n");
  398. else
  399. fprintf(stdout, "darling NOT found\n");
  400. return 0;
  401. }
  402. */