diff --git a/src/sixtypical/fallthru.py b/src/sixtypical/fallthru.py index fc2f609..eebfd89 100644 --- a/src/sixtypical/fallthru.py +++ b/src/sixtypical/fallthru.py @@ -64,12 +64,22 @@ class FallthruAnalyzer(object): self.fall_in_map = new_fall_in_map def serialize(self): + # NOTE, we can probably do this completely differently; + # construct the fall_out map + # construct fall_out chains + # sort these by length + # do the longest ones first + pending_routines = sorted(self.fall_in_map.keys()) routine_names = sorted([routine.name for routine in self.program.routines]) for routine_name in routine_names: if routine_name not in pending_routines: pending_routines.append(routine_name) + # make sure `main` appears first, whatever else may be the case. + pending_routines.remove('main') + pending_routines = ['main'] + pending_routines + roster = [] while pending_routines: # Pick a routine that is still pending to be serialized. diff --git a/tests/SixtyPical Fallthru.md b/tests/SixtyPical Fallthru.md index 7d89734..6bf24ea 100644 --- a/tests/SixtyPical Fallthru.md +++ b/tests/SixtyPical Fallthru.md @@ -104,6 +104,9 @@ If main does a `goto foo`, then it can fall through to `foo`. More than one routine can fall through to a routine. We pick one of them to fall through, when selecting the order of routines. +Also note, `main` is always serialized first, so that the entry +point of the entire program appears at the beginning of the code. + | define foo routine trashes a, z, n | { | ld a, 0 @@ -128,11 +131,11 @@ of them to fall through, when selecting the order of routines. = *** serialization: = [ = [ - = "bar", - = "foo" + = "main" = ], = [ - = "main" + = "bar", + = "foo" = ] = ] @@ -177,11 +180,11 @@ fall through to the other. = *** serialization: = [ = [ - = "foo", - = "bar" + = "main" = ], = [ - = "main" + = "foo", + = "bar" = ] = ] @@ -211,13 +214,13 @@ routine. = *** serialization: = [ = [ + = "main" + = ], + = [ = "bar" = ], = [ = "foo" - = ], - = [ - = "main" = ] = ] @@ -246,12 +249,12 @@ because we don't necessarily know what actual routine the vector contains. = *** serialization: = [ = [ + = "main" + = ], + = [ = "bar" = ], = [ = "foo" - = ], - = [ - = "main" = ] = ]