view python/testc3.py @ 148:e5263f74b287

Added c3 language frontend initial parser
author Windel Bouwman
date Fri, 01 Mar 2013 10:24:01 +0100
parents
children 74241ca312cc
line wrap: on
line source

import c3.parser, c3.semantics
from ppci.errors import printError, Diagnostics

testsrc = """
package test;

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

public function int t2(u32 a, u32 b)
{
   return a + b;
   a = 2
}

"""

def printAst(ast):
   print(ast)
   for c in ast.getChildren():
      printAst(c)
   if isinstance(ast, c3.astnodes.Package):
      print('PACK', ast.scope)

def do():
   print('[0] source:')
   print(testsrc)
   print('[1] parsing')
   diag = Diagnostics()
   sema = c3.semantics.Semantics(diag)
   p = c3.parser.Parser(sema, diag)
   
   p.parseSource(testsrc)

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

do()