Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from __future__ import annotations
from typing import TYPE_CHECKING, Sequence, Optional
if TYPE_CHECKING:
from ..context import SfgContext
from jinja2.filters import do_indent
from pystencils.typing import TypedSymbol
from .basic_nodes import SfgCallTreeNode, SfgCallTreeLeaf
class SfgCondition(SfgCallTreeLeaf):
pass
class SfgCustomCondition(SfgCondition):
def __init__(self, cond_text: str):
self._cond_text = cond_text
def required_symbols(self) -> set(TypedSymbol):
return set()
def defined_symbols(self) -> set(TypedSymbol):
return set()
def get_code(self, ctx: SfgContext) -> str:
return self._cond_text
# class IntEven(SfgCondition):
# def __init__(self, )
class SfgBranch(SfgCallTreeNode):
def __init__(self, cond: SfgCondition, branch_true: SfgCallTreeNode, branch_false: Optional[SfgCallTreeNode] = None):
self._cond = cond
self._branch_true = branch_true
self._branch_false = branch_false
@property
def children(self) -> Sequence[SfgCallTreeNode]:
if self._branch_false is not None:
return (self._branch_true, self._branch_false)
else:
return (self._branch_true,)
def get_code(self, ctx: SfgContext) -> str:
code = f"if({self._cond.get_code(ctx)}) {{\n"
code += ctx.codestyle.indent(self._branch_true.get_code(ctx))
code += "\n}"
if self._branch_false is not None:
code += "else {\n"
code += ctx.codestyle.indent(self._branch_false.get_code(ctx))
code += "\n}"
return code