diff --git a/libs/sudocode_c.py b/libs/sudocode_c.py index 83feb8c..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,6 +17,12 @@ 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 + 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 @@ -21,13 +30,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 @@ -74,20 +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] - 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 += "f\\n\"," + new1[2] + if(variables[index_var-1]=="char"): + 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 += ");" @@ -100,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): @@ -146,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): @@ -181,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 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 +