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
58 changes: 45 additions & 13 deletions libs/sudocode_c.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -17,17 +20,25 @@ 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'):
line_elem = [k for k in line_elem if k is not '']
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])]

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
Expand Down Expand Up @@ -71,22 +82,43 @@ 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 += ");"

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
Expand Down
32 changes: 32 additions & 0 deletions testfiles/sudocode4.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <stdlib.h>

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";
}
}
}
22 changes: 22 additions & 0 deletions testfiles/sudocode4.txt
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions testfiles/sudocode5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>
#include <stdlib.h>

int main()
{

char c='a';
char d='b';
int x=10;
printf("%c\n",c);
printf("%c\n",d);

}
7 changes: 7 additions & 0 deletions testfiles/sudocode5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
start
initialise char c = 'a'
initialise char d = 'b'
initialise int x = 10
print c
print d