forked from Teodoro-lab/Emojilang
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgramatica.py
173 lines (141 loc) · 3.7 KB
/
gramatica.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import re
stack = []
def P(string):
"""
First part of the compiler eliminates all the spaces
in order to have a single line string to be parsed
:param string:
:return:
"""
string = re.sub(r'\s', "", string)
stack.append("#")
Q(string)
def Q(string):
"""
This functions contains all the rules that our language have
and consume the string.
:param string:
:return:
"""
# e / <>... <= ... >= ... < ... > / BOOLEAN
if stack[-1] in ('<>', '<=', '>=', '<', '>'):
del(stack[-1])
stack.append("BOOLEAN")
Q(string)
# e / 1..2..3.. / NUMBER
elif stack[-1] in ('1️⃣','2️⃣','3️⃣','4️⃣') or stack[-1].isdigit():
del(stack[-1])
stack.append("NUMBER")
Q(string)
# e / "'hola mundus!'" / STRING
elif stack[-1] == "'holamundus!'":
del(stack[-1])
stack.append("STRING")
Q(string)
# e / )NUMBER BOOLEAN NUMBER( / CONDITION
elif "".join(stack[-1:-6:-1]) == ")NUMBERBOOLEANNUMBER(":
del(stack[-5:])
stack.append("CONDITION")
Q(string)
# e / STRING🖨 / BODY
elif "".join(stack[-1:-3:-1]) == "STRING🖨":
del(stack[-2:])
stack.append("BODY")
Q(string)
# e / IF_STAT / BODY
elif "".join(stack[-1]) == "IF_STAT":
del (stack[-1])
stack.append("BODY")
Q(string)
# e / !BODY👉CONDITION❓ / IF_STAT
elif "".join(stack[-1:-6:-1]) == "❗BODY➡CONDITION❓":
del(stack[-5:])
stack.append("IF_STAT")
Q(string)
# e / ⏹ IF_STAT ▶ / PROGRAM
elif "".join(stack[-1:-4:-1]) == "⏹IF_STAT▶":
del(stack[-3:])
stack.append("PROGRAM")
Q(string)
# e / ⏹BODY▶ / PROGRAM
elif "".join(stack[-1:-4:-1]) == "⏹BODY▶":
del(stack[-3:])
stack.append("PROGRAM")
Q(string)
# e / PROGRAM / e
elif stack[-1] == "PROGRAM":
del(stack[-1])
Q(string)
# ⏹ / e / ⏹
elif string != "" and string[0] == "⏹":
stack.append(string[0])
string = string.lstrip("⏹")
Q(string)
# ▶ / e / ▶
elif string != "" and string[0] == "▶":
stack.append(string[0])
string = string[1:]
Q(string)
# ) / e / )
elif string != "" and string[0] == ")":
stack.append(string[0])
string = string[1:]
Q(string)
# ( / e / (
elif string != "" and string[0] == "(":
stack.append(string[0])
string = string[1:]
Q(string)
# ❓ / e / ❓
elif string != "" and string[0] == "❓":
stack.append(string[0])
string = string[1:]
Q(string)
# 👉 / e / 👉
elif string != "" and string[0] == "➡":
stack.append(string[0])
string = string[1:]
Q(string)
# ❗ / e / ❗
elif string != "" and string[0] == "❗":
stack.append(string[0])
string = string[1:]
Q(string)
# 🖨️ / e / 🖨️
elif string != "" and string[0] == "🖨":
stack.append(string[0])
string = string[1:]
Q(string)
# '<>', '<=', '>=', '<', '>' / e / '<>', '<=', '>=', '<', '>'
elif len(string) > 2 and string[0:2] in ('<>', '<=', '>=', '=='):
#hicimos una pequeña trampa aquí jeje :P
stack.append('<')
string = string[2:]
Q(string)
elif string != "" and string[0] in ('<', '>'):
stack.append(string[0])
string = string[1:]
Q(string)
# ('1️⃣','2️⃣','3️⃣','4️⃣') / e / ('1️⃣','2️⃣','3️⃣','4️⃣')
elif string != "" and (string[0] in ('1️⃣','2️⃣','3️⃣','4️⃣') or string[0].isdigit()):
stack.append(string[0])
string = string[1:]
Q(string)
# 'holamundus!' / e / 'holamundus!'
elif string.startswith("'holamundus!'"):
stack.append("'holamundus!'")
string = string[len("'holamundus!'"):]
Q(string)
else:
F(string)
def F(string):
"""
The function validates if the compilation was successful or not
:param string:
:return:
"""
if stack == ['#'] and string == "":
print("Successful compilation")
return 0
print("Compilation failed : following strings could't be parsed\n", stack)
return 1