view python/testc3.py @ 163:8104fc8b5e90

Added visitor to c3
author Windel Bouwman
date Mon, 18 Mar 2013 20:13:57 +0100
parents d8c735dc31f9
children 598d3888a11c
line wrap: on
line source

import c3
import time, ppci, x86, ir

testsrc = """package test;

var u32 c, d;
var double e;
var int f;

const int A = 1337;

function void test1() 
{
    var u32 bdd;
    var int a = 10;
    bd = 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 c3compile(src, diag):
   print('[0] source:')
   print(src)
   print('[1] parsing')
   # Structures:
   sema = c3.Semantics(diag)
   p = c3.Parser(sema, diag)
   tc = c3.TypeChecker(diag)
   al = c3.Analyzer(diag)
   cg = c3.CodeGenerator()
   ap = c3.AstPrinter()

   x86gen = x86.X86CodeGen(diag)
   p.parseSource(src)
   ok = len(diag.diags) == 0
   if not ok:
      return
   al.analyzePackage(sema.mod)
   tc.checkPackage(sema.mod)
   print('{0} errors'.format(len(diag.diags)))

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

   #printAst(sema.mod)

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

      print('generating x86 code')

      x86gen.genBin(i)

      with open('dummydummy.asm', 'w') as f:
         f.write('bits 64\n')
         for a in x86gen.asm:
            print(a)
            f.write(str(a) + '\n')
   else:
      print('Not generating code')
   
def do():
   diag = ppci.DiagnosticsManager()
   c3compile(testsrc, diag)

do()