comparison python/c3/builder.py @ 272:e64bae57cda8

refactor ir
author Windel Bouwman
date Sat, 31 Aug 2013 17:58:54 +0200
parents 7416c923a02a
children 1c7c1e619be8
comparison
equal deleted inserted replaced
271:cf7d5fb7d9c8 272:e64bae57cda8
8 """ 8 """
9 Generates IR-code from c3 source. 9 Generates IR-code from c3 source.
10 Reports errors to the diagnostics system 10 Reports errors to the diagnostics system
11 """ 11 """
12 def __init__(self, diag): 12 def __init__(self, diag):
13 self.pack_dir = None
13 self.logger = logging.getLogger('c3') 14 self.logger = logging.getLogger('c3')
14 self.diag = diag 15 self.diag = diag
15 self.parser = Parser(diag) 16 self.parser = Parser(diag)
16 self.tc = TypeChecker(diag) 17 self.tc = TypeChecker(diag)
17 self.al = Analyzer(diag) 18 self.al = Analyzer(diag)
22 """ package provider for use when analyzing """ 23 """ package provider for use when analyzing """
23 if pname in self.packages: 24 if pname in self.packages:
24 return self.packages[pname] 25 return self.packages[pname]
25 else: 26 else:
26 # Try to lookup package from file 27 # Try to lookup package from file
27 fns = glob.glob('./*/{}.c3'.format(pname)) 28 fns = glob.glob('./**/{}.c3'.format(pname))
28 if fns: 29 if fns:
29 with open(fns[0]) as f: 30 with open(fns[0]) as f:
30 src = f.read() 31 src = f.read()
31 self.build(src) 32 self.build(src)
33 if self.pack_dir:
34 fns = glob.glob('{}/{}.c3'.format(self.pack_dir, pname))
35 if fns:
36 with open(fns[0]) as f:
37 src = f.read()
38 self.build(src)
32 if pname in self.packages: 39 if pname in self.packages:
33 return self.packages[pname] 40 return self.packages[pname]
34 41
35 def parse(self, src): 42 def parse(self, src):
36 pkg = self.parser.parseSource(src) 43 pkg = self.parser.parseSource(src)
46 53
47 # Store for later use: 54 # Store for later use:
48 self.packages[pkg.name] = pkg 55 self.packages[pkg.name] = pkg
49 return pkg 56 return pkg
50 57
51 def build(self, src): 58 def build(self, src, pack_dir=None):
52 """ Create IR-code from sources """ 59 """ Create IR-code from sources """
60 self.pack_dir = pack_dir
53 pkg = self.parse(src) 61 pkg = self.parse(src)
54 62
55 # Only return ircode when everything is OK 63 # Only return ircode when everything is OK
56 if pkg: 64 if pkg:
57 return self.cg.gencode(pkg) 65 return self.cg.gencode(pkg)