nsl / nsl / passes / DebugTypes.py

from nsl import ast

class DebugTypeVisitor(ast.DefaultVisitor):
	def GetContext (self):
		return 0

	def _p(self, ctx, s, **args):
		print (' ' * (ctx * 4), end = '')
		print (s, **args)

	def v_StructureDefinition(self, decl, ctx):
		self._p (ctx, 'struct ' + decl.GetName ())
		for t in decl.GetElements ():
			# Resolve here allows for nested types
			self._p (ctx + 1, t.GetName () + ':' + str(t.GetType ()))

	def _ProcessExpression(self, expr, ctx):
		self._p (ctx, str(expr) + ':' + str(expr.type))
		for e in expr:
			self._ProcessExpression (e, ctx + 1)

	def v_CompoundStatement(self, stmt, ctx):
		for s in stmt:
			self.v_Visit (s, ctx + 1)

	def v_PrimaryExpression(self, expr, ctx):
		self._p(ctx, str(expr) + ':' + str(expr.type))

	def v_ExpressionStatement(self, stmt, ctx):
		self._ProcessExpression(stmt.GetExpression(), ctx)

	def v_Function(self, func, ctx):
		'''Computes the function type and processes all statements.'''
		self._p(ctx, str(func.GetType ()))
		self._p (ctx, 'Arguments')
		for (name, type) in func.GetType ().GetArguments().items ():
			self._p (ctx + 1, name + ':' + str(type))

		print ()
		self.v_Visit (func.GetBody(), ctx)
		print ()

	def v_Shader(self, shd, ctx=None):
		self.v_Function(shd, ctx)

	def v_Program(self, prog, ctx):
		# Must visit types first
		for type in prog.GetTypes ():
			self.v_Visit (type, ctx)
		for decl in prog.GetDeclarations ():
			self.v_Visit (decl, ctx)
		for func in prog.GetFunctions ():
			self.v_Visit (func, ctx)

	def v_Generic(self, node, ctx):
		ast.Visitor.v_Generic (self, node, ctx)

def GetPass():
	import nsl.Pass
	return nsl.Pass.MakePassFromVisitor (DebugTypeVisitor (), 'debug-print-types')
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.