def Parse(self): id = Id.Id() statement = Statement.Statement() index = 0 file = open("test.lua") filetext = file.read() text = filetext.split( ) #Uses delimiter SPACE to separate the words into a list ; Stores in list split_text file.close() #Begins initial checks if (text.pop(0) != "function"): sys.exit("function not found, program terminated.") id.Include(text, 0) text.pop(0) if (text.pop(0) != '('): sys.exit("Left paran not found, program terminated.") if (text.pop(0) != ')'): sys.exit("Right paran not found, program terminated.") #Ends initial checks while text[index] != "end": index = statement.determineStatement(text, index) return
def parse(self): # Use one-token lookahead to determine whether it's <int>, <id>, or (<exp>) tokNo = t.tokenizer.get_token() if tokNo == t.Tokens.NUMBER.value: self.__int = Int.Int() self.__int.parse() self.__alternative = 1 elif tokNo == t.Tokens.IDENTIFIER.value: self.__id = Id.Id() self.__id.parse() self.__alternative = 2 elif tokNo == t.Tokens.OPEN_PAREN.value: t.tokenizer.skip_token() # Consume open paren self.__exp = Exp.Exp() self.__exp.parse() self.__alternative = 3 tokNo = t.tokenizer.get_token() t.tokenizer.skip_token() # Consume closed parent if tokNo != t.Tokens.CLOSED_PAREN.value: print("If: Expected token {}, got token {}".format( t.Tokens.CLOSED_PAREN.value, tokNo)) return -1 # print("Loop: Consumed `)` token.") else: print("Op: Invalid Next Token {}!".format(tokNo)) exit(-1) return -1 # Successful error code return 0
def parse(self): # Id self.__id = Id.Id() self.__id.parse() # `=` token tokNo = t.tokenizer.get_token() t.tokenizer.skip_token() if tokNo != t.Tokens.EQUALS.value: print("Assign: Expected token {}, got token {}".format( t.Tokens.EQUALS.value, tokNo)) return -1 # print("Assign: Consumed `=` token.") # Exp self.__exp = Exp.Exp() self.__exp.parse() # `;` token tokNo = t.tokenizer.get_token() t.tokenizer.skip_token() if tokNo != t.Tokens.SEMICOLON.value: print("Assign: Expected token {}, got token {}".format( t.Tokens.SEMICOLON.value, tokNo)) return -1 # print("Assign: Consumed `;` token.") # Successful error code return 0
def determine(self,text,index): mem = Memory.Memory() id = Id.Id() operators=ArithmeticOp.ArithmeticOp() #Determine arithmetic operation if("AssignmentStatement.py" in inspect.stack()[1][1]): if (operators.Contains(text,index)): result = operators.Operate(text,index) mem.Store(result) return index else: if(operators.Contains(text,index)): result = operators.Operate(text,index) return result #Determine id if (id.Include(text,index)): return mem.mem #Determine literal integer if (isinstance(int(text[index]),int)): #Determines if value stored at text[index] is an integer. If not, the if("AssignmentStatement.py" in inspect.stack()[1][1]): #Prevents storing memory for anything but assignment mem.Store(text[index]) elif(inspect.stack()[1][3]=='sPrint'): #This determines if the sPrint method is calling ArithmeticExpression.determine return int(text[index]) #If it is, it returns what is stored in the memory so it can be printed. elif("ArithmeticExpression.py" in inspect.stack()[1][1]): return int(text[index]) index+=1 return(index)
def parse(self): self.id = Id.Id(self.t) self.id.parse() # If the peek token is a comma, then this is a list of identifiers x = int(self.t.peek()) if x == 13: self.t.getToken() self.case = 1 self.iList = IdList(self.t) self.iList.parse()
def parse(self): # Id self.__id = Id.Id() self.__id.parse() # If next is a comma, we are in second comma # `,` token tokNo = t.tokenizer.get_token() if tokNo == t.Tokens.COMMA.value: t.tokenizer.skip_token() # print("IdList: Consumed `,` token.") self.__id_list = IdList() self.__id_list.parse() # Successful error code return 0
def parse(self): # Parse based on one token look ahead x = int(self.t.peek()) if x == 31: self.i = Int.Int(self.t) self.i.parse() elif x == 32: self.case = 1 self.idName = self.t.getValue() self.id = Id.Id(self.t) self.id.parse() elif x == 20: self.case = 2 self.t.getToken() self.e = Exp.Exp(self.t) x = int(self.t.getToken()) if x != 21: print "Expected end parenthesis." exit() else: print "Invalid op." exit()
def determineStatement(self,text,index): if_state = IfStatement.IfStatement() id = Id.Id() print_statement = PrintStatement.PrintStatement() #Determines if statement #if (text[index]=='if'): #index = if_state.determine(text,index): #Determines assignment statement if(id.Include(text,index)): index+=1 #Allows the determine statement to determine if there is a '=' present assign = AssignmentStatement.AssignmentStatement() index = assign.determine(text,index) return index #Determines while statement #Determines print statement if (text[index] == "print"): index+=1 if text[index]== "(": index+=1 index = print_statement.sPrint(text,index) return index else: sys.exit("Left paran not found.")
def __init__(self, t): self.e = Exp.Exp(t) self.id = Id.Id(t) self.t = t
def getId(self): s = self.lex.getToken() if s == "" or len(s) > 1: raise ParserException("id expected, received " + s) return Id(s)