Beispiel #1
0
 def forloop(self, start, condition, iteration):
     """
     Converts dsl_* objects, start, condition, iteration to src_dsl_* object and
     outputs the top part of a C for loop based on these, including a single indent.
     """
     src_start = self.converter.expr_full_convert(start)
     src_condition = self.converter.expr_full_convert(condition)
     src_iteration = self.converter.expr_full_convert(iteration)
     self.printer_obj.out('for( '+src( src_start )+' ; '+\
                          src( src_condition )+' ; '+\
                          src( src_iteration )+' ) {', endl='' )
     self.indent()
 def forloop(self,start,condition,iteration):
     """
     Converts dsl_* objects, start, condition, iteration to src_dsl_* object and
     outputs the top part of a C for loop based on these, including a single indent.
     """
     src_start     = self.converter.expr_full_convert( start )
     src_condition = self.converter.expr_full_convert( condition )
     src_iteration = self.converter.expr_full_convert( iteration )
     self.printer_obj.out('for( '+src( src_start )+' ; '+\
                          src( src_condition )+' ; '+\
                          src( src_iteration )+' ) {', endl='' )
     self.indent()
Beispiel #3
0
 def ifblock(self, condition):
     """
     Converts dsl_* object condition to src_dsl_* object and
     outputs the top part of a C if block including a single indent.
     """
     src_condition = self.converter.expr_full_convert(condition)
     self.printer_obj.out('if( ' + src(src_condition) + ' ) {', endl='')
     self.indent()
 def ifblock(self,condition):
     """
     Converts dsl_* object condition to src_dsl_* object and
     outputs the top part of a C if block including a single indent.
     """
     src_condition = self.converter.expr_full_convert( condition )
     self.printer_obj.out('if( '+src( src_condition )+' ) {',endl='')
     self.indent()
 def elifblock(self,condition):
     """
     Converts dsl_* object condition to src_dsl_* object and
     outputs the following:
         - close of preceding if or elif block, with single outdent,
         - top part of an elif block including a single indent.
     """
     src_condition = self.converter.expr_full_convert( condition )
     self.endblock()
     self.printer_obj.out('else if( '+src( src_condition )+' ) {',endl='')
     self.indent()
Beispiel #6
0
 def elifblock(self, condition):
     """
     Converts dsl_* object condition to src_dsl_* object and
     outputs the following:
         - close of preceding if or elif block, with single outdent,
         - top part of an elif block including a single indent.
     """
     src_condition = self.converter.expr_full_convert(condition)
     self.endblock()
     self.printer_obj.out('else if( ' + src(src_condition) + ' ) {',
                          endl='')
     self.indent()
 def __str__(self):
     out = []
     out.append( self._name )
     args = self._args
     if len(args) > 0:
         out.append( '( ' )
         for arg in args:
             out.append( src( arg ) )
             out.append( ', ' )
         del out[-1]
         out.append( ' )' )
     else:
         out.append( '()' )
     return ''.join( out )
Beispiel #8
0
 def __str__(self):
     out = []
     out.append(self._name)
     args = self._args
     if len(args) > 0:
         out.append('( ')
         for arg in args:
             out.append(src(arg))
             out.append(', ')
         del out[-1]
         out.append(' )')
     else:
         out.append('()')
     return ''.join(out)
    def returnstmt(self,arg=None):
        """
        Converts arg from dsl_* to src_dsl_* object,if appropriate and outputs a
        return statement. 

        If arg == None, then the return statement has no argument.
        """
        if arg == None:
            self.printer_obj.out('return')
        else:
            if isinstance(arg,dsl_base) or isinstance(arg,general_array):
                src_arg = self.converter.expr_full_convert( arg )
            else:
                src_arg = arg
            self.printer_obj.out('return '+src( src_arg ) )
Beispiel #10
0
    def returnstmt(self, arg=None):
        """
        Converts arg from dsl_* to src_dsl_* object,if appropriate and outputs a
        return statement. 

        If arg == None, then the return statement has no argument.
        """
        if arg == None:
            self.printer_obj.out('return')
        else:
            if isinstance(arg, dsl_base) or isinstance(arg, general_array):
                src_arg = self.converter.expr_full_convert(arg)
            else:
                src_arg = arg
            self.printer_obj.out('return ' + src(src_arg))
 def src(self):
     """
     Returns a string representing the operation for source code output.
     The format of the string is dependent on the syntax of the dsl_op.
     
     Note:
     dsl_binop and dsl_unop are the only dsl_* classes with src() methods.
     These are necessary because generator classes may manipulate trees of
     binary and unary operations, whilst leaving the dsl_binop and dsl_unop
     objects intact. In this case, when src() is called on the expression, 
     the dsl_binop and dsl_unop objects must call src() on their arguments.
     """
     brackets = False
     out      = []
     if isinstance(self.arg(),dsl_binop) :
         if self.arg().op().syntax() == 'infix' :
             brackets = True
     elif isinstance(self.arg(),dsl_unop) :
         if self.arg().op().syntax() == 'prefix' or\
            self.arg().op().syntax() == 'suffix' :
             brackets = True
     if self.op().syntax() == 'prefix' :
         out.append( src( self.op() ) )
         if brackets : out.append( '( ' )
         out.append( src( self.arg() ) )
         if brackets : out.append( ' )' )
     elif self.op().syntax() == 'postfix' :
         if brackets : out.append( '( ' )
         out.append( src( self.arg() ) )
         if brackets : out.append( ' )' )
         out.append( src( self.op() ) )
     elif self.op().syntax() == 'function' :
         out.append( src( self.op() ) )
         out.append( '( ' )
         out.append( src( self.arg() ) )
         out.append( ' )' )
     else:
         raise Exception('Operator syntax '+self.op().syntax()
                 +' not understood for unary operator')
     return ''.join(out)
Beispiel #12
0
 def src(self):
     """
     Returns a string representing the operation for source code output.
     The format of the string is dependent on the syntax of the dsl_op.
     
     Note:
     dsl_binop and dsl_unop are the only dsl_* classes with src() methods.
     These are necessary because generator classes may manipulate trees of
     binary and unary operations, whilst leaving the dsl_binop and dsl_unop
     objects intact. In this case, when src() is called on the expression, 
     the dsl_binop and dsl_unop objects must call src() on their arguments.
     """
     brackets = False
     out = []
     if isinstance(self.arg(), dsl_binop):
         if self.arg().op().syntax() == 'infix':
             brackets = True
     elif isinstance(self.arg(), dsl_unop):
         if self.arg().op().syntax() == 'prefix' or\
            self.arg().op().syntax() == 'suffix' :
             brackets = True
     if self.op().syntax() == 'prefix':
         out.append(src(self.op()))
         if brackets: out.append('( ')
         out.append(src(self.arg()))
         if brackets: out.append(' )')
     elif self.op().syntax() == 'postfix':
         if brackets: out.append('( ')
         out.append(src(self.arg()))
         if brackets: out.append(' )')
         out.append(src(self.op()))
     elif self.op().syntax() == 'function':
         out.append(src(self.op()))
         out.append('( ')
         out.append(src(self.arg()))
         out.append(' )')
     else:
         raise Exception('Operator syntax ' + self.op().syntax() +
                         ' not understood for unary operator')
     return ''.join(out)
Beispiel #13
0
 def __getitem__(self, key):
     return dsl_integer_index(src(self.name()), self._angmom[key])
    def src(self):
        """Returns a string representing the operation for source code output.
        The format of the string is dependent on the syntax of the dsl_op,
        the precedence and associativity of the op associated with this dsl_binop, 
        as well as the precedence and associativity of dsl_op objects associated with
        the left or right arguments (if these are dsl_binop objects). Whether brackets
        are added is determined by operator precedence and associativity as defined
        for the dsl_op objects.

        Note:
        dsl_binop and dsl_unop are the only dsl_* classes with src() methods.
        These are necessary because generator classes may manipulate trees of
        binary and unary operations, whilst leaving the dsl_binop and dsl_unop
        objects intact. In this case, when src() is called on the expression, 
        the dsl_binop and dsl_unop objects must call src() on their arguments.
        """

        left_brackets  = False
        right_brackets = False
        out = []
        # Only include brackets where absolutely necessary:
        if isinstance(self.left(),dsl_binop) and\
                self.left().op().syntax() == 'infix':
           if self.left().op().prec() > self.op().prec() :
               left_brackets = True
           if self.left().op().prec() == self.op().prec() and\
                   self.op().assoc() == 'right' :
               left_brackets = True
        if isinstance(self.right(),dsl_binop) and\
                self._right.op().syntax() == 'infix':
           if self._right.op().prec() > self.op().prec() :
                right_brackets = True
           if self._right.op().prec() == self.op().prec() and\
                   self.op().assoc() == 'left' :
                right_brackets = True
        if isinstance(self.left(),dsl_unop) and\
                ( self._left.op().syntax() == 'prefix' or\
                  self._left.op().syntax() == 'postfix' ) :
                    left_brackets = True
        if isinstance(self.right(),dsl_unop) and\
                ( self._right.op().syntax() == 'prefix' or\
                  self._right.op().syntax() == 'postfix' ) :
                    right_brackets = True
        if isinstance(self.left(),dsl_unop) and\
                ( self._left.op().syntax() == 'prefix' or\
                  self._left.op().syntax() == 'postfix' ) :
                    left_brackets = True
        if isinstance(self.right(),dsl_unop) and\
                ( self._right.op().syntax() == 'prefix' or\
                  self._right.op().syntax() == 'postfix' ) :
                    right_brackets = True
        if isinstance(self.left(),numbers.Real):
            if self.left() < 0:
                left_brackets = True # unary negative part of number
        if isinstance(self.right(),numbers.Real):
            if self.right() < 0:
                right_brackets = True # unary negative part of number
        # Include brackets always
#        if isinstance(self._left,dsl_binop) : left_brackets = True
#        if isinstance(self._right,dsl_binop): right_brackets = True
        if self.op().syntax() == 'infix' :
            if left_brackets : out.append('( ')
            out.append( src( self.left() ) )
            if left_brackets : out.append(' )')
            out.append( src( self.op() ) )
            if right_brackets : out.append('( ')
            out.append( src( self.right() ) )
            if right_brackets : out.append(' )')
        elif self.op().syntax() == 'function' :
            out.append( src( self.op() ) )
            out.append( '( ' )
            out.append( src( self.left() ) )
            out.append( ', ' )
            out.append( src( self.right() ) ) 
            out.append( ' )' )
        else :
            raise Exception('Operator syntax '+self.op().syntax()
                    +' not understood for binary operator')
        return ''.join(out)
Beispiel #15
0
 def __getitem__(self, key):
     return dsl_scalar(src(self.name()) + '[' + src(key) + ']',
                       component_of=self)
 def __getitem__(self,key):
     return dsl_pointer( src( self.name() )+'[ '+src(key)+' ]' ) 
Beispiel #17
0
 def __getitem__(self, key):
     return dsl_pointer(src(self.name()) + '[ ' + src(key) + ' ]')
Beispiel #18
0
    def src(self):
        """Returns a string representing the operation for source code output.
        The format of the string is dependent on the syntax of the dsl_op,
        the precedence and associativity of the op associated with this dsl_binop, 
        as well as the precedence and associativity of dsl_op objects associated with
        the left or right arguments (if these are dsl_binop objects). Whether brackets
        are added is determined by operator precedence and associativity as defined
        for the dsl_op objects.

        Note:
        dsl_binop and dsl_unop are the only dsl_* classes with src() methods.
        These are necessary because generator classes may manipulate trees of
        binary and unary operations, whilst leaving the dsl_binop and dsl_unop
        objects intact. In this case, when src() is called on the expression, 
        the dsl_binop and dsl_unop objects must call src() on their arguments.
        """

        left_brackets = False
        right_brackets = False
        out = []
        # Only include brackets where absolutely necessary:
        if isinstance(self.left(),dsl_binop) and\
                self.left().op().syntax() == 'infix':
            if self.left().op().prec() > self.op().prec():
                left_brackets = True
            if self.left().op().prec() == self.op().prec() and\
                    self.op().assoc() == 'right' :
                left_brackets = True
        if isinstance(self.right(),dsl_binop) and\
                self._right.op().syntax() == 'infix':
            if self._right.op().prec() > self.op().prec():
                right_brackets = True
            if self._right.op().prec() == self.op().prec() and\
                    self.op().assoc() == 'left' :
                right_brackets = True
        if isinstance(self.left(),dsl_unop) and\
                ( self._left.op().syntax() == 'prefix' or\
                  self._left.op().syntax() == 'postfix' ) :
            left_brackets = True
        if isinstance(self.right(),dsl_unop) and\
                ( self._right.op().syntax() == 'prefix' or\
                  self._right.op().syntax() == 'postfix' ) :
            right_brackets = True
        if isinstance(self.left(),dsl_unop) and\
                ( self._left.op().syntax() == 'prefix' or\
                  self._left.op().syntax() == 'postfix' ) :
            left_brackets = True
        if isinstance(self.right(),dsl_unop) and\
                ( self._right.op().syntax() == 'prefix' or\
                  self._right.op().syntax() == 'postfix' ) :
            right_brackets = True
        if isinstance(self.left(), numbers.Real):
            if self.left() < 0:
                left_brackets = True  # unary negative part of number
        if isinstance(self.right(), numbers.Real):
            if self.right() < 0:
                right_brackets = True  # unary negative part of number
        # Include brackets always


#        if isinstance(self._left,dsl_binop) : left_brackets = True
#        if isinstance(self._right,dsl_binop): right_brackets = True
        if self.op().syntax() == 'infix':
            if left_brackets: out.append('( ')
            out.append(src(self.left()))
            if left_brackets: out.append(' )')
            out.append(src(self.op()))
            if right_brackets: out.append('( ')
            out.append(src(self.right()))
            if right_brackets: out.append(' )')
        elif self.op().syntax() == 'function':
            out.append(src(self.op()))
            out.append('( ')
            out.append(src(self.left()))
            out.append(', ')
            out.append(src(self.right()))
            out.append(' )')
        else:
            raise Exception('Operator syntax ' + self.op().syntax() +
                            ' not understood for binary operator')
        return ''.join(out)
 def __getitem__(self,key):
     return dsl_scalar( src( self.name() )+'['+src(key)+']', component_of = self) 
 def __getitem__(self,key):
     return dsl_integer_index( src( self.name() ), self._angmom[key] )