forked from vbhaip/CodeBlocks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conditionstruc.py
69 lines (56 loc) · 1.65 KB
/
conditionstruc.py
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
58
59
60
61
62
63
64
65
66
67
68
69
from conditionblock import ConditionBlock
class ConditionStructure:
def __init__(self, cond, if_block, else_block):
self.cond = cond
self.if_block = if_block
self.else_block = else_block
def isTrue(self):
return self.cond()
def evaluate(self):
if (self.isTrue()):
for block in self.if_block:
block.evaluate()
elif self.else_block:
# else block if it exists
for block in self.else_block:
block.evaluate()
def display(self, indents):
indents += 1
print('%sif: %s' % (' ' * indents, self.cond))
for struct in self.if_block:
if type(struct) == type(self):
struct.display(indents + 1)
else:
print('%s%s' % (' ' * (indents + 1), str(struct)))
if self.else_block:
print('%selse:' % ' ' * (indents))
for struct in self.else_block:
if type(struct) == type(self):
struct.display(indents + 1)
else:
print('%s%s' % (' ' * (indents + 1), str(struct)))
def __str__(self):
self.display(0)
return ''
class ConditionStructureLoop:
def __init__(self, loops, loop_block):
self.loops = loops
self.loop_block = loop_block
def evaluate(self):
for i in range(self.loops):
for block in self.loop_block:
block.evaluate()
def display(self, indents):
print('%sfor: %i' % (' ' * indents, self.loops))
for struct in self.loop_block:
if type(struct) == type(self):
struct.display(indents + 1)
else:
print('%s%s' % (' ' * (indents + 1), str(struct)))
def __str__(self):
self.display(0)
return ''
# block1 = ConditionBlock("IF", "something", "action")
# block2 = ConditionBlock("ELSE", None, "action2")
# structure = ConditionStructure(block1, block2)
# structure.if_block.runAction()