| 589 | std::shared_ptr<const QiCustomFunc> definition; |
| 590 | { |
| 591 | std::lock_guard<std::mutex> lock(customFunctionsMutex); |
| 592 | auto customIt = customFunctions.find(token.value); |
| 593 | if (customIt != customFunctions.end()) definition = customIt->second; |
| 594 | } |
| 595 | if (definition) |
| 596 | { |
| 597 | const QiCustomFunc& func = *definition; |
| 598 | if (stack.size() >= func.parameters.size()) |
| 599 | { |
| 600 | QiVarMap funcLocal; |
| 601 | for (int i = func.parameters.size() - 1; i >= 0; i--) { |
| 602 | funcLocal[func.parameters[i]] = stack.back(); |
| 603 | stack.pop_back(); |
| 604 | } |
| 605 | |
| 606 | try |
| 607 | { |
| 608 | executeStatementList(func.statementList, &funcLocal); |
| 609 | stack.push_back(QiVar(0)); |
| 610 | } |
| 611 | catch (const ReturnException& e) { stack.push_back(e.returnValue); } |
| 612 | } |
| 613 | else throw ScriptException(ScriptException::error_invalid_functions_parameter, {}, token.value); |
| 614 | } |
| 615 | else |
| 616 | { |
| 617 | throw ScriptException(ScriptException::error_unknown_functions, {}, token.value); |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | if (stack.size() != 1) |
| 623 | { |
| 624 | std::string exc; for (const auto& token : postfix) exc += token.value; |
| 625 | throw ScriptException(ScriptException::error_invalid_expression, {}, exc); |
| 626 | } |
| 627 | return stack.back(); |
| 628 | } |
| 629 | |
| 630 | std::pair<StatementList, size_t> parseTopLevel(const std::vector<std::string>& lines, size_t start_index) { |
| 631 | StatementList block; |
| 632 | size_t index = start_index; |
| 633 | while (index < lines.size()) |
| 634 | { |
| 635 | auto [stmt, next_index] = parseStatement(lines, index); |
| 636 | block.push_back(stmt); |
| 637 | index = next_index; |
| 638 | } |
| 639 | return { block, index }; |
| 640 | } |
| 641 | std::pair<Statement, size_t> parseStatement(const std::vector<std::string>& lines, size_t start_index) { |
| 642 | if (start_index >= lines.size()) return { Node(""), start_index }; |
| 643 | |
| 644 | std::string line = lines[start_index]; |
| 645 | |
| 646 | if (line.substr(0, 2) == "fn") |
| 647 | { |
| 648 | size_t end_pos = line.find('(', 2); |