Mercurial > hg > Members > nobuyasu > SampleSource
comparison Bison-Flex/BasicCompiler-MemoryBase/EUC/node.h @ 0:db40c85cad7a default tip
upload sample source
author | nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 09 May 2011 03:11:59 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:db40c85cad7a |
---|---|
1 #ifndef __NODE_H__ | |
2 #define __NODE_H__ | |
3 | |
4 #include <string> | |
5 #include <vector> | |
6 #include <map> | |
7 #include <functional> | |
8 #include <algorithm> | |
9 #include "location.hh" | |
10 #include "vm.h" | |
11 | |
12 class compiler; | |
13 class CNode; | |
14 class CValueNode; | |
15 class CArgDef; | |
16 | |
17 // 配列 | |
18 | |
19 template<typename T> | |
20 class CNodeList { | |
21 struct delete_object { | |
22 void operator()(T *ptr){ delete ptr; } | |
23 } ; | |
24 | |
25 public: | |
26 CNodeList(const yy::location& l, T *node) | |
27 { | |
28 args.push_back(node); | |
29 } | |
30 ~CNodeList() | |
31 { | |
32 std::for_each(args.begin(), args.end(), delete_object()); | |
33 } | |
34 | |
35 CNodeList<T> *Add(const yy::location& l, T *add) | |
36 { | |
37 args.push_back(add); | |
38 return this; | |
39 } | |
40 | |
41 template<typename Fn> | |
42 void for_each(const Fn &func) | |
43 { | |
44 std::for_each(args.begin(), args.end(), func); | |
45 } | |
46 | |
47 template<typename Fn> | |
48 void for_each_rev(const Fn &func) | |
49 { | |
50 std::for_each(args.rbegin(), args.rend(), func); | |
51 } | |
52 | |
53 void analyze(compiler *c) | |
54 { | |
55 std::for_each(args.begin(), args.end(), std::bind2nd(std::mem_fun(&T::analyze), c)); | |
56 } | |
57 | |
58 size_t size() const { return args.size(); } | |
59 T *get(size_t idx) { return args[idx]; } | |
60 T *operator[](size_t idx) { return args[idx]; } | |
61 const T *get(size_t idx) const { return args[idx]; } | |
62 const T *operator[](size_t idx) const { return args[idx]; } | |
63 | |
64 private: | |
65 std::vector<T*> args; | |
66 } ; | |
67 | |
68 typedef CNodeList<CNode> CArgs; | |
69 | |
70 // ノードの命令 | |
71 enum { | |
72 OP_NEG, | |
73 OP_PLUS, | |
74 OP_MINUS, | |
75 OP_TIMES, | |
76 OP_DIVIDE, | |
77 OP_MOD, | |
78 OP_EQ, | |
79 OP_NE, | |
80 OP_GT, | |
81 OP_GE, | |
82 OP_LT, | |
83 OP_LE, | |
84 OP_VALUE, | |
85 OP_CONST, | |
86 OP_RANDFUNC, | |
87 } ; | |
88 | |
89 class CNode; | |
90 class CNodeValue { | |
91 public: | |
92 enum { | |
93 CONST, | |
94 VALUE, | |
95 TEMP, | |
96 } ; | |
97 | |
98 public: | |
99 CNodeValue(): type_(-1), value_(-1), c_(0) | |
100 { | |
101 } | |
102 CNodeValue(int type, int value): type_(type), value_(value), c_(0) | |
103 { | |
104 } | |
105 CNodeValue(compiler *c, int type, int value): type_(type), value_(value), c_(c) | |
106 { | |
107 } | |
108 ~CNodeValue() | |
109 { | |
110 if (type_ == TEMP) { | |
111 ReleaseTempValue(); | |
112 } | |
113 } | |
114 | |
115 // コピーコンストラクタ | |
116 CNodeValue(const CNodeValue &a) | |
117 { | |
118 c_ = a.c_; | |
119 type_ = a.type_; | |
120 value_ = a.value_; | |
121 if (type_ == TEMP) { | |
122 UseTempValue(); | |
123 } | |
124 } | |
125 | |
126 // 代入 | |
127 CNodeValue& operator=(const CNodeValue &a) | |
128 { | |
129 if (this == &a) | |
130 return *this; | |
131 | |
132 if (type_ == TEMP) { | |
133 ReleaseTempValue(); | |
134 } | |
135 c_ = a.c_; | |
136 type_ = a.type_; | |
137 value_ = a.value_; | |
138 if (type_ == TEMP) { | |
139 UseTempValue(); | |
140 } | |
141 return *this; | |
142 } | |
143 | |
144 void ReleaseTempValue(); | |
145 void UseTempValue(); | |
146 | |
147 int type() const { return type_; } | |
148 int value() const; | |
149 | |
150 static CNodeValue MakeNodeValue(compiler *c, CNode *node); | |
151 static CNodeValue MakeTempValue(compiler *c, int value); | |
152 | |
153 private: | |
154 int type_; | |
155 int value_; | |
156 compiler *c_; | |
157 } ; | |
158 | |
159 // ノード | |
160 | |
161 class CNode { | |
162 public: | |
163 CNode(const yy::location& l, int op, CNode *left, CNode *right=0) | |
164 : l_(l), op_(op), left_(left), right_(right), value_(0), string_(0) | |
165 { | |
166 } | |
167 CNode(const yy::location& l, int op, int value) | |
168 : l_(l), op_(op), left_(0), right_(0), value_(value), string_(0) | |
169 { | |
170 } | |
171 CNode(const yy::location& l, int op, std::string *str) | |
172 : l_(l), op_(op), left_(0), right_(0), value_(0), string_(str) | |
173 { | |
174 } | |
175 CNode(const yy::location& l, int op, std::string *str, CNode *node) | |
176 : l_(l), op_(op), left_(node), right_(0), value_(0), string_(str) | |
177 { | |
178 } | |
179 virtual ~CNode() | |
180 { | |
181 delete left_; | |
182 delete right_; | |
183 delete string_; | |
184 } | |
185 | |
186 virtual CNodeValue analyze(compiler *c, int ret = -1); | |
187 | |
188 const yy::location &location() const { return l_; } | |
189 int op() const { return op_; } | |
190 int value() const { return value_; } | |
191 const std::string &string() const { return *string_; } | |
192 const CNode *left() const { return left_; } | |
193 const CNode *right() const { return right_; } | |
194 | |
195 static CNode *MakeNode(compiler &c, const yy::location& l, int op, CNode *left, CNode *right=0); | |
196 | |
197 protected: | |
198 const yy::location l_; | |
199 int op_; | |
200 int value_; | |
201 std::string *string_; | |
202 CNode *left_; | |
203 CNode *right_; | |
204 } ; | |
205 | |
206 // 変数ノード | |
207 | |
208 class CValueNode: public CNode { | |
209 public: | |
210 CValueNode(const yy::location& l, std::string *name, CNode *node=NULL) | |
211 : CNode(l, OP_VALUE, name, node) | |
212 { | |
213 } | |
214 } ; | |
215 | |
216 // 代入文用 | |
217 | |
218 class CAssign { | |
219 public: | |
220 CAssign(const yy::location& l, int op, CNode *value, CNode *expr) | |
221 : l_(l), op_(op), value_(value), expr_(expr) | |
222 { | |
223 } | |
224 ~CAssign() | |
225 { | |
226 delete value_; | |
227 delete expr_; | |
228 } | |
229 | |
230 CNodeValue analyze(compiler *c); | |
231 | |
232 private: | |
233 const yy::location l_; | |
234 int op_; | |
235 CNode *value_; | |
236 CNode *expr_; | |
237 } ; | |
238 | |
239 #endif |