as_scriptnode.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2015 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. andreas@angelcode.com
  22. */
  23. //
  24. // as_scriptnode.cpp
  25. //
  26. // A node in the script tree built by the parser for compilation
  27. //
  28. #include "as_scriptnode.h"
  29. #include "as_scriptengine.h"
  30. BEGIN_AS_NAMESPACE
  31. asCScriptNode::asCScriptNode(eScriptNode type)
  32. {
  33. nodeType = type;
  34. tokenType = ttUnrecognizedToken;
  35. tokenPos = 0;
  36. tokenLength = 0;
  37. parent = 0;
  38. next = 0;
  39. prev = 0;
  40. firstChild = 0;
  41. lastChild = 0;
  42. }
  43. void asCScriptNode::Destroy(asCScriptEngine *engine)
  44. {
  45. // Destroy all children
  46. asCScriptNode *node = firstChild;
  47. asCScriptNode *nxt;
  48. while( node )
  49. {
  50. nxt = node->next;
  51. node->Destroy(engine);
  52. node = nxt;
  53. }
  54. // Return the memory to the memory manager
  55. engine->memoryMgr.FreeScriptNode(this);
  56. }
  57. asCScriptNode *asCScriptNode::CreateCopy(asCScriptEngine *engine)
  58. {
  59. void *ptr = engine->memoryMgr.AllocScriptNode();
  60. if( ptr == 0 )
  61. {
  62. // Out of memory
  63. return 0;
  64. }
  65. new(ptr) asCScriptNode(nodeType);
  66. asCScriptNode *node = reinterpret_cast<asCScriptNode*>(ptr);
  67. node->tokenLength = tokenLength;
  68. node->tokenPos = tokenPos;
  69. node->tokenType = tokenType;
  70. asCScriptNode *child = firstChild;
  71. while( child )
  72. {
  73. node->AddChildLast(child->CreateCopy(engine));
  74. child = child->next;
  75. }
  76. return node;
  77. }
  78. void asCScriptNode::SetToken(sToken *token)
  79. {
  80. tokenType = token->type;
  81. }
  82. void asCScriptNode::UpdateSourcePos(size_t pos, size_t length)
  83. {
  84. if( pos == 0 && length == 0 ) return;
  85. if( tokenPos == 0 && tokenLength == 0 )
  86. {
  87. tokenPos = pos;
  88. tokenLength = length;
  89. }
  90. else
  91. {
  92. if( tokenPos > pos )
  93. {
  94. tokenLength = tokenPos + tokenLength - pos;
  95. tokenPos = pos;
  96. }
  97. if( pos + length > tokenPos + tokenLength )
  98. {
  99. tokenLength = pos + length - tokenPos;
  100. }
  101. }
  102. }
  103. void asCScriptNode::AddChildLast(asCScriptNode *node)
  104. {
  105. // We might get a null pointer if the parser encounter an out-of-memory situation
  106. if( node == 0 ) return;
  107. if( lastChild )
  108. {
  109. lastChild->next = node;
  110. node->next = 0;
  111. node->prev = lastChild;
  112. node->parent = this;
  113. lastChild = node;
  114. }
  115. else
  116. {
  117. firstChild = node;
  118. lastChild = node;
  119. node->next = 0;
  120. node->prev = 0;
  121. node->parent = this;
  122. }
  123. UpdateSourcePos(node->tokenPos, node->tokenLength);
  124. }
  125. void asCScriptNode::DisconnectParent()
  126. {
  127. if( parent )
  128. {
  129. if( parent->firstChild == this )
  130. parent->firstChild = next;
  131. if( parent->lastChild == this )
  132. parent->lastChild = prev;
  133. }
  134. if( next )
  135. next->prev = prev;
  136. if( prev )
  137. prev->next = next;
  138. parent = 0;
  139. next = 0;
  140. prev = 0;
  141. }
  142. END_AS_NAMESPACE