|
17 | 17 | Dict,
|
18 | 18 | Match,
|
19 | 19 | Optional,
|
| 20 | + Sequence, |
20 | 21 | Union,
|
21 | 22 | )
|
22 | 23 |
|
@@ -200,33 +201,39 @@ def guardend(name: str) -> str:
|
200 | 201 | name=c_fname(name).upper())
|
201 | 202 |
|
202 | 203 |
|
203 |
| -def cgen_ifcond(ifcond: Union[str, Dict[str, Any]]) -> str: |
| 204 | +def gen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]], |
| 205 | + cond_fmt: str, not_fmt: str, |
| 206 | + all_operator: str, any_operator: str) -> str: |
| 207 | + |
| 208 | + def do_gen(ifcond: Union[str, Dict[str, Any]], need_parens: bool): |
| 209 | + if isinstance(ifcond, str): |
| 210 | + return cond_fmt % ifcond |
| 211 | + assert isinstance(ifcond, dict) and len(ifcond) == 1 |
| 212 | + if 'not' in ifcond: |
| 213 | + return not_fmt % do_gen(ifcond['not'], True) |
| 214 | + if 'all' in ifcond: |
| 215 | + gen = gen_infix(all_operator, ifcond['all']) |
| 216 | + else: |
| 217 | + gen = gen_infix(any_operator, ifcond['any']) |
| 218 | + if need_parens: |
| 219 | + gen = '(' + gen + ')' |
| 220 | + return gen |
| 221 | + |
| 222 | + def gen_infix(operator: str, operands: Sequence[Any]) -> str: |
| 223 | + return operator.join([do_gen(o, True) for o in operands]) |
| 224 | + |
204 | 225 | if not ifcond:
|
205 | 226 | return ''
|
206 |
| - if isinstance(ifcond, str): |
207 |
| - return 'defined(' + ifcond + ')' |
| 227 | + return do_gen(ifcond, False) |
208 | 228 |
|
209 |
| - oper, operands = next(iter(ifcond.items())) |
210 |
| - if oper == 'not': |
211 |
| - return '!' + cgen_ifcond(operands) |
212 |
| - oper = {'all': '&&', 'any': '||'}[oper] |
213 |
| - operands = [cgen_ifcond(o) for o in operands] |
214 |
| - return '(' + (') ' + oper + ' (').join(operands) + ')' |
215 | 229 |
|
| 230 | +def cgen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]]) -> str: |
| 231 | + return gen_ifcond(ifcond, 'defined(%s)', '!%s', ' && ', ' || ') |
216 | 232 |
|
217 |
| -def docgen_ifcond(ifcond: Union[str, Dict[str, Any]]) -> str: |
| 233 | + |
| 234 | +def docgen_ifcond(ifcond: Optional[Union[str, Dict[str, Any]]]) -> str: |
218 | 235 | # TODO Doc generated for conditions needs polish
|
219 |
| - if not ifcond: |
220 |
| - return '' |
221 |
| - if isinstance(ifcond, str): |
222 |
| - return ifcond |
223 |
| - |
224 |
| - oper, operands = next(iter(ifcond.items())) |
225 |
| - if oper == 'not': |
226 |
| - return '!' + docgen_ifcond(operands) |
227 |
| - oper = {'all': ' and ', 'any': ' or '}[oper] |
228 |
| - operands = [docgen_ifcond(o) for o in operands] |
229 |
| - return '(' + oper.join(operands) + ')' |
| 236 | + return gen_ifcond(ifcond, '%s', 'not %s', ' and ', ' or ') |
230 | 237 |
|
231 | 238 |
|
232 | 239 | def gen_if(cond: str) -> str:
|
|
0 commit comments