From 31a845c82ec6e771bd7190d2e43fda26b7a8de80 Mon Sep 17 00:00:00 2001 From: pprabh2007 <32813957+pprabh2007@users.noreply.github.com> Date: Tue, 11 Dec 2018 21:31:48 +0530 Subject: [PATCH 1/4] Add files via upload --- libs/sudocode_c.py | 83 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 70 insertions(+), 13 deletions(-) diff --git a/libs/sudocode_c.py b/libs/sudocode_c.py index 83feb8c..e50fad7 100644 --- a/libs/sudocode_c.py +++ b/libs/sudocode_c.py @@ -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: + # + # + # + # + + + + 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 Date: Mon, 31 Dec 2018 01:01:40 +0530 Subject: [PATCH 3/4] Add files via upload --- libs/sudocode_c.py | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/libs/sudocode_c.py b/libs/sudocode_c.py index 7387b99..7baeccd 100644 --- a/libs/sudocode_c.py +++ b/libs/sudocode_c.py @@ -80,7 +80,7 @@ def get_code(filename): 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 + line_elem[0] = line_elem[0].lower() #converting the first word in list_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 @@ -108,6 +108,7 @@ def get_code(filename): 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 @@ -117,9 +118,11 @@ def get_code(filename): 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 @@ -152,12 +155,11 @@ def get_code(filename): 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 = [] - + return_list = 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 @@ -171,6 +173,7 @@ def get_code(filename): 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 @@ -178,6 +181,7 @@ def get_code(filename): 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 @@ -203,22 +207,11 @@ def get_code(filename): 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") - + if(line_elem[1] in funcs): #checking if func name is funcs stack + line_of_code += 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): @@ -236,6 +229,7 @@ def get_code(filename): line_of_code += line + ";" code_file_ptr.write(line_of_code+'\n') #writing line of code into file + #print(variables) #print(funcs) @@ -244,10 +238,13 @@ def get_code(filename): while(len(funcs)>0): #popping out all func names and no. of args funcs.pop() - #print(variables) - #print(funcs) + + #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 From 59b15fb6ec0f7e57e5269daa3ab5837ef03a2cac Mon Sep 17 00:00:00 2001 From: pprabh2007 <32813957+pprabh2007@users.noreply.github.com> Date: Mon, 31 Dec 2018 01:03:18 +0530 Subject: [PATCH 4/4] Add files via upload