comparison Bison-Flex/Compiler-StackBase/UTF8/node.h @ 5:caede627f691

chage encoding
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 17 May 2011 12:45:07 +0900
parents 805d39d28230
children 86c0a38332fe
comparison
equal deleted inserted replaced
4:805d39d28230 5:caede627f691
1 // 1 //
2 // ¥Î¡¼¥É 2 // ノード
3 // 3 //
4 // (c)2008 Chihiro.SAKAMOTO HyperWorks 4 // (c)2008 Chihiro.SAKAMOTO HyperWorks
5 // 5 //
6 #ifndef __NODE_H__ 6 #ifndef __NODE_H__
7 #define __NODE_H__ 7 #define __NODE_H__
18 class CValueNode; 18 class CValueNode;
19 class CDecl; 19 class CDecl;
20 class CArgDef; 20 class CArgDef;
21 class CStatement; 21 class CStatement;
22 22
23 // ÇÛÎó 23 // é…列
24 24
25 template<typename T> 25 template<typename T>
26 class CNodeList { 26 class CNodeList {
27 struct delete_object { 27 struct delete_object {
28 void operator()(T *ptr){ delete ptr; } 28 void operator()(T *ptr){ delete ptr; }
70 typedef CNodeList<CValueNode> CValueList; 70 typedef CNodeList<CValueNode> CValueList;
71 typedef CNodeList<CArgDef> CArgList; 71 typedef CNodeList<CArgDef> CArgList;
72 typedef CNodeList<CDecl> CDeclList; 72 typedef CNodeList<CDecl> CDeclList;
73 typedef CNodeList<CStatement> CStateList; 73 typedef CNodeList<CStatement> CStateList;
74 74
75 // ÊÑ¿ô¡¢´Ø¿ô¤Î·¿ 75 // 変数ã€é–¢æ•°ã®åž‹
76 enum { 76 enum {
77 TYPE_INTEGER, 77 TYPE_INTEGER,
78 TYPE_STRING, 78 TYPE_STRING,
79 TYPE_INTEGER_REF, 79 TYPE_INTEGER_REF,
80 TYPE_STRING_REF, 80 TYPE_STRING_REF,
81 TYPE_VOID, 81 TYPE_VOID,
82 } ; 82 TYPE_CODE,
83 83 };
84 // ÊÑ¿ô·¿¤ËÂбþ¤·¤¿»²¾È·¿¤òÆÀ¤ë 84
85 // 変数型ã«å¯¾å¿œã—ãŸå‚照型を得る
85 inline int TypeToRef(int type) 86 inline int TypeToRef(int type)
86 { 87 {
87 return type + TYPE_INTEGER_REF - TYPE_INTEGER; 88 return type + TYPE_INTEGER_REF - TYPE_INTEGER;
88 } 89 }
89 90
90 // ¥Î¡¼¥É¤ÎÌ¿Îá 91 // ノードã®å‘½ä»¤
91 enum { 92 enum {
92 OP_NEG, 93 OP_NEG,
93 OP_PLUS, 94 OP_PLUS,
94 OP_MINUS, 95 OP_MINUS,
95 OP_TIMES, 96 OP_TIMES,
111 OP_CONST, 112 OP_CONST,
112 OP_STRING, 113 OP_STRING,
113 OP_FUNCTION, 114 OP_FUNCTION,
114 } ; 115 } ;
115 116
116 // ¥Ð¥¤¥Ê¥ê¥Î¡¼¥É 117 // ãƒã‚¤ãƒŠãƒªãƒŽãƒ¼ãƒ‰
117 118
118 class CNode { 119 class CNode {
119 public: 120 public:
120 CNode(const yy::location& l, int op, CNode *left, CNode *right=0) 121 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 : l_(l), op_(op), left_(left), right_(right), value_(0), string_(0)
159 std::string *string_; 160 std::string *string_;
160 CNode *left_; 161 CNode *left_;
161 CNode *right_; 162 CNode *right_;
162 } ; 163 } ;
163 164
164 // ÊÑ¿ô¥Î¡¼¥É 165 // 変数ノード
165 166
166 class CValueNode: public CNode { 167 class CValueNode: public CNode {
167 public: 168 public:
168 CValueNode(const yy::location& l, std::string *name, CNode *node=NULL) 169 CValueNode(const yy::location& l, std::string *name, CNode *node=NULL)
169 : CNode(l, OP_VALUE, name, node) 170 : CNode(l, OP_VALUE, name, node)
172 173
173 int push(compiler *c) const; 174 int push(compiler *c) const;
174 int pop(compiler *c) const; 175 int pop(compiler *c) const;
175 } ; 176 } ;
176 177
177 // ´Ø¿ô¥Î¡¼¥É 178 // 関数ノード
178 179
179 class CFunctionNode: public CNode { 180 class CFunctionNode: public CNode {
180 public: 181 public:
181 CFunctionNode(const yy::location& l, std::string *name, CArgs *args) 182 CFunctionNode(const yy::location& l, std::string *name, CArgs *args)
182 : CNode(l, OP_FUNCTION, name), args_(args) 183 : CNode(l, OP_FUNCTION, name), args_(args)
192 193
193 private: 194 private:
194 CArgs *args_; 195 CArgs *args_;
195 } ; 196 } ;
196 197
197 // ÂåÆþʸÍÑ 198 // 代入文用
198 199
199 class CAssign { 200 class CAssign {
200 public: 201 public:
201 CAssign(const yy::location& l, int op, CNode *value, CNode *expr) 202 CAssign(const yy::location& l, int op, CNode *value, CNode *expr)
202 : l_(l), op_(op), value_(value), expr_(expr) 203 : l_(l), op_(op), value_(value), expr_(expr)
215 int op_; 216 int op_;
216 CNode *value_; 217 CNode *value_;
217 CNode *expr_; 218 CNode *expr_;
218 } ; 219 } ;
219 220
220 // °ú¿ôÄêµÁ 221 // 引数定義
221 222
222 class CArgDef { 223 class CArgDef {
223 public: 224 public:
224 CArgDef(const yy::location& l, int type, const std::string *name) 225 CArgDef(const yy::location& l, int type, const std::string *name)
225 : l_(l), type_(type), name_(name) 226 : l_(l), type_(type), name_(name)
238 const yy::location l_; 239 const yy::location l_;
239 int type_; 240 int type_;
240 const std::string *name_; 241 const std::string *name_;
241 } ; 242 } ;
242 243
243 // ÊÑ¿ôÄêµÁ 244 // 変数定義
244 245
245 class CDecl { 246 class CDecl {
246 public: 247 public:
247 CDecl(int type, CValueList *list) 248 CDecl(int type, CValueList *list)
248 : type_(type), list_(list) 249 : type_(type), list_(list)
258 private: 259 private:
259 int type_; 260 int type_;
260 CValueList *list_; 261 CValueList *list_;
261 } ; 262 } ;
262 263
263 // '{' '}' ¤Ë¤è¤ëʸ¤Î¤Þ¤È¤Þ¤ê 264 // '{' '}' ã«ã‚ˆã‚‹æ–‡ã®ã¾ã¨ã¾ã‚Š
264 265
265 class CStateBlock { 266 class CStateBlock {
266 public: 267 public:
267 CStateBlock(CDeclList *decls, CStateList *states) 268 CStateBlock(CDeclList *decls, CStateList *states)
268 : decls_(decls), states_(states) 269 : decls_(decls), states_(states)
279 private: 280 private:
280 CDeclList *decls_; 281 CDeclList *decls_;
281 CStateList *states_; 282 CStateList *states_;
282 } ; 283 } ;
283 284
284 // caseʸ¤Î½èÍý 285 // caseæ–‡ã®å‡¦ç†
285 286
286 struct case_action_param { 287 struct case_action_param {
287 compiler *comp_; 288 compiler *comp_;
288 int &default_label; 289 int &default_label;
289 case_action_param(compiler *comp, int &label): comp_(comp), default_label(label) 290 case_action_param(compiler *comp, int &label): comp_(comp), default_label(label)
290 { 291 {
291 } 292 }
292 } ; 293 } ;
293 294
294 // ʸ 295 // 文
295 296
296 class CStatement { 297 class CStatement {
297 public: 298 public:
298 enum { 299 enum {
299 NOP, 300 NOP,
327 protected: 328 protected:
328 const yy::location l_; 329 const yy::location l_;
329 int code_; 330 int code_;
330 } ; 331 } ;
331 332
332 // nopʸ 333 // nop文
333 334
334 class CNopStatement: public CStatement { 335 class CNopStatement: public CStatement {
335 public: 336 public:
336 CNopStatement(const yy::location& l) 337 CNopStatement(const yy::location& l)
337 : CStatement(l, NOP) 338 : CStatement(l, NOP)
338 { 339 {
339 } 340 }
340 virtual void analyze(compiler *c); 341 virtual void analyze(compiler *c);
341 } ; 342 } ;
342 343
343 // ÂåÆþʸ 344 // 代入文
344 345
345 class CAssignStatement: public CStatement { 346 class CAssignStatement: public CStatement {
346 public: 347 public:
347 CAssignStatement(const yy::location& l, CAssign *assign) 348 CAssignStatement(const yy::location& l, CAssign *assign)
348 : CStatement(l, ASSIGN), assign_(assign) 349 : CStatement(l, ASSIGN), assign_(assign)
357 358
358 private: 359 private:
359 CAssign *assign_; 360 CAssign *assign_;
360 } ; 361 } ;
361 362
362 // ´Ø¿ô¸Æ¤Ó½Ð¤· 363 // 関数呼ã³å‡ºã—
363 364
364 class CFunctionStatement: public CStatement { 365 class CFunctionStatement: public CStatement {
365 public: 366 public:
366 CFunctionStatement(const yy::location& l, std::string *name, CArgs *args) 367 CFunctionStatement(const yy::location& l, std::string *name, CArgs *args)
367 : CStatement(l, FUNCTION), node_(l, name, args) 368 : CStatement(l, FUNCTION), node_(l, name, args)
375 376
376 private: 377 private:
377 CFunctionNode node_; 378 CFunctionNode node_;
378 } ; 379 } ;
379 380
380 // ifʸ 381 // if文
381 382
382 class CIfStatement: public CStatement { 383 class CIfStatement: public CStatement {
383 public: 384 public:
384 CIfStatement(const yy::location& l, CNode *expr, CStatement *then_statement, CStatement *else_statement=NULL) 385 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 : CStatement(l, IF), expr_(expr), then_statement_(then_statement), else_statement_(else_statement)
398 CNode *expr_; 399 CNode *expr_;
399 CStatement *then_statement_; 400 CStatement *then_statement_;
400 CStatement *else_statement_; 401 CStatement *else_statement_;
401 } ; 402 } ;
402 403
403 // forʸ 404 // for文
404 405
405 class CForStatement: public CStatement { 406 class CForStatement: public CStatement {
406 public: 407 public:
407 CForStatement(const yy::location& l, CAssign *init, CNode *expr, CAssign *next, CStatement *statement) 408 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 : CStatement(l, FOR), init_(init), expr_(expr), next_(next), statement_(statement)
423 CNode *expr_; 424 CNode *expr_;
424 CAssign *next_; 425 CAssign *next_;
425 CStatement *statement_; 426 CStatement *statement_;
426 } ; 427 } ;
427 428
428 // whileʸ 429 // while文
429 430
430 class CWhileStatement: public CStatement { 431 class CWhileStatement: public CStatement {
431 public: 432 public:
432 CWhileStatement(const yy::location& l, CNode *expr, CStatement *statement) 433 CWhileStatement(const yy::location& l, CNode *expr, CStatement *statement)
433 : CStatement(l, WHILE), expr_(expr), statement_(statement) 434 : CStatement(l, WHILE), expr_(expr), statement_(statement)
444 private: 445 private:
445 CNode *expr_; 446 CNode *expr_;
446 CStatement *statement_; 447 CStatement *statement_;
447 } ; 448 } ;
448 449
449 // switchʸ 450 // switch文
450 451
451 class CSwitchStatement: public CStatement { 452 class CSwitchStatement: public CStatement {
452 public: 453 public:
453 CSwitchStatement(const yy::location& l, CNode *expr, CStateList *list) 454 CSwitchStatement(const yy::location& l, CNode *expr, CStateList *list)
454 : CStatement(l, SWITCH), expr_(expr), list_(list) 455 : CStatement(l, SWITCH), expr_(expr), list_(list)
465 private: 466 private:
466 CNode *expr_; 467 CNode *expr_;
467 CStateList *list_; 468 CStateList *list_;
468 } ; 469 } ;
469 470
470 // caseʸ 471 // case文
471 472
472 class CCaseStatement: public CStatement { 473 class CCaseStatement: public CStatement {
473 public: 474 public:
474 CCaseStatement(const yy::location& l, CNode *expr) 475 CCaseStatement(const yy::location& l, CNode *expr)
475 : CStatement(l, CASE), expr_(expr) 476 : CStatement(l, CASE), expr_(expr)
486 private: 487 private:
487 CNode *expr_; 488 CNode *expr_;
488 int label_; 489 int label_;
489 } ; 490 } ;
490 491
491 // defaultʸ 492 // default文
492 493
493 class CDefaultStatement: public CStatement { 494 class CDefaultStatement: public CStatement {
494 public: 495 public:
495 CDefaultStatement(const yy::location& l) 496 CDefaultStatement(const yy::location& l)
496 : CStatement(l, DEFAULT) 497 : CStatement(l, DEFAULT)
502 503
503 private: 504 private:
504 int label_; 505 int label_;
505 } ; 506 } ;
506 507
507 // breakʸ 508 // break文
508 509
509 class CBreakStatement: public CStatement { 510 class CBreakStatement: public CStatement {
510 public: 511 public:
511 CBreakStatement(const yy::location& l) 512 CBreakStatement(const yy::location& l)
512 : CStatement(l, BREAK) 513 : CStatement(l, BREAK)
514 } 515 }
515 516
516 virtual void analyze(compiler *c); 517 virtual void analyze(compiler *c);
517 } ; 518 } ;
518 519
519 // returnʸ 520 // return文
520 521
521 class CReturnStatement: public CStatement { 522 class CReturnStatement: public CStatement {
522 public: 523 public:
523 CReturnStatement(const yy::location& l, CNode *expr) 524 CReturnStatement(const yy::location& l, CNode *expr)
524 : CStatement(l, RETURN), expr_(expr) 525 : CStatement(l, RETURN), expr_(expr)
533 534
534 private: 535 private:
535 CNode *expr_; 536 CNode *expr_;
536 } ; 537 } ;
537 538
538 // ¥Ö¥í¥Ã¥¯Ê¸ 539 // ブロック文
539 540
540 class CBlockStatement: public CStatement { 541 class CBlockStatement: public CStatement {
541 public: 542 public:
542 CBlockStatement(const yy::location& l, CStateBlock *block) 543 CBlockStatement(const yy::location& l, CStateBlock *block)
543 : CStatement(l, BLOCK), block_(block) 544 : CStatement(l, BLOCK), block_(block)