Skip to content

Commit 56fe505

Browse files
Update code_processing.py
Co-Authored-By: Vo Luu Tuong Anh <[email protected]>
1 parent 2764df8 commit 56fe505

File tree

1 file changed

+3
-75
lines changed

1 file changed

+3
-75
lines changed

jcppkernel/code_processing.py

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,84 +5,12 @@
55
class CPPCodeProcessingUnit:
66

77
main_head = 'int main() {'
8-
main_foot = 'return 0;}'
8+
main_foot = '\treturn 0;\n}'
99
comments = r'\/\/.*?(?=\r?\n)|\/\*[\s\S]*?\*\/'
10-
main_method = r'\b(?:[a-zA-Z_]\w*(?:\s*\*?\s*)?\s+)+main\s*\('
11-
function_define = r'^[\w\s:<>&*,]+\s+\**\s*\w+\s*\(.*\)\s*{'
10+
main_method = r'\bmain\s*\(|^\s*#\s*define\s+\w+\s+main\b'
1211

1312
def _prep_main_function(self, code):
14-
lines = code.splitlines()
15-
defs_lines = [] # Collect lines that belong to function definitions.
16-
main_body_lines = [] # Collect lines that are not part of any function.
17-
in_function = False
18-
brace_count = 0
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:
73-
defs_lines.append(line)
74-
brace_count += line.count('{') - line.count('}')
75-
if brace_count <= 0:
76-
in_function = False
77-
i += 1
78-
79-
defs = "\n".join(defs_lines).strip()
80-
main_body = "\n".join(main_body_lines).strip()
81-
if main_body:
82-
wrapped_main = f"{self.main_head}\n{main_body}\n{self.main_foot}"
83-
else:
84-
wrapped_main = ""
85-
code = (defs + "\n\n" if defs else "") + wrapped_main
13+
code = f"{self.main_head}\n{code}\n{self.main_foot}"
8614
return code
8715

8816
def _add_code_compat(self, code, cpp_res_path):

0 commit comments

Comments
 (0)