@@ -62,21 +62,21 @@ def convert_type(self, value: Expression) -> str:
6262 return self .convert_declaration (value .type , value )
6363
6464 def combine_types (self , expressions : list [Expression ]) -> str :
65- content_types = set (self .convert_type (content ) for content in expressions )
66- if len (content_types ) == 1 :
67- return content_types .pop ()
68- elif len (content_types ) > 1 :
69- return f"std::variant<{ ', ' .join (content_types )} >"
70- else :
71- return "std::any"
65+ content_types = set (self .convert_type (content ) for content in expressions )
66+ if len (content_types ) == 1 :
67+ return content_types .pop ()
68+ elif len (content_types ) > 1 :
69+ return f"std::variant<{ ', ' .join (content_types )} >"
70+ else :
71+ return "std::any"
7272
7373 def sequence_subtype (self , value : Statement ) -> str :
7474 if isinstance (value , SequenceType ):
7575 return self .combine_types (value .data )
7676 else :
7777 return "std::any"
7878
79- def convert_value (self , value : Value , add_type = False ) -> str :
79+ def convert_value (self , value : Value , add_type = False ) -> str :
8080 basic = as_basic_type (value )
8181 if add_type :
8282 tp = value .type
@@ -87,12 +87,14 @@ def convert_value(self, value: Value, add_type = False) -> str:
8787 if bt == BasicNothingTypes .NOTHING :
8888 return value_string
8989
90- if (tp == AdvancedNumericTypes .DOUBLE_EXTENDED
90+ if (
91+ tp == AdvancedNumericTypes .DOUBLE_EXTENDED
9192 or tp == AdvancedNumericTypes .DOUBLE_PRECISION
9293 or tp == AdvancedNumericTypes .SINGLE_PRECISION
9394 or tp == AdvancedStringTypes .CHAR
9495 or bt == BasicBooleanTypes .BOOLEAN
95- or bt == BasicNumericTypes .REAL ):
96+ or bt == BasicNumericTypes .REAL
97+ ):
9698 return f"(({ type_string } ) { value_string } )"
9799
98100 return f"{ type_string } ({ value_string } )"
@@ -116,7 +118,8 @@ def convert_value(self, value: Value, add_type = False) -> str:
116118 + "}"
117119 )
118120 elif (
119- basic .type == BasicSequenceTypes .SEQUENCE or basic .type == BasicSequenceTypes .SET
121+ basic .type == BasicSequenceTypes .SEQUENCE
122+ or basic .type == BasicSequenceTypes .SET
120123 ):
121124 return (
122125 "{"
@@ -195,7 +198,9 @@ def convert_declaration(
195198 return f"std::vector<{ self .sequence_subtype (value )} >"
196199 elif tp == AdvancedSequenceTypes .TUPLE :
197200 if isinstance (value , SequenceType ):
198- subtype_string = ", " .join (self .convert_type (content ) for content in value .data )
201+ subtype_string = ", " .join (
202+ self .convert_type (content ) for content in value .data
203+ )
199204 return f"std::tuple<{ subtype_string } >"
200205 else :
201206 return "std::tuple<std::any>"
@@ -226,7 +231,7 @@ def convert_declaration(
226231 return "void"
227232 raise AssertionError (f"Unknown type: { tp !r} " )
228233
229- def convert_statement (self , statement : Statement , add_value_type = False ) -> str :
234+ def convert_statement (self , statement : Statement , add_value_type = False ) -> str :
230235 if isinstance (statement , PropertyAssignment ):
231236 return (
232237 f"{ self .convert_statement (statement .property )} = "
@@ -248,7 +253,11 @@ def convert_statement(self, statement: Statement, add_value_type = False) -> str
248253 raise AssertionError (f"Unknown statement: { statement !r} " )
249254
250255 def is_pascal_case (self , identifier : Identifier ) -> bool :
251- return len (identifier ) > 0 and identifier [0 ].isupper () and not identifier [1 :].isupper ()
256+ return (
257+ len (identifier ) > 0
258+ and identifier [0 ].isupper ()
259+ and not identifier [1 :].isupper ()
260+ )
252261
253262 def convert_function_call (self , function : FunctionCall ) -> str :
254263 result = function .name
@@ -273,13 +282,17 @@ def convert_function_call(self, function: FunctionCall) -> str:
273282 else :
274283 result = f"{ function .namespace } .{ result } "
275284 else :
276- result = "(" + self .convert_statement (function .namespace ) + ")." + result
285+ result = (
286+ "(" + self .convert_statement (function .namespace ) + ")." + result
287+ )
277288 return result
278289
279290 def spacing (self , depth ):
280291 return " " * depth * 4
281292
282- def convert_testcase (self , tc : PreparedTestcase , pu : PreparedExecutionUnit , depth = 1 ) -> str :
293+ def convert_testcase (
294+ self , tc : PreparedTestcase , pu : PreparedExecutionUnit , depth = 1
295+ ) -> str :
283296 indent = self .spacing (depth )
284297 result = ""
285298 if tc .testcase .is_main_testcase ():
@@ -320,17 +333,23 @@ def generate_internal_context(
320333 # Generate code for each testcase
321334 tc : PreparedTestcase
322335 for i , tc in enumerate (ctx .testcases ):
323- result += self .spacing (i + 1 ) + "try {" + "\n "
324- result += self .spacing (i + 2 ) + f"{ pu .unit .name } _write_separator();\n "
325- result += self .convert_testcase (tc , pu , i + 2 )
326-
327- for i in range (len (ctx .testcases ), 0 , - 1 ):
328- result += self .spacing (i ) + "} catch (...) {\n "
329- result += self .spacing (i + 1 ) + "const std::exception_ptr &e = std::current_exception();\n "
330- result += self .spacing (i + 1 ) + self .convert_statement (
331- tc .exception_statement ("e" )) + ";\n "
332- result += self .spacing (i + 1 )+ "exit_code = 1;\n "
333- result += self .spacing (i ) + "}\n "
336+ result += self .spacing (i + 1 ) + "try {" + "\n "
337+ result += self .spacing (i + 2 ) + f"{ pu .unit .name } _write_separator();\n "
338+ result += self .convert_testcase (tc , pu , i + 2 )
339+
340+ for i in range (len (ctx .testcases ) - 1 , - 1 , - 1 ):
341+ result += self .spacing (i + 1 ) + "} catch (...) {\n "
342+ result += (
343+ self .spacing (i + 2 )
344+ + "const std::exception_ptr &e = std::current_exception();\n "
345+ )
346+ result += (
347+ self .spacing (i + 2 )
348+ + self .convert_statement (ctx .testcases [i ].exception_statement ("e" ))
349+ + ";\n "
350+ )
351+ result += self .spacing (i + 2 ) + "exit_code = 1;\n "
352+ result += self .spacing (i + 1 ) + "}\n "
334353
335354 result += self .spacing (1 ) + ctx .after + "\n "
336355 result += self .spacing (1 ) + "return exit_code;\n "
@@ -460,7 +479,10 @@ def convert_encoder(self, values: list[Value]) -> str:
460479int main() {
461480"""
462481 for value in values :
463- result += self .spacing (1 ) + f"write_value(std::cout, { self .convert_value (value , True )} );\n "
482+ result += (
483+ self .spacing (1 )
484+ + f"write_value(std::cout, { self .convert_value (value , True )} );\n "
485+ )
464486 result += self .spacing (1 ) + 'std::cout << "␞";\n '
465487 result += "}\n "
466488 return result
0 commit comments