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