as_variablescope.cpp 3.1 KB

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