comparison 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
comparison
equal deleted inserted replaced
162:d8c735dc31f9 163:8104fc8b5e90
2 import time, ppci, x86, ir 2 import time, ppci, x86, ir
3 3
4 testsrc = """package test; 4 testsrc = """package test;
5 5
6 var u32 c, d; 6 var u32 c, d;
7 var double e;
8 var int f;
9
10 const int A = 1337;
7 11
8 function void test1() 12 function void test1()
9 { 13 {
10 var u32 b; 14 var u32 bdd;
11 var int a = 10; 15 var int a = 10;
12 b = 20; 16 bd = 20;
13 var int buf; 17 var int buf;
14 var int i; 18 var int i;
15 i = 2; 19 i = 2;
16 var int zero = i - 2; 20 var int zero = i - 2;
17 if (i > 1) 21 if (i > 1)
39 var int hahaa = 23 * 2; 43 var int hahaa = 23 * 2;
40 44
41 45
42 """ 46 """
43 47
44 def printAst(ast, indent=''):
45 print(indent + str(ast))
46 if isinstance(ast, c3.astnodes.Package):
47 for s in ast.scope:
48 printAst(s, indent + ' ')
49 if isinstance(ast, c3.astnodes.Function):
50 for s in ast.scope:
51 printAst(s, indent + ' ')
52 for c in ast.getChildren():
53 printAst(c, indent + ' ')
54
55 def c3compile(src, diag): 48 def c3compile(src, diag):
56 print('[0] source:') 49 print('[0] source:')
57 #print(src) 50 print(src)
58 print('[1] parsing') 51 print('[1] parsing')
52 # Structures:
59 sema = c3.Semantics(diag) 53 sema = c3.Semantics(diag)
60 p = c3.Parser(sema, diag) 54 p = c3.Parser(sema, diag)
61 tc = c3.TypeChecker(diag) 55 tc = c3.TypeChecker(diag)
62 al = c3.Analyzer(diag) 56 al = c3.Analyzer(diag)
63 cg = c3.CodeGenerator() 57 cg = c3.CodeGenerator()
58 ap = c3.AstPrinter()
59
64 x86gen = x86.X86CodeGen(diag) 60 x86gen = x86.X86CodeGen(diag)
65 p.parseSource(src) 61 p.parseSource(src)
66 ok = len(diag.diags) == 0 62 ok = len(diag.diags) == 0
67 if not ok: 63 if not ok:
68 return 64 return
65 al.analyzePackage(sema.mod)
69 tc.checkPackage(sema.mod) 66 tc.checkPackage(sema.mod)
70 print('{0} errors'.format(len(diag.diags))) 67 print('{0} errors'.format(len(diag.diags)))
71 68
72 for d in diag.diags: 69 for d in diag.diags:
73 print('ERROR:') 70 print('ERROR:')
74 ppci.printError(testsrc, d) 71 ppci.printError(testsrc, d)
75 print('[2] ast:') 72 print('[2] ast:')
73 ap.printAst(sema.mod)
74
76 #printAst(sema.mod) 75 #printAst(sema.mod)
77 76
78 ok = len(diag.diags) == 0 77 ok = len(diag.diags) == 0
79 if ok: 78 if ok:
80 print('Generating ir-code') 79 print('Generating ir-code')