Skip to content

Commit

Permalink
Update intepreter.cpp
Browse files Browse the repository at this point in the history
Sub support
  • Loading branch information
gaouservbf authored Oct 14, 2024
1 parent da30182 commit 593aed2
Showing 1 changed file with 47 additions and 24 deletions.
71 changes: 47 additions & 24 deletions intepreter.cpp
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
#include <string>
#include <iostream>
#include <regex>
#include <vector>

struct FunctionDetails {
std::string type;
std::string name;
std::string parameters;
std::string code;
bool isPublic;
std::string returnType;
};

FunctionDetails InterpretFunctionDetails(const std::string& input) {
FunctionDetails details;
std::vector<FunctionDetails> InterpretFunctionDetails(const std::string& input) {
std::vector<FunctionDetails> functions;

// Regex patterns to extract function details
std::regex functionPattern(R"((public|private)?\s*Function\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(([^)]*)\)\s+As\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*End\s+Function)");
// Regex patterns to extract function and sub details
std::regex functionPattern(R"((Public|Private)?\s*(Function|Sub)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*\(([^)]*)\)\s*(As\s+([a-zA-Z_][a-zA-Z0-9_]*))?\s*(.+?)\s*End\s+\2)");
std::smatch matches;

if (std::regex_search(input, matches, functionPattern)) {
details.isPublic = matches[1] == "public";
details.name = matches[2];
details.parameters = matches[3];
details.returnType = matches[4];
std::string::const_iterator searchStart(input.cbegin());
while (std::regex_search(searchStart, input.cend(), matches, functionPattern)) {
FunctionDetails details;
details.isPublic = matches[1] == "Public";
details.type = matches[2];
details.name = matches[3];
details.parameters = matches[4];
if (details.type == "Function") {
details.returnType = matches[6];
}
details.code = matches[7];
functions.push_back(details);
searchStart = matches.suffix().first;
}

// Extracting code (simplified for example purposes)
std::regex codePattern(R"(Function\s+[a-zA-Z_][a-zA-Z0-9_]*\s*\([^)]*\)\s+As\s+[a-zA-Z_][a-zA-Z0-9_]*\s*(.+?)\s*End\s+Function)");
if (std::regex_search(input, matches, codePattern)) {
details.code = matches[1];
}

return details;
return functions;
}

void ExecuteFunction(const FunctionDetails& details) {
Expand All @@ -40,20 +44,39 @@ void ExecuteFunction(const FunctionDetails& details) {
int result = a + b;
std::cout << "Result: " << result << std::endl;
}
// Add more function/sub execution logic here
}

int main() {
std::string vbFunction = "public Function MyFunction(a As Integer, b As Integer) As Integer Function MyFunction = a + b End Function";
FunctionDetails details = InterpretFunctionDetails(vbFunction);
std::string vbFunctions = R"(
Public Function MyFunction(a As Integer, b As Integer) As Integer
MyFunction = a + b
End Function
Private Sub MySub()
' Some sub code here
End Sub
)";

std::vector<FunctionDetails> functions = InterpretFunctionDetails(vbFunctions);

std::cout << "Name: " << details.name << std::endl;
std::cout << "Parameters: " << details.parameters << std::endl;
std::cout << "Code: " << details.code << std::endl;
std::cout << "Public: " << (details.isPublic ? "Yes" : "No") << std::endl;
std::cout << "Return Type: " << details.returnType << std::endl;
for (const auto& details : functions) {
std::cout << "Type: " << details.type << std::endl;
std::cout << "Name: " << details.name << std::endl;
std::cout << "Parameters: " << details.parameters << std::endl;
std::cout << "Code: " << details.code << std::endl;
std::cout << "Public: " << (details.isPublic ? "Yes" : "No") << std::endl;
if (details.type == "Function") {
std::cout << "Return Type: " << details.returnType << std::endl;
}
std::cout << std::endl;

// Execute the function
ExecuteFunction(details);
}

ExecuteFunction(details);
std::cout << "Press Enter to continue...";
std::cin.ignore();

return 0;
}

0 comments on commit 593aed2

Please sign in to comment.