-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_code_generator.py
More file actions
280 lines (220 loc) · 7.91 KB
/
Copy pathpython_code_generator.py
File metadata and controls
280 lines (220 loc) · 7.91 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import json
import re
import os
splash_text = r"""
____ ______
/ __ \__ __/ ____/__ ____ ___ _____
/ /_/ / / / / / __/ _ \/ __ \/ _ \/ ___/
/ ____/ /_/ / /_/ / __/ / / / __/ /
/_/ \__, /\____/\___/_/ /_/\___/_/
/____/
"""
user_guide = r"""
!Note: Please select by the number (ex: 1 = print)
1. print 2. If Statement 3. For Loop 4. Variable 5. If-Else
6. Math(expression)
command: press "c" for clear the system
"""
def detect_value(value):
if value.isdigit():
return value
elif value == "True" or value == "False":
return value
else:
return f'"{value}"'
def change_space(var_name):
var_name = var_name.lower()
var_name = var_name.replace(" ", "_")
var_name = re.sub(r'[^a-zA-Z0-9_]', '', var_name)
return var_name
def generate_action(action, indent=1):
space = " " * indent
if action["type"] == "print":
return space + f'print("{action["text"]}")\n'
elif action["type"] == "if":
if_state = space + f'if {action["condition"]}:\n'
for child in action["action"]:
if_state += generate_action(child, indent + 1)
return if_state
elif action["type"] == "if-else":
if_state = space + f'if {action["condition"]}:\n'
for child in action["action"]:
if_state += generate_action(child, indent + 1)
else_state = space + f'else:\n'
for child in action["action"]:
else_state += generate_action(child, indent + 1)
return if_state + else_state
elif action["type"] == "loop":
for_loop = space + f'for i in range({action["times"]}):\n'
for child in action["action"]:
for_loop += generate_action(child, indent + 1)
return for_loop
elif action["type"] == "set":
value = detect_value(action["value"])
return space + f'{action["name"]} = {value}\n'
elif action["type"] == "math":
return space + f'print({action["left"]} {action["operator"]} {action["right"]})\n'
return space + "#unknown action / value :("
def generate_code(data):
code = ""
if data["event"] == "function":
code += "\nOUTPUT CODE:\ndef MyFunction():\n"
for action in data["action"]:
code += generate_action(action)
return code
# print splash also the input/output
print(f'{splash_text}\n')
while True:
print("What do you want to make? (command --help to see the list or 'q' to quit):")
input_select = input(">> ").lower()
if input_select == "--help":
print(user_guide)
elif input_select == "c":
os.system("clear")
elif input_select == "q":
break
elif input_select == "1":
print("type any text to print: ")
user_data = input(">> ")
data = {
"event": "function",
"action": [
{"type": "print", "text": user_data}
]
}
result = generate_code(data)
print(result)
elif input_select == "2":
print("Build If Condition: ")
user_data = input(">> ")
data = {
"event": "function",
"action": [
{
"type": "if",
"condition": user_data,
"action": [
{"type": "print", "text": "If Statement was created!"}
]
}
]
}
result = generate_code(data)
print(result)
elif input_select == "3":
print("Build For Loop: ")
user_data = input(">> ")
data = {
"event": "function",
"action": [
{
"type": "loop",
"times": user_data,
"action": [
{"type": "print", "text": "for loop is created!"}
]
}
]
}
result = generate_code(data)
print(result)
elif input_select == "4":
print("Give The variable name: ")
var_name = change_space(input(">> "))
print(f'Give the value for {var_name} ex: String/Boolean/Integer: ')
var_value = input(">> ")
data = {
"event": "function",
"action": [
{
"type": "set",
"name": var_name,
"value": var_value
}
]
}
result = generate_code(data)
print(result)
elif input_select == "5":
print("build the if-else state condition: ")
user_data = input(">> ")
data = {
"event": "function",
"action": [
{
"type": "if-else",
"condition": user_data,
"action": [
{"type": "print", "text": "if-else state was created!"}
]
}
]
}
result = generate_code(data)
print(result)
elif input_select == "6":
print("select the expression: \n1. + (to add, ex(1 + 2))\n2. - (to subtract, ex(2 - 1))\n3. * (to multiply, ex(2 * 3))\n4. / (to divide, ex(8 / 4))")
while True:
expression_input = input("note: press x to leave math >> ").lower()
if expression_input == "+":
data = {
"event": "function",
"action": [
{
"type": "math",
"operator": expression_input,
"left": "x",
"right": 5
}
]
}
result = generate_code(data)
print(result)
elif expression_input == "-":
data = {
"event": "function",
"action": [
{
"type": "math",
"operator": expression_input,
"left": "x",
"right": 5
}
]
}
result = generate_code(data)
print(result)
elif expression_input == "*":
data = {
"event": "function",
"action": [
{
"type": "math",
"operator": expression_input,
"left": "x",
"right": 5
}
]
}
result = generate_code(data)
print(result)
elif expression_input == "/":
data = {
"event": "function",
"action": [
{
"type": "math",
"operator": expression_input,
"left": "x",
"right": 5
}
]
}
result = generate_code(data)
print(result)
elif expression_input == "x":
break
else:
print("\nplease enter expression, ex(+, -, /, *) or x to exit math mode\n")
else:
print("\n!#UNKNOWN ACTION :(\n")