-
Notifications
You must be signed in to change notification settings - Fork 0
/
intepreter.cpp
82 lines (69 loc) · 2.5 KB
/
intepreter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#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;
};
std::vector<FunctionDetails> InterpretFunctionDetails(const std::string& input) {
std::vector<FunctionDetails> functions;
// 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;
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;
}
return functions;
}
void ExecuteFunction(const FunctionDetails& details) {
if (details.name == "MyFunction") {
// Dummy parameters for the example
int a = 5, b = 3;
int result = a + b;
std::cout << "Result: " << result << std::endl;
}
// Add more function/sub execution logic here
}
int main() {
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);
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);
}
std::cout << "Press Enter to continue...";
std::cin.ignore();
return 0;
}