1 //**********************************************************
2 // TL.CPP
3 // Coco/R C++ Taste Example.
4 // Author: Frankie Arzu <farzu@uvg.edu.gt>
5 //
6 // May 24, 1996 Version 1.06
7 // Jun 16, 1998 Version 1.08 (Minor changes)
8 //**********************************************************
9
10 #include "tl.hpp"
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14
SymTable(CRError * E)15 SymTable::SymTable(CRError *E)
16 {
17 Error = E;
18 topScope = NULL;
19 curLevel = 0;
20 undefObj = (Objectnode *) malloc(sizeof(struct Objectnode));
21 strcpy(undefObj->name, "");
22 undefObj->type = UNDEF;
23 undefObj->kind = VARS;
24 undefObj->adr = 0;
25 undefObj->level = 0;
26 undefObj->next = NULL;
27 }
28
EnterScope()29 void SymTable::EnterScope()
30 {
31 Object scope;
32 scope = (Objectnode *) malloc(sizeof(struct Objectnode));
33 strcpy(scope->name, "");
34 scope->type = UNDEF;
35 scope->kind = SCOPES;
36 scope->locals = NULL;
37 scope->nextAdr = 3;
38 scope->next = topScope;
39 topScope = scope;
40 curLevel++;
41 }
42
LeaveScope()43 void SymTable::LeaveScope()
44 {
45 topScope = topScope->next;
46 curLevel--;
47 }
48
DataSpace()49 int SymTable::DataSpace ()
50 {
51 return topScope->nextAdr - 3;
52 }
53
NewObj(char name[],int kind)54 Object SymTable::NewObj (char name[], int kind)
55 {
56 Object obj, p;
57 obj = (Objectnode *) malloc(sizeof(struct Objectnode));
58 strcpy(obj->name, name);
59 obj->type = UNDEF;
60 obj->kind = kind;
61 obj->level = curLevel;
62
63 p = topScope->locals;
64 while (p!=NULL) {
65 if (strcmp(p->name, name) == 0) Error->ReportError(117);
66 p = p->next;
67 }
68 obj->next = topScope->locals;
69 topScope->locals = obj;
70 if (kind == VARS) {
71 obj->adr = topScope->nextAdr;
72 topScope->nextAdr++;
73 }
74 return obj;
75 }
76
Obj(char name[])77 Object SymTable::Obj(char name[])
78 {
79 Object obj, scope;
80 scope = topScope;
81
82 while (scope != NULL) {
83 obj = scope->locals;
84 while (obj != NULL) {
85 if (strcmp(obj->name, name) == 0) return obj;
86 obj = obj->next;
87 }
88 scope = scope->next;
89 }
90 Error->ReportError(118);
91 return undefObj;
92 }
93