Commits

Anteru committed 4dc37bb

Add lots of string converters; move DebugVisitor into passes and remove duplicate code.

Comments (0)

Files changed (3)

                     visitor.v_Generic (c, ctx)
             elif cn == 'dict' or cn == 'OrderedDict':
                 for c in e.values ():
-                    assert isinstance (c, Node), 'Node child must be of type Node'
+                    assert isinstance (c, Node), 'Node child must be of type Node but was of type {}'.format (c.__class__.__name__)
                     visitor.v_Generic (c, ctx)
             else:
-                assert isinstance (e, Node), 'Node child must be of type Node'
+                assert isinstance (e, Node), 'Node child must be of type Node but was of type {}'.format (e.__class__.__name__)
                 visitor.v_Generic (e, ctx)
 
 class Program (Node):
     def AddType (self, decl):
         self.types [decl.GetName ()] = decl
 
+    def __str__(self):
+        return '''Program ({0} variable(s), {1} function(s), {2} type(s))'''.format(
+            len(self.variables), len(self.functions), len(self.types))
+
 class Expression(Node):
     def __init__(self, children=[]):
         self.children = children
         return self.name
 
     def __str__(self):
-        return 'struct {0} ({1} fields)'.format (self.GetName (), len (self.elements))
+        return 'struct {0} ({1} field(s))'.format (self.GetName (), len (self.elements))
 
     def GetElements (self):
         return self.elements
     def GetShaderType(self):
         return self.shaderType
 
+    def __str__(self):
+        return '''shader: '{0}' ({1} argument(s))'''.format(
+            ShaderTypeToString(self.shaderType), len(self.arguments))
+
 class Statement(Node):
     pass
 
     def GetExpression(self):
         return self.expression
 
+    def __str__(self):
+        return 'Expression'
+
 class CompoundStatement(Statement):
     '''Compound statement consisting of zero or more statements.
     Compound statements also create a new visibility block.'''
         '''Iterate over the statements.'''
         return self.statements.__iter__()
 
+    def __str__(self):
+        return '{0} statement(s)'.format (len(self))
+
 class ReturnStatement(FlowStatement):
     def __init__(self, expression):
         self.expression = expression
     def GetExpression(self):
         return self.expression
 
+    def __str__(self):
+        return 'return ' + str(self.expression)
+
 class DeclarationStatement(Statement):
     def __init__(self, variableDeclarations):
         self.declarations = variableDeclarations
     def _GetChildFields(self):
         return ['declarations']
 
+    def __str__(self):
+        return '{0} declaration(s)'.format(len(self.declarations))
+
 class IfStatement(FlowStatement):
     def __init__(self, cond, true_path, else_path=None):
         self.cond = cond
     def v_Default(self, obj, ctx=None):
         '''Traverse further if possible.'''
         if hasattr (obj, 'Traverse'):
-            obj.Traverse (self, ctx)
-
-class DebugVisitor(Visitor):
-    def GetContext (self):
-        return 0
-
-    def v_Generic(self, obj, ctx=None):
-        Visitor.v_Generic (self, obj, ctx)
-        if hasattr (obj, 'Traverse'):
-            obj.Traverse (self, ctx + 1)
-
-    def v_Default(self, obj, ctx):
-        print (' '*ctx*2, obj.__class__.__name__,)
-        print (' '*(ctx*2 + 4), str (obj))
+            obj.Traverse (self, ctx)

nsl/passes/ComputeTypes.py

     at 0.
     '''
     from .. import Utility
-    # TODO: Check that a swizzle mask only contains xyzw or rgba
-    if (Utility.ContainsAnyOf(mask, 'xyzw')):
-        if (Utility.ContainsAnyOf (mask, 'rgba')):
-            Errors.ERROR_MIXED_SWIZZLE_MASK.Raise ()
-    elif (Utility.ContainsAnyOf(mask, 'rgba')):
-        if (Utility.ContainsAnyOf (mask, 'xyzw')):
-            Errors.ERROR_MIXED_SWIZZLE_MASK.Raise ()
 
     mapping = { 'x' : 0,
      'y' : 1,

nsl/passes/DebugPrint.py

-import nsl.ast
+from nsl import ast
+
+class DebugVisitor(ast.Visitor):
+    def GetContext (self):
+        return 0
+
+    def v_Generic(self, obj, ctx=None):
+        ast.Visitor.v_Generic (self, obj, ctx)
+        if hasattr (obj, 'Traverse'):
+            obj.Traverse (self, ctx + 1)
+
+    def v_Default(self, obj, ctx):
+        print (' '*ctx*2, obj.__class__.__name__)
+        print (' '*(ctx*2 + 4), str (obj))
 
 def GetPass():
     import nsl.Pass
-    return nsl.Pass.MakePassFromVisitor (nsl.ast.DebugVisitor (), 'debug-print')
+    return nsl.Pass.MakePassFromVisitor (DebugVisitor (), 'debug-print')