0
|
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 // ノード
|
|
90
|
|
91 class CNode {
|
|
92 public:
|
|
93 CNode(const yy::location& l, int op, CNode *left, CNode *right=0)
|
|
94 : l_(l), op_(op), left_(left), right_(right), value_(0), string_(0)
|
|
95 {
|
|
96 }
|
|
97 CNode(const yy::location& l, int op, int value)
|
|
98 : l_(l), op_(op), left_(0), right_(0), value_(value), string_(0)
|
|
99 {
|
|
100 }
|
|
101 CNode(const yy::location& l, int op, std::string *str)
|
|
102 : l_(l), op_(op), left_(0), right_(0), value_(0), string_(str)
|
|
103 {
|
|
104 }
|
|
105 CNode(const yy::location& l, int op, std::string *str, CNode *node)
|
|
106 : l_(l), op_(op), left_(node), right_(0), value_(0), string_(str)
|
|
107 {
|
|
108 }
|
|
109 virtual ~CNode()
|
|
110 {
|
|
111 delete left_;
|
|
112 delete right_;
|
|
113 delete string_;
|
|
114 }
|
|
115
|
|
116 virtual void push(compiler *c) const;
|
|
117 virtual void pop(compiler *c) const;
|
|
118
|
|
119 const yy::location &location() const { return l_; }
|
|
120 int op() const { return op_; }
|
|
121 int value() const { return value_; }
|
|
122 const std::string &string() const { return *string_; }
|
|
123 const CNode *left() const { return left_; }
|
|
124 const CNode *right() const { return right_; }
|
|
125
|
|
126 static CNode *MakeNode(compiler &c, const yy::location& l, int op, CNode *left, CNode *right=0);
|
|
127
|
|
128 protected:
|
|
129 const yy::location l_;
|
|
130 int op_;
|
|
131 int value_;
|
|
132 std::string *string_;
|
|
133 CNode *left_;
|
|
134 CNode *right_;
|
|
135 } ;
|
|
136
|
|
137 // 変数ノード
|
|
138
|
|
139 class CValueNode: public CNode {
|
|
140 public:
|
|
141 CValueNode(const yy::location& l, std::string *name, CNode *node=NULL)
|
|
142 : CNode(l, OP_VALUE, name, node)
|
|
143 {
|
|
144 }
|
|
145
|
|
146 void push(compiler *c) const;
|
|
147 void pop(compiler *c) const;
|
|
148 } ;
|
|
149
|
|
150 // 代入文用
|
|
151
|
|
152 class CAssign {
|
|
153 public:
|
|
154 CAssign(const yy::location& l, int op, CNode *value, CNode *expr)
|
|
155 : l_(l), op_(op), value_(value), expr_(expr)
|
|
156 {
|
|
157 }
|
|
158 ~CAssign()
|
|
159 {
|
|
160 delete value_;
|
|
161 delete expr_;
|
|
162 }
|
|
163
|
|
164 void analyze(compiler *c);
|
|
165 void push_value(compiler *c)
|
|
166 {
|
|
167 value_->push(c);
|
|
168 }
|
|
169 void pop_value(compiler *c)
|
|
170 {
|
|
171 value_->pop(c);
|
|
172 }
|
|
173
|
|
174 private:
|
|
175 const yy::location l_;
|
|
176 int op_;
|
|
177 CNode *value_;
|
|
178 CNode *expr_;
|
|
179 } ;
|
|
180
|
|
181 #endif
|