Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2: Problems in initialise statement (Solved) #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
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