Skip to content

Commit 2764df8

Browse files
update for main handle
Co-Authored-By: Vo Luu Tuong Anh <[email protected]>
1 parent 284afd7 commit 2764df8

File tree

2 files changed

+63
-18
lines changed

2 files changed

+63
-18
lines changed

jcppkernel/__main__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,8 @@ def do_execute(
123123
self, code, silent, store_history=True, user_expressions=None, allow_stdin=True
124124
):
125125
cpp_res_path = f'"{self.resDir}/gcpph.hpp"'
126-
code = CPPCodeProcessingUnit._add_code_compat(
127-
CPPCodeProcessingUnit, code, cpp_res_path
128-
)
129-
126+
code = CPPCodeProcessingUnit()._add_code_compat(code, cpp_res_path)
127+
130128
with CPPTempFileProcessing._new_temp_file(
131129
CPPTempFileProcessing, self.files, suffix=".cpp"
132130
) as source_file, CPPTempFileProcessing._new_temp_file(

jcppkernel/code_processing.py

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,73 @@ class CPPCodeProcessingUnit:
99
comments = r'\/\/.*?(?=\r?\n)|\/\*[\s\S]*?\*\/'
1010
main_method = r'\b(?:[a-zA-Z_]\w*(?:\s*\*?\s*)?\s+)+main\s*\('
1111
function_define = r'^[\w\s:<>&*,]+\s+\**\s*\w+\s*\(.*\)\s*{'
12-
12+
1313
def _prep_main_function(self, code):
1414
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.
1717
in_function = False
1818
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:
2673
defs_lines.append(line)
2774
brace_count += line.count('{') - line.count('}')
2875
if brace_count <= 0:
2976
in_function = False
30-
else:
31-
main_body_lines.append(line)
77+
i += 1
78+
3279
defs = "\n".join(defs_lines).strip()
3380
main_body = "\n".join(main_body_lines).strip()
3481
if main_body:
@@ -37,11 +84,11 @@ def _prep_main_function(self, code):
3784
wrapped_main = ""
3885
code = (defs + "\n\n" if defs else "") + wrapped_main
3986
return code
40-
87+
4188
def _add_code_compat(self, code, cpp_res_path):
4289
code = code_sub(self.comments, r'', code)
4390
if not code_search(self.main_method, code):
44-
code = self._prep_main_function(self, code)
91+
code = self._prep_main_function(code)
4592

4693
code = f"#include {cpp_res_path}\n{code}"
4794
code = CPPExternalHeader._support_external_header(CPPExternalHeader, code)

0 commit comments

Comments
 (0)