1.1 --- a/generator.py Sat Oct 29 00:48:27 2016 +0200
1.2 +++ b/generator.py Sat Oct 29 20:17:14 2016 +0200
1.3 @@ -309,6 +309,10 @@
1.4 for value, n in self.optimiser.constants.items():
1.5 self.make_literal_constant(f_decls, f_defs, n, value)
1.6
1.7 + # Finish the main source file.
1.8 +
1.9 + self.write_main_program(f_code, f_signatures)
1.10 +
1.11 # Output more boilerplate.
1.12
1.13 print >>f_consts, """\
1.14 @@ -813,7 +817,7 @@
1.15 __attr %s(__attr __args[])
1.16 {
1.17 __args[0] = __new(&%s, &%s, sizeof(%s));
1.18 - %s
1.19 + %s(__args);
1.20 return __args[0];
1.21 }
1.22 """ % (
1.23 @@ -822,6 +826,33 @@
1.24 encode_function_pointer(init_ref.get_origin())
1.25 )
1.26
1.27 + def write_main_program(self, f_code, f_signatures):
1.28 +
1.29 + """
1.30 + Write the main program to 'f_code', invoking the program's modules. Also
1.31 + write declarations for module main functions to 'f_signatures'.
1.32 + """
1.33 +
1.34 + print >>f_code, """\
1.35 +int main(int argc, char *argv[])
1.36 +{"""
1.37 +
1.38 + for name in self.importer.modules.keys():
1.39 + function_name = "__main_%s" % encode_path(name)
1.40 + print >>f_signatures, "void %s();" % function_name
1.41 +
1.42 + # Emit the main module's function last.
1.43 +
1.44 + if name != "__main__":
1.45 + print >>f_code, """\
1.46 + %s();""" % function_name
1.47 +
1.48 + print >>f_code, """\
1.49 + __main___main__();
1.50 + return 0;
1.51 +}
1.52 +"""
1.53 +
1.54 def encode_size(table_type, path=None):
1.55 return "__%ssize%s" % (table_type, path and "_%s" % encode_path(path) or "")
1.56
2.1 --- a/templates/Makefile Sat Oct 29 00:48:27 2016 +0200
2.2 +++ b/templates/Makefile Sat Oct 29 20:17:14 2016 +0200
2.3 @@ -1,4 +1,4 @@
2.4 -SRC = exceptions.c progops.c progtypes.c ops.c $(wildcard src/*.c)
2.5 +SRC = exceptions.c main.c ops.c progops.c progtypes.c $(wildcard src/*.c)
2.6 OBJ = $(SRC:.c=.o)
2.7 CFLAGS = -I.
2.8