gd_compiler.h 6.3 KB

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