Exemplo n.º 1
0
	def _DF(self):
		print "DF()"
		self.scope = SymbolScope.LOCAL
		if self.bs._symbols.defined(self.operand.toString()):
			sym = self.bs._symbols.get(self.operand.toString())
			sym.setSymbolType(SymbolType.DEFINED_FUNCTION)
		else:
			sym = Symbol(self.operand.toString(), SymbolType.DEFINED_FUNCTION, SymbolScope.GLOBAL)
			self.bs._symbols.insert(sym)
		sym.setAddress(self.jvm.getPC())
		
		if sym.toString() == "Main":
			print "Main Function Definition"
			struct = Structure(StructureType.MAIN)
		else:
			struct = Structure(StructureType.FUNCTION)
			print "Function Definition"
		self.structureStack.append(struct)
		print self.structureStack
		print len(self.structureStack), ": structure pushed, "
Exemplo n.º 2
0
	def _CA(self):
		print "CA()"
		print "calling a function: ", self.currentOp.toString(), self.operand.toString(), self.nextOp.toString()
		if self.operand.toString() == "write":
			print "calling the write function"
			self.jvm.emit3byte(Opcode.GETSTATIC, 6)
			self.compileExpression()
			self.jvm.emit3byte(Opcode.INVOKEVIRTUAL, 7)
		else:
			if self.bs._symbols.defined(self.operand.toString()) == False:
				print "function not defined"
				sym = Symbol(self.operand.toString(), SymbolType.FORWARD_FUNCTION, self.scope)
				self.bs._symbols.insert(sym)
			
			sym = self.bs._symbols.get(self.operand.toString())
			if sym.getSymbolType() == SymbolType.FORWARD_FUNCTION:
				self.saveForwardReference(sym, self.jvm.getPC())
				self.jvm.emit3byte(Opcode.JSR, 0) #jsr
			elif sym.getSymbolType() == SymbolType.DEFINED_FUNCTION:
				self.jvm.emit3byte(Opcode.JSR, sym.getAddress() - self.jvm.getPC()) #jsr
			else:
				raise CompilerException("Error, call to "+sym.toString()+" does not reference a function, "+self.bs.fln())
Exemplo n.º 3
0
	def _DE(self):
		print "DE()"
		#self.suppressAdvance = True
		print "declaring a variable...", self.currentOp.toString(), self.operand.toString(), self.nextOp.toString()
		if self.bs._symbols.defined(self.operand.toString()):
			self.setError("Error: Multiple Declaration")
			raise CompilerException("Multiple Declaration of variable "+ self.operand.toString() + self.bs.fln())
		#if self.nextOp.toString() == ";":
		#	print "default declaration"
		#	sym = Symbol(self.operand.toString(), SymbolType.VARIABLE, self.scope)
		#	sym.setAddress(self.nextLocation)
		#	self.nexLocation += 1
		#	self.pushConstantInt(0)
		#	self.jvm.chooseOp(Opcode.ISTORE, Opcode.ISTORE_0, sym.getAddress())
		#	self.advance()
		#	self.bs._symbols.insert(sym)
		#	return
		#	
		#while self.nextOp.toString() != ";":
		#	print self.operand.toString(), "SYMBOL!!!"
		#	self.advance()
		#	sym.setAddress(self.nextLocation)
		#	if self.nextOp.toString() == "[":
		#		sym.setSymbolType(SymbolType.ARRAY)
		#		self.advance() # co = '[', no = ']'
		#		self.pushConstant(self.operand)
		#		size = self.operand.toString()
		#		self.jvm.emit2byte(Opcode.NEWARRAY, 10)
		#		sym.setAddress(self.nextLocation)
		#		self.jvm.chooseOp(Opcode.ASTORE, Opcode.ASTORE_0, sym.getAddress())
		#		self.advance() # co = ']', no = ('=' | ';')
		#		if self.nextOp.toString() == "=":
		#			self.advance() # co = '=', no = '{'
		#			if self.nextOp.toString() != "{":
		#				self.setError("Syntax Error: Missing { after = ");
		#			
		#			self.advance() #co = '{', no = ','
		#			arr = []
		#			while self.nextOp.toString() != ";":
		#				print "Array Declaration :", self.operand.toString()
		#				arr.append(self.operand)
		#				self.advance() 
		#			if len(arr) > size:
		#				self.setError("Syntax Error: Array Subscript exceeded")
		#			else:
		#				i = 0
		#				for val in arr:
		#					self.jvm.chooseOp(Opcode.ALOAD, Opcode.ALOAD_0, sym.getAddress())
		#					self.pushConstantInt(i)
		#					self.pushConstant(val)
		#					self.jvm.emit1byte(Opcode.IASTORE)
		#					i += 1							
		#					
		#	else: #if not an array
		#		sym.setSymbolType(SymbolType.VARIABLE)
		#		#sym.setAddress(self.nextLocation)
		#		if self.nextOp.toString() == "=":
		#			self.advance()
		#			self.pushConstant(self.operand)
		#			self.jvm.chooseOp(Opcode.ISTORE, Opcode.ISTORE_0, sym.getAddress())
		#		
		#		print "declared a variable"
		#		
		#		#do variable declaration things
		#	self.bs._symbols.insert(sym)
		#	self.nextLocation += 1
		self.done = False
		while self.done is False:
			#if self.currentOp.toString() == "int":
			#	self.advance()
			if self.nextOp.toString() == ";":
				#declare an uninitialzed variable, then end
				sym = Symbol(self.operand.toString(), SymbolType.VARIABLE, self.scope)
				sym.setAddress(self.nextLocation)
				self.nextLocation += 1
				self.pushConstantInt(0)
				self.jvm.chooseOp(Opcode.ISTORE, Opcode.ISTORE_0, sym.getAddress())
				self.advance()
				self.bs._symbols.insert(sym)
				self.done = True

			elif self.nextOp.toString() == ",":
				#declare this uninitalized variable, then go on to the next one
				if self.currentOp.toString() == "," or self.currentOp.toString() == "int":
					sym = Symbol(self.operand.toString(), SymbolType.VARIABLE, self.scope)
					sym.setAddress(self.nextLocation)
					self.nextLocation += 1
					self.pushConstantInt(0)
					self.jvm.chooseOp(Opcode.ISTORE, Opcode.ISTORE_0, sym.getAddress())
					self.advance()
					self.bs._symbols.insert(sym)
				else:
					self.advance()

			elif self.nextOp.toString() == "[":
				#declare an array, check the nextOp, and continue or end depending
				sym = Symbol(self.operand.toString(), SymbolType.ARRAY, self.scope)
				self.advance() # co = '[', no = ']'
				print self.operand.toString(), "OPERAND"
				self.pushConstant(self.operand)
				size = self.operand.toString()
				print "ARRAY SIZE", size
				self.jvm.emit2byte(Opcode.NEWARRAY, 10)
				sym.setAddress(self.nextLocation)
				self.nextLocation += 1
				self.bs._symbols.insert(sym)
				self.jvm.chooseOp(Opcode.ASTORE, Opcode.ASTORE_0, sym.getAddress())
				self.advance() # co = ']', no = ('=' | ';' | ',')
				if self.nextOp.toString() == "=":
					self.advance() # co = '=', no = '{'
					if self.nextOp.toString() != "{":
						self.setError("Syntax Error: Missing { after = ");
					
					self.advance() #co = '{', no = ','
					
					arr = []
					while self.currentOp.toString() != "}":
						print "Array Declaration :", self.operand.toString()
						arr.append(self.operand)
						self.advance() 
					print self.nextOp.toString(), "NEXT OP ========="
					print "Attempted array declaration list size", len(arr)
					if len(arr) > size:
						self.setError("Syntax Error: Array Subscript exceeded")
 						raise CompilerException("Out-of-Bounds Error, Array subscript exceeded at symbol " + sym.toString() + self.bs.fln())

					else:
						i = 0
						for val in arr:
							print "assigning value", val, "to array"
							self.jvm.chooseOp(Opcode.ALOAD, Opcode.ALOAD_0, sym.getAddress())
							self.pushConstantInt(i)
							self.pushConstant(val)
							self.jvm.emit1byte(Opcode.IASTORE)
							i += 1
				if self.nextOp.toString() == ";":
					self.done = True
				
				print "AFTER DECLARATION, NEXTOP =", self.nextOp.toString()
				
			elif self.nextOp.toString() == "=":
				print "declare an initialized variable"
				sym = Symbol(self.operand.toString(), SymbolType.VARIABLE, self.scope)
				self.advance()
				print self.operand.toString()
				sym.setAddress(self.nextLocation)
				self.nextLocation += 1
				self.pushConstant(self.operand)
				self.bs._symbols.insert(sym)
				self.jvm.chooseOp(Opcode.ISTORE, Opcode.ISTORE_0, sym.getAddress())
				if self.nextOp.toString() == ";":
					self.done = True
				pass
			else:
				#error
				self.done = True
				raise CompilerException("Invalid Assignment Sequence" + self.bs.fln())
				pass

				
		print "OUT OF LOOPS"
		try:
			if self.scope == SymbolScope.GLOBAL and self.nextOp.toString() == "void":
				print "END OF A DECLARATION STATEMENT"
				sym = self.bs._symbols.get("Main")
				self.saveForwardReference(sym, self.jvm.getPC())
				self.jvm.emit3byte(Opcode.GOTO, 0)
		except:			
			pass