Exemplo n.º 1
0
    def test_struct(self):
        a=''' struct Number {
                int a ;
                int b ;
                            } ; '''

        """         struct (root[0])
                       |
                -----------------
                |               |
              Number            {
                                |_____int
                                |      |
                                |      a
                                |
                                |______int
                                        |
                                        b

        """

        root=CParser.parse(a)
        self.assertEqual(root[0].id,'struct')
        Number=root[0].first
        self.assertEqual((Number.id),'StructIdentifier')
        self.assertEqual(valueof(Number),'Number')
        statement=root[0].second
        inta=statement.first[0]
        self.assertEqual(inta.id,'int')
        a=inta.first
        self.assertEqual(valueof(a),'a')
        intb=statement.first[1]
        self.assertEqual(intb.id,'int')
        b=intb.first
        self.assertEqual(valueof(b),'b')
Exemplo n.º 2
0
 def test_type_with_declaration_of_variable_more_than_once_double(self):
     a=''' double a = 1 , b = 2 , c , d = 3 ; '''
     """
                         =
                       /   \
                   double   [1,2,None,3]
                      |
                 [a,b,c,d]
                                             """
     root=CParser.parse(a)
     equal=root[0]
     self.assertEqual(equal.id,'=')
     DOUBLE=equal.first
     self.assertEqual(DOUBLE.id,'double')
     arrayfirst=equal.first
     a=arrayfirst.first[0]
     self.assertEqual(valueof(a),'a')
     b=arrayfirst.first[1]
     self.assertEqual(valueof(b),'b')
     c=arrayfirst.first[2]
     self.assertEqual(valueof(c),'c')
     d=arrayfirst.first[3]
     self.assertEqual(valueof(d),'d')
     arraysecond=root[0].second
     one=arraysecond[0]
     self.assertEqual(valueof(one),'1')
     two=arraysecond[1]
     self.assertEqual(valueof(two),'2')
     NONE=arraysecond[2]
     self.assertIsNone(NONE)
     three=arraysecond[3]
     self.assertEqual(valueof(three),'3')
Exemplo n.º 3
0
    def latertest_typedef_char_alias_with_pointer(self):
        a=''' typedef char * alias [ 80 ]'''

        """                 typedef (root[0])
                                |
                       ----------------------------------
                       |                                |
                       |                               char
                       |                                |
                       |                                *
                       |                                |
                     alias                              ['
                                                        |
                                                        80

                                                                    """
        root=CParser.parse(a)
        self.assertEqual(root[0].id,'typedef')
        alias=root[0].first
        self.assertEqual(valueof(alias),'alias')
        char=root[0].second
        self.assertEqual(char.id,'char')
        pointer=char.first
        self.assertEqual(pointer.id,'*')
        bracket=pointer.first
        self.assertEqual(bracket.id,'[')
        eighty=bracket.first
        self.assertEqual(valueof(eighty),'80')
Exemplo n.º 4
0
 def test_type_with_assign_value_more_than_one_to_array(self):
     a=''' int a [ 8 ] = { 2 , 3 } ; '''
     """
                 =
               /   \
             int    {--2
              |     |--3
              [
             / \
            a   8         """
     root=CParser.parse(a)
     equal=root[0]
     self.assertEqual(equal.id,'=')
     INT=root[0].first
     self.assertEqual(INT.id,'int')
     braket=INT.first
     self.assertEqual(braket.id,'[')
     a=braket.first
     self.assertEqual(valueof(a),'a')
     eight=braket.second
     self.assertEqual(valueof(eight),'8')
     brace=root[0].second
     self.assertEqual(brace.id,'{')
     two=brace.first[0]
     self.assertEqual(valueof(two),'2')
     three=brace.first[1]
     self.assertEqual(valueof(three),'3')
Exemplo n.º 5
0
    def test_string(self):
        a=''' char name [ 80 ] = " hello " ; '''
        """
                            =
                          /   \
                        char   hello
                         |
                         [
                       /   \
                     name   80                                                """

        root=CParser.parse(a)
        equal=root[0]
        self.assertEqual(equal.id,'=')
        char=equal.first
        self.assertEqual(char.id,'char')
        braket=char.first
        self.assertEqual(braket.id,'[')
        name=braket.first
        self.assertEqual(valueof(name),'name')
        eighty=braket.second
        self.assertEqual(valueof(eighty),'80')
        identifier=root[0].second
        self.assertEqual(identifier.id,'(identifier)')
        hello=root[0].second.first
        self.assertEqual(valueof(hello),'hello')
Exemplo n.º 6
0
    def test_integer_with_pointer_with_function(self):
        a='int * ( * ptr ) ;'

        """     int
                 |
                 *
                 |
                 (
                 |
                 *
                 |
                ptr     """

        root=CParser.parse(a)
        self.assertEqual(root[0].id,'int')
        address=root[0].first
        self.assertEqual(address.id,'*')
        self.assertEqual(address.arity,"unary")
        bracket=address.first
        self.assertEqual(bracket.id,'(')
        self.assertEqual(bracket.arity,"grouping")
        address2=bracket.first
        ptr=address2.first
        self.assertEqual(valueof(ptr),'ptr')
        self.assertEqual(ptr.id,'(identifier)')
Exemplo n.º 7
0
    def test_type_with_declaration_of_variable_more_than_once_enum_type(self):
        a='''enum DAY_A
                        {
                            saturday ,
                            sunday
                                        } workday ;
                    enum DAY_A a , b ;'''
        """
                                    (root[1])
                        enum DAY_A
                         /     \
                    [a , b]     {
                                |
                                [-------saturday
                                  |
                                  |
                                  |
                                  |-----sunday

                                                            """
        root=CParser.parse(a)
        enumType=root[1]
        self.assertEqual(enumType.id,'enum DAY_A')
        arrayfirst=enumType.first
        a=arrayfirst[0]
        self.assertEqual(valueof(a),'a')
        b=arrayfirst[1]
        self.assertEqual(valueof(b),'b')
        brace=enumType.second
        self.assertEqual(brace.id,'{')
        saturday=brace.first[0]
        self.assertEqual(valueof(saturday),'saturday')
        sunday=brace.first[1]
        self.assertEqual(valueof(sunday),'sunday')
Exemplo n.º 8
0
    def testnegativeoneplus2plusnegativethree(self):
        a='- 1 + 2  + - 3 ;'
        """         +
                  /   \
                +       -
              /   \     |
             -     2    3
             |
             1"""

        root=CParser.parse(a)
        self.assertEqual(root[0].id,'+')
        self.assertEqual(root[0].arity,'binary')
        plus1=root[0].first
        self.assertEqual(plus1.id,'+')
        self.assertEqual(plus1.arity,'binary')
        negative1=plus1.first
        self.assertEqual(negative1.id,'-')
        self.assertEqual(negative1.arity,'unary')
        one=negative1.first
        self.assertEqual(valueof(one),'1')
        self.assertEqual(one.id,'(literal)')
        two=plus1.second
        self.assertEqual(valueof(two),'2')
        self.assertEqual(two.id,'(literal)')
        negative2=root[0].second
        self.assertEqual(negative2.id,'-')
        self.assertEqual(negative2.arity,'unary')
        three=negative2.first
        self.assertEqual(valueof(three),'3')
        self.assertEqual(three.id,'(literal)')
Exemplo n.º 9
0
    def testxequalwplusyequalz(self):
        a='x = w + y = z ;'
        """     =
               / \
              x   =
                 /  \
                +   z
              /  \
             w   y """

        root=CParser.parse(a)
        self.assertEqual(root[0].id,'=')
        self.assertEqual(root[0].arity,'binary')
        x=root[0].first
        self.assertEqual(valueof(x),'x')
        self.assertEqual(x.id,'(identifier)')
        equal=root[0].second
        self.assertEqual(equal.id,'=')
        self.assertEqual(equal.arity,'binary')
        plus=equal.first
        self.assertEqual(plus.id,'+')
        self.assertEqual(plus.arity,'binary')
        w=plus.first
        self.assertEqual(valueof(w),'w')
        self.assertEqual(w.id,'(identifier)')
        y=plus.second
        self.assertEqual(valueof(y),'y')
        self.assertEqual(y.id,'(identifier)')
        z=equal.second
        self.assertEqual(valueof(z),'z')
        self.assertEqual(z.id,'(identifier)')
Exemplo n.º 10
0
 def test2plus3plus4plus5(self):
       a='2 + 3 + 4 + 5 ;'
       """           +
                   /   \
                  +     5
                /  \
               +     4
             /  \
            2    3"""
       root=CParser.parse(a)
       self.assertEqual(root[0].id,'+')
       self.assertEqual(root[0].arity,'binary')
       plus2=root[0].first
       self.assertEqual(plus2.id,'+')
       self.assertEqual(plus2.arity,'binary')
       plus3=plus2.first
       self.assertEqual(plus3.id,'+')
       self.assertEqual(plus3.arity,'binary')
       two=plus3.first
       self.assertEqual(valueof(two),'2')
       self.assertEqual(two.id,'(literal)')
       three=plus3.second
       self.assertEqual(valueof(three),'3')
       self.assertEqual(three.id,'(literal)')
       four=plus2.second
       self.assertEqual(valueof(four),'4')
       self.assertEqual(four.id,'(literal)')
       five=root[0].second
       self.assertEqual(valueof(five),'5')
       self.assertEqual(five.id,'(literal)')
Exemplo n.º 11
0
 def test2plus3multiply4plus5(self):
     a='2 + 3 * 4 + 5 ;'
     """       +
             /   \
           +      5
         /  \
        2    *
            /  \
           3    4"""
     root=CParser.parse(a)
     self.assertEqual(root[0].id,'+')
     self.assertEqual(root[0].arity,'binary')
     plus2=root[0].first
     self.assertEqual(plus2.id,'+')
     self.assertEqual(plus2.arity,'binary')
     two=plus2.first
     self.assertEqual(valueof(two),'2')
     self.assertEqual(two.id,'(literal)')
     multiply=plus2.second
     self.assertEqual(multiply.id,'*')
     self.assertEqual(multiply.arity,'binary')
     three=multiply.first
     self.assertEqual(valueof(three),'3')
     self.assertEqual(three.id,'(literal)')
     four=multiply.second
     self.assertEqual(valueof(four),'4')
     self.assertEqual(four.id,'(literal)')
     five=root[0].second
     self.assertEqual(valueof(five),'5')
     self.assertEqual(five.id,'(literal)')
Exemplo n.º 12
0
    def test_statement_if_with_condition_only_and_else(self):
        a='if ( y > 4 ) ; else z = 2 ;'
        """      if
               /    \
               (     else
               |        \
               >         =
            /  \        /  \
          y      4     z    2"""

        root=CParser.parse(a)
        self.assertEqual(root[0].id,'if')
        else1=root[0].third
        self.assertEqual(else1.id,'else')
        equal=else1.first
        self.assertEqual(equal.id,'=')
        z=equal.first
        self.assertEqual(valueof(z),'z')
        three=equal.second
        self.assertEqual(valueof(three),'2')
        bracket=root[0].first
        self.assertEqual(bracket.id,'(')
        bigger=bracket.first
        self.assertEqual(bigger.id,'>')
        y=bigger.first
        self.assertEqual(valueof(y),'y')
        four=bigger.second
        self.assertEqual(valueof(four),'4')
Exemplo n.º 13
0
    def test_two_define_statement_with_spaces(self):
        a='''# define Str 10 + (
             # define Strtwo 20 * y'''
        """    # (root[0])
               |
            define
               |
              Str
               |
            ' 10 + ('

               # (root[1])
               |
            define
               |
              Strtwo
               |
            ' 20 * y '      """
        root=CParser.parse(a)
        self.assertEqual(root[0].id,'#')
        define=root[0].second
        self.assertEqual(valueof(define),'define')
        Strsymbol=root[0].first
        self.assertEqual((Strsymbol.id),'(identifier)')
        self.assertEqual(valueof(Strsymbol),'Str')
        statement=Strsymbol.constantidentifiercontent
        self.assertEqual(Strsymbol.constantidentifiercontent,'10 + (')
        self.assertEqual(root[1].id,'#')
        define=root[1].second
        self.assertEqual(valueof(define),'define')
        Strsymbol=root[1].first
        self.assertEqual((Strsymbol.id),'(identifier)')
        self.assertEqual(valueof(Strsymbol),'Strtwo')
        statement=Strsymbol.constantidentifiercontent
        self.assertEqual(Strsymbol.constantidentifiercontent,'20 * y')
Exemplo n.º 14
0
    def test_enum_with_workdays_and_value_assigned(self):
        a=''' enum DAY2
                        {
                            saturday ,
                            sunday = 0  ,
                            monday ,
                            tuesday ,
                            wednesday ,
                            thursday ,
                            friday
                                        } workday ;         '''

        """                    enum (root[0])
                                |
                       ---------------------------------
                       |               |               |
                      DAY2             |               [ __
                                       |                  |- workday
                                       {
                                       |
                                       ----saturday
                                       |
                                       ----sunday = 0
                                       |
                                       ----monday
                                       |
                                       ----tuesday
                                       |
                                       ----wednesday
                                       |
                                       ----thursday
                                       |
                                       ----friday
                                                              """
        root=CParser.parse(a)
        self.assertEqual(root[0].id,'enum')
        DAY=root[0].first
        self.assertEqual(valueof(DAY),'DAY2')
        brace=root[0].second
        self.assertEqual(brace.id,'{')
        saturday=brace.first[0]
        self.assertEqual(valueof(saturday),'saturday')
        equal=brace.first[1]
        self.assertEqual(equal.id,'=')
        sunday=equal.first
        self.assertEqual(valueof(sunday),'sunday')
        zero=equal.second
        self.assertEqual(valueof(zero),'0')
        monday=brace.first[2]
        self.assertEqual(valueof(monday),'monday')
        tuesday=brace.first[3]
        self.assertEqual(valueof(tuesday),'tuesday')
        wednesday=brace.first[4]
        self.assertEqual(valueof(wednesday),'wednesday')
        thursday=brace.first[5]
        self.assertEqual(valueof(thursday),'thursday')
        friday=brace.first[6]
        self.assertEqual(valueof(friday),'friday')
        workday=root[0].third[0]
        self.assertEqual(valueof(workday),'workday')
Exemplo n.º 15
0
    def test_for_loop_without_a_body(self):
        a='for ( x = 0 ; x = 5 ; x ++ ) ;'
        """
              for-------------------
                  |         |      |
                  =         |      ++
                 / \        =      |
                x   0      / \     x
                          x   5

               """
        root=CParser.parse(a)
        self.assertEqual(root[0].id,'for')
        equal=root[0].first
        self.assertEqual(equal.id,'=')
        x=equal.first
        self.assertEqual(valueof(x),'x')
        zero=equal.second
        self.assertEqual(valueof(zero),'0')
        equal2=root[0].second
        self.assertEqual(equal2.id,'=')
        x2=equal2.first
        self.assertEqual(valueof(x2),'x')
        five=equal2.second
        self.assertEqual(valueof(five),'5')
        plusplus=root[0].third
        self.assertEqual(plusplus.id,'++')
        x3=plusplus.first
        self.assertEqual(valueof(x3),'x')
Exemplo n.º 16
0
 def test_cannot_declare_twice(self):
     scope=Scope()
     a="""int a ;
          int a ;"""
     root=CParser.parse(a)
     scope.declareVariable(root[0])
     self.assertRaises(SyntaxError,scope.declareVariable,root[1])
Exemplo n.º 17
0
 def test_do_with_body_statement_follow_by_while(self):
     a = "do {  x ++ ; } while ( x < 1 ) ;"
     """  do
         /  \
        {   while
        |     |
        ++    (
        |     |
        x     <
             / \
            X   1"""
     root = CParser.parse(a)
     self.assertEqual(root[0].id, "do")
     bracket = root[0].first
     self.assertEqual(bracket.id, "{")
     plusplus = bracket.first[0]
     self.assertEqual(plusplus.id, "++")
     x = plusplus.first
     self.assertEqual(valueof(x), "x")
     while1 = root[0].second
     self.assertEqual(while1.id, "while")
     bracket1 = while1.first
     self.assertEqual(bracket1.id, "(")
     smaller = bracket1.first
     self.assertEqual(smaller.id, "<")
     x = smaller.first
     self.assertEqual(valueof(x), "x")
     one = smaller.second
     self.assertEqual(valueof(one), "1")
 def test_equal_without_int_error_interpreter(self):
     a="a = 1 ;"
     """ =
       /   \
      a     1 """
     root=CParser.oneTimeParse(a)
     self.assertRaises(SyntaxError,root[0].interpreter)
 def test_while_loop_None_interpreter(self):
     a="""int x = 0 ;
          while ( x == 0 )  ;
         """
     root=CParser.oneTimeParse(a)
     root[0].interpreter()
     self.assertRaises(SyntaxError,root[1].interpreter)
 def test_braceket_interpreter(self):
     a=" ( 2 + 3 ) * 4 ;"
     """     *
           /    \
          (      4
          |
          +
        /   \
       2     3 """
     root=CParser.oneTimeParse(a)
     value=root[0].interpreter()
     self.assertEqual(value,20)
     bracket=root[0].first
     value=bracket.interpreter()
     self.assertEqual(value,5)
     four=root[0].second
     value=four.interpreter()
     self.assertEqual(value,4)
     plus=bracket.first
     value=plus.interpreter()
     self.assertEqual(value,5)
     two=plus.first
     value=two.interpreter()
     self.assertEqual(value,2)
     three=plus.second
     value=three.interpreter()
     self.assertEqual(value,3)
 def test_do_while_loop_interpreter_2nd(self):
     a="""int x = 0 ;
             do { }
          while ( x < 3 ) ;"""
     root=CParser.oneTimeParse(a)
     root[0].interpreter()
     self.assertRaises(SyntaxError,root[1].interpreter)
 def test_while_infinity_lopp_raise_syntax_interpreter(self):
     a="""int x = 0 ;
          while ( 1 ) x ++ ;
         """
     root=CParser.oneTimeParse(a)
     root[0].interpreter()
     self.assertRaises(SyntaxError,root[1].interpreter)
 def test_int_limit_assign_after_declare_interpreter(self):
     a="""int main ( )
          {
             short int a = 70000 , b = - 70000 ;
             unsigned short int c = 70000 , d = - 70000 ;
             unsigned int e = 5000000000 , f = - 5000000000 ;
             int g = 5000000000 , h = - 5000000000 ;
             long int i = 5000000000 , j = - 5000000000 ;
          }"""
     root=CParser.oneTimeParse(a)
     Runinterpreter(root)
     temp=scope.findVariable('a')
     self.assertEqual(temp[1],32767)
     temp=scope.findVariable('b')
     self.assertEqual(temp[1],-32768)
     temp=scope.findVariable('c')
     self.assertEqual(temp[1],65535)
     temp=scope.findVariable('d')
     self.assertEqual(temp[1],0)
     temp=scope.findVariable('e')
     self.assertEqual(temp[1],4294967295)
     temp=scope.findVariable('f')
     self.assertEqual(temp[1],0)
     temp=scope.findVariable('g')
     self.assertEqual(temp[1],2147483647)
     temp=scope.findVariable('h')
     self.assertEqual(temp[1],-2147483648)
     temp=scope.findVariable('i')
     self.assertEqual(temp[1],2147483647)
     temp=scope.findVariable('j')
     self.assertEqual(temp[1],-2147483648)
Exemplo n.º 24
0
 def test_back_address_switch_statement_with_condition_and_statement_block(self):
     a='''switch ( choice )
         {
             case ' A ' : x = 1 ;
             case ' C ' : y = 2 ; if ( x == 2 ) { w = v ;
             case ' B ' : s = t ; } default : z = 4 ; }  '''
     root=CParser.parse(a)
     bracket=root[0].second
     self.assertEqual(bracket.id,'{')
     listofchoice=bracket.address
     bracket=listofchoice['A'][1]
     listofchoice1=bracket.first
     casea=listofchoice1[0]
     self.assertEqual(casea.id,'case')
     a=casea.first
     self.assertEqual(valueof(a),'A')
     self.assertEqual(a.id,'(identifier)')
     self.assertEqual(bracket.id,'{')
     bracket=listofchoice['B'][1]
     listofchoice1=bracket.first
     equal=listofchoice1[0]
     w=equal.first
     self.assertEqual(valueof(w),'w')
     self.assertEqual(w.id,'(identifier)')
     v=equal.second
     self.assertEqual(valueof(v),'v')
     self.assertEqual(v.id,'(identifier)')
Exemplo n.º 25
0
 def test_find_the__variable_from_diff_scope_with_variables(self):
     scope=Scope()
     a="int a = 3 ;"
     root=CParser.parse(a)
     scope.declareVariable(root[0],int(root[0].second.interpreter()))
     scope.addScope()
     temp=scope.findVariable('a')
     self.assertEqual(temp[1],3)
     self.assertEqual(temp[0][0],symbolTable[root[0].first.id])
     b="double b = 4 ;"
     root=CParser.parse(b)
     scope.declareVariable(root[0],int(root[0].second.interpreter()))
     scope.addScope()
     temp=scope.findVariable('b')
     self.assertEqual(temp[1],4)
     self.assertEqual(temp[0][0],symbolTable[root[0].first.id])
Exemplo n.º 26
0
 def test_add_variable_scope(self):
     scope=Scope()
     a="int b = 2 ;"
     root=CParser.parse(a)
     scope.declareVariable(root[0],int(root[0].second.interpreter()))
     temp=scope.findVariable('b')
     self.assertEqual(temp[1],2)
     self.assertEqual(temp[0][0],symbolTable[root[0].first.id])
 def test_int_a_without_value_interpreter(self):
     a="int a ;"
     """int-a"""
     root=CParser.oneTimeParse(a)
     root[0].interpreter()
     temp=scope.findVariable('a')
     self.assertEqual(temp[0][0],symbolTable['int'])
     self.assertEqual(temp[1],0)
 def test_for_loop_with_declaration_interpreter(self):
     a="""int x ;
          for ( x = 0 ; x < 5 ; x ++ ) ;"""
     root=CParser.oneTimeParse(a)
     Runinterpreter(root)
     temp=scope.findVariable('x')
     self.assertEqual(temp[0][0],symbolTable['int'])
     self.assertEqual(temp[1], 5)
 def test_enum_with_no_variable_interpreter(self):
     a=""" enum DAY {
                         saturday ,
                         sunday
                                     } ; """
     root=CParser.parse(a)
     Runinterpreter(root)
     self.assertEqual(symbolTable['enum DAY'].id,'enum DAY')
Exemplo n.º 30
0
 def test_can_declare_twice_in_diif_scope(self):
     scope=Scope()
     a="""int a ;
          int a ;"""
     root=CParser.parse(a)
     scope.declareVariable(root[0])
     scope.addScope()
     scope.declareVariable(root[1])
Exemplo n.º 31
0
import CParser

parser = CParser.CParser('glfw3.h', processAll=False)
parser.processAll(noCacheWarning=False)


type_to_ctype_map = {
	'char': 'ctypes.c_char',
	'wchar': 'ctypes.c_wchar',
	'unsigned char': 'ctypes.c_ubyte',
	'short': 'ctypes.c_short',
	'short int': 'ctypes.c_short',
	'unsigned short': 'ctypes.c_ushort',
	'unsigned short int': 'ctypes.c_ushort',
	'int': 'ctypes.c_int',
	'unsigned': 'ctypes.c_uint',
	'unsigned int': 'ctypes.c_uint',
	'long': 'ctypes.c_long',
	'long int': 'ctypes.c_long',
	'unsigned long': 'ctypes.c_ulong',
	'unsigned long int': 'ctypes.c_ulong',
	'__int64': 'ctypes.c_longlong',
	'long long': 'ctypes.c_longlong',
	'long long int': 'ctypes.c_longlong',
	'unsigned __int64': 'ctypes.c_ulonglong',
	'unsigned long long': 'ctypes.c_ulonglong',
	'unsigned long long int': 'ctypes.c_ulonglong',
	'float': 'ctypes.c_float',
	'double': 'ctypes.c_double',
	'long double': 'ctypes.c_longdouble'
}
Exemplo n.º 32
0
import CParser
import json
import os
import shutil
from collections import OrderedDict

files = ["../src/gui/widgets/widgets.h",
         "../src/gui/widgets/layouts/layouts.h",
         "../src/common.h",
         "../src/fc/fc.h",
         "../src/fc/conf.h"]

layout_file = "../src/gui/widgets/layouts/layouts.cpp"

p = CParser.CParser(files)

# p.printAll()

sizes = {
    "uint8_t": 1,
    "int8_t": 1,
    "uint16_t": 2,
    "int16_t": 2,
    "uint32_t": 4,
    "int32_t": 4,
    "float": 4,
    "char": 1,
     }

# print sizes