paul@113 | 1 | #!/usr/bin/env python |
paul@113 | 2 | |
paul@113 | 3 | """ |
paul@113 | 4 | Translate programs. |
paul@113 | 5 | |
paul@113 | 6 | Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> |
paul@113 | 7 | |
paul@113 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@113 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@113 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@113 | 11 | version. |
paul@113 | 12 | |
paul@113 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@113 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@113 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@113 | 16 | details. |
paul@113 | 17 | |
paul@113 | 18 | You should have received a copy of the GNU General Public License along with |
paul@113 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@113 | 20 | """ |
paul@113 | 21 | |
paul@113 | 22 | from common import * |
paul@113 | 23 | from encoders import * |
paul@113 | 24 | from os.path import exists, join |
paul@113 | 25 | from os import makedirs |
paul@113 | 26 | import compiler |
paul@113 | 27 | import results |
paul@113 | 28 | |
paul@113 | 29 | class Translator(CommonOutput): |
paul@113 | 30 | |
paul@113 | 31 | "A program translator." |
paul@113 | 32 | |
paul@113 | 33 | def __init__(self, importer, deducer, optimiser, output): |
paul@113 | 34 | self.importer = importer |
paul@113 | 35 | self.deducer = deducer |
paul@113 | 36 | self.optimiser = optimiser |
paul@113 | 37 | self.output = output |
paul@113 | 38 | |
paul@113 | 39 | def to_output(self): |
paul@113 | 40 | output = join(self.output, "src") |
paul@113 | 41 | |
paul@113 | 42 | if not exists(output): |
paul@113 | 43 | makedirs(output) |
paul@113 | 44 | |
paul@113 | 45 | self.check_output() |
paul@113 | 46 | |
paul@113 | 47 | for module in self.importer.modules.values(): |
paul@113 | 48 | tm = TranslatedModule(module.name, self.importer, self.deducer, self.optimiser) |
paul@113 | 49 | tm.translate(module.filename, join(output, "%s.c" % module.name)) |
paul@113 | 50 | |
paul@113 | 51 | # Classes representing intermediate translation results. |
paul@113 | 52 | |
paul@113 | 53 | class TranslationResult: |
paul@113 | 54 | |
paul@113 | 55 | "An abstract translation result mix-in." |
paul@113 | 56 | |
paul@113 | 57 | pass |
paul@113 | 58 | |
paul@113 | 59 | class Expression(results.Result, TranslationResult): |
paul@113 | 60 | |
paul@113 | 61 | "A general expression." |
paul@113 | 62 | |
paul@113 | 63 | def __init__(self, s): |
paul@113 | 64 | self.s = s |
paul@113 | 65 | def __str__(self): |
paul@113 | 66 | return self.s |
paul@113 | 67 | def __repr__(self): |
paul@113 | 68 | return "Expression(%r)" % self.s |
paul@113 | 69 | |
paul@113 | 70 | class TrResolvedNameRef(results.ResolvedNameRef, TranslationResult): |
paul@113 | 71 | |
paul@113 | 72 | "A reference to a name in the translation." |
paul@113 | 73 | |
paul@113 | 74 | def __str__(self): |
paul@113 | 75 | |
paul@113 | 76 | "Return an output representation of the referenced name." |
paul@113 | 77 | |
paul@113 | 78 | # Use any alias in preference to the origin of the name. |
paul@113 | 79 | |
paul@113 | 80 | name = self.get_name() |
paul@113 | 81 | |
paul@113 | 82 | # Use any static origin in preference to any alias. |
paul@113 | 83 | # For sources, any identified static origin will be constant and thus |
paul@113 | 84 | # usable directly. For targets, no constant should be assigned and thus |
paul@113 | 85 | # the alias (or any plain name) will be used. |
paul@113 | 86 | |
paul@137 | 87 | ref = self.static() |
paul@137 | 88 | origin = ref and self.get_origin() |
paul@137 | 89 | static_name = origin and encode_path(origin) |
paul@137 | 90 | dynamic_name = name and encode_path(name) or encode_path(self.name) |
paul@113 | 91 | |
paul@113 | 92 | # Assignments. |
paul@113 | 93 | |
paul@113 | 94 | if self.expr: |
paul@113 | 95 | |
paul@113 | 96 | # Eliminate assignments between constants. |
paul@113 | 97 | |
paul@113 | 98 | if self.static() and isinstance(self.expr, results.ResolvedNameRef) and self.expr.static(): |
paul@113 | 99 | return "" |
paul@113 | 100 | else: |
paul@113 | 101 | return "%s = %s" % (name, self.expr) |
paul@113 | 102 | |
paul@113 | 103 | # Expressions. |
paul@113 | 104 | |
paul@137 | 105 | elif static_name: |
paul@137 | 106 | parent = ref.parent() |
paul@137 | 107 | context = ref.has_kind("<function>") and parent or None |
paul@137 | 108 | return "((__attr) {&%s, &%s})" % (context or "0", static_name) |
paul@137 | 109 | |
paul@113 | 110 | else: |
paul@137 | 111 | return dynamic_name |
paul@113 | 112 | |
paul@113 | 113 | class TrConstantValueRef(results.ConstantValueRef, TranslationResult): |
paul@113 | 114 | |
paul@113 | 115 | "A constant value reference in the translation." |
paul@113 | 116 | |
paul@113 | 117 | def __str__(self): |
paul@136 | 118 | return encode_literal_constant(self.number) |
paul@113 | 119 | |
paul@113 | 120 | class TrLiteralSequenceRef(results.LiteralSequenceRef, TranslationResult): |
paul@113 | 121 | |
paul@113 | 122 | "A reference representing a sequence of values." |
paul@113 | 123 | |
paul@113 | 124 | def __str__(self): |
paul@113 | 125 | return str(self.node) |
paul@113 | 126 | |
paul@113 | 127 | class AttrResult(Expression, TranslationResult): |
paul@113 | 128 | |
paul@113 | 129 | "A translation result for an attribute access." |
paul@113 | 130 | |
paul@113 | 131 | def __init__(self, s, refs): |
paul@113 | 132 | Expression.__init__(self, s) |
paul@113 | 133 | self.refs = refs |
paul@113 | 134 | |
paul@113 | 135 | def get_origin(self): |
paul@113 | 136 | return self.refs and len(self.refs) == 1 and first(self.refs).get_origin() |
paul@113 | 137 | |
paul@118 | 138 | def has_kind(self, kinds): |
paul@118 | 139 | if not self.refs: |
paul@118 | 140 | return False |
paul@118 | 141 | for ref in self.refs: |
paul@118 | 142 | if ref.has_kind(kinds): |
paul@118 | 143 | return True |
paul@118 | 144 | return False |
paul@118 | 145 | |
paul@113 | 146 | def __repr__(self): |
paul@136 | 147 | return "AttrResult(%r, %r)" % (self.s, self.get_origin()) |
paul@113 | 148 | |
paul@113 | 149 | class PredefinedConstantRef(AttrResult): |
paul@113 | 150 | |
paul@113 | 151 | "A predefined constant reference." |
paul@113 | 152 | |
paul@113 | 153 | def __init__(self, value): |
paul@113 | 154 | self.value = value |
paul@113 | 155 | |
paul@113 | 156 | def __str__(self): |
paul@136 | 157 | if self.value in ("False", "True"): |
paul@136 | 158 | return encode_path("__builtins__.bool.%s" % self.value) |
paul@136 | 159 | elif self.value == "None": |
paul@136 | 160 | return encode_path("__builtins__.none.%s" % self.value) |
paul@136 | 161 | elif self.value == "NotImplemented": |
paul@136 | 162 | return encode_path("__builtins__.notimplemented.%s" % self.value) |
paul@136 | 163 | else: |
paul@136 | 164 | return self.value |
paul@113 | 165 | |
paul@113 | 166 | def __repr__(self): |
paul@113 | 167 | return "PredefinedConstantRef(%r)" % self.value |
paul@113 | 168 | |
paul@113 | 169 | def make_expression(expr): |
paul@113 | 170 | |
paul@113 | 171 | "Make a new expression from the existing 'expr'." |
paul@113 | 172 | |
paul@113 | 173 | if isinstance(expr, results.Result): |
paul@113 | 174 | return expr |
paul@113 | 175 | else: |
paul@113 | 176 | return Expression(str(expr)) |
paul@113 | 177 | |
paul@113 | 178 | # The actual translation process itself. |
paul@113 | 179 | |
paul@113 | 180 | class TranslatedModule(CommonModule): |
paul@113 | 181 | |
paul@113 | 182 | "A module translator." |
paul@113 | 183 | |
paul@113 | 184 | def __init__(self, name, importer, deducer, optimiser): |
paul@113 | 185 | CommonModule.__init__(self, name, importer) |
paul@113 | 186 | self.deducer = deducer |
paul@113 | 187 | self.optimiser = optimiser |
paul@113 | 188 | |
paul@113 | 189 | # Output stream. |
paul@113 | 190 | |
paul@113 | 191 | self.out = None |
paul@113 | 192 | self.indent = 0 |
paul@113 | 193 | self.tabstop = " " |
paul@113 | 194 | |
paul@113 | 195 | # Recorded namespaces. |
paul@113 | 196 | |
paul@113 | 197 | self.namespaces = [] |
paul@113 | 198 | self.in_conditional = False |
paul@113 | 199 | |
paul@113 | 200 | # Attribute access counting. |
paul@113 | 201 | |
paul@113 | 202 | self.attr_accesses = {} |
paul@113 | 203 | |
paul@113 | 204 | def __repr__(self): |
paul@113 | 205 | return "TranslatedModule(%r, %r)" % (self.name, self.importer) |
paul@113 | 206 | |
paul@113 | 207 | def translate(self, filename, output_filename): |
paul@113 | 208 | |
paul@113 | 209 | """ |
paul@113 | 210 | Parse the file having the given 'filename', writing the translation to |
paul@113 | 211 | the given 'output_filename'. |
paul@113 | 212 | """ |
paul@113 | 213 | |
paul@113 | 214 | self.parse_file(filename) |
paul@113 | 215 | |
paul@113 | 216 | # Collect function namespaces for separate processing. |
paul@113 | 217 | |
paul@113 | 218 | self.record_namespaces(self.astnode) |
paul@113 | 219 | |
paul@113 | 220 | # Reset the lambda naming (in order to obtain the same names again) and |
paul@113 | 221 | # translate the program. |
paul@113 | 222 | |
paul@113 | 223 | self.reset_lambdas() |
paul@113 | 224 | |
paul@113 | 225 | self.out = open(output_filename, "w") |
paul@113 | 226 | try: |
paul@128 | 227 | self.start_output() |
paul@128 | 228 | |
paul@113 | 229 | # Process namespaces, writing the translation. |
paul@113 | 230 | |
paul@113 | 231 | for path, node in self.namespaces: |
paul@113 | 232 | self.process_namespace(path, node) |
paul@113 | 233 | |
paul@113 | 234 | # Process the module namespace including class namespaces. |
paul@113 | 235 | |
paul@113 | 236 | self.process_namespace([], self.astnode) |
paul@113 | 237 | |
paul@113 | 238 | finally: |
paul@113 | 239 | self.out.close() |
paul@113 | 240 | |
paul@113 | 241 | def have_object(self): |
paul@113 | 242 | |
paul@113 | 243 | "Return whether a namespace is a recorded object." |
paul@113 | 244 | |
paul@113 | 245 | return self.importer.objects.get(self.get_namespace_path()) |
paul@113 | 246 | |
paul@113 | 247 | def get_builtin(self, name): |
paul@113 | 248 | return self.importer.get_object("__builtins__.%s" % name) |
paul@113 | 249 | |
paul@113 | 250 | def in_method(self, path): |
paul@113 | 251 | class_name, method_name = path.rsplit(".", 1) |
paul@113 | 252 | return self.importer.classes.has_key(class_name) and class_name |
paul@113 | 253 | |
paul@113 | 254 | # Namespace recording. |
paul@113 | 255 | |
paul@113 | 256 | def record_namespaces(self, node): |
paul@113 | 257 | |
paul@113 | 258 | "Process the program structure 'node', recording namespaces." |
paul@113 | 259 | |
paul@113 | 260 | for n in node.getChildNodes(): |
paul@113 | 261 | self.record_namespaces_in_node(n) |
paul@113 | 262 | |
paul@113 | 263 | def record_namespaces_in_node(self, node): |
paul@113 | 264 | |
paul@113 | 265 | "Process the program structure 'node', recording namespaces." |
paul@113 | 266 | |
paul@113 | 267 | # Function namespaces within modules, classes and other functions. |
paul@113 | 268 | # Functions appearing within conditional statements are given arbitrary |
paul@113 | 269 | # names. |
paul@113 | 270 | |
paul@113 | 271 | if isinstance(node, compiler.ast.Function): |
paul@113 | 272 | self.record_function_node(node, (self.in_conditional or self.in_function) and self.get_lambda_name() or node.name) |
paul@113 | 273 | |
paul@113 | 274 | elif isinstance(node, compiler.ast.Lambda): |
paul@113 | 275 | self.record_function_node(node, self.get_lambda_name()) |
paul@113 | 276 | |
paul@113 | 277 | # Classes are visited, but may be ignored if inside functions. |
paul@113 | 278 | |
paul@113 | 279 | elif isinstance(node, compiler.ast.Class): |
paul@113 | 280 | self.enter_namespace(node.name) |
paul@113 | 281 | if self.have_object(): |
paul@113 | 282 | self.record_namespaces(node) |
paul@113 | 283 | self.exit_namespace() |
paul@113 | 284 | |
paul@113 | 285 | # Conditional nodes are tracked so that function definitions may be |
paul@113 | 286 | # handled. Since "for" loops are converted to "while" loops, they are |
paul@113 | 287 | # included here. |
paul@113 | 288 | |
paul@113 | 289 | elif isinstance(node, (compiler.ast.For, compiler.ast.If, compiler.ast.While)): |
paul@113 | 290 | in_conditional = self.in_conditional |
paul@113 | 291 | self.in_conditional = True |
paul@113 | 292 | self.record_namespaces(node) |
paul@113 | 293 | self.in_conditional = in_conditional |
paul@113 | 294 | |
paul@113 | 295 | # All other nodes are processed depth-first. |
paul@113 | 296 | |
paul@113 | 297 | else: |
paul@113 | 298 | self.record_namespaces(node) |
paul@113 | 299 | |
paul@113 | 300 | def record_function_node(self, n, name): |
paul@113 | 301 | |
paul@113 | 302 | """ |
paul@113 | 303 | Record the given function, lambda, if expression or list comprehension |
paul@113 | 304 | node 'n' with the given 'name'. |
paul@113 | 305 | """ |
paul@113 | 306 | |
paul@113 | 307 | self.in_function = True |
paul@113 | 308 | self.enter_namespace(name) |
paul@113 | 309 | |
paul@113 | 310 | if self.have_object(): |
paul@113 | 311 | |
paul@113 | 312 | # Record the namespace path and the node itself. |
paul@113 | 313 | |
paul@113 | 314 | self.namespaces.append((self.namespace_path[:], n)) |
paul@113 | 315 | self.record_namespaces_in_node(n.code) |
paul@113 | 316 | |
paul@113 | 317 | self.exit_namespace() |
paul@113 | 318 | self.in_function = False |
paul@113 | 319 | |
paul@113 | 320 | # Constant referencing. |
paul@113 | 321 | |
paul@113 | 322 | def get_literal_instance(self, n, name): |
paul@113 | 323 | |
paul@113 | 324 | """ |
paul@113 | 325 | For node 'n', return a reference for the type of the given 'name'. |
paul@113 | 326 | """ |
paul@113 | 327 | |
paul@113 | 328 | ref = self.get_builtin(name) |
paul@113 | 329 | |
paul@113 | 330 | if name in ("dict", "list", "tuple"): |
paul@113 | 331 | return self.process_literal_sequence_node(n, name, ref, TrLiteralSequenceRef) |
paul@113 | 332 | else: |
paul@113 | 333 | path = self.get_namespace_path() |
paul@113 | 334 | local_number = self.importer.all_constants[path][n.value] |
paul@113 | 335 | constant_name = "$c%d" % local_number |
paul@113 | 336 | objpath = self.get_object_path(constant_name) |
paul@113 | 337 | number = self.optimiser.constant_numbers[objpath] |
paul@113 | 338 | return TrConstantValueRef(constant_name, ref.instance_of(), n.value, number) |
paul@113 | 339 | |
paul@113 | 340 | # Namespace translation. |
paul@113 | 341 | |
paul@113 | 342 | def process_namespace(self, path, node): |
paul@113 | 343 | |
paul@113 | 344 | """ |
paul@113 | 345 | Process the namespace for the given 'path' defined by the given 'node'. |
paul@113 | 346 | """ |
paul@113 | 347 | |
paul@113 | 348 | self.namespace_path = path |
paul@113 | 349 | |
paul@113 | 350 | if isinstance(node, (compiler.ast.Function, compiler.ast.Lambda)): |
paul@113 | 351 | self.in_function = True |
paul@113 | 352 | self.process_function_body_node(node) |
paul@113 | 353 | else: |
paul@113 | 354 | self.in_function = False |
paul@113 | 355 | self.start_module() |
paul@113 | 356 | self.process_structure(node) |
paul@113 | 357 | self.end_module() |
paul@113 | 358 | |
paul@113 | 359 | def process_structure(self, node): |
paul@113 | 360 | |
paul@113 | 361 | "Process the given 'node' or result." |
paul@113 | 362 | |
paul@113 | 363 | if isinstance(node, results.Result): |
paul@113 | 364 | return node |
paul@113 | 365 | else: |
paul@113 | 366 | return CommonModule.process_structure(self, node) |
paul@113 | 367 | |
paul@113 | 368 | def process_structure_node(self, n): |
paul@113 | 369 | |
paul@113 | 370 | "Process the individual node 'n'." |
paul@113 | 371 | |
paul@113 | 372 | # Plain statements emit their expressions. |
paul@113 | 373 | |
paul@113 | 374 | if isinstance(n, compiler.ast.Discard): |
paul@113 | 375 | expr = self.process_structure_node(n.expr) |
paul@113 | 376 | self.statement(expr) |
paul@113 | 377 | |
paul@113 | 378 | # Nodes using operator module functions. |
paul@113 | 379 | |
paul@113 | 380 | elif isinstance(n, compiler.ast.Operator): |
paul@113 | 381 | return self.process_operator_node(n) |
paul@113 | 382 | |
paul@113 | 383 | elif isinstance(n, compiler.ast.AugAssign): |
paul@113 | 384 | self.process_augassign_node(n) |
paul@113 | 385 | |
paul@113 | 386 | elif isinstance(n, compiler.ast.Compare): |
paul@113 | 387 | return self.process_compare_node(n) |
paul@113 | 388 | |
paul@113 | 389 | elif isinstance(n, compiler.ast.Slice): |
paul@113 | 390 | return self.process_slice_node(n) |
paul@113 | 391 | |
paul@113 | 392 | elif isinstance(n, compiler.ast.Sliceobj): |
paul@113 | 393 | return self.process_sliceobj_node(n) |
paul@113 | 394 | |
paul@113 | 395 | elif isinstance(n, compiler.ast.Subscript): |
paul@113 | 396 | return self.process_subscript_node(n) |
paul@113 | 397 | |
paul@113 | 398 | # Classes are visited, but may be ignored if inside functions. |
paul@113 | 399 | |
paul@113 | 400 | elif isinstance(n, compiler.ast.Class): |
paul@113 | 401 | self.process_class_node(n) |
paul@113 | 402 | |
paul@113 | 403 | # Functions within namespaces have any dynamic defaults initialised. |
paul@113 | 404 | |
paul@113 | 405 | elif isinstance(n, compiler.ast.Function): |
paul@113 | 406 | self.process_function_node(n) |
paul@113 | 407 | |
paul@113 | 408 | # Lambdas are replaced with references to separately-generated |
paul@113 | 409 | # functions. |
paul@113 | 410 | |
paul@113 | 411 | elif isinstance(n, compiler.ast.Lambda): |
paul@113 | 412 | return self.process_lambda_node(n) |
paul@113 | 413 | |
paul@113 | 414 | # Assignments. |
paul@113 | 415 | |
paul@113 | 416 | elif isinstance(n, compiler.ast.Assign): |
paul@113 | 417 | |
paul@113 | 418 | # Handle each assignment node. |
paul@113 | 419 | |
paul@113 | 420 | for node in n.nodes: |
paul@113 | 421 | self.process_assignment_node(node, n.expr) |
paul@113 | 422 | |
paul@113 | 423 | # Assignments within non-Assign nodes. |
paul@113 | 424 | # NOTE: Cover all possible nodes employing these. |
paul@113 | 425 | |
paul@113 | 426 | elif isinstance(n, compiler.ast.AssName): |
paul@113 | 427 | self.process_assignment_node(n, compiler.ast.Name("$temp")) |
paul@113 | 428 | |
paul@113 | 429 | elif isinstance(n, compiler.ast.AssAttr): |
paul@113 | 430 | self.process_attribute_access(n) |
paul@113 | 431 | |
paul@113 | 432 | # Accesses. |
paul@113 | 433 | |
paul@113 | 434 | elif isinstance(n, compiler.ast.Getattr): |
paul@113 | 435 | return self.process_attribute_access(n) |
paul@113 | 436 | |
paul@113 | 437 | # Names. |
paul@113 | 438 | |
paul@113 | 439 | elif isinstance(n, compiler.ast.Name): |
paul@113 | 440 | return self.process_name_node(n) |
paul@113 | 441 | |
paul@113 | 442 | # Loops and conditionals. |
paul@113 | 443 | |
paul@113 | 444 | elif isinstance(n, compiler.ast.For): |
paul@113 | 445 | self.process_for_node(n) |
paul@113 | 446 | |
paul@113 | 447 | elif isinstance(n, compiler.ast.While): |
paul@113 | 448 | self.process_while_node(n) |
paul@113 | 449 | |
paul@113 | 450 | elif isinstance(n, compiler.ast.If): |
paul@113 | 451 | self.process_if_node(n) |
paul@113 | 452 | |
paul@113 | 453 | elif isinstance(n, (compiler.ast.And, compiler.ast.Or)): |
paul@113 | 454 | return self.process_logical_node(n) |
paul@113 | 455 | |
paul@113 | 456 | elif isinstance(n, compiler.ast.Not): |
paul@113 | 457 | return self.process_not_node(n) |
paul@113 | 458 | |
paul@113 | 459 | # Exception control-flow tracking. |
paul@113 | 460 | |
paul@113 | 461 | elif isinstance(n, compiler.ast.TryExcept): |
paul@113 | 462 | self.process_try_node(n) |
paul@113 | 463 | |
paul@113 | 464 | elif isinstance(n, compiler.ast.TryFinally): |
paul@113 | 465 | self.process_try_finally_node(n) |
paul@113 | 466 | |
paul@113 | 467 | # Control-flow modification statements. |
paul@113 | 468 | |
paul@113 | 469 | elif isinstance(n, compiler.ast.Break): |
paul@128 | 470 | self.writestmt("break;") |
paul@113 | 471 | |
paul@113 | 472 | elif isinstance(n, compiler.ast.Continue): |
paul@128 | 473 | self.writestmt("continue;") |
paul@113 | 474 | |
paul@113 | 475 | elif isinstance(n, compiler.ast.Return): |
paul@113 | 476 | expr = self.process_structure_node(n.value) |
paul@113 | 477 | if expr: |
paul@128 | 478 | self.writestmt("return %s;" % expr) |
paul@113 | 479 | else: |
paul@128 | 480 | self.writestmt("return;") |
paul@113 | 481 | |
paul@113 | 482 | # Invocations. |
paul@113 | 483 | |
paul@113 | 484 | elif isinstance(n, compiler.ast.CallFunc): |
paul@113 | 485 | return self.process_invocation_node(n) |
paul@113 | 486 | |
paul@113 | 487 | elif isinstance(n, compiler.ast.Keyword): |
paul@113 | 488 | return self.process_structure_node(n.expr) |
paul@113 | 489 | |
paul@113 | 490 | # Constant usage. |
paul@113 | 491 | |
paul@113 | 492 | elif isinstance(n, compiler.ast.Const): |
paul@113 | 493 | return self.get_literal_instance(n, n.value.__class__.__name__) |
paul@113 | 494 | |
paul@113 | 495 | elif isinstance(n, compiler.ast.Dict): |
paul@113 | 496 | return self.get_literal_instance(n, "dict") |
paul@113 | 497 | |
paul@113 | 498 | elif isinstance(n, compiler.ast.List): |
paul@113 | 499 | return self.get_literal_instance(n, "list") |
paul@113 | 500 | |
paul@113 | 501 | elif isinstance(n, compiler.ast.Tuple): |
paul@113 | 502 | return self.get_literal_instance(n, "tuple") |
paul@113 | 503 | |
paul@113 | 504 | # All other nodes are processed depth-first. |
paul@113 | 505 | |
paul@113 | 506 | else: |
paul@113 | 507 | self.process_structure(n) |
paul@113 | 508 | |
paul@113 | 509 | def process_assignment_node(self, n, expr): |
paul@113 | 510 | |
paul@113 | 511 | "Process the individual node 'n' to be assigned the contents of 'expr'." |
paul@113 | 512 | |
paul@113 | 513 | # Names and attributes are assigned the entire expression. |
paul@113 | 514 | |
paul@113 | 515 | if isinstance(n, compiler.ast.AssName): |
paul@113 | 516 | name_ref = self.process_name_node(n, self.process_structure_node(expr)) |
paul@113 | 517 | self.statement(name_ref) |
paul@113 | 518 | |
paul@113 | 519 | elif isinstance(n, compiler.ast.AssAttr): |
paul@124 | 520 | in_assignment = self.in_assignment |
paul@124 | 521 | self.in_assignment = self.process_structure_node(expr) |
paul@124 | 522 | self.statement(self.process_attribute_access(n)) |
paul@124 | 523 | self.in_assignment = in_assignment |
paul@113 | 524 | |
paul@113 | 525 | # Lists and tuples are matched against the expression and their |
paul@113 | 526 | # items assigned to expression items. |
paul@113 | 527 | |
paul@113 | 528 | elif isinstance(n, (compiler.ast.AssList, compiler.ast.AssTuple)): |
paul@113 | 529 | self.process_assignment_node_items(n, expr) |
paul@113 | 530 | |
paul@113 | 531 | # Slices and subscripts are permitted within assignment nodes. |
paul@113 | 532 | |
paul@113 | 533 | elif isinstance(n, compiler.ast.Slice): |
paul@113 | 534 | self.statement(self.process_slice_node(n, expr)) |
paul@113 | 535 | |
paul@113 | 536 | elif isinstance(n, compiler.ast.Subscript): |
paul@113 | 537 | self.statement(self.process_subscript_node(n, expr)) |
paul@113 | 538 | |
paul@124 | 539 | def process_attribute_access(self, n): |
paul@113 | 540 | |
paul@113 | 541 | """ |
paul@113 | 542 | Process the given attribute access node 'n'. |
paul@113 | 543 | |
paul@113 | 544 | Where a name is provided, a single access should be recorded |
paul@113 | 545 | involving potentially many attributes, thus providing a path to an |
paul@113 | 546 | object. The remaining attributes are then accessed dynamically. |
paul@113 | 547 | The remaining accesses could be deduced and computed, but they would |
paul@113 | 548 | also need to be tested. |
paul@113 | 549 | |
paul@113 | 550 | Where no name is provided, potentially many accesses should be |
paul@113 | 551 | recorded, one per attribute name. These could be used to provide |
paul@113 | 552 | computed accesses, but the accessors would need to be tested in each |
paul@113 | 553 | case. |
paul@113 | 554 | """ |
paul@113 | 555 | |
paul@113 | 556 | # Obtain any completed chain and return the reference to it. |
paul@113 | 557 | |
paul@113 | 558 | attr_expr = self.process_attribute_chain(n) |
paul@113 | 559 | if self.have_access_expression(n): |
paul@113 | 560 | return attr_expr |
paul@113 | 561 | |
paul@113 | 562 | # Where the start of the chain of attributes has been reached, process |
paul@113 | 563 | # the complete access. |
paul@113 | 564 | |
paul@113 | 565 | name_ref = attr_expr and attr_expr.is_name() and attr_expr |
paul@113 | 566 | name = name_ref and name_ref.name or None |
paul@113 | 567 | |
paul@113 | 568 | location = self.get_access_location(name) |
paul@113 | 569 | refs = self.get_referenced_attributes(location) |
paul@113 | 570 | |
paul@113 | 571 | # Generate access instructions. |
paul@113 | 572 | |
paul@113 | 573 | subs = { |
paul@113 | 574 | "<expr>" : str(attr_expr), |
paul@124 | 575 | "<assexpr>" : str(self.in_assignment), |
paul@113 | 576 | "<context>" : "__tmp_context", |
paul@113 | 577 | "<accessor>" : "__tmp_value", |
paul@113 | 578 | } |
paul@113 | 579 | |
paul@113 | 580 | output = [] |
paul@113 | 581 | |
paul@113 | 582 | for instruction in self.optimiser.access_instructions[location]: |
paul@113 | 583 | output.append(encode_access_instruction(instruction, subs)) |
paul@113 | 584 | |
paul@128 | 585 | if len(output) == 1: |
paul@128 | 586 | out = output[0] |
paul@128 | 587 | else: |
paul@128 | 588 | out = "(\n%s\n)" % ",\n".join(output) |
paul@113 | 589 | |
paul@113 | 590 | del self.attrs[0] |
paul@113 | 591 | return AttrResult(out, refs) |
paul@113 | 592 | |
paul@113 | 593 | def get_referenced_attributes(self, location): |
paul@113 | 594 | |
paul@113 | 595 | """ |
paul@113 | 596 | Convert 'location' to the form used by the deducer and retrieve any |
paul@113 | 597 | identified attribute. |
paul@113 | 598 | """ |
paul@113 | 599 | |
paul@113 | 600 | access_location = self.deducer.const_accesses.get(location) |
paul@113 | 601 | refs = [] |
paul@113 | 602 | for attrtype, objpath, attr in self.deducer.referenced_attrs[access_location or location]: |
paul@113 | 603 | refs.append(attr) |
paul@113 | 604 | return refs |
paul@113 | 605 | |
paul@113 | 606 | def get_access_location(self, name): |
paul@113 | 607 | |
paul@113 | 608 | """ |
paul@113 | 609 | Using the current namespace and the given 'name', return the access |
paul@113 | 610 | location. |
paul@113 | 611 | """ |
paul@113 | 612 | |
paul@113 | 613 | path = self.get_path_for_access() |
paul@113 | 614 | |
paul@113 | 615 | # Get the location used by the deducer and optimiser and find any |
paul@113 | 616 | # recorded access. |
paul@113 | 617 | |
paul@113 | 618 | attrnames = ".".join(self.attrs) |
paul@113 | 619 | access_number = self.get_access_number(path, name, attrnames) |
paul@113 | 620 | self.update_access_number(path, name, attrnames) |
paul@113 | 621 | return (path, name, attrnames, access_number) |
paul@113 | 622 | |
paul@113 | 623 | def get_access_number(self, path, name, attrnames): |
paul@113 | 624 | access = name, attrnames |
paul@113 | 625 | if self.attr_accesses.has_key(path) and self.attr_accesses[path].has_key(access): |
paul@113 | 626 | return self.attr_accesses[path][access] |
paul@113 | 627 | else: |
paul@113 | 628 | return 0 |
paul@113 | 629 | |
paul@113 | 630 | def update_access_number(self, path, name, attrnames): |
paul@113 | 631 | access = name, attrnames |
paul@113 | 632 | if name: |
paul@113 | 633 | init_item(self.attr_accesses, path, dict) |
paul@113 | 634 | init_item(self.attr_accesses[path], access, lambda: 1) |
paul@113 | 635 | |
paul@113 | 636 | def process_class_node(self, n): |
paul@113 | 637 | |
paul@113 | 638 | "Process the given class node 'n'." |
paul@113 | 639 | |
paul@113 | 640 | self.enter_namespace(n.name) |
paul@113 | 641 | |
paul@113 | 642 | if self.have_object(): |
paul@113 | 643 | class_name = self.get_namespace_path() |
paul@113 | 644 | self.write_comment("Class: %s" % class_name) |
paul@113 | 645 | |
paul@113 | 646 | self.process_structure(n) |
paul@113 | 647 | |
paul@113 | 648 | self.exit_namespace() |
paul@113 | 649 | |
paul@113 | 650 | def process_function_body_node(self, n): |
paul@113 | 651 | |
paul@113 | 652 | """ |
paul@113 | 653 | Process the given function, lambda, if expression or list comprehension |
paul@113 | 654 | node 'n', generating the body. |
paul@113 | 655 | """ |
paul@113 | 656 | |
paul@113 | 657 | function_name = self.get_namespace_path() |
paul@113 | 658 | self.start_function(function_name) |
paul@113 | 659 | |
paul@113 | 660 | # Process the function body. |
paul@113 | 661 | |
paul@113 | 662 | in_conditional = self.in_conditional |
paul@113 | 663 | self.in_conditional = False |
paul@113 | 664 | |
paul@113 | 665 | expr = self.process_structure_node(n.code) |
paul@113 | 666 | if expr: |
paul@128 | 667 | self.writestmt("return %s;" % expr) |
paul@113 | 668 | |
paul@113 | 669 | self.in_conditional = in_conditional |
paul@113 | 670 | |
paul@113 | 671 | self.end_function() |
paul@113 | 672 | |
paul@113 | 673 | def process_function_node(self, n): |
paul@113 | 674 | |
paul@113 | 675 | """ |
paul@113 | 676 | Process the given function, lambda, if expression or list comprehension |
paul@113 | 677 | node 'n', generating any initialisation statements. |
paul@113 | 678 | """ |
paul@113 | 679 | |
paul@113 | 680 | # Where a function is declared conditionally, use a separate name for |
paul@113 | 681 | # the definition, and assign the definition to the stated name. |
paul@113 | 682 | |
paul@113 | 683 | if self.in_conditional or self.in_function: |
paul@113 | 684 | original_name = n.name |
paul@113 | 685 | name = self.get_lambda_name() |
paul@113 | 686 | else: |
paul@113 | 687 | original_name = None |
paul@113 | 688 | name = n.name |
paul@113 | 689 | |
paul@113 | 690 | # Obtain details of the defaults. |
paul@113 | 691 | |
paul@113 | 692 | defaults = self.process_function_defaults(n, name, self.get_object_path(name)) |
paul@113 | 693 | if defaults: |
paul@113 | 694 | for default in defaults: |
paul@113 | 695 | self.writeline("%s;" % default) |
paul@113 | 696 | |
paul@113 | 697 | # Where a function is set conditionally, assign the name. |
paul@113 | 698 | |
paul@113 | 699 | if original_name: |
paul@113 | 700 | self.process_assignment_for_function(original_name, name) |
paul@113 | 701 | |
paul@113 | 702 | def process_function_defaults(self, n, name, instance_name): |
paul@113 | 703 | |
paul@113 | 704 | """ |
paul@113 | 705 | Process the given function or lambda node 'n', initialising defaults |
paul@113 | 706 | that are dynamically set. The given 'name' indicates the name of the |
paul@113 | 707 | function. The given 'instance_name' indicates the name of any separate |
paul@113 | 708 | instance of the function created to hold the defaults. |
paul@113 | 709 | |
paul@113 | 710 | Return a list of operations setting defaults on a function instance. |
paul@113 | 711 | """ |
paul@113 | 712 | |
paul@113 | 713 | function_name = self.get_object_path(name) |
paul@113 | 714 | function_defaults = self.importer.function_defaults.get(function_name) |
paul@113 | 715 | if not function_defaults: |
paul@113 | 716 | return None |
paul@113 | 717 | |
paul@113 | 718 | # Determine whether any unidentified defaults are involved. |
paul@113 | 719 | |
paul@113 | 720 | need_defaults = [argname for argname, default in function_defaults if default.has_kind("<var>")] |
paul@113 | 721 | if not need_defaults: |
paul@113 | 722 | return None |
paul@113 | 723 | |
paul@113 | 724 | # Where defaults are involved but cannot be identified, obtain a new |
paul@113 | 725 | # instance of the lambda and populate the defaults. |
paul@113 | 726 | |
paul@113 | 727 | defaults = [] |
paul@113 | 728 | |
paul@113 | 729 | # Join the original defaults with the inspected defaults. |
paul@113 | 730 | |
paul@113 | 731 | original_defaults = [(argname, default) for (argname, default) in compiler.ast.get_defaults(n) if default] |
paul@113 | 732 | |
paul@113 | 733 | for i, (original, inspected) in enumerate(map(None, original_defaults, function_defaults)): |
paul@113 | 734 | |
paul@113 | 735 | # Obtain any reference for the default. |
paul@113 | 736 | |
paul@113 | 737 | if original: |
paul@113 | 738 | argname, default = original |
paul@113 | 739 | name_ref = self.process_structure_node(default) |
paul@113 | 740 | elif inspected: |
paul@113 | 741 | argname, default = inspected |
paul@113 | 742 | name_ref = TrResolvedNameRef(argname, default) |
paul@113 | 743 | else: |
paul@113 | 744 | continue |
paul@113 | 745 | |
paul@113 | 746 | if name_ref: |
paul@113 | 747 | defaults.append("__SETDEFAULT(%s, %s, %s)" % (encode_path(instance_name), i, name_ref)) |
paul@113 | 748 | |
paul@113 | 749 | return defaults |
paul@113 | 750 | |
paul@113 | 751 | def process_if_node(self, n): |
paul@113 | 752 | |
paul@113 | 753 | """ |
paul@113 | 754 | Process the given "if" node 'n'. |
paul@113 | 755 | """ |
paul@113 | 756 | |
paul@113 | 757 | first = True |
paul@113 | 758 | for test, body in n.tests: |
paul@113 | 759 | test_ref = self.process_structure_node(test) |
paul@113 | 760 | self.start_if(first, test_ref) |
paul@113 | 761 | |
paul@113 | 762 | in_conditional = self.in_conditional |
paul@113 | 763 | self.in_conditional = True |
paul@113 | 764 | self.process_structure_node(body) |
paul@113 | 765 | self.in_conditional = in_conditional |
paul@113 | 766 | |
paul@113 | 767 | self.end_if() |
paul@113 | 768 | first = False |
paul@113 | 769 | |
paul@113 | 770 | if n.else_: |
paul@113 | 771 | self.start_else() |
paul@113 | 772 | self.process_structure_node(n.else_) |
paul@113 | 773 | self.end_else() |
paul@113 | 774 | |
paul@113 | 775 | def process_invocation_node(self, n): |
paul@113 | 776 | |
paul@113 | 777 | "Process the given invocation node 'n'." |
paul@113 | 778 | |
paul@113 | 779 | expr = self.process_structure_node(n.node) |
paul@113 | 780 | objpath = expr.get_origin() |
paul@118 | 781 | target = None |
paul@113 | 782 | |
paul@113 | 783 | # Obtain details of the callable. |
paul@113 | 784 | |
paul@113 | 785 | if objpath: |
paul@113 | 786 | parameters = self.importer.function_parameters.get(objpath) |
paul@118 | 787 | if expr.has_kind("<class>"): |
paul@118 | 788 | target = encode_instantiator_pointer(objpath) |
paul@118 | 789 | elif expr.has_kind("<function>"): |
paul@118 | 790 | target = encode_function_pointer(objpath) |
paul@113 | 791 | else: |
paul@113 | 792 | parameters = None |
paul@113 | 793 | |
paul@113 | 794 | stages = [] |
paul@113 | 795 | |
paul@122 | 796 | # First, the invocation target is presented. |
paul@113 | 797 | |
paul@113 | 798 | stages.append("__tmp_target = %s" % expr) |
paul@122 | 799 | |
paul@122 | 800 | # Arguments are presented in a temporary frame array with any context |
paul@122 | 801 | # always being the first argument (although it may be set to null for |
paul@122 | 802 | # invocations where it would be unused). |
paul@122 | 803 | |
paul@122 | 804 | args = ["__CONTEXT_AS_VALUE(__tmp_target)"] |
paul@122 | 805 | args += [None] * (not parameters and len(n.args) or parameters and len(parameters) or 0) |
paul@122 | 806 | kwcodes = [] |
paul@122 | 807 | kwargs = [] |
paul@122 | 808 | |
paul@122 | 809 | for i, arg in enumerate(n.args): |
paul@122 | 810 | argexpr = self.process_structure_node(arg) |
paul@122 | 811 | |
paul@122 | 812 | # Store a keyword argument, either in the argument list or |
paul@122 | 813 | # in a separate keyword argument list for subsequent lookup. |
paul@122 | 814 | |
paul@122 | 815 | if isinstance(arg, compiler.ast.Keyword): |
paul@113 | 816 | |
paul@122 | 817 | # With knowledge of the target, store the keyword |
paul@122 | 818 | # argument directly. |
paul@122 | 819 | |
paul@122 | 820 | if parameters: |
paul@122 | 821 | argnum = parameters.index(arg.name) |
paul@122 | 822 | args[argnum+1] = str(argexpr) |
paul@122 | 823 | |
paul@122 | 824 | # Otherwise, store the details in a separate collection. |
paul@122 | 825 | |
paul@122 | 826 | else: |
paul@122 | 827 | kwargs.append(str(argexpr)) |
paul@122 | 828 | kwcodes.append("{%s, %s}" % ( |
paul@122 | 829 | encode_symbol("ppos", arg.name), |
paul@122 | 830 | encode_symbol("pcode", arg.name))) |
paul@122 | 831 | |
paul@122 | 832 | else: |
paul@122 | 833 | args[i+1] = str(argexpr) |
paul@113 | 834 | |
paul@113 | 835 | # Defaults are added to the frame where arguments are missing. |
paul@113 | 836 | |
paul@122 | 837 | if parameters: |
paul@122 | 838 | function_defaults = self.importer.function_defaults.get(objpath) |
paul@122 | 839 | if function_defaults: |
paul@122 | 840 | |
paul@122 | 841 | # Visit each default and set any missing arguments. |
paul@122 | 842 | |
paul@122 | 843 | for i, (argname, default) in enumerate(function_defaults): |
paul@122 | 844 | argnum = parameters.index(argname) |
paul@122 | 845 | if not args[argnum+1]: |
paul@122 | 846 | args[argnum+1] = "__GETDEFAULT(%s, %d)" % (target, i) |
paul@122 | 847 | |
paul@122 | 848 | argstr = "__ARGS(%s)" % ", ".join(args) |
paul@122 | 849 | kwargstr = kwargs and ("__ARGS(%s)" % ", ".join(kwargs)) or "0" |
paul@122 | 850 | kwcodestr = kwcodes and ("__KWARGS(%s)" % ", ".join(kwcodes)) or "0" |
paul@122 | 851 | |
paul@122 | 852 | # The callable is then obtained. |
paul@113 | 853 | |
paul@118 | 854 | if target: |
paul@136 | 855 | callable = target |
paul@118 | 856 | |
paul@118 | 857 | elif self.always_callable: |
paul@136 | 858 | callable = "__load_via_object(__tmp_target.value, %s).fn" % \ |
paul@113 | 859 | encode_symbol("pos", "__fn__") |
paul@113 | 860 | else: |
paul@136 | 861 | callable = "__check_and_load_via_object(__tmp_target.value, %s, %s).fn" % ( |
paul@113 | 862 | encode_symbol("pos", "__fn__"), encode_symbol("code", "__fn__")) |
paul@113 | 863 | |
paul@122 | 864 | stages.append(callable) |
paul@122 | 865 | |
paul@122 | 866 | # With a known target, the function is obtained directly and called. |
paul@122 | 867 | |
paul@122 | 868 | if target: |
paul@136 | 869 | output = "(\n%s\n)(%s)" % (",\n".join(stages), argstr) |
paul@113 | 870 | |
paul@122 | 871 | # With unknown targets, the generic invocation function is applied to |
paul@122 | 872 | # the callable and argument collections. |
paul@113 | 873 | |
paul@122 | 874 | else: |
paul@122 | 875 | output = "__invoke(\n(\n%s\n),\n%d, %s, %s,\n%d, %s\n)" % ( |
paul@122 | 876 | ",\n".join(stages), |
paul@122 | 877 | len(kwargs), kwcodestr, kwargstr, |
paul@122 | 878 | len(args), argstr) |
paul@122 | 879 | |
paul@122 | 880 | return make_expression(output) |
paul@113 | 881 | |
paul@113 | 882 | def always_callable(self, refs): |
paul@113 | 883 | |
paul@113 | 884 | "Determine whether all 'refs' are callable." |
paul@113 | 885 | |
paul@113 | 886 | for ref in refs: |
paul@113 | 887 | if not ref.static(): |
paul@113 | 888 | return False |
paul@113 | 889 | else: |
paul@113 | 890 | origin = ref.final() |
paul@113 | 891 | if not self.importer.get_attribute(origin, "__fn__"): |
paul@113 | 892 | return False |
paul@113 | 893 | return True |
paul@113 | 894 | |
paul@113 | 895 | def need_default_arguments(self, objpath, nargs): |
paul@113 | 896 | |
paul@113 | 897 | """ |
paul@113 | 898 | Return whether any default arguments are needed when invoking the object |
paul@113 | 899 | given by 'objpath'. |
paul@113 | 900 | """ |
paul@113 | 901 | |
paul@113 | 902 | parameters = self.importer.function_parameters.get(objpath) |
paul@113 | 903 | return nargs < len(parameters) |
paul@113 | 904 | |
paul@113 | 905 | def process_lambda_node(self, n): |
paul@113 | 906 | |
paul@113 | 907 | "Process the given lambda node 'n'." |
paul@113 | 908 | |
paul@113 | 909 | name = self.get_lambda_name() |
paul@113 | 910 | function_name = self.get_object_path(name) |
paul@113 | 911 | |
paul@113 | 912 | defaults = self.process_function_defaults(n, name, "__tmp") |
paul@113 | 913 | if not defaults: |
paul@113 | 914 | return make_expression(encode_path(function_name)) |
paul@113 | 915 | else: |
paul@113 | 916 | return make_expression("(__COPY(%s, __tmp), %s)" % (encode_path(function_name), ", ".join(defaults))) |
paul@113 | 917 | |
paul@113 | 918 | def process_logical_node(self, n): |
paul@113 | 919 | |
paul@113 | 920 | "Process the given operator node 'n'." |
paul@113 | 921 | |
paul@113 | 922 | if isinstance(n, compiler.ast.And): |
paul@113 | 923 | op = " && " |
paul@113 | 924 | else: |
paul@113 | 925 | op = " || " |
paul@113 | 926 | |
paul@113 | 927 | # NOTE: This needs to evaluate whether the operands are true or false |
paul@113 | 928 | # NOTE: according to Python rules. |
paul@113 | 929 | |
paul@113 | 930 | results = [("(%s)" % self.process_structure_node(node)) for node in n.nodes] |
paul@113 | 931 | return make_expression("(%s)" % op.join(results)) |
paul@113 | 932 | |
paul@113 | 933 | def process_name_node(self, n, expr=None): |
paul@113 | 934 | |
paul@113 | 935 | "Process the given name node 'n' with the optional assignment 'expr'." |
paul@113 | 936 | |
paul@113 | 937 | # Determine whether the name refers to a static external entity. |
paul@113 | 938 | |
paul@113 | 939 | if n.name in predefined_constants: |
paul@113 | 940 | return PredefinedConstantRef(n.name) |
paul@113 | 941 | |
paul@113 | 942 | # Convert literal references. |
paul@113 | 943 | |
paul@113 | 944 | elif n.name.startswith("$L"): |
paul@113 | 945 | literal_name = n.name[len("$L"):] |
paul@113 | 946 | ref = self.importer.get_object("__builtins__.%s" % literal_name) |
paul@113 | 947 | return TrResolvedNameRef(n.name, ref) |
paul@113 | 948 | |
paul@113 | 949 | # Convert operator function names to references. |
paul@113 | 950 | |
paul@113 | 951 | elif n.name.startswith("$op"): |
paul@136 | 952 | ref = self.importer.get_module(self.name).special.get(n.name) |
paul@113 | 953 | return TrResolvedNameRef(n.name, ref) |
paul@113 | 954 | |
paul@113 | 955 | # Get the appropriate name for the name reference, using the same method |
paul@113 | 956 | # as in the inspector. |
paul@113 | 957 | |
paul@113 | 958 | path = self.get_object_path(n.name) |
paul@113 | 959 | ref = self.importer.get_object(path) |
paul@113 | 960 | name = self.get_name_for_tracking(n.name, ref and ref.final()) |
paul@113 | 961 | |
paul@113 | 962 | # Get the static identity of the name. |
paul@113 | 963 | |
paul@113 | 964 | ref = self.importer.identify(path) |
paul@113 | 965 | |
paul@113 | 966 | # Obtain any resolved names for non-assignment names. |
paul@113 | 967 | |
paul@113 | 968 | if not expr and not ref and self.in_function: |
paul@113 | 969 | locals = self.importer.function_locals.get(self.get_namespace_path()) |
paul@113 | 970 | ref = locals and locals.get(n.name) |
paul@113 | 971 | |
paul@113 | 972 | # Qualified names are used for resolved static references or for |
paul@113 | 973 | # static namespace members. The reference should be configured to return |
paul@113 | 974 | # such names. |
paul@113 | 975 | |
paul@113 | 976 | return TrResolvedNameRef(name, ref, expr=expr) |
paul@113 | 977 | |
paul@113 | 978 | def process_not_node(self, n): |
paul@113 | 979 | |
paul@113 | 980 | "Process the given operator node 'n'." |
paul@113 | 981 | |
paul@113 | 982 | # NOTE: This needs to evaluate whether the operand is true or false |
paul@113 | 983 | # NOTE: according to Python rules. |
paul@113 | 984 | |
paul@113 | 985 | return make_expression("(!(%s))" % n.expr) |
paul@113 | 986 | |
paul@113 | 987 | def process_try_node(self, n): |
paul@113 | 988 | |
paul@113 | 989 | """ |
paul@113 | 990 | Process the given "try...except" node 'n'. |
paul@113 | 991 | """ |
paul@113 | 992 | |
paul@113 | 993 | # NOTE: Placeholders/macros. |
paul@113 | 994 | |
paul@113 | 995 | self.writeline("TRY") |
paul@113 | 996 | self.writeline("{") |
paul@113 | 997 | self.indent += 1 |
paul@113 | 998 | self.process_structure_node(n.body) |
paul@113 | 999 | self.indent -= 1 |
paul@113 | 1000 | self.writeline("}") |
paul@113 | 1001 | |
paul@113 | 1002 | for name, var, handler in n.handlers: |
paul@113 | 1003 | if name is not None: |
paul@113 | 1004 | name_ref = self.process_structure_node(name) |
paul@113 | 1005 | self.writeline("EXCEPT(%s)" % name_ref) |
paul@113 | 1006 | |
paul@113 | 1007 | self.writeline("{") |
paul@113 | 1008 | self.indent += 1 |
paul@113 | 1009 | |
paul@113 | 1010 | # Establish the local for the handler. |
paul@113 | 1011 | # NOTE: Need to provide the exception value. |
paul@113 | 1012 | |
paul@113 | 1013 | if var is not None: |
paul@113 | 1014 | var_ref = self.process_structure_node(var) |
paul@113 | 1015 | |
paul@113 | 1016 | if handler is not None: |
paul@113 | 1017 | self.process_structure_node(handler) |
paul@113 | 1018 | |
paul@113 | 1019 | self.indent -= 1 |
paul@113 | 1020 | self.writeline("}") |
paul@113 | 1021 | |
paul@113 | 1022 | if n.else_: |
paul@113 | 1023 | self.process_structure_node(n.else_) |
paul@113 | 1024 | |
paul@113 | 1025 | def process_try_finally_node(self, n): |
paul@113 | 1026 | |
paul@113 | 1027 | """ |
paul@113 | 1028 | Process the given "try...finally" node 'n'. |
paul@113 | 1029 | """ |
paul@113 | 1030 | |
paul@113 | 1031 | # NOTE: Placeholders/macros. |
paul@113 | 1032 | |
paul@113 | 1033 | self.writeline("TRY") |
paul@113 | 1034 | self.writeline("{") |
paul@113 | 1035 | self.indent += 1 |
paul@113 | 1036 | self.process_structure_node(n.body) |
paul@113 | 1037 | self.indent -= 1 |
paul@113 | 1038 | self.writeline("}") |
paul@113 | 1039 | self.writeline("FINALLY") |
paul@113 | 1040 | self.writeline("{") |
paul@113 | 1041 | self.indent += 1 |
paul@113 | 1042 | self.process_structure_node(n.final) |
paul@113 | 1043 | self.indent -= 1 |
paul@113 | 1044 | self.writeline("}") |
paul@113 | 1045 | |
paul@113 | 1046 | def process_while_node(self, n): |
paul@113 | 1047 | |
paul@113 | 1048 | "Process the given while node 'n'." |
paul@113 | 1049 | |
paul@113 | 1050 | self.writeline("while (1)") |
paul@113 | 1051 | self.writeline("{") |
paul@113 | 1052 | self.indent += 1 |
paul@113 | 1053 | test = self.process_structure_node(n.test) |
paul@113 | 1054 | |
paul@113 | 1055 | # Emit the loop termination condition unless "while <true value>" is |
paul@113 | 1056 | # indicated. |
paul@113 | 1057 | |
paul@113 | 1058 | if not (isinstance(test, PredefinedConstantRef) and test.value): |
paul@113 | 1059 | |
paul@113 | 1060 | # NOTE: This needs to evaluate whether the operand is true or false |
paul@113 | 1061 | # NOTE: according to Python rules. |
paul@113 | 1062 | |
paul@113 | 1063 | self.writeline("if (!(%s))" % test) |
paul@113 | 1064 | self.writeline("{") |
paul@113 | 1065 | self.indent += 1 |
paul@113 | 1066 | if n.else_: |
paul@113 | 1067 | self.process_structure_node(n.else_) |
paul@128 | 1068 | self.writestmt("break;") |
paul@113 | 1069 | self.indent -= 1 |
paul@113 | 1070 | self.writeline("}") |
paul@113 | 1071 | |
paul@113 | 1072 | in_conditional = self.in_conditional |
paul@113 | 1073 | self.in_conditional = True |
paul@113 | 1074 | self.process_structure_node(n.body) |
paul@113 | 1075 | self.in_conditional = in_conditional |
paul@113 | 1076 | |
paul@113 | 1077 | self.indent -= 1 |
paul@113 | 1078 | self.writeline("}") |
paul@113 | 1079 | |
paul@113 | 1080 | # Output generation. |
paul@113 | 1081 | |
paul@128 | 1082 | def start_output(self): |
paul@128 | 1083 | print >>self.out, """\ |
paul@128 | 1084 | #include "types.h" |
paul@128 | 1085 | #include "ops.h" |
paul@128 | 1086 | #include "progconsts.h" |
paul@128 | 1087 | #include "progops.h" |
paul@128 | 1088 | #include "progtypes.h" |
paul@137 | 1089 | #include "main.h" |
paul@128 | 1090 | """ |
paul@128 | 1091 | |
paul@113 | 1092 | def start_module(self): |
paul@113 | 1093 | print >>self.out, "void __main_%s()" % encode_path(self.name) |
paul@113 | 1094 | print >>self.out, "{" |
paul@113 | 1095 | self.indent += 1 |
paul@113 | 1096 | |
paul@113 | 1097 | def end_module(self): |
paul@113 | 1098 | self.indent -= 1 |
paul@113 | 1099 | self.end_function() |
paul@113 | 1100 | |
paul@113 | 1101 | def start_function(self, name): |
paul@113 | 1102 | print >>self.out, "__attr %s(__attr __args[])" % encode_function_pointer(name) |
paul@113 | 1103 | print >>self.out, "{" |
paul@113 | 1104 | self.indent += 1 |
paul@136 | 1105 | self.writeline("__ref __tmp_context, __tmp_value;") |
paul@136 | 1106 | self.writeline("__attr __tmp_target;") |
paul@113 | 1107 | |
paul@113 | 1108 | # Obtain local names from parameters. |
paul@113 | 1109 | |
paul@113 | 1110 | parameters = self.importer.function_parameters[name] |
paul@113 | 1111 | names = [] |
paul@113 | 1112 | locals = self.importer.function_locals[name].keys() |
paul@113 | 1113 | |
paul@113 | 1114 | for n in locals: |
paul@113 | 1115 | |
paul@113 | 1116 | # Filter out special names and parameters. Note that self is a local |
paul@113 | 1117 | # regardless of whether it originally appeared in the parameters or |
paul@113 | 1118 | # not. |
paul@113 | 1119 | |
paul@113 | 1120 | if n.startswith("$l") or n in parameters or n == "self": |
paul@113 | 1121 | continue |
paul@113 | 1122 | names.append(encode_path(n)) |
paul@113 | 1123 | |
paul@113 | 1124 | # Emit required local names. |
paul@113 | 1125 | |
paul@113 | 1126 | if names: |
paul@113 | 1127 | names.sort() |
paul@113 | 1128 | self.writeline("__attr %s;" % ", ".join(names)) |
paul@113 | 1129 | |
paul@113 | 1130 | # Generate any self reference. |
paul@113 | 1131 | |
paul@113 | 1132 | if self.in_method(name): |
paul@113 | 1133 | self.writeline("#define self (__args[0])") |
paul@113 | 1134 | |
paul@113 | 1135 | # Generate aliases for the parameters. |
paul@113 | 1136 | |
paul@113 | 1137 | for i, parameter in enumerate(parameters): |
paul@113 | 1138 | self.writeline("#define %s (__args[%d])" % (encode_path(parameter), i+1)) |
paul@113 | 1139 | |
paul@113 | 1140 | def end_function(self): |
paul@113 | 1141 | self.indent -= 1 |
paul@113 | 1142 | print >>self.out, "}" |
paul@128 | 1143 | print >>self.out |
paul@113 | 1144 | |
paul@113 | 1145 | def start_if(self, first, test_ref): |
paul@113 | 1146 | |
paul@113 | 1147 | # NOTE: This needs to evaluate whether the operand is true or false |
paul@113 | 1148 | # NOTE: according to Python rules. |
paul@113 | 1149 | |
paul@128 | 1150 | self.writestmt("%sif (%s)" % (not first and "else " or "", test_ref)) |
paul@113 | 1151 | self.writeline("{") |
paul@113 | 1152 | self.indent += 1 |
paul@113 | 1153 | |
paul@113 | 1154 | def end_if(self): |
paul@113 | 1155 | self.indent -= 1 |
paul@113 | 1156 | self.writeline("}") |
paul@113 | 1157 | |
paul@113 | 1158 | def start_else(self): |
paul@113 | 1159 | self.writeline("else") |
paul@113 | 1160 | self.writeline("{") |
paul@113 | 1161 | self.indent += 1 |
paul@113 | 1162 | |
paul@113 | 1163 | def end_else(self): |
paul@113 | 1164 | self.indent -= 1 |
paul@113 | 1165 | self.writeline("}") |
paul@113 | 1166 | |
paul@113 | 1167 | def statement(self, expr): |
paul@113 | 1168 | # NOTE: Should never be None. |
paul@113 | 1169 | if not expr: |
paul@128 | 1170 | self.writestmt("...;") |
paul@113 | 1171 | s = str(expr) |
paul@113 | 1172 | if s: |
paul@128 | 1173 | self.writestmt("%s;" % s) |
paul@113 | 1174 | |
paul@113 | 1175 | def statements(self, results): |
paul@113 | 1176 | for result in results: |
paul@113 | 1177 | self.statement(result) |
paul@113 | 1178 | |
paul@113 | 1179 | def pad(self, extra=0): |
paul@113 | 1180 | return (self.indent + extra) * self.tabstop |
paul@113 | 1181 | |
paul@113 | 1182 | def indenttext(self, s, levels): |
paul@116 | 1183 | lines = s.split("\n") |
paul@116 | 1184 | out = [lines[0]] |
paul@116 | 1185 | for line in lines[1:]: |
paul@116 | 1186 | out.append(levels * self.tabstop + line) |
paul@116 | 1187 | if line.endswith("("): |
paul@116 | 1188 | levels += 1 |
paul@122 | 1189 | elif line.startswith(")"): |
paul@116 | 1190 | levels -= 1 |
paul@116 | 1191 | return "\n".join(out) |
paul@113 | 1192 | |
paul@113 | 1193 | def writeline(self, s): |
paul@113 | 1194 | print >>self.out, "%s%s" % (self.pad(), self.indenttext(s, self.indent + 1)) |
paul@113 | 1195 | |
paul@128 | 1196 | def writestmt(self, s): |
paul@128 | 1197 | print >>self.out |
paul@128 | 1198 | self.writeline(s) |
paul@128 | 1199 | |
paul@113 | 1200 | def write_comment(self, s): |
paul@128 | 1201 | self.writestmt("/* %s */" % s) |
paul@113 | 1202 | |
paul@113 | 1203 | # vim: tabstop=4 expandtab shiftwidth=4 |