Mercurial > lcfOS
comparison python/ppci/c3/builder.py @ 396:fb3c1f029b30
Added baselexer into c3 lexer
author | Windel Bouwman |
---|---|
date | Tue, 27 May 2014 22:19:32 +0200 |
parents | 39bf68bf1891 |
children |
comparison
equal
deleted
inserted
replaced
395:3b0c495e3008 | 396:fb3c1f029b30 |
---|---|
93 self.cg = CodeGenerator(diag) | 93 self.cg = CodeGenerator(diag) |
94 self.topScope = createTopScope(target) # Scope with built in types | 94 self.topScope = createTopScope(target) # Scope with built in types |
95 | 95 |
96 def build(self, srcs, imps=[]): | 96 def build(self, srcs, imps=[]): |
97 """ Create IR-code from sources """ | 97 """ Create IR-code from sources """ |
98 self.logger.debug('Building {} source files'.format(len(srcs))) | 98 self.logger.debug('Building {} source files'.format(len(srcs + imps))) |
99 iter(srcs) # Check if srcs are iterable | 99 iter(srcs) # Check if srcs are iterable |
100 iter(imps) | 100 iter(imps) |
101 self.ok = True | 101 self.ok = True |
102 self.pkgs = {} | 102 self.pkgs = {} |
103 | 103 |
104 # Parsing stage (phase 1) | 104 # Lexing and parsing stage (phase 1) |
105 def doParse(src): | 105 def doParse(src): |
106 tokens = self.lexer.lex(src) | 106 tokens = self.lexer.lex(src) |
107 return self.parser.parseSource(tokens) | 107 pkg = self.parser.parseSource(tokens) |
108 return pkg | |
108 s_pkgs = list(map(doParse, srcs)) | 109 s_pkgs = list(map(doParse, srcs)) |
109 i_pkgs = list(map(doParse, imps)) | 110 i_pkgs = list(map(doParse, imps)) |
110 all_pkgs = s_pkgs + i_pkgs | 111 all_pkgs = s_pkgs + i_pkgs |
111 if not all(all_pkgs): | 112 if not all(all_pkgs): |
112 self.ok = False | 113 self.ok = False |
114 self.logger.debug('Parsing failed') | |
113 return | 115 return |
116 | |
117 self.logger.debug('Parsed {} packages'.format(len(all_pkgs))) | |
114 | 118 |
115 # Fix scopes and package refs (phase 1.5) | 119 # Fix scopes and package refs (phase 1.5) |
116 packages = {pkg.name: pkg for pkg in all_pkgs} | 120 packages = {pkg.name: pkg for pkg in all_pkgs} |
117 self.pkgs = packages | 121 self.pkgs = packages |
118 | 122 |
120 # Fix scopes: | 124 # Fix scopes: |
121 for pkg in all_pkgs: | 125 for pkg in all_pkgs: |
122 scopeFiller.addScope(pkg) | 126 scopeFiller.addScope(pkg) |
123 if not all(pkg.ok for pkg in all_pkgs): | 127 if not all(pkg.ok for pkg in all_pkgs): |
124 self.ok = False | 128 self.ok = False |
129 self.logger.debug('Scope filling failed') | |
125 return | 130 return |
126 | 131 |
127 # Generate intermediate code (phase 2) | 132 # Generate intermediate code (phase 2) |
128 # Only return ircode when everything is OK | 133 # Only return ircode when everything is OK |
129 for pkg in s_pkgs: | 134 for pkg in s_pkgs: |
130 yield self.cg.gencode(pkg) | 135 yield self.cg.gencode(pkg) |
131 if not all(pkg.ok for pkg in all_pkgs): | 136 if not all(pkg.ok for pkg in all_pkgs): |
137 self.logger.debug('Code generation failed') | |
132 self.ok = False | 138 self.ok = False |
139 self.logger.debug('C3 build complete!') |