1.1 --- a/translator.py Sun Oct 30 22:33:22 2016 +0100
1.2 +++ b/translator.py Mon Oct 31 18:26:36 2016 +0100
1.3 @@ -836,10 +836,20 @@
1.4 expr = self.process_structure_node(n.node)
1.5 objpath = expr.get_origin()
1.6 target = None
1.7 + literal_instantiation = False
1.8
1.9 # Obtain details of the callable.
1.10
1.11 - if objpath:
1.12 + # Literals may be instantiated specially.
1.13 +
1.14 + if expr.is_name() and expr.name.startswith("$L") and objpath:
1.15 + literal_instantiation = True
1.16 + parameters = None
1.17 + target = encode_literal_instantiator(objpath)
1.18 +
1.19 + # Identified targets employ function pointers directly.
1.20 +
1.21 + elif objpath:
1.22 parameters = self.importer.function_parameters.get(objpath)
1.23 if expr.has_kind("<class>"):
1.24 target = encode_instantiator_pointer(objpath)
1.25 @@ -847,6 +857,9 @@
1.26 elif expr.has_kind("<function>"):
1.27 target = encode_function_pointer(objpath)
1.28 target_structure = encode_path(objpath)
1.29 +
1.30 + # Other targets are retrieved at run-time.
1.31 +
1.32 else:
1.33 parameters = None
1.34
1.35 @@ -906,6 +919,12 @@
1.36 kwargstr = kwargs and ("__ARGS(%s)" % ", ".join(kwargs)) or "0"
1.37 kwcodestr = kwcodes and ("__KWARGS(%s)" % ", ".join(kwcodes)) or "0"
1.38
1.39 + # Where literal instantiation is occurring, add an argument indicating
1.40 + # the number of values.
1.41 +
1.42 + if literal_instantiation:
1.43 + argstr += ", %d" % (len(args) - 1)
1.44 +
1.45 # First, the invocation expression is presented.
1.46
1.47 stages = []
1.48 @@ -1260,6 +1279,9 @@
1.49 # Output generation.
1.50
1.51 def start_output(self):
1.52 +
1.53 + "Write the declarations at the top of each source file."
1.54 +
1.55 print >>self.out, """\
1.56 #include "types.h"
1.57 #include "exceptions.h"
1.58 @@ -1271,16 +1293,25 @@
1.59 """
1.60
1.61 def start_module(self):
1.62 +
1.63 + "Write the start of each module's main function."
1.64 +
1.65 print >>self.out, "void __main_%s()" % encode_path(self.name)
1.66 print >>self.out, "{"
1.67 self.indent += 1
1.68 self.write_temporaries()
1.69
1.70 def end_module(self):
1.71 +
1.72 + "End each module by closing its main function."
1.73 +
1.74 self.indent -= 1
1.75 print >>self.out, "}"
1.76
1.77 def start_function(self, name):
1.78 +
1.79 + "Start the function having the given 'name'."
1.80 +
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 @@ -1311,17 +1342,30 @@
1.85 self.write_parameters(name, True)
1.86
1.87 def end_function(self, name):
1.88 +
1.89 + "End the function having the given 'name'."
1.90 +
1.91 self.write_parameters(name, False)
1.92 self.indent -= 1
1.93 print >>self.out, "}"
1.94 print >>self.out
1.95
1.96 def write_temporaries(self):
1.97 +
1.98 + "Write temporary storage employed by functions."
1.99 +
1.100 self.writeline("__ref __tmp_context, __tmp_value;")
1.101 self.writeline("__attr __tmp_target, __tmp_result;")
1.102 self.writeline("__exc __tmp_exc;")
1.103
1.104 def write_parameters(self, name, define=True):
1.105 +
1.106 + """
1.107 + For the function having the given 'name', write definitions of
1.108 + parameters found in the arguments array if 'define' is set to a true
1.109 + value, or write "undefinitions" if 'define' is set to a false value.
1.110 + """
1.111 +
1.112 parameters = self.importer.function_parameters[name]
1.113
1.114 # Generate any self reference.
1.115 @@ -1370,6 +1414,16 @@
1.116 for result in results:
1.117 self.statement(result)
1.118
1.119 + def writeline(self, s):
1.120 + print >>self.out, "%s%s" % (self.pad(), self.indenttext(s, self.indent + 1))
1.121 +
1.122 + def writestmt(self, s):
1.123 + print >>self.out
1.124 + self.writeline(s)
1.125 +
1.126 + def write_comment(self, s):
1.127 + self.writestmt("/* %s */" % s)
1.128 +
1.129 def pad(self, extra=0):
1.130 return (self.indent + extra) * self.tabstop
1.131
1.132 @@ -1384,14 +1438,4 @@
1.133 levels -= 1
1.134 return "\n".join(out)
1.135
1.136 - def writeline(self, s):
1.137 - print >>self.out, "%s%s" % (self.pad(), self.indenttext(s, self.indent + 1))
1.138 -
1.139 - def writestmt(self, s):
1.140 - print >>self.out
1.141 - self.writeline(s)
1.142 -
1.143 - def write_comment(self, s):
1.144 - self.writestmt("/* %s */" % s)
1.145 -
1.146 # vim: tabstop=4 expandtab shiftwidth=4