-
Notifications
You must be signed in to change notification settings - Fork 1
/
Imp.py
126 lines (96 loc) · 2.98 KB
/
Imp.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from colorama import init, Fore, Back, Style
from Exprs import *
init(autoreset=True)
class Command:
pass
class Skip(Command):
def __eq__(self,other):
match other:
case Skip():
return True
case _:
return False
class Assgn(Command):
def __init__(self,v,e):
self.__vname = v
self.__expr = e
def name(self):
return self.__vname
def value(self):
return self.__expr
def __eq__(self,other):
match other:
case Assgn():
if self.__vname == other.name() and self.__expr == other.value():
return True
else:
return False
case _:
return False
def __str__(self):
return str(self.__vname) + " "+ Fore.GREEN + ":= " + Style.RESET_ALL + str(self.__expr) + " ;"
class Seq(Command):
def __init__(self,cl,cr):
self.__cl = cl
self.__cr = cr
def left(self):
return self.__cl
def right(self):
return self.__cr
def __eq__(self,other):
match other:
case Seq():
return ((self.__cl == other.left()) and
(self.__cr == other.right()))
case _:
return False
def __str__(self):
return (str(self.__cl) + str(self.__cr))
class IfThen(Command):
def __init__(self,b,ct,cf):
self.__cond = b
self.__ct = ct
self.__cf = cf
def cond(self):
return self.__cond
def left(self):
return self.__ct
def right(self):
return self.__cf
def __eq__(self,other):
match other:
case IfThen():
return ((self.__cond == other.cond()) and
(self.__ct == other.left()) and
(self.__cf == other.right()))
case _:
return False
def __str__(self):
b = Fore.GREEN + "If" + "(" + str(self.__cond) + ") "
ls = Fore.GREEN + "then" + " { " + str(self.__ct) + " }"
rs = Fore.GREEN + "else" + " { " + str(self.__ct) + " }"
return (b + ls + rs)
class While(Command):
def __init__(self,b,i,c):
self.__cond = b
self.__inv = i
self.__body = c
def cond(self):
return self.__cond
def inv(self):
return self.__inv
def body(self):
return self.__body
def __eq__(self,other):
match other:
case Seq():
return ((self.__cond == other.cond()) and
(self.__inv == other.inv()) and
(self.__body == other.body())
)
case _:
return False
def __str__(self):
b = Fore.GREEN + "While" + Style.RESET_ALL + "(" + str(self.__cond) + ") "
i = Fore.CYAN + " { " + str(self.__inv) + " } " + Style.RESET_ALL
return (b + i + "{ " + str(self.__body) + " }")