-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_generator.py
More file actions
434 lines (407 loc) · 15.8 KB
/
code_generator.py
File metadata and controls
434 lines (407 loc) · 15.8 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import re
def parse_line(nl_line, language):
"""
Converts a single line of vague natural language into a code snippet for the specified language.
The regex patterns have been broadened to capture more casual phrasing.
"""
original_line = nl_line.strip()
line = original_line.lower().strip()
if not line:
return ""
# ----------------------------
# Helpers
# ----------------------------
def comment(text: str) -> str:
if language == "python":
return f"# {text}"
if language in ["c++", "java"]:
return f"// {text}"
return f"% {text}"
# ----------------------------
# Comments
# ----------------------------
m = re.match(r'(?:comment|add comment)\s*:\s*(.+)', line, re.IGNORECASE)
if m:
return comment(m.group(1))
# ----------------------------
# Vague For Loop (repeat N times)
# Examples:
# "can i have a for loop that loops around 5 times"
# "make a loop that repeats 10 times"
# "loop 3 times"
# ----------------------------
m = re.search(
r'(?:can i have|please give me|i need|i want|make|create|give me)?\s*'
r'(?:a\s+)?(?:for\s+loop|loop)\s*'
r'(?:that\s+)?(?:loops\s+around|repeats|runs|iterates|goes\s+through)?\s*'
r'(\d+)\s*(?:times?)',
line,
re.IGNORECASE
)
if m:
count = m.group(1)
if language == "python":
return f"for i in range({count}):\n print(i)"
if language == "c++":
return f"for (int i = 0; i < {count}; i++) {{\n std::cout << i << std::endl;\n}}"
if language == "java":
return f"for (int i = 0; i < {count}; i++) {{\n System.out.println(i);\n}}"
return f"for i = 1:{count}\n disp(i)\nend"
# ----------------------------
# Nested loops (matrix / rows / columns)
# Example:
# "for each row in matrix, for each column in row, print value"
# ----------------------------
m = re.search(
r'for each row in (\w+)\s*,?\s*for each (\w+)\s*in row\s*,?\s*print\s+(.+)',
line, re.IGNORECASE
)
if m:
collection = m.group(1)
inner_var = m.group(2)
print_val = m.group(3).strip()
if print_val.lower() == "value":
print_val = inner_var
if language == "python":
return f"for row in {collection}:\n for {inner_var} in row:\n print({print_val})"
if language == "c++":
return (
f"for (auto row : {collection}) {{\n"
f" for (auto {inner_var} : row) {{\n"
f" std::cout << {print_val} << std::endl;\n"
f" }}\n"
f"}}"
)
if language == "java":
# Assumes a 2D array. For collections, users can adjust.
return (
f"for (int i = 0; i < {collection}.length; i++) {{\n"
f" for (int j = 0; j < {collection}[i].length; j++) {{\n"
f" System.out.println({collection}[i][j]);\n"
f" }}\n"
f"}}"
)
# MATLAB matrix iteration
return (
f"for i = 1:size({collection}, 1)\n"
f" for j = 1:size({collection}, 2)\n"
f" disp({collection}(i,j))\n"
f" end\n"
f"end"
)
# ----------------------------
# Vague Function Definition
# Examples:
# "i need a function called add that takes two numbers and returns a + b"
# "make a function called greet that takes name and prints 'hi'"
# ----------------------------
m = re.search(
r'(?:can i have|i need|please give me|create|make|write)?\s*'
r'(?:a\s+)?function\s*(?:called\s+|named\s+)?(\w+)\s*'
r'(?:that\s+)?(?:takes|accepts|with)?\s*(.*?)'
r'(?:\s+and\s+(?:returns|gives|outputs|prints)\s+(.+))?$',
line,
re.IGNORECASE
)
if m and "function" in line:
func_name = m.group(1)
params_str = (m.group(2) or "").strip()
action = (m.group(3) or "").strip()
# Light normalization for common phrases
params = ""
if params_str:
if "two numbers" in params_str:
params = "a, b"
elif "one number" in params_str:
params = "a"
else:
# Replace words like "and" with comma in params, then clean
params = re.sub(r'\band\b', ',', params_str)
params = re.sub(r'\s+', ' ', params).strip()
# Default behavior if not specified
if not action:
action = f"print('{func_name} called')"
if language == "python":
return f"def {func_name}({params}):\n {action}"
if language == "c++":
return f"void {func_name}({params}) {{\n {action};\n}}"
if language == "java":
return f"public void {func_name}({params}) {{\n {action};\n}}"
return f"function {func_name}({params})\n {action}\nend"
# ----------------------------
# Vague Class Definition
# Examples:
# "create a class named Person with attributes name and age"
# "make a class called Car with attributes brand, year"
# ----------------------------
m = re.search(
r'(?:can i have|please create|make|create|write)?\s*'
r'(?:a\s+)?class\s*(?:called|named)\s+(\w+)'
r'(?:\s+with\s+attributes?\s+(.+))?',
line,
re.IGNORECASE
)
if m and "class" in line:
class_name = m.group(1)
attributes = (m.group(2) or "").strip()
# Parse attributes "name and age" / "name, age"
attrs = []
if attributes:
attrs = [a.strip() for part in attributes.split("and") for a in part.split(",")]
attrs = [a for a in attrs if a]
if language == "python":
if attrs:
init_params = ", ".join(attrs)
assignments = "\n ".join([f"self.{a} = {a}" for a in attrs])
return f"class {class_name}:\n def __init__(self, {init_params}):\n {assignments}"
return f"class {class_name}:\n pass"
if language == "c++":
if attrs:
decls, params, inits = [], [], []
for a in attrs:
t = "int" if a.lower() == "age" else "std::string" if a.lower() == "name" else "auto"
decls.append(f" {t} {a};")
params.append(f"{t} {a}")
inits.append(f"{a}({a})")
params_str = ", ".join(params)
init_list = " : " + ", ".join(inits) if inits else ""
return (
f"class {class_name} {{\n"
f"public:\n"
f"{chr(10).join(decls)}\n"
f" {class_name}({params_str}){init_list} {{}}\n"
f"}};"
)
return f"class {class_name} {{\n}};"
if language == "java":
if attrs:
decls, params, assigns = [], [], []
for a in attrs:
t = "int" if a.lower() == "age" else "String" if a.lower() == "name" else "Object"
decls.append(f" {t} {a};")
params.append(f"{t} {a}")
assigns.append(f" this.{a} = {a};")
return (
f"public class {class_name} {{\n"
f"{chr(10).join(decls)}\n\n"
f" public {class_name}({', '.join(params)}) {{\n"
f"{chr(10).join(assigns)}\n"
f" }}\n"
f"}}"
)
return f"public class {class_name} {{\n}}"
# MATLAB
if attrs:
props = "\n ".join(attrs)
params = ", ".join(attrs)
assigns = "\n ".join([f"obj.{a} = {a};" for a in attrs])
return (
f"classdef {class_name}\n"
f" properties\n"
f" {props}\n"
f" end\n"
f" methods\n"
f" function obj = {class_name}({params})\n"
f" {assigns}\n"
f" end\n"
f" end\n"
f"end"
)
return f"classdef {class_name}\nend"
# ----------------------------
# Exception handling: try open file, else print error
# Examples:
# "try to open a file named data.txt, if it fails print Error"
# "try opening data.txt, if it fails print 'Error'"
# ----------------------------
m = re.search(
r'try\s+(?:to\s+)?open(?:ing)?\s+(?:a\s+)?file(?:\s+named\s+|\s+called\s+)?(\S+)?\s*,?\s*'
r'(?:and\s+)?(?:if it fails|if it doesn\'t work|on error|if error)\s+print\s+["\']?([^"\']+)["\']?',
line, re.IGNORECASE
)
if m:
filename = m.group(1) or "file.txt"
msg = m.group(2)
if language == "python":
return f"try:\n f = open('{filename}', 'r')\nexcept Exception:\n print('{msg}')"
if language == "c++":
return (
"#include <iostream>\n#include <fstream>\n#include <stdexcept>\n\n"
"try {\n"
f" std::ifstream file(\"{filename}\");\n"
" if (!file) {\n"
f" throw std::runtime_error(\"{msg}\");\n"
" }\n"
"} catch (const std::exception &e) {\n"
" std::cout << e.what() << std::endl;\n"
"}"
)
if language == "java":
return (
"try {\n"
f" java.io.FileReader fr = new java.io.FileReader(\"{filename}\");\n"
"} catch (Exception e) {\n"
f" System.out.println(\"{msg}\");\n"
"}"
)
return (
"try\n"
f" fid = fopen('{filename}', 'r');\n"
" if fid == -1, error('File error'); end\n"
"catch\n"
f" disp('{msg}')\n"
"end"
)
# ----------------------------
# Vague file write
# Examples:
# "create a file named notes.txt and write hello"
# "write hello into a file called out.txt"
# ----------------------------
m = re.search(
r'(?:write|put|save|store)\s+["\']?([^"\']+)["\']?\s+'
r'(?:into|to|in)\s+(?:a\s+)?(?:text\s+)?file\s*(?:named|called)?\s*(\S+)?',
line, re.IGNORECASE
)
if m:
text = m.group(1)
filename = m.group(2) or "output.txt"
if language == "python":
return f"with open('{filename}', 'w') as f:\n f.write(\"{text}\")"
if language == "c++":
return (
"#include <fstream>\n\n"
f"std::ofstream file(\"{filename}\");\n"
f"file << \"{text}\";\n"
"file.close();"
)
if language == "java":
return (
"try {\n"
f" java.io.FileWriter fw = new java.io.FileWriter(\"{filename}\");\n"
f" fw.write(\"{text}\");\n"
" fw.close();\n"
"} catch (Exception e) {\n"
" System.out.println(e);\n"
"}"
)
return (
f"fid = fopen('{filename}', 'w');\n"
f"fprintf(fid, '{text}');\n"
"fclose(fid);"
)
# ----------------------------
# If / If-Else (some vague phrasing)
# Examples:
# "if x > 10 then print 'big' else print 'small'"
# "if x is bigger than 10 then print big"
# ----------------------------
m = re.search(r'if\s+(.+?)\s*(?:,?\s*then\s+|\s+then\s+)(.+?)\s+else\s+(.+)$', line, re.IGNORECASE)
if m:
cond = m.group(1).strip()
t_action = m.group(2).strip()
f_action = m.group(3).strip()
if language == "python":
return f"if {cond}:\n {t_action}\nelse:\n {f_action}"
if language in ["c++", "java"]:
return f"if ({cond}) {{\n {t_action};\n}} else {{\n {f_action};\n}}"
return f"if {cond}\n {t_action}\nelse\n {f_action}\nend"
m = re.search(r'if\s+(.+?)\s*(?:,?\s*then\s+|\s+then\s+)(.+)$', line, re.IGNORECASE)
if m:
cond = m.group(1).strip()
action = m.group(2).strip()
if language == "python":
return f"if {cond}:\n {action}"
if language in ["c++", "java"]:
return f"if ({cond}) {{\n {action};\n}}"
return f"if {cond}\n {action}\nend"
# ----------------------------
# Variable assignment (casual)
# Examples:
# "set x to 5"
# "make x equal 5"
# ----------------------------
m = re.search(r'(?:set|make)\s+(\w+)\s+(?:to|equal|equals)\s+(.+)$', line, re.IGNORECASE)
if m:
var = m.group(1)
val = m.group(2).strip()
if language == "python":
return f"{var} = {val}"
if language == "c++":
return f"auto {var} = {val};"
if language == "java":
return f"var {var} = {val};"
return f"{var} = {val};"
# ----------------------------
# Print / display (casual)
# Examples:
# "print hello"
# "show x"
# ----------------------------
m = re.search(r'(?:print|show|display)\s+(.+)$', line, re.IGNORECASE)
if m:
content = m.group(1).strip()
if language == "python":
return f"print({content})"
if language == "c++":
return f"std::cout << {content} << std::endl;"
if language == "java":
return f"System.out.println({content});"
return f"disp({content})"
# ----------------------------
# Fallback
# ----------------------------
return comment(f"Could not parse: {original_line}")
def natural_to_code(nl_input, language):
"""
Converts multi-line natural language input into code for the selected language.
"""
lines = nl_input.strip().split("\n")
return "\n".join(parse_line(line, language) for line in lines if line.strip())
def execute_code(code, language):
"""
Executes generated code only for Python.
"""
if language != "python":
print("Execution is only supported for Python in this tool. Copy the output to your C++/Java/MATLAB environment.")
return
try:
print("Executing the following code:")
print("-" * 40)
print(code)
print("-" * 40)
exec(code, {}, {})
except Exception as e:
print(f"Execution error: {e}")
def main():
print("Select a language (Python, C++, Java, MATLAB):")
lang_choice = input("> ").strip().lower()
if lang_choice not in ["python", "c++", "java", "matlab"]:
print("Unsupported language. Defaulting to Python.")
lang_choice = "python"
print(f"Language set to {lang_choice}.")
print("Enter your natural language description (type 'exit' to quit).")
print("Submit a blank line to process your multi-line input.")
full_input = ""
while True:
user_input = input("> ")
if user_input.strip().lower() == "exit":
break
if user_input.strip() == "":
if not full_input.strip():
continue
generated_code = natural_to_code(full_input, lang_choice)
print("\nGenerated Code:\n")
print(generated_code)
print("\n-----------------\n")
if lang_choice == "python":
run = input("Execute the generated code? (y/n): ").strip().lower()
if run.startswith("y"):
execute_code(generated_code, lang_choice)
else:
print("Execution is not available for this language here. Copy/paste into your IDE/compiler.")
full_input = ""
else:
full_input += user_input + "\n"
if __name__ == "__main__":
main()