1.1 --- a/translator.py Mon Oct 24 21:35:11 2016 +0200
1.2 +++ b/translator.py Mon Oct 24 21:49:39 2016 +0200
1.3 @@ -210,6 +210,8 @@
1.4
1.5 self.out = open(output_filename, "w")
1.6 try:
1.7 + self.start_output()
1.8 +
1.9 # Process namespaces, writing the translation.
1.10
1.11 for path, node in self.namespaces:
1.12 @@ -451,17 +453,17 @@
1.13 # Control-flow modification statements.
1.14
1.15 elif isinstance(n, compiler.ast.Break):
1.16 - self.writeline("break;")
1.17 + self.writestmt("break;")
1.18
1.19 elif isinstance(n, compiler.ast.Continue):
1.20 - self.writeline("continue;")
1.21 + self.writestmt("continue;")
1.22
1.23 elif isinstance(n, compiler.ast.Return):
1.24 expr = self.process_structure_node(n.value)
1.25 if expr:
1.26 - self.writeline("return %s;" % expr)
1.27 + self.writestmt("return %s;" % expr)
1.28 else:
1.29 - self.writeline("return;")
1.30 + self.writestmt("return;")
1.31
1.32 # Invocations.
1.33
1.34 @@ -566,7 +568,10 @@
1.35 for instruction in self.optimiser.access_instructions[location]:
1.36 output.append(encode_access_instruction(instruction, subs))
1.37
1.38 - out = "(\n%s\n)" % ",\n".join(output)
1.39 + if len(output) == 1:
1.40 + out = output[0]
1.41 + else:
1.42 + out = "(\n%s\n)" % ",\n".join(output)
1.43
1.44 del self.attrs[0]
1.45 return AttrResult(out, refs)
1.46 @@ -645,7 +650,7 @@
1.47
1.48 expr = self.process_structure_node(n.code)
1.49 if expr:
1.50 - self.writeline("return %s;" % expr)
1.51 + self.writestmt("return %s;" % expr)
1.52
1.53 self.in_conditional = in_conditional
1.54
1.55 @@ -1054,7 +1059,7 @@
1.56 self.indent += 1
1.57 if n.else_:
1.58 self.process_structure_node(n.else_)
1.59 - self.writeline("break;")
1.60 + self.writestmt("break;")
1.61 self.indent -= 1
1.62 self.writeline("}")
1.63
1.64 @@ -1068,6 +1073,15 @@
1.65
1.66 # Output generation.
1.67
1.68 + def start_output(self):
1.69 + print >>self.out, """\
1.70 +#include "types.h"
1.71 +#include "ops.h"
1.72 +#include "progconsts.h"
1.73 +#include "progops.h"
1.74 +#include "progtypes.h"
1.75 +"""
1.76 +
1.77 def start_module(self):
1.78 print >>self.out, "void __main_%s()" % encode_path(self.name)
1.79 print >>self.out, "{"
1.80 @@ -1081,6 +1095,7 @@
1.81 print >>self.out, "__attr %s(__attr __args[])" % encode_function_pointer(name)
1.82 print >>self.out, "{"
1.83 self.indent += 1
1.84 + self.writeline("__attr __tmp_context, __tmp_target, __tmp_value;")
1.85
1.86 # Obtain local names from parameters.
1.87
1.88 @@ -1117,13 +1132,14 @@
1.89 def end_function(self):
1.90 self.indent -= 1
1.91 print >>self.out, "}"
1.92 + print >>self.out
1.93
1.94 def start_if(self, first, test_ref):
1.95
1.96 # NOTE: This needs to evaluate whether the operand is true or false
1.97 # NOTE: according to Python rules.
1.98
1.99 - self.writeline("%sif (%s)" % (not first and "else " or "", test_ref))
1.100 + self.writestmt("%sif (%s)" % (not first and "else " or "", test_ref))
1.101 self.writeline("{")
1.102 self.indent += 1
1.103
1.104 @@ -1143,10 +1159,10 @@
1.105 def statement(self, expr):
1.106 # NOTE: Should never be None.
1.107 if not expr:
1.108 - self.writeline("...;")
1.109 + self.writestmt("...;")
1.110 s = str(expr)
1.111 if s:
1.112 - self.writeline("%s;" % s)
1.113 + self.writestmt("%s;" % s)
1.114
1.115 def statements(self, results):
1.116 for result in results:
1.117 @@ -1169,7 +1185,11 @@
1.118 def writeline(self, s):
1.119 print >>self.out, "%s%s" % (self.pad(), self.indenttext(s, self.indent + 1))
1.120
1.121 + def writestmt(self, s):
1.122 + print >>self.out
1.123 + self.writeline(s)
1.124 +
1.125 def write_comment(self, s):
1.126 - self.writeline("/* %s */" % s)
1.127 + self.writestmt("/* %s */" % s)
1.128
1.129 # vim: tabstop=4 expandtab shiftwidth=4