Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 49 additions & 12 deletions libs/cleaner.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import os
import subprocess
import sys

def get_platform():
platforms = {
'linux' : 'Linux',
'linux1' : 'Linux',
'linux2' : 'Linux',
'win32' : 'Windows'
}
#print(sys.platform)
if sys.platform not in platforms:
return False

return platforms[sys.platform]

#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
Expand All @@ -29,6 +46,9 @@ def code_cleaner(filename):
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

temp_file_ptr.close()
main_file_ptr.close()

os.remove(filename) #Deleting main code file
os.rename(temp_file,filename) #Renaming the temp file with main code file

Expand All @@ -40,21 +60,38 @@ def code_cleaner(filename):
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")
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)
platform=get_platform()
#print(platform)
if(platform):
try:
if(platform=='Windows'):
compile_command="gcc "+ filename+ " -o "+ find[0]+".exe"
os.system(compile_command)
exec_command=find[0]+".exe"
os.system(exec_command)

if(platform=='Linux'):
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)
except:
print("There was some error executing your file.. Please try again.. ")
else:
print("Sorry, but can't execute the your " + find[1] + " code mate! :(")
print("Sorry, your platform is currently unsupported! We are working on it.. ")




83 changes: 72 additions & 11 deletions libs/sudocode_c.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,67 @@
#print(__name__)

def variable_line(line_elem):

''' Uncomment for testing this function individually

line_elem = line_elem.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()
line_elem=list(filter(lambda x: x!='', line_elem))
'''

elem_no=0 #the index of the word in the line we are probing
var_type="" #the type of variable
var_value='' #the value of variable
var_identifier="" #variable indentifier
if(line_elem[elem_no] in ["int", "float"]): #check if there is a type specified
var_type=line_elem[elem_no] #assign the type
elem_no+=1 #increment elem_no i.e. to probe the next element
if(var_type=="int"):
var_value="0"
elif(var_type=="float"):
var_value="0.0"

else: # assign float
var_type="float"
var_value="0.0"


#if there is an equal to, separate the identifier and value

#CASES TO BE DEALT WITH:
# <indentifier><equal to><value>
# <identifier><space><equal to><value>
# <identifier><space><equal to><space><value>
# <identifier><equal to><space><value>



if('=' in line_elem[elem_no]):
pos=line_elem[elem_no].find("=")
var_identifier=line_elem[elem_no][0:pos]
line_elem[elem_no]=line_elem[elem_no][pos:]

else:
var_identifier=line_elem[elem_no]
elem_no+=1

#print(len(line_elem))

#checking if there is a value assigned or not. If it is assign it.

if(elem_no<len(line_elem)):
if(line_elem[elem_no]=="="):
var_value=line_elem[elem_no+1]

elif (line_elem[elem_no][0]=="="):
var_value=line_elem[elem_no][1:]

return (var_type, var_identifier, var_value)




def get_code(filename):
return_list = [] #stores the last defined function details to get return statement accordingly.
variables = [] #Stores the list of all variables
Expand All @@ -21,17 +83,15 @@ def get_code(filename):
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:

line_elem=list(filter(lambda x: x!='', line_elem)) #filter out excessive spaces
var_type, var_identifier, var_value=variable_line(line_elem[1:]) #send the line to function except the word 'initialise' as it has been processed once

variables.append(var_type)
variables.append(var_identifier)
line_of_code=var_type+" "+var_identifier+"="+var_value+";"

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])
Expand Down Expand Up @@ -192,4 +252,5 @@ def get_code(filename):
code_file_ptr.write("}") #ending the code with a last }

code_file_ptr.close() #closing code file ptr.
file_ptr.close() #closing file ptr

file_ptr.close() #closing file ptr