view python/runtests.py @ 222:c3f1ce8b638f

Fixup of parser
author Windel Bouwman
date Tue, 09 Jul 2013 17:36:31 +0200
parents 6b2bec5653f1
children
line wrap: on
line source

#!/usr/bin/python

import unittest
import os

from compiler.compiler import Compiler
from compiler.errors import CompilerException, printError
from compiler import lexer
from compiler.parser import Parser
from compiler import assembler
from compiler.codegenerator import CodeGenerator
from project import Project

class CompilerTestCase(unittest.TestCase):
   """ test methods start with 'test*' """
   def testSource1(self):
      source = """
      module lcfos;
      var  
        a : integer;

      procedure putchar(num : integer);
      begin
      end putchar;

      procedure WriteNum( num: integer);
        var 
          d, base :  integer;
          dgt : integer;
        begin
          d := 1;
          base := 10;
          while num div d >= base do
            d := d * base
          end;
          while d <> 0 do
             dgt := num div d;
             num := num mod d;
             d   := d div base;
             putchar(48 + dgt)
           end
        end WriteNum;

      begin
        a := 1;
        while a < 26
         do
           putchar(65+a);
           a := a * 2
         end;
      end lcfos.
      """
      pc = Compiler()
      pc.compilesource(source)
   def testSource2(self):
      source = """
      module lcfos;
      var  
        a, b : integer;
        arr: array 30 of integer;
        arr2: array 10, 12 of integer;
        procedure t2*() : integer;
        begin
        a := 2;
        while a < 5 do
           b := arr[a-1] + arr[a-2];
           arr2[a,2] := b;
           arr2[a,3] := arr2[a,2] + arr2[a,2]*3 + b;
           arr[a] := b;
           a := a  + 1;
         end;
         return b
        end t2;
        begin
         b := 12;
         arr[0] := 1;
         arr[1] := 1;
      end lcfos.
      """
      pc = Compiler()
      mod = pc.compilesource(source)
   def testSource5(self):
      source = """
      module lcfos;
      procedure WriteLn() : integer;
        const zzz = 13;
        var
          a, b, c: integer;
        begin
         a := 2;
         b := 7;
         c := 10 * a + b*10*a;
         return c
        end WriteLn;
      begin  end lcfos.
      """
      pc = Compiler()
      pc.compilesource(source)
   def tstForStatement(self):
      source = """
      module fortest;
      var  
        a,b,c : integer;
      begin
         c := 0;
         for a := 1 to 10 by 1 do
            b := a + 15;
            c := c + b * a;
         end;
      end fortest.
      """
      pc = Compiler()
      pc.compilesource(source)
   def testSourceIfAndWhilePattern(self):
      source = """
      module lcfos;
      procedure WriteLn() : integer;
        const zzz = 13;
        var
          a, b, c: integer;
        begin
         a := 1;
         b := 2;
         if a * 3 > b then
            c := 10*a + b*10*a*a*a*b;
         else
            c := 13;
         end;
         while a < 101 do
            a := a + 1;
            c := c + 2;
         end;
         return c
        end WriteLn;
      begin end lcfos.
      """
      pc = Compiler()
      pc.compilesource(source)

   def testPattern1(self):
      """ Test if expression can be compiled into byte code """
      src = "12*13+33-12*2*3"
      tokens = lexer.tokenize(src)
      ast = Parser(tokens).parseExpression()
      code = CodeGenerator().genexprcode(ast)

   def testProject(self):
      p = Project('test.xml', isnew=True)
      p.name = "Test project"
      p.files.append('main.mod')
      p.files.append('test.mod')
      p.save('test.xml')

      q = Project('test.xml')

      assert(p.name == q.name)
      assert(p.files == q.files)
      # TODO: remove test.xml test file
      os.remove('test.xml')

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