Example #1
0
def forcaseStat():
		global token
		flag = intermediate.newTemp()
		forQuad = intermediate.nextQuad()
		intermediate.genQuad(":=", "0", "_", flag)
		while token[0] != "endforcase":
				if token[0] == "when":
						token = lexi.lex()
						(bTrue, bFalse) = condition()
						intermediate.backPatch(bTrue, intermediate.nextQuad())
						if token[0] == "upDownTk":
								token = lexi.lex()
								statements()
								genQuad(":=", "1", "_", flag)
								intermediate.backPatch(bFalse, intermediate.nextQuad())
						else:
								line = lexi.getLineAndColumn()
								print("Syntax error: expected ':' at line " + str(line[0]+1)+".") 
								exit()
				else:
						line = lexi.getLineAndColumn()
						print("Syntax error: expected 'when' at line " + str(line[0]+1)+".") 
						exit()
								
		intermediate.genQuad("=", flag, "1", forQuad)
		token = lexi.lex()
		return
Example #2
0
def program():
		global token 
		token = lexi.lex()
		if token[0] == "program":
				token = lexi.lex()
				if token[0] == "Id":
						symbolTable.newScope()
						programId = "__main"
						token = lexi.lex()
						block(programId)
						symbolTable.setLastFunctionFrameLength()
						if token[0] == "endprogram":
								final.genQuad("halt", "_", "_", "_")
								intermediate.genQuad("end_block", programId, "_", "_")
								final.genFinalCode()
								symbolTable.deleteScope()
								token = lexi.lex()
								if token == "EOF":
										print("Passed Test Successfully!")
								else:
										line = lexi.getLineAndColumn()
										print("Syntax error: code after 'endprogram' statement at line " + str(line[0] + 1)+".")
										exit()
						else:
								print("Syntax error: 'endprogram' statement is missing.")
								exit()
				else:
						print("Syntax error: program name is missing at line.")
						exit()
		else:
				print("Syntax error: 'program' statement is missing.")
				exit()
Example #3
0
def callStat(callId):
	
		global token
		global a
		global param
		global counter
		global temp
	
		if counter == 0:
			a = intermediate.newTemp()
			param.append(a)
			counter += 1
		if token[0] == "Id":
				functionItem, _ = symbolTable.findEntity(token[1])
				procedureName = token[1]
				token = lexi.lex()
				actualPars(procedureName, functionItem) 	
				
				if token[0] == "rightparenthesisTk" or token[0]== "commaTk":		
					
					intermediate.genQuad("par", temp, "RET", "_")
					intermediate.genQuad("call", callId, "_", "_")
				else:
					for i in  range(1,len(param)):
						intermediate.genQuad("par",param[i],"in","_")
					intermediate.genQuad("par",param[0],"RET","_")
					intermediate.genQuad("call", callId, "_", "_")
					counter = 0
					param = []
						
		else:
				line = lexi.getLineAndColumn()
				print("Syntax error: 'function' name is missing at line " + str(line[0]+1)+".") 
				exit()
Example #4
0
def block(blockId = None):
		declarations()
		subprograms()
		if blockId != None:
			symbolTable.setLastFunctionStartQuad(nextQuad())
			intermediate.genQuad("begin_block", blockId, "_", "_")

		statements()
Example #5
0
def inputStat():
		global token
		if token[0] == "Id":
				idToken = token[1]
				intermediate.genQuad("inp", idToken, "_", "_")
				token = lexi.lex()
				return
		else:
				line = lexi.getLineAndColumn()
				print("Syntax error: input is missing at line " + str(line[0]+1)+".") 
				exit()
Example #6
0
def expression():
		global token
		optionalSign()
		t1 = term()                             #first term of expression
		while token[0] == "addOperTk":
				tokenOper = token[1]    #operator (+ or -)
				addOper()
				t2 = term()             #second term of expression
				w = intermediate.newTemp()
				intermediate.genQuad(tokenOper,t1,t2,w)
				t1 = w
				
		return t1
Example #7
0
def whileStat():
		global token
		q = intermediate.nextQuad()
		(bTrue, bFalse) = condition()
		intermediate.backPatch(bTrue, intermediate.nextQuad())
		statements()
		intermediate.genQuad("jump","_","_", q)
		intermediate.backPatch(bFalse,nextQuad())
		if token[0] == "endwhile":
				token = lexi.lex()
				return
		else:
				print("Syntax error: 'endwhile' statement is missing. " ) 
				exit()
Example #8
0
def actualParitem(functionItem, argumentId):
	global token
	global param
	global counter
	global temp
	if token[0] == "in":
		if functionItem.argumentList[argumentId].isIn == False:
			print("Expected 'inout' but found 'in'")
			exit()
		token = lexi.lex()
		actualParId = token[1]
		exp = expression()
		intermediate.genQuad("par", exp, "CV", "_")
		return
	elif token[0] == "inout":
		if functionItem.argumentList[argumentId].isIn == True:
			print("Expected 'in' but found 'inout'")
			exit()
		token = lexi.lex()
		if token[0] == "Id":
			checkUndeclaredIdentifier()
			actualParId = token[1]
			intermediate.genQuad("par", actualParId, "REF", "_")
			token = lexi.lex()
			return
		else:
			line = lexi.getLineAndColumn()
			print("Syntax error: 'Parameter' is missing at line " + str(line[0]+1)+".") 
			exit()
	elif token[0] == "call":
		temp = intermediate.newTemp()
		param.append(temp)
		counter += 1
		token = lexi.lex()
		callId = token[1]
		callStat(callId)
	else:
		line = lexi.getLineAndColumn()
		print("Syntax error: 'in' or 'inout' is missing at line " + str(line[0] + 1) +".") 
		exit()
Example #9
0
def switchStat():
		global token
		exitList = intermediate.emptyList()
		e1 = expression()
		while token[0] != "endswitch": 
				if token[0] == "case":
						token = lexi.lex()
						e2 = expression()
						if token[0] == "upDownTk":

								bTrue = intermediate.makeList(intermediate.nextQuad())    #references ["=", e1, e2, "_"]
								intermediate.genQuad("=", e1, e2, "_")
								bFalse = intermediate.makeList(intermediate.nextQuad())   #references ["jump", "_", "_", "_"]
								intermediate.genQuad("jump", "_", "_", "_")
								intermediate.backPatch(bTrue, intermediate.nextQuad())    # ["=", e1, e2, tells where to go]

								token = lexi.lex()
								statements()

								tList = intermediate.makeList(intermediate.nextQuad())
								intermediate.genQuad("jump", "_", "_", "_")
								exitList = intermediate.merge(exitList, tList)
								intermediate.backPatch(bFalse, intermediate.nextQuad())
						else:
								line = getLineAndColumn()
								print("Syntax error: expected ':' at line " + str(line[0]+1)+".") 
								exit()
				else:
						line = lexi.getLineAndColumn()
						print("Syntax error: 'endswitch' or 'case' statement is missing .") 
						exit()

		intermediate.backPatch(exitList, intermediate.nextQuad())
		token = lexi.lex()
		return
Example #10
0
def boolfactor():
		global token
		if token[0] == "not":
				token = lexi.lex()
				if token[0] == "leftlogicparenthesisTk":
						token = lexi.lex()
						(q1True, q1False) = condition()
						if token[0] != "rightlogicparenthesisTk":
								line = lexi.getLineAndColumn()
								print("Syntax error: ']' statement is missing at line " + str(line[0]+1)+".") 
								exit()

						token = lexi.lex()
						return (q1False,q1True)
				else:
						line = lexi.getLineAndColumn()
						print("Syntax error: '[' statement is missing at line " + str(line[0]+1)+".") 
						exit()

		elif token[0] == "leftlogicparenthesisTk":
				token = lexi.lex()
				(q1True, q1False) = condition()
				if token[0] != "rightlogicparenthesisTk":
						line = lexi.getLineAndColumn()
						print("Syntax error: ']' statement is missing at line " + str(line[0]+1)+".") 
						exit()
				token = lexi.lex()
				return (q1True, q1False)
		elif token[0] == "true" or token[0] == "false":
				if token[0] =="true":
						q2True = intermediate.makeList(intermediate.nextQuad())
						q2False = intermediate.emptyList()
						intermediate.genQuad("true", "_", "_", "_")
				else:
						q2False = intermediate.makeList(intermediate.nextQuad())
						q2True = intermediate.emptyList()
						intermediate.genQuad("false", "_", "_", "_")
				token = lexi.lex()
				return (q2True, q2False)
		else:
				expression1 = expression()
				relop = relationalOper()
				expression2 = expression()
				rTrue = makeList(intermediate.nextQuad())
				intermediate.genQuad(relop, expression1, expression2, "_")
				rFalse = makeList(intermediate.nextQuad())
				intermediate.genQuad("jump", "_", "_", "_")
				return (rTrue, rFalse)
Example #11
0
def ifStat():   
		global token
		(bTrue, bFalse) = condition()
		if token[0] == "then":
				
				intermediate.backPatch(bTrue, intermediate.nextQuad())
				token = lexi.lex()
				statements()
				ifList = intermediate.makeList(intermediate.nextQuad())
				intermediate.genQuad("jump","_","_","_")
				intermediate.backPatch(bFalse, intermediate.nextQuad())
				elsepart()
				intermediate.backPatch(ifList, intermediate.nextQuad())
				if token[0] == "endif":
						token = lexi.lex()
						return
				else:
						print("Syntax error: 'endif' statement is missing.") 
						exit()
		else:
				line = lexi.getLineAndColumn()
				print("Syntax error: 'then' statement is missing at line " + str(line[0]+1)+".") 
				exit()
Example #12
0
def idTail(identifier):
		global var
		global token
		global funcList
		
		functionItem, _ = symbolTable.findEntity(identifier) 
		a = actualPars(identifier, functionItem)
		if identifier in funcList:
			current_Temp = intermediate.newTemp()
			intermediate.genQuad("par",current_Temp,"RET","_")
			intermediate.genQuad("call",identifier,"_","_")
			intermediate.genQuad(":=",current_Temp,"_",var)
			
		return a
Example #13
0
def assignmentStat(var):
		expr = expression()
		if expr != None: 
			intermediate.genQuad(":=", expr, "_", var)
		return
Example #14
0
def printStat():
		global token
		e = expression()
		intermediate.genQuad("out", e, "_", "_")
		return
Example #15
0
def procorfuncbody(typeOfToken, functionName):
		formalpars()
		block(functionName)
		symbolTable.setLastFunctionFrameLength()
		intermediate.genQuad("end_block", functionName, "_", "_")
		return