@@ -9,26 +9,73 @@ class CPPCodeProcessingUnit:
9
9
comments = r'\/\/.*?(?=\r?\n)|\/\*[\s\S]*?\*\/'
10
10
main_method = r'\b(?:[a-zA-Z_]\w*(?:\s*\*?\s*)?\s+)+main\s*\('
11
11
function_define = r'^[\w\s:<>&*,]+\s+\**\s*\w+\s*\(.*\)\s*{'
12
-
12
+
13
13
def _prep_main_function (self , code ):
14
14
lines = code .splitlines ()
15
- defs_lines = []
16
- main_body_lines = []
15
+ defs_lines = [] # Collect lines that belong to function definitions.
16
+ main_body_lines = [] # Collect lines that are not part of any function.
17
17
in_function = False
18
18
brace_count = 0
19
- func_def_regex = code_compile (self .function_define )
20
- for line in lines :
21
- if not in_function and func_def_regex .search (line ):
22
- in_function = True
23
- brace_count = line .count ('{' ) - line .count ('}' )
24
- defs_lines .append (line )
25
- elif in_function :
19
+
20
+ # Regex for a complete function definition with the opening brace on the same line.
21
+ func_def_regex = code_compile (
22
+ r'^\s*(?:(?:'
23
+ r'inline|virtual|static|constexpr|_Noreturn'
24
+ r')\s+)*(?!\b(?:'
25
+ r'break|case|continue|default|do|else|for|goto|if|return|switch|while|else if'
26
+ r')\b)(.*?)\s+(\w+)\s*\([^)]*\)\s*{'
27
+ )
28
+
29
+ # Regex for a potential function header without the opening brace.
30
+ func_header_regex = code_compile (
31
+ r'^\s*(?:(?:'
32
+ r'inline|virtual|static|constexpr|_Noreturn'
33
+ r')\s+)*(?!\b(?:'
34
+ r'break|case|continue|default|do|else|for|goto|if|return|switch|while|else if'
35
+ r')\b)(.*?)\s+(\w+)\s*\([^)]*\)\s*$'
36
+ )
37
+
38
+ header_buffer = [] # Buffer to accumulate lines for multi-line function headers.
39
+ i = 0
40
+ while i < len (lines ):
41
+ line = lines [i ]
42
+
43
+ if not in_function :
44
+ if func_def_regex .search (line ):
45
+ in_function = True
46
+ brace_count = line .count ('{' ) - line .count ('}' )
47
+ defs_lines .append (line )
48
+ elif func_header_regex .search (line ):
49
+ header_buffer = [line ]
50
+ j = i + 1
51
+ found_brace = False
52
+ while j < len (lines ):
53
+ next_line = lines [j ]
54
+ header_buffer .append (next_line )
55
+ if '{' in next_line :
56
+ found_brace = True
57
+ in_function = True
58
+ brace_count = next_line .count ('{' ) - next_line .count ('}' )
59
+ break
60
+ if next_line .strip () == "" :
61
+ break
62
+ j += 1
63
+ if found_brace :
64
+ defs_lines .extend (header_buffer )
65
+ i = j
66
+ header_buffer = []
67
+ else :
68
+ main_body_lines .extend (header_buffer )
69
+ header_buffer = []
70
+ else :
71
+ main_body_lines .append (line )
72
+ else :
26
73
defs_lines .append (line )
27
74
brace_count += line .count ('{' ) - line .count ('}' )
28
75
if brace_count <= 0 :
29
76
in_function = False
30
- else :
31
- main_body_lines . append ( line )
77
+ i += 1
78
+
32
79
defs = "\n " .join (defs_lines ).strip ()
33
80
main_body = "\n " .join (main_body_lines ).strip ()
34
81
if main_body :
@@ -37,11 +84,11 @@ def _prep_main_function(self, code):
37
84
wrapped_main = ""
38
85
code = (defs + "\n \n " if defs else "" ) + wrapped_main
39
86
return code
40
-
87
+
41
88
def _add_code_compat (self , code , cpp_res_path ):
42
89
code = code_sub (self .comments , r'' , code )
43
90
if not code_search (self .main_method , code ):
44
- code = self ._prep_main_function (self , code )
91
+ code = self ._prep_main_function (code )
45
92
46
93
code = f"#include { cpp_res_path } \n { code } "
47
94
code = CPPExternalHeader ._support_external_header (CPPExternalHeader , code )
0 commit comments