From 32e83aa70e83b967a5dcdde6a339eab375623ffc Mon Sep 17 00:00:00 2001 From: AviBomb <38415320+AviBomb@users.noreply.github.com> Date: Mon, 10 Dec 2018 00:52:06 +0530 Subject: [PATCH 1/4] Update sudocode_c.py --- libs/sudocode_c.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libs/sudocode_c.py b/libs/sudocode_c.py index 83feb8c..78d6e3a 100644 --- a/libs/sudocode_c.py +++ b/libs/sudocode_c.py @@ -17,6 +17,10 @@ def get_code(filename): #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 (line_elem[0]=='initialise' and len(line_elem)==3): + line_elem[1:3] = [''.join(line_elem[1:3])] + if (line_elem[0]=='initialise' and len(line_elem)==4): + line_elem[1:4] = [''.join(line_elem[1:4])] if("start" in line_elem): #start keyword starts main function line_of_code +="int main()\n{\n" From ab7420747aefcd2b57c1da3dc65800533cf4bb85 Mon Sep 17 00:00:00 2001 From: AviBomb <38415320+AviBomb@users.noreply.github.com> Date: Mon, 10 Dec 2018 13:49:18 +0530 Subject: [PATCH 2/4] Update sudocode_c.py --- libs/sudocode_c.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/sudocode_c.py b/libs/sudocode_c.py index 78d6e3a..04edfcb 100644 --- a/libs/sudocode_c.py +++ b/libs/sudocode_c.py @@ -25,13 +25,13 @@ 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)): + elif(("initialise" in line_elem) and ("int" not in line_elem) and ("float" not in line_elem) and ("char" 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))): + elif(("initialise" in line_elem) and (("int" in line_elem) or ("float" in line_elem) or ("char" 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 @@ -86,6 +86,8 @@ def get_code(filename): line_of_code += "d\\n\"," + variables[index_var] if(variables[index_var-1]=="float"): line_of_code += "f\\n\"," + variables[index_var] + if(variables[index_var-1]=="char"): + line_of_code += "c\\n\"," + variables[index_var] break else: if(i==len(line_elem)-1): #check if last word of string is being printed From 78b71d36defc2bc04f8a52bdde7357f6d2de3b08 Mon Sep 17 00:00:00 2001 From: AviBomb <38415320+AviBomb@users.noreply.github.com> Date: Mon, 10 Dec 2018 17:18:43 +0530 Subject: [PATCH 3/4] Update sudocode_c.py --- libs/sudocode_c.py | 67 +++++++++++++++++++++++++++++----------------- 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/libs/sudocode_c.py b/libs/sudocode_c.py index 04edfcb..f5a13dd 100644 --- a/libs/sudocode_c.py +++ b/libs/sudocode_c.py @@ -1,3 +1,6 @@ +import re +def find(s, ch): + return [i for i, ltr in enumerate(s) if ltr == ch] #print(__name__) def get_code(filename): @@ -14,13 +17,15 @@ def get_code(filename): 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 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 if (line_elem[0]=='initialise' and len(line_elem)==3): line_elem[1:3] = [''.join(line_elem[1:3])] if (line_elem[0]=='initialise' and len(line_elem)==4): line_elem[1:4] = [''.join(line_elem[1:4])] + if (line_elem[0]=='initialise' and len(line_elem)==5): + line_elem[2:5] = [''.join(line_elem[2:5])] + #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" @@ -78,22 +83,42 @@ def get_code(filename): 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 + if ('"' in line): + b = re.split(' |(")',line) + new = [k for k in b if k is not None] + new1=[n for n in new if n is not ''] + c='"' + d=find(new1,c) + new1[(d[0]+1):d[1]] = [' '.join(new1[(d[0]+1):d[1]])] + line_of_code += new1[2] + "\\n\"" #adding \n char for new line + elif ('(' in line and ')' in line): + b = re.split(' |(\))|(\()|(\+)|(\-)|(\*)|(\/)',line) + new = [k for k in b if k is not None] + new1=[n for n in new if n is not ''] + c=new1.index('(') + d=new1.index(')') + var=new1[c+1] + new1[(c+1):d] = [''.join(new1[(c+1):d])] + index_var = variables.index(var) #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] + line_of_code += "d\\n\"," + new1[2] if(variables[index_var-1]=="float"): - line_of_code += "f\\n\"," + variables[index_var] + line_of_code += "f\\n\"," + new1[2] if(variables[index_var-1]=="char"): - line_of_code += "c\\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 + line_of_code += "c\\n\"," + new1[2] + else: + 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] + if(variables[index_var-1]=="char"): + line_of_code += "c\\n\"," + variables[index_var] break - line_of_code += line_elem[i] + " " #spacing need for each word line_of_code += ");" @@ -106,7 +131,7 @@ def get_code(filename): 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 + 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): @@ -152,7 +177,7 @@ def get_code(filename): 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): @@ -187,12 +212,4 @@ 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 From ff327ce6c6f4cbc767b8135a1485b5351dcee47e Mon Sep 17 00:00:00 2001 From: AviBomb <38415320+AviBomb@users.noreply.github.com> Date: Mon, 10 Dec 2018 17:19:42 +0530 Subject: [PATCH 4/4] Add files via upload --- testfiles/sudocode4.c | 32 ++++++++++++++++++++++++++++++++ testfiles/sudocode4.txt | 22 ++++++++++++++++++++++ testfiles/sudocode5.c | 13 +++++++++++++ testfiles/sudocode5.txt | 7 +++++++ 4 files changed, 74 insertions(+) create mode 100644 testfiles/sudocode4.c create mode 100644 testfiles/sudocode4.txt create mode 100644 testfiles/sudocode5.c create mode 100644 testfiles/sudocode5.txt diff --git a/testfiles/sudocode4.c b/testfiles/sudocode4.c new file mode 100644 index 0000000..06dfd28 --- /dev/null +++ b/testfiles/sudocode4.c @@ -0,0 +1,32 @@ +#include +#include + +int main() +{ + + float a=10; + float hi=20; + char c='a'; + char d='b'; + int e=5; + int f=5; + printf("%c\n",c); + printf("%c\n",d); + printf("hi\n"); + printf("%f\n",hi+a); + printf("%d\n",e+f); + printf("%f\n",hi); + printf("%d\n",e); + + for(int i=0; i <= 10; i++) + { + if(i%2==0) + { + printf("%d\n",i); + } + else + { + "print not even"; + } + } +} \ No newline at end of file diff --git a/testfiles/sudocode4.txt b/testfiles/sudocode4.txt new file mode 100644 index 0000000..eac1025 --- /dev/null +++ b/testfiles/sudocode4.txt @@ -0,0 +1,22 @@ +start +initialise a = 10 +initialise hi =20 +initialise char c = 'a' +initialise char d = 'b' +initialise int e = 5 +initialise int f = 5 +print c +print d +print "hi" +print ( hi + a ) +print ( e+ f ) +print hi +print e +call test_3 with values 0,10,10 +for int i=0 to 10 +if i%2==0 +print i +else +"print not even" +fi +endfor diff --git a/testfiles/sudocode5.c b/testfiles/sudocode5.c new file mode 100644 index 0000000..3be2f56 --- /dev/null +++ b/testfiles/sudocode5.c @@ -0,0 +1,13 @@ +#include +#include + +int main() +{ + + char c='a'; + char d='b'; + int x=10; + printf("%c\n",c); + printf("%c\n",d); + +} \ No newline at end of file diff --git a/testfiles/sudocode5.txt b/testfiles/sudocode5.txt new file mode 100644 index 0000000..7f6dbcf --- /dev/null +++ b/testfiles/sudocode5.txt @@ -0,0 +1,7 @@ +start +initialise char c = 'a' +initialise char d = 'b' +initialise int x = 10 +print c +print d +