gd_compiler.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*************************************************************************/
  2. /* gd_compiler.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef GD_COMPILER_H
  30. #define GD_COMPILER_H
  31. #include "gd_parser.h"
  32. #include "gd_script.h"
  33. class GDCompiler {
  34. const GDParser *parser;
  35. struct CodeGen {
  36. GDScript *script;
  37. const GDParser::ClassNode *class_node;
  38. const GDParser::FunctionNode *function_node;
  39. bool debug_stack;
  40. List< Map<StringName,int> > stack_id_stack;
  41. Map<StringName,int> stack_identifiers;
  42. List<GDFunction::StackDebug> stack_debug;
  43. List< Map<StringName,int> > block_identifier_stack;
  44. Map<StringName,int> block_identifiers;
  45. void add_stack_identifier(const StringName& p_id,int p_stackpos) {
  46. stack_identifiers[p_id]=p_stackpos;
  47. if (debug_stack) {
  48. block_identifiers[p_id]=p_stackpos;
  49. GDFunction::StackDebug sd;
  50. sd.added=true;
  51. sd.line=current_line;
  52. sd.identifier=p_id;
  53. sd.pos=p_stackpos;
  54. stack_debug.push_back(sd);
  55. }
  56. }
  57. void push_stack_identifiers() {
  58. stack_id_stack.push_back( stack_identifiers );
  59. if (debug_stack) {
  60. block_identifier_stack.push_back(block_identifiers);
  61. block_identifiers.clear();
  62. }
  63. }
  64. void pop_stack_identifiers() {
  65. stack_identifiers = stack_id_stack.back()->get();
  66. stack_id_stack.pop_back();
  67. if (debug_stack) {
  68. for (Map<StringName,int>::Element *E=block_identifiers.front();E;E=E->next()) {
  69. GDFunction::StackDebug sd;
  70. sd.added=false;
  71. sd.identifier=E->key();
  72. sd.line=current_line;
  73. sd.pos=E->get();
  74. stack_debug.push_back(sd);
  75. }
  76. block_identifiers=block_identifier_stack.back()->get();
  77. block_identifier_stack.pop_back();
  78. }
  79. }
  80. // int get_identifier_pos(const StringName& p_dentifier) const;
  81. HashMap<Variant,int,VariantHasher> constant_map;
  82. Map<StringName,int> name_map;
  83. int get_name_map_pos(const StringName& p_identifier) {
  84. int ret;
  85. if (!name_map.has(p_identifier)) {
  86. ret=name_map.size();
  87. name_map[p_identifier]=ret;
  88. } else {
  89. ret=name_map[p_identifier];
  90. }
  91. return ret;
  92. }
  93. int get_constant_pos(const Variant& p_constant) {
  94. if (constant_map.has(p_constant))
  95. return constant_map[p_constant];
  96. int pos = constant_map.size();
  97. constant_map[p_constant]=pos;
  98. return pos;
  99. }
  100. Vector<int> opcodes;
  101. void alloc_stack(int p_level) { if (p_level >= stack_max) stack_max=p_level+1; }
  102. void alloc_call(int p_params) { if (p_params >= call_max) call_max=p_params; }
  103. int current_line;
  104. int stack_max;
  105. int call_max;
  106. };
  107. #if 0
  108. void _create_index(const GDParser::OperatorNode *on);
  109. void _create_call(const GDParser::OperatorNode *on);
  110. int _parse_expression(const GDParser::Node *p_expr,CodeGen& codegen);
  111. void _parse_block(GDParser::BlockNode *p_block);
  112. void _parse_function(GDParser::FunctionNode *p_func);
  113. Ref<GDScript> _parse_class(GDParser::ClassNode *p_class);
  114. #endif
  115. void _set_error(const String& p_error,const GDParser::Node *p_node);
  116. bool _create_unary_operator(CodeGen& codegen,const GDParser::OperatorNode *on,Variant::Operator op, int p_stack_level);
  117. bool _create_binary_operator(CodeGen& codegen,const GDParser::OperatorNode *on,Variant::Operator op, int p_stack_level);
  118. //int _parse_subexpression(CodeGen& codegen,const GDParser::BlockNode *p_block,const GDParser::Node *p_expression);
  119. int _parse_assign_right_expression(CodeGen& codegen,const GDParser::OperatorNode *p_expression, int p_stack_level);
  120. int _parse_expression(CodeGen& codegen,const GDParser::Node *p_expression, int p_stack_level,bool p_root=false);
  121. Error _parse_block(CodeGen& codegen,const GDParser::BlockNode *p_block,int p_stack_level=0,int p_break_addr=-1,int p_continue_addr=-1);
  122. Error _parse_function(GDScript *p_script,const GDParser::ClassNode *p_class,const GDParser::FunctionNode *p_func);
  123. Error _parse_class(GDScript *p_script,GDScript *p_owner,const GDParser::ClassNode *p_class);
  124. int err_line;
  125. int err_column;
  126. StringName source;
  127. String error;
  128. public:
  129. Error compile(const GDParser *p_parser,GDScript *p_script);
  130. String get_error() const;
  131. int get_error_line() const;
  132. int get_error_column() const;
  133. GDCompiler();
  134. };
  135. #endif // COMPILER_H