forked from pssrawat/artemis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.hpp
148 lines (128 loc) · 3.85 KB
/
templates.hpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#ifndef __SYMTAB_HPP__
#define __SYMTAB_HPP__
#include <cstdio>
#include <cstring>
#include <map>
#include <cstdlib>
#include <iostream>
#include <cassert>
#include <vector>
class arrayDecl;
class exprNode;
/* A class that represents vector. Needed for parsing */
template <typename T>
class vecTemplate {
protected:
std::vector<T> vec_list;
public:
virtual void push_back (T);
virtual void set_element (T, int);
virtual std::vector<T> get_list (void);
};
template <typename T>
inline void vecTemplate<T>::push_back (T value) {
vec_list.push_back (value);
}
template <typename T>
inline void vecTemplate<T>::set_element (T value, int pos) {
assert ((int)vec_list.size () >= pos);
vec_list[pos] = value;
}
template <typename T>
inline std::vector<T> vecTemplate<T>::get_list (void) {
return vec_list;
}
/* A class that represents a vector of args */
class argList : public vecTemplate<std::string> {
private:
std::vector<std::string> inouts;
public:
void push_inout (std::string);
std::vector<std::string> get_inouts (void);
};
inline void argList::push_inout (std::string value) {
std::cout << "Pushing " << value << std::endl;
inouts.push_back (value);
}
inline std::vector<std::string> argList::get_inouts (void) {
return inouts;
}
/* A class that represents symbol table. Basically a map from string to
any data structure. */
template <typename T>
class symtabTemplate {
protected:
std::map<std::string, T> symbol_map;
public:
void push_symbol (std::string, T);
void push_symbol (char *, T);
void delete_symbol (char *);
void delete_symbol (std::string);
void delete_symbols (void);
T find_symbol (char *);
T find_symbol (std::string);
bool symbol_present (std::string);
bool symbol_present (char *);
std::map<std::string, T> get_symbol_map (void);
};
template <typename T>
inline std::map<std::string, T> symtabTemplate<T>::get_symbol_map (void) {
return symbol_map;
}
template <typename T>
inline void symtabTemplate<T>::push_symbol (std::string s, T value) {
if (symbol_map.find (s) != symbol_map.end ()) {
assert (symbol_map[s]==value && "Pushing symbol that is already present with different type");
}
else
symbol_map.insert (make_pair (s, value));
}
template <typename T>
inline void symtabTemplate<T>::push_symbol (char *s, T value) {
std::string key = std::string (s);
if (symbol_map.find (key) != symbol_map.end ()) {
assert (symbol_map[key]==value && "Pushing symbol that is already present with different type");
}
else
symbol_map.insert (make_pair (key, value));
}
template <typename T>
inline void symtabTemplate<T>::delete_symbol (char *s) {
std::string key = std::string (s);
if (symbol_map.find (key) != symbol_map.end ())
symbol_map.erase (key);
}
template <typename T>
inline void symtabTemplate<T>::delete_symbol (std::string key) {
if (symbol_map.find (key) != symbol_map.end ())
symbol_map.erase (key);
}
template <typename T>
inline void symtabTemplate<T>::delete_symbols (void) {
symbol_map.clear ();
}
template <typename T>
inline T symtabTemplate<T>::find_symbol (char *s) {
std::string key = std::string (s);
typename std::map <std::string, T>::iterator it = symbol_map.find (key);
assert (it != symbol_map.end ());
return it->second;
}
template <typename T>
inline T symtabTemplate<T>::find_symbol (std::string key) {
typename std::map <std::string, T>::iterator it = symbol_map.find (key);
assert (it != symbol_map.end ());
return it->second;
}
template <typename T>
inline bool symtabTemplate<T>::symbol_present (char *s) {
std::string key = std::string (s);
typename std::map <std::string, T>::iterator it = symbol_map.find (key);
return it != symbol_map.end ();
}
template <typename T>
inline bool symtabTemplate<T>::symbol_present (std::string key) {
typename std::map <std::string, T>::iterator it = symbol_map.find (key);
return it != symbol_map.end ();
}
#endif