MCPcopy Create free account
hub / github.com/El-EnderJ/NeoCalculator / execute

Method execute

src/apps/PythonEngine.cpp:450–577  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

448// ═══════════════════════════════════════════════════════════════
449
450bool PythonEngine::execute(const char* code) {
451 if (!_initialized) {
452 appendErr("RuntimeError: Engine not initialized\n");
453 return false;
454 }
455
456 clearOutput();
457 resetVars();
458
459 if (!code || !*code) return true;
460
461 // Split into lines and interpret
462 // Handle for loops with simple range()
463 char lineBuf[512];
464 const char* p = code;
465 bool inForLoop = false;
466 char forVar[32] = {};
467 int forStart = 0, forEnd = 0, forStep = 1;
468 char forBody[1024] = {};
469 int forBodyLen = 0;
470 int indentLevel = 0;
471
472 while (*p) {
473 // Read one line
474 int i = 0;
475 while (*p && *p != '\n' && i < (int)sizeof(lineBuf) - 1) {
476 lineBuf[i++] = *p++;
477 }
478 lineBuf[i] = '\0';
479 if (*p == '\n') p++;
480
481 // Count leading spaces
482 int indent = 0;
483 const char* lp = lineBuf;
484 while (*lp == ' ') { indent++; lp++; }
485
486 if (inForLoop) {
487 if (indent > indentLevel || (*lp && indent == indentLevel && (*lp == ' ' || *lp == '\t'))) {
488 // Part of the for loop body
489 if (forBodyLen + i + 2 < (int)sizeof(forBody)) {
490 if (forBodyLen > 0) forBody[forBodyLen++] = '\n';
491 strcpy(forBody + forBodyLen, lp);
492 forBodyLen += strlen(lp);
493 }
494 continue;
495 } else {
496 // End of for loop — execute it
497 for (int idx = forStart; idx < forEnd; idx += forStep) {
498 setVar(forVar, (double)idx);
499 // Execute body lines
500 char bodyLine[512];
501 const char* bp = forBody;
502 while (*bp) {
503 int bi = 0;
504 while (*bp && *bp != '\n' && bi < (int)sizeof(bodyLine) - 1) {
505 bodyLine[bi++] = *bp++;
506 }
507 bodyLine[bi] = '\0';

Callers 1

runScriptMethod · 0.80

Calls 2

resetVarsFunction · 0.85
setVarFunction · 0.85

Tested by

no test coverage detected