-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode
279 lines (215 loc) · 11.7 KB
/
Code
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
sudocode.c (also there in C++ and phython)
#print(__name__)
def get_code(filename):
return_list = [] #stores the last defined function details to get return statement accordingly.
variables = [] #Stores the list of all variables
funcs = [] #stores list of all functions and number of args to each func
func_args = 0 #variable to store number of args in ever function
file_ptr = open(filename, "r")
code_file_ptr = open(filename[0:len(filename)-4]+".c", "w")
code_file_ptr.write("#include <stdio.h>\n#include <stdlib.h>\n\n")
no = 0
for line in file_ptr: #going through every line
no+=1 #incrementing line count
line_elem = line.split(" ") #tokenisation at space
line_elem[-1] = line_elem[-1].replace("\n","") #replacing new line char with null char
line_elem[0] = line_elem[0].lower() #converting the first word in line_elem to all lower case letters
#print("\"",line_elem[0],"\"")
#print("gap" in line_elem)
line_of_code = "" #initialising var to get the final line of code to be inserted in file
if("start" in line_elem): #start keyword starts main function
line_of_code +="int main()\n{\n"
elif(("initialise" in line_elem) and ("int" not in line_elem) and ("float" not in line_elem)):
line_of_code += "float " + line_elem[1] + ";" #default type is float
variables.append("float") #storing var type in stack
line_elem[1] = (line_elem[1].split("="))[0] #need to store var name so tokenising at = in order to get name of var
variables.append(line_elem[1]) #pushing var name into variables stack
elif(("initialise" in line_elem) and (("int" in line_elem) or ("float" in line_elem))):
line_of_code += line_elem[1] + " " + line_elem[2] + ";"
variables.append(line_elem[1]) #storing var type in stack
line_elem[2] = (line_elem[2].split("="))[0] #need to store var name so tokenising at = in order to get name of var
variables.append(line_elem[2]) #pushing var name into variables stack
elif(("for" in line_elem) and ("gap" not in (line_elem[-1].split("="))[0])): #by default gap is 1 and since gap is not in line_elem, it'll be considered at 1 only.
#print(line_elem[2][-1], line_elem[4])
start_for = (line_elem[2].split("="))[-1] #tokenising at = to understand what the start value is
variables.append("int") #appending the type int to variables stack as we're assuming that the looping var is a newly defined temp one
variables.append((line_elem[2].split("="))[0]) #tokenising at = to know what the var name is to push into stack
if(int(start_for) < int(line_elem[4])): #check to see whether loop is incrementing loop or decrementing loop
line_of_code += "for(int " + line_elem[2] + "; " + (line_elem[2].split("="))[0] + " <= " + line_elem[4] + "; " + (line_elem[2].split("="))[0] + "++)\n{"
else:
line_of_code += "for(int " + line_elem[2] + "; " + (line_elem[2].split("="))[0] + " >= " + line_elem[4] + "; " + (line_elem[2].split("="))[0] + "--)\n{"
elif(("for" in line_elem) and ("gap" in (line_elem[-1].split("="))[0])): #since gap is mentioned, the jumps of looping variable must be changed
start_for = (line_elem[2].split("="))[-1] #tokenising at = to understand what the start value is
variables.append("int") #appending the type int to variables stack as we're assuming that the looping var is a newly defined temp one
variables.append((line_elem[2].split("="))[0]) #tokenising at = to know what the var name is to push into stack
if(int(start_for) < int(line_elem[4])): #check to see whether loop is incrementing loop or decrementing loop
line_of_code += "for(int " + line_elem[2] + "; " + (line_elem[2].split("="))[0] + " <= " + line_elem[4] + "; " + (line_elem[2].split("="))[0] + "+=" + (line_elem[-1].split("="))[-1] + ")\n{"
else:
line_of_code += "for(int " + line_elem[2] + "; " + (line_elem[2].split("="))[0] + " >= " + line_elem[4] + "; " + (line_elem[2].split("="))[0] + "-=" + (line_elem[-1].split("="))[-1] + ")\n{"
elif("while" in line_elem): #check if while loop implementation
line_of_code += "while(" + line_elem[-1] + ")\n{" #condition added in while
elif(("endfor" in line_elem) or ("endwhile" in line_elem)): #checking if for or while loop is ending
line_of_code += "}"
if("endfor" in line_elem): #if for loop ending, pop out last 2 values as they are temp var type and name
variables.pop()
variables.pop()
elif(("if" in line_elem)):
line_of_code += "if(" + line_elem[1]+ ")\n{"
elif("else" in line_elem):
line_of_code += "}\nelse\n{"
elif("fi" in line_elem):
line_of_code += "}"
elif("print" in line_elem): #print function implementation
line_of_code += "printf(\""
for i in range(1,len(line_elem)): #getting each word to be printed
if(line_elem[i] in variables): #checking if print statement is referring to variables being printed
index_var = variables.index(line_elem[i]) #get index of that particular variable
line_of_code += "%"
if(variables[index_var-1]=="int"): #checking one index before the var name to check var type in order to get right format specifier
line_of_code += "d\\n\"," + variables[index_var]
if(variables[index_var-1]=="float"):
line_of_code += "f\\n\"," + variables[index_var]
break
else:
if(i==len(line_elem)-1): #check if last word of string is being printed
line_of_code += line_elem[i] + "\\n\"" #adding \n char for new line
break
line_of_code += line_elem[i] + " " #spacing need for each word
line_of_code += ");"
elif("function" in line_elem): #check for functions part
funcs.append(line_elem[1]) #adding func name to funcs stack
return_list.append(line_elem) #storing the line_elem list vals in return_list to get return type
#temp = []
#print(line_elem)
line_of_code += line_elem[3] + " " + line_elem[1] + "(" #func defn
index_arg = line_elem.index("args") #check where args is indexed
#print(line_of_code)
for i in range(index_arg+1,len(line_elem), 2): #start going through list in gaps of 2 to get var type and name
func_args += 1 #incrementing the func_args by 1
variables.append(line_elem[i]) #pushing args to variables list for checking return value validity
variables.append(line_elem[i+1])
if(i+1 == len(line_elem)-1):
line_of_code += line_elem[i] + " " + line_elem[i+1] + ")\n{"
break
line_elem[i+1] = line_elem[i+1].replace(",","")
line_of_code += line_elem[i] + " " + line_elem[i+1] + ","
#print(func_args)
funcs.append(func_args) #appending number of args to stack
'''temp = list(line_of_code)
temp[-1] = ")\n{"
line_of_code = "".join(temp)'''
elif(("return" in line_elem) and ("print" not in line_elem)): #check for return statement
line_of_code = "return"
for i in range(1,len(variables),2): #jumping through 2 in number to remove the commas at the end
variables[i] = (variables[i].split(","))[0]
if(line_elem[1] in variables): #check if return var in variables stack
index_return = variables.index(line_elem[1]) #getting index of return var name
#print(return_list)
if(variables[index_return-1] == return_list[0][3]): #checking types are same or not
line_of_code += " " + variables[index_return] #then adding to line_of_code
else:
raise("the return type in function definition and variable type of returned value don't match!")
line_of_code += ";\n"
elif("endfunction" in line_elem): #endfunction keyword check
i=1
var_len = len(variables)/2
while(i<=var_len): #popping out all function arg vars
variables.pop()
variables.pop()
i += 1
line_of_code += "}"
#return_list = [] #return list set to empty
elif("call" in line_elem): #to call functions in main
num_values = 0
index_values = 0
#print(return_list)
for func in return_list:
#print(func, line_elem[1])
#if(line_elem[1] in funcs): checking if func name is funcs stack
if('void' in func):
line_of_code += line_elem[1] + "("
#index_num_args = funcs.index(line_elem[1]) + 1
index_values = line_elem.index("values")
else:
line_of_code += return_list[0][3] +" var = " + line_elem[1] + "("
index_num_args = funcs.index(line_elem[1]) + 1
index_values = line_elem.index("values")
for i in range(index_values+1,len(line_elem)):
num_values += 1
if(i == len(line_elem)-1):
line_of_code += line_elem[i] + ");"
break
line_elem[i] = line_elem[i].replace(",","")
line_of_code += line_elem[i] + ","
#print(num_values)
elif("" == line_elem[0]): #if nothing exists then leave line
code_file_ptr.write("\n")
continue
else: #for normal statements like adding and all.
line = line.replace("\n","")
line_of_code += line + ";"
code_file_ptr.write(line_of_code+'\n') #writing line of code into file
#print(variables)
#print(funcs)
while(len(variables)>0): #popping out all variables type and name
variables.pop()
while(len(funcs)>0): #popping out all func names and no. of args
funcs.pop()
#print(variables)
#print(funcs)
code_file_ptr.write("}") #ending the code with a last }
code_file_ptr.close() #closing code file ptr.
file_ptr.close() #closing file ptr
Cleaner file
import os
import subprocess
#print(__name__)
def code_cleaner(filename):
try:
temp_file = filename[0:(len(filename)-2)] + "_temp.c" #Adding the "_temp.c" to filename
main_file_ptr = open(filename,"r") #Creating file pointer for the main code file
temp_file_ptr = open(temp_file,"w") #Creating file pointer for temp file
count=0 #variable to keep count of the number of "{" or "}"
count_close = 0
count_open = 0
for line in main_file_ptr:
spaces = '\t'*count #Giving count number of tabs from next line onwards
tab_count = line.count('\t')
line = line.replace('\t'*tab_count,'')
#print(tab_count)
if "{" in line:
count+=1 #incrementing count whenever "{"
#print count
count_open +=1
if "}" in line:
count-=1 #Decrementing count whenever "}"
spaces = '\t'*count
count_close +=1
#print count
temp_file_ptr.write(spaces) #First writing spaces into every line
temp_file_ptr.write(line) #Then copy contents of every line from main code
os.remove(filename) #Deleting main code file
os.rename(temp_file,filename) #Renaming the temp file with main code file
#print count_close, count_open
if((count_close - count_open) > 0): #Checking if } more than {
print("Looks like you have more number of \"}\" somewhere")
if((count_close - count_open) < 0): #Checking if { more than }
print("Looks like you have more number of \"{\" somewhere")
return count
except IOError: #If wrong file name gives
print("Sorry, but no such file exists in your current working directory")
def code_execute(filename):
find = filename.split(".")
#print(find)
if(find[1] == 'c'):
compile_command = "gcc " + filename + " -o " + filename[0:(len(filename)-2)] + "_c" #Command for compiling with gcc along with filename and executable name
os.system(compile_command) #Running the above command from script to compile the code
exec_file = "./"+filename[0:(len(filename)-2)] + "_c" #To execute file
os.system(exec_file)
elif(find[1] == "cpp"):
compile_command = "gcc " + filename + " -o " + filename[0:(len(filename)-4)] + "_cpp" #Command for compiling with gcc along with filename and executable name
os.system(compile_command) #Running the above command from script to compile the code
exec_file = "./"+filename[0:(len(filename)-4)]+"_cpp" #To execute file
os.system(exec_file)
else:
print("Sorry, but can't execute the your " + find[1] + " code mate! :(")