view python/testc3.py @ 232:e621e3ba78d2

Added left shift instruction
author Windel Bouwman
date Sun, 14 Jul 2013 11:50:58 +0200
parents 521567d17388
children ef683881c64e
line wrap: on
line source

import c3
import time, ppci, x86, ir
import unittest
import glob

testsrc = """package test;

/*
 demo of the source that is correct :)
*/ 
var int c, d;
var double e;
var int f;

const int A = 1337;

function void test1()
{
    var int bdd;
    var int a = 10;
    bdd = 20;
    var int buf;
    var int i;
    i = 2;
    var int zero = i - 2;
    if (i > 1)
    {
       buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1;
    }
    else
    {
      ;;;
    }

    t2(2, 3);
}

function int t2(int a, int b)
{
   if (a > 0)
   {
      a = 2 + t2(a - 1, 10);
   }

   return a + b;
}

var int a, b;

function int t3(int aap, int blah)
{
   if (a > blah and blah < 45 + 33 or 33 > aap or 6 > 2 and true)
   {
      a = 2 + t2(a - 1, 0);
   }

   return a + b;
}

var int hahaa = 23 * 2;


"""

class testLexer(unittest.TestCase):
    def testUnexpectedCharacter(self):
        snippet = """ var s \u6c34 """
        with self.assertRaises(ppci.CompilerError):
            list(c3.lexer.tokenize(snippet))

    def testBlockComment(self):
        snippet = """
          /* Demo */
          var int x = 0;
        """
        toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
        self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks)

    def testBlockCommentMultiLine(self):
        snippet = """
          /* Demo
          bla1
          bla2
          */
          var int x = 0;
        """
        toks = ['var', 'ID', 'ID', '=', 'NUMBER', ';', 'END']
        self.assertSequenceEqual([tok.typ for tok in c3.lexer.tokenize(snippet)], toks)

class testBuilder(unittest.TestCase):
    def setUp(self):
        self.diag = ppci.DiagnosticsManager()
        self.builder = c3.Builder(self.diag)
        self.diag.clear()

    def testSrc(self):
        self.expectOK(testsrc)

    def expectErrors(self, snippet, rows):
        """ Helper to test for expected errors on rows """
        ircode = self.builder.build(snippet)
        actualErrors = [err.row for err in self.diag.diags]
        if rows != actualErrors:
            self.diag.printErrors(snippet)
        self.assertSequenceEqual(rows, actualErrors)
        self.assertFalse(ircode)

    def expectOK(self, snippet):
        ircode = self.builder.build(snippet)
        if not ircode:
            self.diag.printErrors(snippet)
        self.assertTrue(ircode)
        return ircode

    def expectIR(self, snippet, ir_out):
        ircode = self.builder.build(snippet)
        if not ircode:
            self.diag.printErrors(snippet)
        self.assertTrue(ircode)
        actual_ins = [str(i) for i in ircode.Instructions]
        expected_ins = [i.strip() for i in ir_out.split('\n')]
        self.assertSequenceEqual(expected_ins, actual_ins)
        return ircode

    def testFunctArgs(self):
      snippet = """
         package testargs;
         function void t2(int a, double b)
         {
            t2(2, 2);
            t2(2);
            t2(1, 1.2);
         }
      """
      self.expectErrors(snippet, [5, 6])

    def testExpressions(self):
      snippet = """
         package test;
         function void t(int a, double b)
         {
            var int a2;
            var bool c;

            a2 = b * a;
            c = a;
            c = b > 1;
         }
      """
      self.expectErrors(snippet, [8, 9, 10])

    def testExpression1(self):
      snippet = """
         package testexpr1;
         function void t()
         {
            var int a, b, c;
            a = 1;
            b = a * 2 + a * a;
            c = b * a - 3;
         }
      """
      block_code = """ a0 = alloc
        b1 = alloc
        c2 = alloc
        t3 = 1
        [a0] = t3
        t4 = [a0]
        t5 = 2
        mul6 = t4 * t5
        t7 = [a0]
        t8 = [a0]
        mul9 = t7 * t8
        add10 = mul6 + mul9
        [b1] = add10
        t11 = [b1]
        t12 = [a0]
        mul13 = t11 * t12
        t14 = 3
        sub15 = mul13 - t14
        [c2] = sub15
      ret """
      self.expectIR(snippet, block_code)

    def testEmpty(self):
      snippet = """
      package A
      """
      self.expectErrors(snippet, [3])

    def testEmpty2(self):
      snippet = ""
      self.expectErrors(snippet, [1])

    def testRedefine(self):
      snippet = """
      package test;
      var int a;
      var int b;
      var int a;
      """
      self.expectErrors(snippet, [5])

    def testWhile(self):
      snippet = """
      package tstwhile;
      var int a;
      function void t()
      {
         var int i = 0;
         while (i < 1054)
         {
            i = i + 3;
            a = a + i;
         }

         while(true)
         {
         }

         while(false)
         {
         }
      }
      """
      self.expectOK(snippet)

    def testIf(self):
        snippet = """
      package tstIFF;
      function void t(int b)
      {
         var int a;
         a = 2;
         if (a > b)
         {
            if (a > 1337)
            {
               b = 2;
            }
         }
         else
         {
            b = 1;
         }

         return b;
      }
        """
        self.expectOK(snippet)

    def testTypeDef(self):
        snippet = """
         package testtypedef;
         type int my_int;
         function void t()
         {
            var my_int a;
            var int b;
            a = 2;
            b = a + 2;
         }
        """
        self.expectOK(snippet)

    def testLocalVariable(self):
        snippet = """
         package testlocalvar;
         function void t()
         {
            var int a, b;
            a = 2;
            b = a + 2;
         }
        """
        block_code = """a0 = alloc
         b1 = alloc
         t2 = 2
         [a0] = t2
         t3 = [a0]
         t4 = 2
         add5 = t3 + t4
         [b1] = add5
         ret  """
        self.expectIR(snippet, block_code)

    def testStruct1(self):
        snippet = """
         package teststruct1;
         function void t()
         {
            var struct {int x, y;} a;
            a.x = 2;
            a.y = a.x + 2;
         }
        """
        block_code = """a0 = alloc
         t1 = 2
         off_x2 = 0
         adr_x3 = a0 + off_x2
         [adr_x3] = t1
         off_x4 = 0
         adr_x5 = a0 + off_x4
         t6 = [adr_x5]
         t7 = 2
         add8 = t6 + t7
         off_y9 = 4
         adr_y10 = a0 + off_y9
         [adr_y10] = add8
         ret  """
        self.expectIR(snippet, block_code)

    def testPointerType1(self):
        snippet = """
         package testpointer1;
         var int* pa;
         function void t()
         {
            var int a;
            pa = &a;
            *pa = 22;
         }
        """
        self.expectOK(snippet)

    def testPointerType(self):
        snippet = """
         package testpointer;
         var int* pa, pb;
         function void t(int a, double b)
         {
            pa = &a;
            pb = pa;
            *pa = 22;
         }
        """
        self.expectOK(snippet)

    def testPointerTypeInCorrect(self):
        snippet = """
         package testpointerincorrect;
         var int* pa;
         function void t(int a, double b)
         {
            pa = 2; // type conflict
            pa = &a;
            pa = &2;
            &a = pa; // No valid lvalue
            **pa = 22; // Cannot deref int
         }
        """
        self.expectErrors(snippet, [6, 9, 10])

    def testPointerTypeIr(self):
        snippet = """
         package testptr_ir;
         function void t()
         {
            var int* a;
            a = cast<int*>(40);
            *a = 2;
         }
        """
        block_code = """a0 = alloc
         t1 = 40
         [a0] = t1
         t2 = 2
         deref3 = [a0]
         [deref3] = t2
         ret  """
        self.expectIR(snippet, block_code)

    def testPointerTypeIr2(self):
        snippet = """
         package testptr_ir;
         type struct {int x,y;}* gpio;
         function void t()
         {
            var gpio a;
            a = cast<gpio>(40);
            a->x = 2;
            a->y = a->x - 14;
         }
        """
        block_code = """a0 = alloc
         t1 = 40
         [a0] = t1
         t2 = 2
         deref3 = [a0]
         off_x4 = 0
         adr_x5 = deref3 + off_x4
         [adr_x5] = t2
         deref6 = [a0]
         off_x7 = 0
         adr_x8 = deref6 + off_x7
         t9 = [adr_x8]
         t10 = 14
         sub11 = t9 - t10
         deref12 = [a0]
         off_y13 = 4
         adr_y14 = deref12 + off_y13
         [adr_y14] = sub11
         ret  """
        self.expectIR(snippet, block_code)

    def testWrongCast(self):
        snippet = """
         package testptr_ir;
         type struct {int x,y;}* gpio;
         function void t()
         {
            var gpio a;
            *cast<gpio>(*a);
         }
        """
        # TODO: remove the duplicate error:
        self.expectErrors(snippet, [7, 7])

    def testComplexType(self):
        snippet = """
         package testpointer;
         type int my_int;

         type struct {
          int x, y;
         } point;

         type struct {
           int mem1;
           int memb2;
           point P1;
         } my_struct;

         type my_struct* my_sptr;
         var int* pa;

         function void t(int a, int b, my_sptr x)
         {
            var my_struct *msp;

            var my_struct u, v;
            var point *pt;

            pt = &msp->P1;
            msp = x;
            *pa = 22 + u.mem1 * v.memb2 - u.P1.x;
            x->memb2 = *pa + a * b;

            msp->P1.x = a * x->P1.y;
         }
        """
        self.expectOK(snippet)

    def testExamples(self):
        """ Test all examples in the c3/examples directory """
        example_filenames = glob.glob('./c3/examples/*.c3')
        for filename in example_filenames:
            with open(filename, 'r') as f:
                src = f.read()
            self.expectOK(src)

    def test2(self):
        # testsrc2 is valid code:
        snippet = """
        package test2;

        function void tst()
        {
           var int a, b;
           a = 2 * 33 - 12;
           b = a * 2 + 13;
           a = b + a;
           if (a > b and b == 3)
           {
              var int x = a;
              x = b * 2 - a;
              a = x*x;
           }
           else
           {
              a = b + a;
           }
        }

        """
        ircode = self.expectOK(snippet)
        self.assertEqual(1, len(ircode.Functions))

if __name__ == '__main__':
   unittest.main()