-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemantics.cc
More file actions
50 lines (38 loc) · 961 Bytes
/
semantics.cc
File metadata and controls
50 lines (38 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <map>
#include <cassert>
#include <vector>
using namespace std;
#include "semantics.h"
Symtab::Symtab(Symtab *p) : parent(p) {}
semantics * Symtab::lookup(string key) {
//return local_lookup(key) or parent and parent->lookup(key);
// really want that to work. But alas.
// if (answer) return answer;
//if (parent) return parent->lookup(key);
//return NULL;
// ugly. Simplify:
semantics *answer;
return
(answer = local_lookup(key)) ? answer :
parent ? parent->lookup(key) : NULL;
}
semantics * Symtab::local_lookup(string key) {
return dict[key];
}
void Symtab::insert(string key, semantics * item) {
assert (dict[key] == NULL);
dict[key] = item;
}
// TEST PROGRAM BELOW:
Symtab * currentScope = NULL;
void openscope()
{
currentScope = new Symtab(currentScope);
}
Symtab *closescope()
{
Symtab *v = currentScope;
currentScope = currentScope->parent;
return v;
}