view python/testc3.py @ 157:8f3924b6076e

Added some code generator things
author Windel Bouwman
date Sun, 03 Mar 2013 18:14:35 +0100
parents b28a11c01dbe
children 9683a4cd848f
line wrap: on
line source

import c3, time, ppci, x86

testsrc = """
package test;

var u32 c, d;

function void test1() 
{
    var u32 b;
    var int a = 10;
    b = 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(u32 a, u32 b)
{
   if (a > 0)
   {
      a = 2 + t2(a - 1);
   }

   return a + b;
}

var int hahaa = 23 * 2;


"""

def printAst(ast, indent=''):
   print(indent + str(ast))
   if isinstance(ast, c3.astnodes.Package):
      for s in ast.scope:
         printAst(s, indent + '  ')
   if isinstance(ast, c3.astnodes.Function):
      for s in ast.scope:
         printAst(s, indent + '  ')
   for c in ast.getChildren():
      printAst(c, indent + '  ')

def c3compile(src, diag):
   print('[0] source:')
   #print(src)
   print('[1] parsing')
   sema = c3.Semantics(diag)
   p = c3.Parser(sema, diag)
   tc = c3.TypeChecker(diag)
   al = c3.Analyzer(diag)
   cg = c3.CodeGenerator()
   x86gen = x86.X86CodeGen(diag)
   t1 = time.time()
   p.parseSource(src)
   t2 = time.time() 
   print('parsetime: {0} [s]'.format(t2 - t1))
   t2 = time.time() 
   tc.checkPackage(sema.mod)
   t3 = time.time() 
   print('checktime: {0} [s]'.format(t3 - t2))
   print('{0} errors'.format(len(diag.diags)))

   for d in diag.diags:
      print('ERROR:')
      ppci.printError(testsrc, d)
   print('[2] ast:')
   #printAst(sema.mod)

   ok = len(diag.diags) == 0
   if ok:
      print('Generating code')
      i = cg.gencode(sema.mod)

      print(i)
      print(i.Globals)
      x86gen.genBin(i)
   else:
      print('Not generating code')
   
def do():
   diag = ppci.DiagnosticsManager()
   c3compile(testsrc, diag)

do()