1.1 --- a/translator.py Thu Oct 27 17:55:28 2016 +0200
1.2 +++ b/translator.py Fri Oct 28 14:06:54 2016 +0200
1.3 @@ -104,7 +104,7 @@
1.4
1.5 elif static_name:
1.6 parent = ref.parent()
1.7 - context = ref.has_kind("<function>") and parent or None
1.8 + context = ref.has_kind("<function>") and encode_path(parent) or None
1.9 return "((__attr) {&%s, &%s})" % (context or "0", static_name)
1.10
1.11 else:
1.12 @@ -166,6 +166,16 @@
1.13 def __repr__(self):
1.14 return "PredefinedConstantRef(%r)" % self.value
1.15
1.16 +class BooleanResult(Expression, TranslationResult):
1.17 +
1.18 + "A expression producing a boolean result."
1.19 +
1.20 + def __str__(self):
1.21 + return "__builtins___bool_bool(%s)" % self.s
1.22 +
1.23 + def __repr__(self):
1.24 + return "BooleanResult(%r)" % self.s
1.25 +
1.26 def make_expression(expr):
1.27
1.28 "Make a new expression from the existing 'expr'."
1.29 @@ -744,7 +754,7 @@
1.30 continue
1.31
1.32 if name_ref:
1.33 - defaults.append("__SETDEFAULT(%s, %s, %s)" % (encode_path(instance_name), i, name_ref))
1.34 + defaults.append("__SETDEFAULT(&%s, %s, %s)" % (encode_path(instance_name), i, name_ref))
1.35
1.36 return defaults
1.37
1.38 @@ -917,18 +927,33 @@
1.39
1.40 def process_logical_node(self, n):
1.41
1.42 - "Process the given operator node 'n'."
1.43 + """
1.44 + Process the given operator node 'n'.
1.45 +
1.46 + Convert ... to ...
1.47 +
1.48 + <a> and <b>
1.49 + (__tmp_result = <a>, !__BOOL(__tmp_result)) ? __tmp_result : <b>
1.50 +
1.51 + <a> or <b>
1.52 + (__tmp_result = <a>, __BOOL(__tmp_result)) ? __tmp_result : <b>
1.53 + """
1.54
1.55 if isinstance(n, compiler.ast.And):
1.56 - op = " && "
1.57 + op = "!"
1.58 else:
1.59 - op = " || "
1.60 + op = ""
1.61 +
1.62 + results = []
1.63
1.64 - # NOTE: This needs to evaluate whether the operands are true or false
1.65 - # NOTE: according to Python rules.
1.66 + for node in n.nodes[:-1]:
1.67 + expr = self.process_structure_node(node)
1.68 + results.append("(__tmp_result = %s, %s__BOOL(__tmp_result)) ? __tmp_result : " % (expr, op))
1.69
1.70 - results = [("(%s)" % self.process_structure_node(node)) for node in n.nodes]
1.71 - return make_expression("(%s)" % op.join(results))
1.72 + expr = self.process_structure_node(n.nodes[-1])
1.73 + results.append(str(expr))
1.74 +
1.75 + return make_expression("(%s)" % "".join(results))
1.76
1.77 def process_name_node(self, n, expr=None):
1.78
1.79 @@ -1103,7 +1128,7 @@
1.80 print >>self.out, "{"
1.81 self.indent += 1
1.82 self.writeline("__ref __tmp_context, __tmp_value;")
1.83 - self.writeline("__attr __tmp_target;")
1.84 + self.writeline("__attr __tmp_target, __tmp_result;")
1.85
1.86 # Obtain local names from parameters.
1.87