Esempio n. 1
0
	def __init__(self, project, language):

		# The parent project
		self.project = project

		# Create the root scope
		self.root = WittyRoot(self)

		# All the types by name » variable
		self.types = {}

		# Files
		self.files = {}

		# The language
		self.language = language

		self.reset()
Esempio n. 2
0
class Intel:

	def __init__(self, project, language):

		# The parent project
		self.project = project

		# Create the root scope
		self.root = WittyRoot(self)

		# All the types by name » variable
		self.types = {}

		# Files
		self.files = {}

		# The language
		self.language = language

		self.reset()

	## Reset all the class variables
	def reset(self):

		# The scopes
		self.scopes = []

		# The scopes by filename
		self.scopesByFilename = {}

		# All the variables, no matter the scope
		self.variables = []

		# Globals
		self.globals = []

		# Also reset root (it'll add itself to the scopes)
		self.root.resetIntel()

	# Called after every parse, so every save
	def postParse(self):

		# Reset everything
		# @todo: we could make it only reset the currently saved file,
		# but I haven't noticed any speed problems yet, so I'll do it later
		self.reset()
		info('Witty data has been reset, processing data ...')

		# Begin the real work
		for filename, wittyFile in self.files.items():

			pr('Processing ' + filename)

			# Prepare all the scopes
			# Here, we assume the file itself is also a scope
			# That's kind-of true for node.js, but false for javascript
			fileScope = self.root.addChildScope(wittyFile)
			fileScope.setName(filename)
			fileScope.makeFileScope(True)

			# Make a temporary map of the scopes inside this file
			scopeMap = {1: fileScope}

			# Loop over every scope
			for scope in wittyFile.scopes:

				# Skip the first 2 scopes
				if scope['id'] < 2:
					continue

				# Get this scope's parent scope
				parentScope = scopeMap[scope['parent']]
				
				newScope = parentScope.addChildScope()
				newScope.setName(scope['name'])
				newScope.setIdInFile(scope['id'])

				scopeMap[scope['id']] = newScope

			#for statement in wittyFile.statements:
			#	pr('Adding statement ' + statement.typeName + ' from line ' + str(statement.lineNr) + ' to scope ' + str(statement.scopeId))

			for statement in wittyFile.statements:
				# Get the statement's scope
				statementScope = scopeMap[statement.scopeId]
				statementScope.addVariable(statement)

			for scope in wittyFile.scopes:
				if scope['id'] == 0:
					targetScope = self.root
				else:
					targetScope = scopeMap[scope['id']]

				for name, varinfo in scope['variables'].items():
					targetScope.addVariable(False, varinfo)

				wf.log(scope, 'witty-' + self.language + '-simplescopes', True)

			for i, scope in scopeMap.items():
				wf.log(scope, 'witty-' + self.language + '-WTScopes')

			self.registerTypes()

		# for name, file in self.files.items():
		# 	wf.log('\n\nFILENAME: "' + name + '"', 'witty-final-variables')
		# 	wf.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n'*3, 'witty-final-variables')
			
		for scope in self.scopes:
			wf.log('\n\n>>\nScope: ' + str(scope.name), 'witty-' + self.language + '-final-variables')
			wf.log((('>>>'*20) + '\n')*2, 'witty-' + self.language + '-final-variables')
			wf.log(scope.variables, 'witty-' + self.language + '-final-variables', True)



	## Register all the types
	#  @param   self        The object pointer
	def registerTypes(self):

		# Reset the types
		self.types = {}

		# Register all the new types
		for variable in self.variables:

			if variable.statement and variable.statement.hasAttribute('typename'):

				# Register them for every name given
				for name in variable.statement.name:
					# Only add not-already-existing types
					if not name in self.types:
						# @todo: apparently names like 'age) {' are still captured!
						# That should be fixed (but not here)
						self.types[name] = variable

	## Add a WittyVariable to the given scope without making a fuss
	#  @param   self        The object pointer
	#  @param   statement   A WittyStatement
	#  @param   variable    The variable
	#  
	#  @returns newVar
	def createEmptyVariable(self):

		# Create the new variable
		newVar = WittyVariable()

		# Set the id
		newVar.id = len(self.variables)

		# Store it among ALL the variables
		self.variables.append(newVar)

		return newVar

	## Get the scope from a specific file
	#  @param   self        The object pointer
	#  @param   filename    The filename the scope should be in
	#  @param   scopename   The 'name' of the scope (fileline)
	def getScope(self, filename, scopename):
		if not filename in self.scopesByFilename:
			wf.warn('File "' + filename + '" was not found while looking for scope "' + scopename + '"')
			return False
		else:
			pr('Filename scopes:')
			for scope in self.scopesByFilename[filename]:
				pr(scope.__dict__)
				if scope.name == scopename:
					return scope

	## Register the scope
	def registerScope(self, scope):

		# Get the new id for this scope
		scope.id = len(self.scopes)

		# And now add this scope to the project
		self.scopes.append(scope)

		# And add it to the scopes by filename
		if not scope.parentFile.name in self.scopesByFilename:
			self.scopesByFilename[scope.parentFile.name] = []

		self.scopesByFilename[scope.parentFile.name].append(scope)