Mercurial > hg > Members > nobuyasu > SampleSource
comparison WindowsOnly/WinScript2/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 <algorithm> | |
8 #include "location.hh" | |
9 #include "vm.h" | |
10 | |
11 class compiler; | |
12 class CNode; | |
13 class CValueNode; | |
14 class CDecl; | |
15 class CArgDef; | |
16 class CStatement; | |
17 | |
18 // 配列 | |
19 | |
20 template<typename T> | |
21 class CNodeList { | |
22 struct delete_object { | |
23 void operator()(T *ptr){ delete ptr; } | |
24 } ; | |
25 | |
26 public: | |
27 CNodeList(T *node) | |
28 { | |
29 args.push_back(node); | |
30 } | |
31 ~CNodeList() | |
32 { | |
33 std::for_each(args.begin(), args.end(), delete_object()); | |
34 } | |
35 | |
36 CNodeList<T> *Add(T *add) | |
37 { | |
38 args.push_back(add); | |
39 return this; | |
40 } | |
41 | |
42 template<typename Fn> | |
43 void for_each(const Fn &func) | |
44 { | |
45 std::for_each(args.begin(), args.end(), func); | |
46 } | |
47 | |
48 template<typename Fn> | |
49 void for_each_rev(const Fn &func) | |
50 { | |
51 std::for_each(args.rbegin(), args.rend(), func); | |
52 } | |
53 | |
54 size_t size() const { return args.size(); } | |
55 T *get(size_t idx) { return args[idx]; } | |
56 T *operator[](size_t idx) { return args[idx]; } | |
57 const T *get(size_t idx) const { return args[idx]; } | |
58 const T *operator[](size_t idx) const { return args[idx]; } | |
59 | |
60 private: | |
61 std::vector<T*> args; | |
62 } ; | |
63 | |
64 typedef CNodeList<CNode> CArgs; | |
65 typedef CNodeList<CValueNode> CValueList; | |
66 typedef CNodeList<CArgDef> CArgList; | |
67 typedef CNodeList<CDecl> CDeclList; | |
68 typedef CNodeList<CStatement> CStateList; | |
69 | |
70 // 変数、関数の型 | |
71 enum { | |
72 TYPE_INTEGER, | |
73 TYPE_STRING, | |
74 TYPE_INTEGER_REF, | |
75 TYPE_STRING_REF, | |
76 TYPE_VOID, | |
77 } ; | |
78 | |
79 // 変数型に対応した参照型を得る | |
80 inline int TypeToRef(int type) | |
81 { | |
82 return type + TYPE_INTEGER_REF - TYPE_INTEGER; | |
83 } | |
84 | |
85 // ノードの命令 | |
86 enum { | |
87 OP_NEG, | |
88 OP_PLUS, | |
89 OP_MINUS, | |
90 OP_TIMES, | |
91 OP_DIVIDE, | |
92 OP_MOD, | |
93 OP_AND, | |
94 OP_OR, | |
95 OP_LSHIFT, | |
96 OP_RSHIFT, | |
97 OP_LOGAND, | |
98 OP_LOGOR, | |
99 OP_EQ, | |
100 OP_NE, | |
101 OP_GT, | |
102 OP_GE, | |
103 OP_LT, | |
104 OP_LE, | |
105 OP_VALUE, | |
106 OP_CONST, | |
107 OP_STRING, | |
108 OP_FUNCTION, | |
109 } ; | |
110 | |
111 // ノード | |
112 | |
113 class CNode { | |
114 public: | |
115 CNode(const yy::location& l, int op, CNode *left, CNode *right=0) | |
116 : l_(l), op_(op), left_(left), right_(right), value_(0), string_(0) | |
117 { | |
118 } | |
119 CNode(const yy::location& l, int op, int value) | |
120 : l_(l), op_(op), left_(0), right_(0), value_(value), string_(0) | |
121 { | |
122 } | |
123 CNode(const yy::location& l, int op, std::string *str) | |
124 : l_(l), op_(op), left_(0), right_(0), value_(0), string_(str) | |
125 { | |
126 } | |
127 CNode(const yy::location& l, int op, std::string *str, CNode *node) | |
128 : l_(l), op_(op), left_(node), right_(0), value_(0), string_(str) | |
129 { | |
130 } | |
131 virtual ~CNode() | |
132 { | |
133 delete left_; | |
134 delete right_; | |
135 delete string_; | |
136 } | |
137 | |
138 virtual int push(compiler *c) const; | |
139 virtual int pop(compiler *c) const; | |
140 | |
141 const yy::location &location() const { return l_; } | |
142 int op() const { return op_; } | |
143 int value() const { return value_; } | |
144 const std::string &string() const { return *string_; } | |
145 const CNode *left() const { return left_; } | |
146 const CNode *right() const { return right_; } | |
147 | |
148 static CNode *MakeNode(compiler &c, const yy::location& l, int op, CNode *left, CNode *right=0); | |
149 | |
150 protected: | |
151 const yy::location l_; | |
152 int op_; | |
153 int value_; | |
154 std::string *string_; | |
155 CNode *left_; | |
156 CNode *right_; | |
157 } ; | |
158 | |
159 // 変数ノード | |
160 | |
161 class CValueNode: public CNode { | |
162 public: | |
163 CValueNode(const yy::location& l, std::string *name, CNode *node=NULL) | |
164 : CNode(l, OP_VALUE, name, node) | |
165 { | |
166 } | |
167 | |
168 int push(compiler *c) const; | |
169 int pop(compiler *c) const; | |
170 } ; | |
171 | |
172 // 関数ノード | |
173 | |
174 class CFunctionNode: public CNode { | |
175 public: | |
176 CFunctionNode(const yy::location& l, std::string *name, CArgs *args) | |
177 : CNode(l, OP_FUNCTION, name), args_(args) | |
178 { | |
179 } | |
180 ~CFunctionNode() | |
181 { | |
182 delete args_; | |
183 } | |
184 | |
185 int push(compiler *c) const; | |
186 int pop(compiler *c) const; | |
187 | |
188 private: | |
189 CArgs *args_; | |
190 } ; | |
191 | |
192 // 代入文用 | |
193 | |
194 class CAssign { | |
195 public: | |
196 CAssign(const yy::location& l, int op, CNode *value, CNode *expr) | |
197 : l_(l), op_(op), value_(value), expr_(expr) | |
198 { | |
199 } | |
200 ~CAssign() | |
201 { | |
202 delete value_; | |
203 delete expr_; | |
204 } | |
205 | |
206 void analyze(compiler *c); | |
207 | |
208 private: | |
209 const yy::location l_; | |
210 int op_; | |
211 CNode *value_; | |
212 CNode *expr_; | |
213 } ; | |
214 | |
215 // 引数定義 | |
216 | |
217 class CArgDef { | |
218 public: | |
219 CArgDef(const yy::location& l, int type, const std::string *name) | |
220 : l_(l), type_(type), name_(name) | |
221 { | |
222 } | |
223 ~CArgDef() | |
224 { | |
225 delete name_; | |
226 } | |
227 | |
228 const yy::location &location() const { return l_; } | |
229 int type() const { return type_; } | |
230 const std::string &name() const { return *name_; } | |
231 | |
232 private: | |
233 const yy::location l_; | |
234 int type_; | |
235 const std::string *name_; | |
236 } ; | |
237 | |
238 // 変数定義 | |
239 | |
240 class CDecl { | |
241 public: | |
242 CDecl(int type, CValueList *list) | |
243 : type_(type), list_(list) | |
244 { | |
245 } | |
246 ~CDecl() | |
247 { | |
248 delete list_; | |
249 } | |
250 | |
251 void analyze(compiler *c); | |
252 | |
253 private: | |
254 int type_; | |
255 CValueList *list_; | |
256 } ; | |
257 | |
258 // '{' '}' による文のまとまり | |
259 | |
260 class CStateBlock { | |
261 public: | |
262 CStateBlock(CDeclList *decls, CStateList *states) | |
263 : decls_(decls), states_(states) | |
264 { | |
265 } | |
266 ~CStateBlock() | |
267 { | |
268 delete decls_; | |
269 delete states_; | |
270 } | |
271 | |
272 void analyze(compiler *c); | |
273 | |
274 private: | |
275 CDeclList *decls_; | |
276 CStateList *states_; | |
277 } ; | |
278 | |
279 // case文の処理 | |
280 | |
281 struct case_action_param { | |
282 compiler *comp_; | |
283 int &default_label; | |
284 case_action_param(compiler *comp, int &label): comp_(comp), default_label(label) | |
285 { | |
286 } | |
287 } ; | |
288 | |
289 // 文 | |
290 | |
291 class CStatement { | |
292 public: | |
293 enum { | |
294 NOP, | |
295 ASSIGN, | |
296 FUNCTION, | |
297 IF, | |
298 FOR, | |
299 WHILE, | |
300 SWITCH, | |
301 CASE, | |
302 DEFAULT, | |
303 BREAK, | |
304 RETURN, | |
305 BLOCK, | |
306 } ; | |
307 | |
308 public: | |
309 CStatement(const yy::location& l, int code) | |
310 : l_(l), code_(code) | |
311 { | |
312 } | |
313 virtual ~CStatement() | |
314 { | |
315 } | |
316 | |
317 virtual void analyze(compiler *c) = 0; | |
318 virtual void case_analyze(case_action_param *param) | |
319 { | |
320 } | |
321 | |
322 protected: | |
323 const yy::location l_; | |
324 int code_; | |
325 } ; | |
326 | |
327 // nop文 | |
328 | |
329 class CNopStatement: public CStatement { | |
330 public: | |
331 CNopStatement(const yy::location& l) | |
332 : CStatement(l, NOP) | |
333 { | |
334 } | |
335 virtual void analyze(compiler *c); | |
336 } ; | |
337 | |
338 // 代入文 | |
339 | |
340 class CAssignStatement: public CStatement { | |
341 public: | |
342 CAssignStatement(const yy::location& l, CAssign *assign) | |
343 : CStatement(l, ASSIGN), assign_(assign) | |
344 { | |
345 } | |
346 ~CAssignStatement() | |
347 { | |
348 delete assign_; | |
349 } | |
350 | |
351 virtual void analyze(compiler *c); | |
352 | |
353 private: | |
354 CAssign *assign_; | |
355 } ; | |
356 | |
357 // 関数呼び出し | |
358 | |
359 class CFunctionStatement: public CStatement { | |
360 public: | |
361 CFunctionStatement(const yy::location& l, std::string *name, CArgs *args) | |
362 : CStatement(l, FUNCTION), node_(l, name, args) | |
363 { | |
364 } | |
365 ~CFunctionStatement() | |
366 { | |
367 } | |
368 | |
369 virtual void analyze(compiler *c); | |
370 | |
371 private: | |
372 CFunctionNode node_; | |
373 } ; | |
374 | |
375 // if文 | |
376 | |
377 class CIfStatement: public CStatement { | |
378 public: | |
379 CIfStatement(const yy::location& l, CNode *expr, CStatement *then_statement, CStatement *else_statement=NULL) | |
380 : CStatement(l, IF), expr_(expr), then_statement_(then_statement), else_statement_(else_statement) | |
381 { | |
382 } | |
383 ~CIfStatement() | |
384 { | |
385 delete expr_; | |
386 delete then_statement_; | |
387 delete else_statement_; | |
388 } | |
389 | |
390 virtual void analyze(compiler *c); | |
391 | |
392 private: | |
393 CNode *expr_; | |
394 CStatement *then_statement_; | |
395 CStatement *else_statement_; | |
396 } ; | |
397 | |
398 // while文 | |
399 | |
400 class CForStatement: public CStatement { | |
401 public: | |
402 CForStatement(const yy::location& l, CAssign *init, CNode *expr, CAssign *next, CStatement *statement) | |
403 : CStatement(l, FOR), init_(init), expr_(expr), next_(next), statement_(statement) | |
404 { | |
405 } | |
406 ~CForStatement() | |
407 { | |
408 delete init_; | |
409 delete expr_; | |
410 delete next_; | |
411 delete statement_; | |
412 } | |
413 | |
414 virtual void analyze(compiler *c); | |
415 | |
416 private: | |
417 CAssign *init_; | |
418 CNode *expr_; | |
419 CAssign *next_; | |
420 CStatement *statement_; | |
421 } ; | |
422 | |
423 // while文 | |
424 | |
425 class CWhileStatement: public CStatement { | |
426 public: | |
427 CWhileStatement(const yy::location& l, CNode *expr, CStatement *statement) | |
428 : CStatement(l, WHILE), expr_(expr), statement_(statement) | |
429 { | |
430 } | |
431 ~CWhileStatement() | |
432 { | |
433 delete expr_; | |
434 delete statement_; | |
435 } | |
436 | |
437 virtual void analyze(compiler *c); | |
438 | |
439 private: | |
440 CNode *expr_; | |
441 CStatement *statement_; | |
442 } ; | |
443 | |
444 // switch文 | |
445 | |
446 class CSwitchStatement: public CStatement { | |
447 public: | |
448 CSwitchStatement(const yy::location& l, CNode *expr, CStateList *list) | |
449 : CStatement(l, SWITCH), expr_(expr), list_(list) | |
450 { | |
451 } | |
452 ~CSwitchStatement() | |
453 { | |
454 delete expr_; | |
455 delete list_; | |
456 } | |
457 | |
458 virtual void analyze(compiler *c); | |
459 | |
460 private: | |
461 CNode *expr_; | |
462 CStateList *list_; | |
463 } ; | |
464 | |
465 // case文 | |
466 | |
467 class CCaseStatement: public CStatement { | |
468 public: | |
469 CCaseStatement(const yy::location& l, CNode *expr) | |
470 : CStatement(l, CASE), expr_(expr) | |
471 { | |
472 } | |
473 ~CCaseStatement() | |
474 { | |
475 delete expr_; | |
476 } | |
477 | |
478 virtual void analyze(compiler *c); | |
479 virtual void case_analyze(case_action_param *param); | |
480 | |
481 private: | |
482 CNode *expr_; | |
483 int label_; | |
484 } ; | |
485 | |
486 // default文 | |
487 | |
488 class CDefaultStatement: public CStatement { | |
489 public: | |
490 CDefaultStatement(const yy::location& l) | |
491 : CStatement(l, DEFAULT) | |
492 { | |
493 } | |
494 | |
495 virtual void analyze(compiler *c); | |
496 virtual void case_analyze(case_action_param *param); | |
497 | |
498 private: | |
499 int label_; | |
500 } ; | |
501 | |
502 // break文 | |
503 | |
504 class CBreakStatement: public CStatement { | |
505 public: | |
506 CBreakStatement(const yy::location& l) | |
507 : CStatement(l, BREAK) | |
508 { | |
509 } | |
510 | |
511 virtual void analyze(compiler *c); | |
512 } ; | |
513 | |
514 // return文 | |
515 | |
516 class CReturnStatement: public CStatement { | |
517 public: | |
518 CReturnStatement(const yy::location& l, CNode *expr) | |
519 : CStatement(l, RETURN), expr_(expr) | |
520 { | |
521 } | |
522 ~CReturnStatement() | |
523 { | |
524 delete expr_; | |
525 } | |
526 | |
527 virtual void analyze(compiler *c); | |
528 | |
529 private: | |
530 CNode *expr_; | |
531 } ; | |
532 | |
533 // ブロック文 | |
534 | |
535 class CBlockStatement: public CStatement { | |
536 public: | |
537 CBlockStatement(const yy::location& l, CStateBlock *block) | |
538 : CStatement(l, BLOCK), block_(block) | |
539 { | |
540 } | |
541 ~CBlockStatement() | |
542 { | |
543 delete block_; | |
544 } | |
545 | |
546 virtual void analyze(compiler *c); | |
547 | |
548 private: | |
549 CStateBlock *block_; | |
550 } ; | |
551 | |
552 #endif |