229 |
delete m_varPerfTimer; |
delete m_varPerfTimer; |
230 |
} |
} |
231 |
|
|
232 |
VMParserContext* ScriptVM::loadScript(const String& s) { |
VMParserContext* ScriptVM::loadScript(std::istream* is, |
233 |
std::istringstream iss(s); |
const std::map<String,String>& patchVars, |
234 |
return loadScript(&iss); |
std::map<String,String>* patchVarsDef) |
235 |
|
{ |
236 |
|
std::string s(std::istreambuf_iterator<char>(*is),{}); |
237 |
|
return loadScript(s, patchVars, patchVarsDef); |
238 |
} |
} |
239 |
|
|
240 |
VMParserContext* ScriptVM::loadScript(std::istream* is) { |
VMParserContext* ScriptVM::loadScript(const String& s, |
241 |
|
const std::map<String,String>& patchVars, |
242 |
|
std::map<String,String>* patchVarsDef) |
243 |
|
{ |
244 |
|
ParserContext* context = (ParserContext*) loadScriptOnePass(s); |
245 |
|
if (!context->vErrors.empty()) |
246 |
|
return context; |
247 |
|
|
248 |
|
if (!context->patchVars.empty() && (!patchVars.empty() || patchVarsDef)) { |
249 |
|
String s2 = s; |
250 |
|
|
251 |
|
typedef std::pair<String,PatchVarBlock> Var; |
252 |
|
std::map<int,Var> varsByPos; |
253 |
|
for (const Var& var : context->patchVars) { |
254 |
|
const String& name = var.first; |
255 |
|
const PatchVarBlock& block = var.second; |
256 |
|
const int pos = |
257 |
|
(block.exprBlock) ? |
258 |
|
block.exprBlock->firstByte : |
259 |
|
block.nameBlock.firstByte + block.nameBlock.lengthBytes; |
260 |
|
varsByPos[pos] = var; |
261 |
|
if (patchVarsDef) { |
262 |
|
(*patchVarsDef)[name] = |
263 |
|
(block.exprBlock) ? |
264 |
|
s.substr(pos, block.exprBlock->lengthBytes) : ""; |
265 |
|
} |
266 |
|
} |
267 |
|
|
268 |
|
if (patchVars.empty()) |
269 |
|
return context; |
270 |
|
|
271 |
|
for (std::map<int,Var>::reverse_iterator it = varsByPos.rbegin(); |
272 |
|
it != varsByPos.rend(); ++it) |
273 |
|
{ |
274 |
|
const String name = it->second.first; |
275 |
|
if (patchVars.find(name) != patchVars.end()) { |
276 |
|
const int pos = it->first; |
277 |
|
const PatchVarBlock& block = it->second.second; |
278 |
|
const int length = |
279 |
|
(block.exprBlock) ? block.exprBlock->lengthBytes : 0; |
280 |
|
String value; |
281 |
|
if (!length) |
282 |
|
value += " := "; |
283 |
|
value += patchVars.find(name)->second; |
284 |
|
s2.replace(pos, length, value); |
285 |
|
} |
286 |
|
} |
287 |
|
|
288 |
|
if (s2 != s) { |
289 |
|
delete context; |
290 |
|
context = (ParserContext*) loadScriptOnePass(s2); |
291 |
|
} |
292 |
|
} |
293 |
|
|
294 |
|
return context; |
295 |
|
} |
296 |
|
|
297 |
|
VMParserContext* ScriptVM::loadScriptOnePass(const String& s) { |
298 |
ParserContext* context = new ParserContext(this); |
ParserContext* context = new ParserContext(this); |
299 |
//printf("parserCtx=0x%lx\n", (uint64_t)context); |
//printf("parserCtx=0x%lx\n", (uint64_t)context); |
300 |
|
std::istringstream iss(s); |
301 |
|
|
302 |
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
303 |
context->registerBuiltInConstRealVariables( builtInConstRealVariables() ); |
context->registerBuiltInConstRealVariables( builtInConstRealVariables() ); |
305 |
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
306 |
context->registerBuiltInDynVariables( builtInDynamicVariables() ); |
context->registerBuiltInDynVariables( builtInDynamicVariables() ); |
307 |
|
|
308 |
context->createScanner(is); |
context->createScanner(&iss); |
309 |
|
|
310 |
InstrScript_parse(context); |
InstrScript_parse(context); |
311 |
dmsg(2,("Allocating %" PRId64 " bytes of global int VM memory.\n", int64_t(context->globalIntVarCount * sizeof(vmint)))); |
dmsg(2,("Allocating %" PRId64 " bytes of global int VM memory.\n", int64_t(context->globalIntVarCount * sizeof(vmint)))); |