compiledEval.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "console/console.h"
  24. #include "console/ast.h"
  25. #include "core/tAlgorithm.h"
  26. #include "core/strings/findMatch.h"
  27. #include "core/strings/stringUnit.h"
  28. #include "console/consoleInternal.h"
  29. #include "core/stream/fileStream.h"
  30. #include "console/compiler.h"
  31. #include "console/simBase.h"
  32. #include "console/telnetDebugger.h"
  33. #include "sim/netStringTable.h"
  34. #include "console/ICallMethod.h"
  35. #include "console/stringStack.h"
  36. #include "util/messaging/message.h"
  37. #include "core/frameAllocator.h"
  38. #include "console/codeInterpreter.h"
  39. #ifndef TORQUE_TGB_ONLY
  40. #include "materials/materialDefinition.h"
  41. #include "materials/materialManager.h"
  42. #endif
  43. using namespace Compiler;
  44. namespace Con
  45. {
  46. // Current script file name and root, these are registered as
  47. // console variables.
  48. extern StringTableEntry gCurrentFile;
  49. extern StringTableEntry gCurrentRoot;
  50. extern S32 gObjectCopyFailures;
  51. }
  52. namespace Con
  53. {
  54. const char *getNamespaceList(Namespace *ns)
  55. {
  56. U32 size = 1;
  57. Namespace * walk;
  58. for (walk = ns; walk; walk = walk->mParent)
  59. size += dStrlen(walk->mName) + 4;
  60. char *ret = Con::getReturnBuffer(size);
  61. ret[0] = 0;
  62. for (walk = ns; walk; walk = walk->mParent)
  63. {
  64. dStrcat(ret, walk->mName, size);
  65. if (walk->mParent)
  66. dStrcat(ret, " -> ", size);
  67. }
  68. return ret;
  69. }
  70. }
  71. //------------------------------------------------------------
  72. F64 consoleStringToNumber(const char *str, StringTableEntry file, U32 line)
  73. {
  74. F64 val = dAtof(str);
  75. if (val != 0)
  76. return val;
  77. else if (!dStricmp(str, "true"))
  78. return 1;
  79. else if (!dStricmp(str, "false"))
  80. return 0;
  81. else if (file)
  82. {
  83. Con::warnf(ConsoleLogEntry::General, "%s (%d): string always evaluates to 0.", file, line);
  84. return 0;
  85. }
  86. return 0;
  87. }
  88. //------------------------------------------------------------
  89. namespace Con
  90. {
  91. char *getReturnBuffer(U32 bufferSize)
  92. {
  93. return STR.getReturnBuffer(bufferSize);
  94. }
  95. char *getReturnBuffer(const char *stringToCopy)
  96. {
  97. U32 len = dStrlen(stringToCopy) + 1;
  98. char *ret = STR.getReturnBuffer(len);
  99. dMemcpy(ret, stringToCopy, len);
  100. return ret;
  101. }
  102. char* getReturnBuffer(const String& str)
  103. {
  104. const U32 size = str.size();
  105. char* ret = STR.getReturnBuffer(size);
  106. dMemcpy(ret, str.c_str(), size);
  107. return ret;
  108. }
  109. char* getReturnBuffer(const StringBuilder& str)
  110. {
  111. char* buffer = Con::getReturnBuffer(str.length() + 1);
  112. str.copy(buffer);
  113. buffer[str.length()] = '\0';
  114. return buffer;
  115. }
  116. char *getArgBuffer(U32 bufferSize)
  117. {
  118. return STR.getArgBuffer(bufferSize);
  119. }
  120. char *getFloatArg(F64 arg)
  121. {
  122. char *ret = STR.getArgBuffer(32);
  123. dSprintf(ret, 32, "%g", arg);
  124. return ret;
  125. }
  126. char *getIntArg(S32 arg)
  127. {
  128. char *ret = STR.getArgBuffer(32);
  129. dSprintf(ret, 32, "%d", arg);
  130. return ret;
  131. }
  132. char* getBoolArg(bool arg)
  133. {
  134. char *ret = STR.getArgBuffer(32);
  135. dSprintf(ret, 32, "%d", arg);
  136. return ret;
  137. }
  138. char *getStringArg(const char *arg)
  139. {
  140. U32 len = dStrlen(arg) + 1;
  141. char *ret = STR.getArgBuffer(len);
  142. dMemcpy(ret, arg, len);
  143. return ret;
  144. }
  145. char* getStringArg(const String& arg)
  146. {
  147. const U32 size = arg.size();
  148. char* ret = STR.getArgBuffer(size);
  149. dMemcpy(ret, arg.c_str(), size);
  150. return ret;
  151. }
  152. }
  153. //------------------------------------------------------------
  154. void ExprEvalState::setCurVarName(StringTableEntry name)
  155. {
  156. if (name[0] == '$')
  157. currentVariable = globalVars.lookup(name);
  158. else if (getStackDepth() > 0)
  159. currentVariable = getCurrentFrame().lookup(name);
  160. if (!currentVariable && gWarnUndefinedScriptVariables)
  161. Con::warnf(ConsoleLogEntry::Script, "Variable referenced before assignment: %s", name);
  162. }
  163. void ExprEvalState::setCurVarNameCreate(StringTableEntry name)
  164. {
  165. if (name[0] == '$')
  166. currentVariable = globalVars.add(name);
  167. else if (getStackDepth() > 0)
  168. currentVariable = getCurrentFrame().add(name);
  169. else
  170. {
  171. currentVariable = NULL;
  172. Con::warnf(ConsoleLogEntry::Script, "Accessing local variable in global scope... failed: %s", name);
  173. }
  174. }
  175. //------------------------------------------------------------
  176. S32 ExprEvalState::getIntVariable()
  177. {
  178. return currentVariable ? currentVariable->getIntValue() : 0;
  179. }
  180. F64 ExprEvalState::getFloatVariable()
  181. {
  182. return currentVariable ? currentVariable->getFloatValue() : 0;
  183. }
  184. const char *ExprEvalState::getStringVariable()
  185. {
  186. return currentVariable ? currentVariable->getStringValue() : "";
  187. }
  188. //------------------------------------------------------------
  189. void ExprEvalState::setIntVariable(S32 val)
  190. {
  191. AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!");
  192. currentVariable->setIntValue(val);
  193. }
  194. void ExprEvalState::setFloatVariable(F64 val)
  195. {
  196. AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!");
  197. currentVariable->setFloatValue(val);
  198. }
  199. void ExprEvalState::setStringVariable(const char *val)
  200. {
  201. AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!");
  202. currentVariable->setStringValue(val);
  203. }
  204. void ExprEvalState::setStringStackPtrVariable(StringStackPtr str)
  205. {
  206. AssertFatal(currentVariable != NULL, "Invalid evaluator state - trying to set null variable!");
  207. currentVariable->setStringStackPtrValue(str);
  208. }
  209. void ExprEvalState::setCopyVariable()
  210. {
  211. if (copyVariable)
  212. {
  213. switch (copyVariable->value.type)
  214. {
  215. case ConsoleValue::TypeInternalInt:
  216. currentVariable->setIntValue(copyVariable->getIntValue());
  217. break;
  218. case ConsoleValue::TypeInternalFloat:
  219. currentVariable->setFloatValue(copyVariable->getFloatValue());
  220. break;
  221. default:
  222. currentVariable->setStringValue(copyVariable->getStringValue());
  223. break;
  224. }
  225. }
  226. }
  227. //------------------------------------------------------------
  228. ConsoleValueRef CodeBlock::exec(U32 ip, const char *functionName, Namespace *thisNamespace, U32 argc, ConsoleValueRef *argv, bool noCalls, StringTableEntry packageName, S32 setFrame)
  229. {
  230. CodeInterpreter interpreter(this);
  231. return interpreter.exec(ip, functionName, thisNamespace, argc, argv, noCalls, packageName, setFrame);
  232. }
  233. //------------------------------------------------------------