0
|
1 #ifndef __NODE_H__
|
|
2 #define __NODE_H__
|
|
3
|
|
4 #include <iostream>
|
|
5 #include <string>
|
|
6 #include <vector>
|
|
7 #include <boost/shared_ptr.hpp>
|
|
8 #include <boost/bind.hpp>
|
|
9
|
|
10 class compiler;
|
|
11
|
|
12 // ノード
|
|
13
|
|
14 // 変数、関数の型
|
|
15 enum VALUE_TYPE {
|
|
16 TYPE_INTEGER,
|
|
17 TYPE_STRING,
|
|
18 TYPE_VOID,
|
|
19 TYPE_REF = 0x80,
|
|
20 TYPE_INTEGER_REF = TYPE_INTEGER | TYPE_REF,
|
|
21 TYPE_STRING_REF = TYPE_STRING | TYPE_REF,
|
|
22 } ;
|
|
23
|
|
24 // ノードの命令
|
|
25 enum OPCODE {
|
|
26 OP_NEG,
|
|
27 OP_ADD,
|
|
28 OP_SUB,
|
|
29 OP_MUL,
|
|
30 OP_DIV,
|
|
31 OP_MOD,
|
|
32 OP_AND,
|
|
33 OP_OR,
|
|
34 OP_LSHIFT,
|
|
35 OP_RSHIFT,
|
|
36 OP_LOGAND,
|
|
37 OP_LOGOR,
|
|
38 OP_EQ,
|
|
39 OP_NE,
|
|
40 OP_GT,
|
|
41 OP_GE,
|
|
42 OP_LT,
|
|
43 OP_LE,
|
|
44 OP_ASSIGN,
|
|
45 OP_ADD_ASSIGN,
|
|
46 OP_SUB_ASSIGN,
|
|
47 OP_MUL_ASSIGN,
|
|
48 OP_DIV_ASSIGN,
|
|
49 OP_MOD_ASSIGN,
|
|
50 OP_AND_ASSIGN,
|
|
51 OP_OR_ASSIGN,
|
|
52 OP_LSHIFT_ASSIGN,
|
|
53 OP_RSHIFT_ASSIGN,
|
|
54 OP_NUMBER,
|
|
55 OP_IDENTIFIER,
|
|
56 OP_STRING,
|
|
57 OP_FUNCTION,
|
|
58 OP_ARRAY,
|
|
59 } ;
|
|
60
|
|
61 // ノード
|
|
62
|
|
63 class cnode;
|
|
64 class cnode_list;
|
|
65 typedef boost::shared_ptr<cnode> cnode_t;
|
|
66 typedef boost::shared_ptr<cnode_list> cnode_list_t;
|
|
67
|
|
68 class cnode {
|
|
69 public:
|
|
70 cnode(int op, const cnode_t &left, const cnode_t &right)
|
|
71 : op_(op), left_(left), right_(right), number_(0)
|
|
72 {
|
|
73 }
|
|
74 cnode(int op, const cnode_t &left)
|
|
75 : op_(op), left_(left), number_(0)
|
|
76 {
|
|
77 }
|
|
78 cnode(int op, int number)
|
|
79 : op_(op), number_(number)
|
|
80 {
|
|
81 }
|
|
82 cnode(int op, const std::string &str)
|
|
83 : op_(op), number_(0), string_(str)
|
|
84 {
|
|
85 }
|
|
86 cnode(int op, const char *b, const char *e)
|
|
87 : op_(op), string_(b, e)
|
|
88 {
|
|
89 }
|
|
90 virtual ~cnode()
|
|
91 {
|
|
92 }
|
|
93
|
|
94 int op() const { return op_; }
|
|
95 int number() const { return number_; }
|
|
96 const std::string &string() const { return string_; }
|
|
97 cnode_t left() const { return left_; }
|
|
98 cnode_t right() const { return right_; }
|
|
99
|
|
100 virtual int push(compiler *c) const;
|
|
101 virtual int pop(compiler *c) const;
|
|
102
|
|
103 void assign(compiler *c) const;
|
|
104 int call(compiler *c, const std::string &name, const std::vector<cnode_t> *args) const;
|
|
105
|
|
106 static cnode_t make_node(int op, int number)
|
|
107 {
|
|
108 return cnode_t(new cnode(op, number));
|
|
109 }
|
|
110
|
|
111 static cnode_t make_node(int op, const std::string &str);
|
|
112 static cnode_t make_node(int op, cnode_t left);
|
|
113 static cnode_t make_node(int op, cnode_t left, cnode_t right);
|
|
114 static cnode_t make_node(int op, cnode_t left, cnode_list_t right);
|
|
115
|
|
116 protected:
|
|
117 int op_;
|
|
118 int number_;
|
|
119 std::string string_;
|
|
120 cnode_t left_;
|
|
121 cnode_t right_;
|
|
122 } ;
|
|
123
|
|
124 // 変数ノード
|
|
125
|
|
126 class cvalue_node: public cnode {
|
|
127 public:
|
|
128 cvalue_node(const std::string &name)
|
|
129 : cnode(OP_IDENTIFIER, name)
|
|
130 {
|
|
131 }
|
|
132
|
|
133 int push(compiler *c) const;
|
|
134 int pop(compiler *c) const;
|
|
135 } ;
|
|
136
|
|
137 // ノードリスト
|
|
138
|
|
139 class cnode_list {
|
|
140 public:
|
|
141 cnode_list(cnode_t node)
|
|
142 {
|
|
143 args_.push_back(node);
|
|
144 }
|
|
145
|
|
146 cnode_list *add(cnode_t add)
|
|
147 {
|
|
148 args_.push_back(add);
|
|
149 return this;
|
|
150 }
|
|
151
|
|
152 size_t size() const { return args_.size(); }
|
|
153 cnode_t get(size_t index) const { return args_[index]; }
|
|
154
|
|
155 public:
|
|
156 std::vector<cnode_t> args_;
|
|
157 } ;
|
|
158
|
|
159 // 関数のノード
|
|
160
|
|
161 class cfunction_node: public cnode {
|
|
162 public:
|
|
163 cfunction_node(int op, const cnode_t &node, const cnode_list_t &list)
|
|
164 : cnode(op, node), node_list_(list)
|
|
165 {
|
|
166 }
|
|
167
|
|
168 virtual int push(compiler *c) const;
|
|
169 virtual int pop(compiler *c) const;
|
|
170
|
|
171 private:
|
|
172 cnode_list_t node_list_;
|
|
173 } ;
|
|
174
|
|
175 // ステートメント
|
|
176
|
|
177 enum STATE_TYPE {
|
|
178 NOP_STATE,
|
|
179 ASSIGN_STATE,
|
|
180 CALL_STATE,
|
|
181 CASE_STATE,
|
|
182 DEFAULT_STATE,
|
|
183 BREAK_STATE,
|
|
184 RETURN_STATE,
|
|
185 IF_STATE,
|
|
186 FOR_STATE,
|
|
187 WHILE_STATE,
|
|
188 SWITCH_STATE,
|
|
189 BLOCK_STATE,
|
|
190 } ;
|
|
191
|
|
192 class cblock;
|
|
193 typedef boost::shared_ptr<cblock> cblock_t;
|
|
194
|
|
195 class cstatement;
|
|
196 typedef boost::shared_ptr<cstatement> cstatement_t;
|
|
197
|
|
198 // 文
|
|
199 class cstatement {
|
|
200 public:
|
|
201 virtual void add(cstatement_t statement)
|
|
202 {
|
|
203 std::cerr << "内部エラー:add(statement)が呼ばれました" << std::endl;
|
|
204 }
|
|
205
|
|
206 virtual void add(cnode_t node)
|
|
207 {
|
|
208 std::cerr << "内部エラー:add(node)が呼ばれました" << std::endl;
|
|
209 }
|
|
210
|
|
211 virtual void add(int index, cstatement_t statement)
|
|
212 {
|
|
213 std::cerr << "内部エラー:add(index, statement)が呼ばれました" << std::endl;
|
|
214 }
|
|
215
|
|
216 virtual void add(int index, cnode_t node)
|
|
217 {
|
|
218 std::cerr << "内部エラー:add(index, node)が呼ばれました" << std::endl;
|
|
219 }
|
|
220
|
|
221 virtual void analyze(compiler *c) const = 0;
|
|
222
|
|
223 virtual void case_analyze(compiler *c, int *default_label)
|
|
224 {
|
|
225 }
|
|
226
|
|
227 static cstatement_t make_statement(int state);
|
|
228 static cstatement_t make_statement(int state, cnode_t node);
|
|
229 static cstatement_t make_statement(int state, cblock_t block);
|
|
230 } ;
|
|
231
|
|
232 // nop文
|
|
233 class cnop_statement: public cstatement {
|
|
234 public:
|
|
235 void analyze(compiler *c) const;
|
|
236 } ;
|
|
237
|
|
238 // 代入文
|
|
239 class cassign_statement: public cstatement {
|
|
240 public:
|
|
241 cassign_statement(cnode_t node)
|
|
242 : node_(node)
|
|
243 {
|
|
244 }
|
|
245
|
|
246 void analyze(compiler *c) const;
|
|
247
|
|
248 private:
|
|
249 cnode_t node_;
|
|
250 } ;
|
|
251
|
|
252 // 関数呼び出し文
|
|
253 class ccall_statement: public cstatement {
|
|
254 public:
|
|
255 ccall_statement(cnode_t node)
|
|
256 : node_(node)
|
|
257 {
|
|
258 }
|
|
259
|
|
260 void analyze(compiler *c) const;
|
|
261
|
|
262 private:
|
|
263 cnode_t node_;
|
|
264 } ;
|
|
265
|
|
266 // case文
|
|
267 class ccase_statement: public cstatement {
|
|
268 public:
|
|
269 ccase_statement(cnode_t node)
|
|
270 : node_(node)
|
|
271 {
|
|
272 }
|
|
273
|
|
274 void analyze(compiler *c) const;
|
|
275 void case_analyze(compiler *c, int *default_label);
|
|
276
|
|
277 private:
|
|
278 cnode_t node_;
|
|
279 int label_;
|
|
280 } ;
|
|
281
|
|
282 // default文
|
|
283 class cdefault_statement: public cstatement {
|
|
284 public:
|
|
285 void analyze(compiler *c) const;
|
|
286 void case_analyze(compiler *c, int *default_label);
|
|
287
|
|
288 private:
|
|
289 int label_;
|
|
290 } ;
|
|
291
|
|
292 // break文
|
|
293 class cbreak_statement: public cstatement {
|
|
294 public:
|
|
295 void analyze(compiler *c) const;
|
|
296 } ;
|
|
297
|
|
298 // return文
|
|
299 class creturn_statement: public cstatement {
|
|
300 public:
|
|
301 void add(cnode_t node)
|
|
302 {
|
|
303 node_ = node;
|
|
304 }
|
|
305
|
|
306 void analyze(compiler *c) const;
|
|
307
|
|
308 private:
|
|
309 cnode_t node_;
|
|
310 } ;
|
|
311
|
|
312 // if文
|
|
313 class cif_statement: public cstatement {
|
|
314 public:
|
|
315 cif_statement()
|
|
316 {
|
|
317 }
|
|
318
|
|
319 void add(cnode_t node)
|
|
320 {
|
|
321 node_ = node;
|
|
322 }
|
|
323
|
|
324 void add(int index, cstatement_t statement)
|
|
325 {
|
|
326 statement_[index] = statement;
|
|
327 }
|
|
328
|
|
329 void analyze(compiler *c) const;
|
|
330
|
|
331 private:
|
|
332 cnode_t node_;
|
|
333 cstatement_t statement_[2];
|
|
334 } ;
|
|
335
|
|
336 // for文
|
|
337 class cfor_statement: public cstatement {
|
|
338 public:
|
|
339 void add(cstatement_t statement)
|
|
340 {
|
|
341 statement_ = statement;
|
|
342 }
|
|
343
|
|
344 void add(int index, cnode_t node)
|
|
345 {
|
|
346 node_[index] = node;
|
|
347 }
|
|
348
|
|
349 void analyze(compiler *c) const;
|
|
350
|
|
351 private:
|
|
352 cstatement_t statement_;
|
|
353 cnode_t node_[3];
|
|
354 } ;
|
|
355
|
|
356 // while文
|
|
357 class cwhile_statement: public cstatement {
|
|
358 public:
|
|
359 void add(cstatement_t statement)
|
|
360 {
|
|
361 statement_ = statement;
|
|
362 }
|
|
363
|
|
364 void add(cnode_t node)
|
|
365 {
|
|
366 node_ = node;
|
|
367 }
|
|
368
|
|
369 void analyze(compiler *c) const;
|
|
370
|
|
371 private:
|
|
372 cnode_t node_;
|
|
373 cstatement_t statement_;
|
|
374 } ;
|
|
375
|
|
376 // switch文
|
|
377 class cswitch_statement: public cstatement {
|
|
378 public:
|
|
379 cswitch_statement(cnode_t node)
|
|
380 : node_(node)
|
|
381 {
|
|
382 }
|
|
383
|
|
384 void add(cstatement_t statement)
|
|
385 {
|
|
386 statement_.push_back(statement);
|
|
387 }
|
|
388
|
|
389 void analyze(compiler *c) const;
|
|
390
|
|
391 private:
|
|
392 cnode_t node_;
|
|
393 std::vector<cstatement_t> statement_;
|
|
394 } ;
|
|
395
|
|
396 // block文
|
|
397 class cblock_statement: public cstatement {
|
|
398 public:
|
|
399 cblock_statement(cblock_t block)
|
|
400 : block_(block)
|
|
401 {
|
|
402 }
|
|
403
|
|
404 void analyze(compiler *c) const;
|
|
405
|
|
406 private:
|
|
407 cblock_t block_;
|
|
408 } ;
|
|
409
|
|
410 // 宣言
|
|
411
|
|
412 class cdeclaration: public cstatement {
|
|
413 public:
|
|
414 cdeclaration(int type)
|
|
415 :type_(type), is_func_(false)
|
|
416 {
|
|
417 }
|
|
418
|
|
419 cdeclaration(int type, const std::string &name)
|
|
420 :type_(type), name_(name), is_func_(true)
|
|
421 {
|
|
422 }
|
|
423
|
|
424 void add(cnode_t node)
|
|
425 {
|
|
426 node_.push_back(node);
|
|
427 }
|
|
428
|
|
429 void add(int type)
|
|
430 {
|
|
431 arg_.push_back(type);
|
|
432 }
|
|
433
|
|
434 void analyze(compiler *c) const;
|
|
435
|
|
436 private:
|
|
437 int type_; // 型
|
|
438 bool is_func_; // 関数か変数か
|
|
439 std::vector<cnode_t> node_; // 変数
|
|
440 std::string name_; // 関数名
|
|
441 std::vector<int> arg_; // 関数の引数
|
|
442 } ;
|
|
443
|
|
444 typedef boost::shared_ptr<cdeclaration> cdeclaration_t;
|
|
445
|
|
446 // 文ブロック
|
|
447
|
|
448 class cblock {
|
|
449 public:
|
|
450 void add(const cdeclaration_t &decl)
|
|
451 {
|
|
452 decl_.push_back(decl);
|
|
453 }
|
|
454 void add(const cstatement_t &state)
|
|
455 {
|
|
456 state_.push_back(state);
|
|
457 }
|
|
458
|
|
459 void analyze(compiler *c) const;
|
|
460
|
|
461 private:
|
|
462 std::vector<cdeclaration_t> decl_;
|
|
463 std::vector<cstatement_t> state_;
|
|
464 } ;
|
|
465
|
|
466 // 関数の引数
|
|
467
|
|
468 class cargdef {
|
|
469 public:
|
|
470 cargdef()
|
|
471 : type_(0)
|
|
472 {
|
|
473 }
|
|
474
|
|
475 cargdef(int type)
|
|
476 : type_(type)
|
|
477 {
|
|
478 }
|
|
479
|
|
480 void set_ref()
|
|
481 {
|
|
482 type_ |= TYPE_REF;
|
|
483 }
|
|
484
|
|
485 void set_name(const std::string &name)
|
|
486 {
|
|
487 name_ = name;
|
|
488 }
|
|
489
|
|
490 int type() const { return type_; }
|
|
491 const std::string &name() const { return name_; }
|
|
492
|
|
493 private:
|
|
494 int type_;
|
|
495 std::string name_;
|
|
496 } ;
|
|
497
|
|
498 // 関数
|
|
499
|
|
500 class cfunction {
|
|
501 public:
|
|
502 cfunction(int type, const std::string &name)
|
|
503 :type_(type), name_(name)
|
|
504 {
|
|
505 }
|
|
506
|
|
507 void add(cargdef arg)
|
|
508 {
|
|
509 args_.push_back(arg);
|
|
510 }
|
|
511
|
|
512 void add(cblock_t block)
|
|
513 {
|
|
514 block_ = block;
|
|
515 }
|
|
516
|
|
517 void analyze(compiler *c) const;
|
|
518
|
|
519 private:
|
|
520 int type_;
|
|
521 std::string name_;
|
|
522 std::vector<cargdef> args_;
|
|
523 cblock_t block_;
|
|
524 } ;
|
|
525
|
|
526 typedef boost::shared_ptr<cfunction> cfunction_t;
|
|
527
|
|
528 #endif
|