Mercurial > hg > Members > nobuyasu > myCompiler
comparison Bison-Flex/Compiler-StackBase/UTF8/compiler.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 __COMPILER_H__ | 6 #ifndef __COMPILER_H__ |
7 #define __COMPILER_H__ | 7 #define __COMPILER_H__ |
8 | 8 |
9 #include "script-parser.hh" | 9 #include "script-parser.hh" |
10 #include "vm.h" | 10 #include "vm.h" |
11 #include "node.h" | 11 #include "node.h" |
12 | 12 |
13 // 前方宣言 | 13 // 劫h |
14 class compiler; | 14 class compiler; |
15 | 15 |
16 // flexの関数宣言 | 16 // flex∽医h |
17 #define YY_DECL \ | 17 #define YY_DECL \ |
18 yy::script_parser::token_type \ | 18 yy::script_parser::token_type \ |
19 yylex(yy::script_parser::semantic_type* yylval, \ | 19 yylex(yy::script_parser::semantic_type* yylval, \ |
20 yy::script_parser::location_type* yylloc, \ | 20 yy::script_parser::location_type* yylloc, \ |
21 compiler& driver) | 21 compiler& driver) |
22 | 22 |
23 YY_DECL; | 23 YY_DECL; |
24 | 24 |
25 // 仮想マシンコード生成用 | 25 // 篁潟激潟潟若 |
26 | 26 |
27 class CVMCode { | 27 class CVMCode { |
28 public: | 28 public: |
29 CVMCode(unsigned char op) // 引数なし命令 | 29 CVMCode(unsigned char op) // 綣違巡擦 |
30 : size_(1), op_(op), arg1_(0) | 30 : size_(1), op_(op), arg1_(0) |
31 { | 31 { |
32 } | 32 } |
33 CVMCode(unsigned char op, int arg1) // 引数1つの命令 | 33 CVMCode(unsigned char op, int arg1) // 綣逸ゃ巡擦 |
34 : size_(5), op_(op), arg1_(arg1) | 34 : size_(5), op_(op), arg1_(arg1) |
35 { | 35 { |
36 } | 36 } |
37 | 37 |
38 unsigned char *Get(unsigned char *p) const | 38 unsigned char *Get(unsigned char *p) const |
39 { | 39 { |
40 if (op_ != VM_MAXCOMMAND) { // ラベルのダミーコマンド | 40 if (op_ != VM_MAXCOMMAND) { // 若潟潟 |
41 *p++ = op_; | 41 *p++ = op_; |
42 if (size_ > 1) { | 42 if (size_ > 1) { |
43 *(int *)p = arg1_; | 43 *(int *)p = arg1_; |
44 p += 4; | 44 p += 4; |
45 } | 45 } |
51 unsigned char size_; | 51 unsigned char size_; |
52 unsigned char op_; | 52 unsigned char op_; |
53 int arg1_; | 53 int arg1_; |
54 } ; | 54 } ; |
55 | 55 |
56 // ラベル | 56 // |
57 | 57 |
58 class CLabel { | 58 class CLabel { |
59 public: | 59 public: |
60 CLabel(int index) | 60 CLabel(int index) |
61 : index_(index), pos_(0) | 61 : index_(index), pos_(0) |
68 public: | 68 public: |
69 int index_; | 69 int index_; |
70 int pos_; | 70 int pos_; |
71 } ; | 71 } ; |
72 | 72 |
73 // 変数テーブル | 73 // 紊違若 |
74 | 74 |
75 class CValueTag { | 75 class CValueTag { |
76 public: | 76 public: |
77 CValueTag(): addr_(-1), type_(TYPE_INTEGER), size_(1), global_(false) | 77 CValueTag(): addr_(-1), type_(TYPE_INTEGER), size_(1), global_(false) |
78 { | 78 { |
87 int type_; | 87 int type_; |
88 int size_; | 88 int size_; |
89 bool global_; | 89 bool global_; |
90 } ; | 90 } ; |
91 | 91 |
92 // 変数テーブル | 92 // 紊違若 |
93 | 93 |
94 class CValueTable { | 94 class CValueTable { |
95 private: | 95 private: |
96 typedef std::map<std::string, CValueTag>::iterator iter; | 96 typedef std::map<std::string, CValueTag>::iterator iter; |
97 typedef std::map<std::string, CValueTag>::const_iterator const_iter; | 97 typedef std::map<std::string, CValueTag>::const_iterator const_iter; |
159 std::map<std::string, CValueTag> variables_; | 159 std::map<std::string, CValueTag> variables_; |
160 int addr_; | 160 int addr_; |
161 bool global_; | 161 bool global_; |
162 } ; | 162 } ; |
163 | 163 |
164 // 関数定義用 | 164 // ∽医臂 |
165 | 165 |
166 class CFunctionTag { | 166 class CFunctionTag { |
167 private: | 167 private: |
168 enum { | 168 enum { |
169 flag_declaration = 1 << 0, | 169 flag_declaration = 1 << 0, |
216 return true; | 216 return true; |
217 } | 217 } |
218 | 218 |
219 bool ChkArgList(const CArgList *args) const | 219 bool ChkArgList(const CArgList *args) const |
220 { | 220 { |
221 // 引数が無い場合 | 221 // 綣違<翫 |
222 if (args == 0) | 222 if (args == 0) |
223 return args_.empty(); | 223 return args_.empty(); |
224 | 224 |
225 // 引数の個数が異なる | 225 // 綣違違違 |
226 if (args_.size() != args->size()) | 226 if (args_.size() != args->size()) |
227 return false; | 227 return false; |
228 | 228 |
229 // 全引数の型をチェック | 229 // 綣違с |
230 size_t size = args_.size(); | 230 size_t size = args_.size(); |
231 for (size_t i=0; i < size; i++) { | 231 for (size_t i=0; i < size; i++) { |
232 if (args->get(i)->type() != (int)args_[i]) | 232 if (args->get(i)->type() != (int)args_[i]) |
233 return false; | 233 return false; |
234 } | 234 } |
235 return true; | 235 return true; |
236 } | 236 } |
237 | 237 |
238 // 指定の引数の型を得る | 238 // 絎綣違緇 |
239 | 239 |
240 int GetArg(int index) const | 240 int GetArg(int index) const |
241 { | 241 { |
242 return args_[index]; | 242 return args_[index]; |
243 } | 243 } |
244 | 244 |
245 int ArgSize() const { return args_.size(); } | 245 int ArgSize() const { return args_.size(); } |
246 | 246 |
247 void SetIndex(int index) { index_ = index; } | 247 void SetIndex(int index) { index_ = index; } |
248 void SetDeclaration() { flags_ |= flag_declaration; } // 宣言 | 248 void SetDeclaration() { flags_ |= flag_declaration; } // 絎h |
249 void SetDefinition() { flags_ |= flag_definition; } // 定義 | 249 void SetDefinition() { flags_ |= flag_definition; } // 絎臂 |
250 void SetSystem() { flags_ |= flag_system; } | 250 void SetSystem() { flags_ |= flag_system; } |
251 | 251 |
252 int GetIndex() const { return index_; } | 252 int GetIndex() const { return index_; } |
253 bool IsDeclaration() const { return (flags_ & flag_declaration) != 0; } | 253 bool IsDeclaration() const { return (flags_ & flag_declaration) != 0; } |
254 bool IsDefinition() const { return (flags_ & flag_definition) != 0; } | 254 bool IsDefinition() const { return (flags_ & flag_definition) != 0; } |
259 int flags_; | 259 int flags_; |
260 int index_; | 260 int index_; |
261 std::vector<char> args_; | 261 std::vector<char> args_; |
262 } ; | 262 } ; |
263 | 263 |
264 // 関数テーブル | 264 // ∽違若 |
265 | 265 |
266 class CFunctionTable { | 266 class CFunctionTable { |
267 private: | 267 private: |
268 typedef std::map<std::string, CFunctionTag>::iterator iter; | 268 typedef std::map<std::string, CFunctionTag>::iterator iter; |
269 typedef std::map<std::string, CFunctionTag>::const_iterator const_iter; | 269 typedef std::map<std::string, CFunctionTag>::const_iterator const_iter; |
304 | 304 |
305 private: | 305 private: |
306 std::map<std::string, CFunctionTag> functions_; | 306 std::map<std::string, CFunctionTag> functions_; |
307 } ; | 307 } ; |
308 | 308 |
309 // コンパイラ | 309 // 潟潟ゃ |
310 | 310 |
311 class compiler { | 311 class compiler { |
312 public: | 312 public: |
313 compiler(); | 313 compiler(); |
314 virtual ~compiler(); | 314 virtual ~compiler(); |
322 bool add_function(int index, int type, const char *name, const char *args); | 322 bool add_function(int index, int type, const char *name, const char *args); |
323 | 323 |
324 void DefineValue(const yy::location& l, int type, CValueList *value_list); | 324 void DefineValue(const yy::location& l, int type, CValueList *value_list); |
325 void DefineFunction(const yy::location& l, int type, const std::string *name, CArgList *args); | 325 void DefineFunction(const yy::location& l, int type, const std::string *name, CArgList *args); |
326 void AddFunction(const yy::location& l, int type, const std::string *name, CArgList *args, CStateBlock *states); | 326 void AddFunction(const yy::location& l, int type, const std::string *name, CArgList *args, CStateBlock *states); |
327 | 327 void AddGotoFunction(const yy::location& l, int type, const std::string *name, CArgList *args, CStateBlock *states); |
328 // 変数の検索、内側のブロックから検索する。 | 328 |
329 // 紊違罎膣≪眼罎膣≪ | |
329 const CValueTag *GetValueTag(const std::string &name) const | 330 const CValueTag *GetValueTag(const std::string &name) const |
330 { | 331 { |
331 int size = (int)variables.size(); | 332 int size = (int)variables.size(); |
332 for (int i=size-1; i>=0; i--) { | 333 for (int i=size-1; i>=0; i--) { |
333 const CValueTag *tag = variables[i].find(name); | 334 const CValueTag *tag = variables[i].find(name); |
335 return tag; | 336 return tag; |
336 } | 337 } |
337 return NULL; | 338 return NULL; |
338 } | 339 } |
339 | 340 |
340 // 関数の検索 | 341 // ∽違罎膣 |
341 const CFunctionTag *GetFunctionTag(const std::string &name) const | 342 const CFunctionTag *GetFunctionTag(const std::string &name) const |
342 { | 343 { |
343 return functions.find(name); | 344 return functions.find(name); |
344 } | 345 } |
345 | 346 |