I say it's difficult. Maybe someone has a better idea...?
I know why it
doesn't work in HXA. There is a single "fetch next input line" function. Disregarding details, the core logic is:
Code:
IF (there-an-active-macro)
return( next-macro-line )
ELSEIF (the-current-input-file-is-not-empty)
return( next-input-line )
ELSE
return( end-of-file )
Macro expansions take precedence over file reads. So when a macro is expanded, the assembler "naturally" suspends input from the current input file and processes macro lines instead.
What if you want to nest macro expansions? That's fine. There's some behind-the-scenes stacking, but as far as the "fetch next input line" function is concerned, all that matters is a macro is active.
What if you want to nest file inclusions? That's fine. There's some behind-the-scenes stacking, but as far the "fetch next input line" function is concerned, all that matters is the file is not empty. When it does become empty, then there's some behind-the-scenes unstacking to restore the calling file (and if there isn't one, then this pass is done).
Incidentally, you'd want to keep track of what the current input line number is in each stacked input file, so it can be restored properly as the file inclusion stack unwinds. You'd want to do that so you can report any errors with the proper line numbers for any input file, of course.
What if you want to expand a macro within an included file? That's fine. Nothing special needs to be done that isn't already done. The macro expansion suspends further reading of the included file until it's done expanding (however deep it nests).
Okay, here's the rub: what if you want to include a file within a macro expansion? Hmmm...there's no real problem with switching file input to the include file. The file-stacking logic still works just fine. However, the next time the "fetch next input line" function is called, as far as it's concerned a macro is still active. So the next input line will come from the macro expansion, not the include file.
When will input start coming from the include file? Only when the macro expansion stops. So you get the effect of "delayed" reading of the include file.
Is there a work around? Sure. If the include file instruction is the LAST line of the macro (which is itself not a nested macro), then there's no more macro to expand. So the next line WILL come from the include file. Nothing's really changed as far as how things work or why they do, but it
appears to work "as expected".
So the question: should this be considered a "bug"? Is there a simple design I've overlooked that would "naturally" suspend processing of a macro expansion in favor of an include file without a lot of fuss?
I haven't given it a whole lot of thought, but if it was possible to make a macro expansion "look like" a file it might not be so bad. I can even see actually writing out a complete macro expansion to a temporary file and then switching input to that file. Is the capability so important that it would be worth going to those lengths?