as_variablescope.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2010 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. [email protected]
  22. */
  23. //
  24. // as_variablescope.cpp
  25. //
  26. // A manager class for variable declarations
  27. //
  28. #include "as_config.h"
  29. #include "as_variablescope.h"
  30. BEGIN_AS_NAMESPACE
  31. asCVariableScope::asCVariableScope(asCVariableScope *parent)
  32. {
  33. this->parent = parent;
  34. Reset();
  35. }
  36. asCVariableScope::~asCVariableScope()
  37. {
  38. Reset();
  39. }
  40. void asCVariableScope::Reset()
  41. {
  42. isBreakScope = false;
  43. isContinueScope = false;
  44. for( asUINT n = 0; n < variables.GetLength(); n++ )
  45. if( variables[n] )
  46. {
  47. asDELETE(variables[n],sVariable);
  48. }
  49. variables.SetLength(0);
  50. }
  51. int asCVariableScope::DeclareVariable(const char *name, const asCDataType &type, int stackOffset, bool onHeap)
  52. {
  53. // TODO: optimize: Improve linear search
  54. // See if the variable is already declared
  55. if( strcmp(name, "") != 0 )
  56. {
  57. for( asUINT n = 0; n < variables.GetLength(); n++ )
  58. {
  59. if( variables[n]->name == name )
  60. return -1;
  61. }
  62. }
  63. sVariable *var = asNEW(sVariable);
  64. var->name = name;
  65. var->type = type;
  66. var->stackOffset = stackOffset;
  67. var->isInitialized = false;
  68. var->isPureConstant = false;
  69. var->onHeap = onHeap;
  70. // Parameters are initialized
  71. if( stackOffset <= 0 )
  72. var->isInitialized = true;
  73. variables.PushLast(var);
  74. return 0;
  75. }
  76. sVariable *asCVariableScope::GetVariable(const char *name)
  77. {
  78. // TODO: optimize: Improve linear search
  79. // Find the variable
  80. for( asUINT n = 0; n < variables.GetLength(); n++ )
  81. {
  82. if( variables[n]->name == name )
  83. return variables[n];
  84. }
  85. if( parent )
  86. return parent->GetVariable(name);
  87. return 0;
  88. }
  89. sVariable *asCVariableScope::GetVariableByOffset(int offset)
  90. {
  91. // TODO: optimize: Improve linear search
  92. // Find the variable
  93. for( asUINT n = 0; n < variables.GetLength(); n++ )
  94. {
  95. if( variables[n]->stackOffset == offset )
  96. return variables[n];
  97. }
  98. if( parent )
  99. return parent->GetVariableByOffset(offset);
  100. return 0;
  101. }
  102. END_AS_NAMESPACE