| 287 | // ═══════════════════════════════════════════════════════════════ |
| 288 | |
| 289 | bool PythonEngine::interpretLine(const char* line) { |
| 290 | // Skip leading whitespace |
| 291 | while (*line && isspace((unsigned char)*line)) line++; |
| 292 | if (!*line || line[0] == '#') return true; // Empty line or comment |
| 293 | |
| 294 | // import statement (just acknowledge) |
| 295 | if (strncmp(line, "import ", 7) == 0) return true; |
| 296 | if (strncmp(line, "from ", 5) == 0) return true; |
| 297 | |
| 298 | // print(...) |
| 299 | if (strncmp(line, "print(", 6) == 0) { |
| 300 | int len = strlen(line); |
| 301 | if (line[len - 1] != ')') { |
| 302 | appendErr("SyntaxError: missing closing parenthesis\n"); |
| 303 | return false; |
| 304 | } |
| 305 | // Extract content inside print(...) |
| 306 | char content[256]; |
| 307 | int clen = len - 7; // minus "print(" and ")" |
| 308 | if (clen < 0) clen = 0; |
| 309 | if (clen >= (int)sizeof(content)) clen = sizeof(content) - 1; |
| 310 | memcpy(content, line + 6, clen); |
| 311 | content[clen] = '\0'; |
| 312 | |
| 313 | // Check if it's a string literal |
| 314 | int cl = strlen(content); |
| 315 | if (cl >= 2 && ((content[0] == '"' && content[cl - 1] == '"') || |
| 316 | (content[0] == '\'' && content[cl - 1] == '\''))) { |
| 317 | char str[256]; |
| 318 | memcpy(str, content + 1, cl - 2); |
| 319 | str[cl - 2] = '\0'; |
| 320 | appendOut(str); |
| 321 | appendOut("\n"); |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | // Try format string: f"..." or f'...' |
| 326 | if (cl >= 3 && content[0] == 'f' && |
| 327 | ((content[1] == '"' && content[cl - 1] == '"') || |
| 328 | (content[1] == '\'' && content[cl - 1] == '\''))) { |
| 329 | // Simple f-string: just output the template |
| 330 | char str[256]; |
| 331 | memcpy(str, content + 2, cl - 3); |
| 332 | str[cl - 3] = '\0'; |
| 333 | // Replace {var} with variable values |
| 334 | char output[256]; |
| 335 | int oi = 0; |
| 336 | for (int i = 0; str[i] && oi < 254; ++i) { |
| 337 | if (str[i] == '{') { |
| 338 | int j = i + 1; |
| 339 | while (str[j] && str[j] != '}') j++; |
| 340 | if (str[j] == '}') { |
| 341 | char varName[64]; |
| 342 | int vl = j - i - 1; |
| 343 | if (vl >= (int)sizeof(varName)) vl = sizeof(varName) - 1; |
| 344 | memcpy(varName, str + i + 1, vl); |
| 345 | varName[vl] = '\0'; |
| 346 | double val; |
nothing calls this directly
no test coverage detected