@DavidCarlisle I'm sorry but the logic makes no sense to me. I realize that tex processes things a bit different but in standard programming things work differently. AfterEveryPage is a macro(a function) called after every page has been "created" by TeX, right? That is, it processes the .tex input file and forms the output(which is a pdf in my case) and for each page in the pdf that is generated, AfterEveryPage is executed. This way we can add watermarks or other things to the generated pages after they have been generated. Correct so far or not?
I'll assume I'm right to continue to the next issue. \thepage is a macro that returns the current page that has been generated. We could use it, for example, along with AfterEveryPage to number the pages. Right? Ok, if that's correct \inputlineno returns the line number for the input source code? If so, by the same logic as \thepage, we could have a macro AfterEveryInputLine that would be called after each input line has been procssed, and say, turn .'s in in to !. (it may not be robust but in some cases it should work) Is that right? In fact with such a macro we could apply it to your example
We don't have a macro to do that of course. But I'm actually only interested in mapping the line of a source code to the page that it appears(if it does) in the pdf. AfterEveryLine would allow that to work but all we have is AfterEvery page. Now, the way I see it is that somewhere in the example you gave there are "imaginary" \newpage's that are inserted because the tex code has went beyond the page. Those "imaginary" \newpages could be called, like you have done with typeout directly. But we don't want to do it have to manually find the spot where the break is by comparing it with the pdf.
So, we use AfterEveryPage to find it for us(that is it's purpose) and stick the typout in that macro. BUT when we do this we do not get the same results as if we manually did it(Just like how your AAA typeout returns 15 but AfterEveryPage's version(the same purpose) is much different). This is illogical to me in standard programming interpretation because it means that the macro AfterEveryPage doesn't expand \thepage and \inputlineno the same way as the AAA version(even though they are the same code). I realize that macros in tex behave different than typical computer procedures.
and I realize that macros have the ability to alter the context from what one interprets and this gives tex some power and flexibility that it wouldn't otherwise have. OTH, it makes certain simple things difficult and "tricks" or complex methods must be used. This is fine, I don't mind that, but regardless of how tex works the results must be interpretable in a logical manner. One can't expect another to know everything about the tex architecture to realize that basic logical reasoning(e.g., implication, substitution principles, etc) cannot be used as normal.
I do not believe that you have explained sufficiently to me why the two cases are different. My mind is seeing \AddEverypageHook{\typeout{Page = \thepage, Line = \the\inputlineno}}
and \typeout{AAA:Page = \thepage, Line = \the\inputlineno}
should function identically except that AddEverypageHook should be automatically executing the macro(inserting it in to the stream as if I typed it manually, as a function call would work in C/C++/C#/D/F#/php/perl/python/asm/JS/etc/etc/etc) at the end of each page in the output.
This is obviously not the case and I don't understand why except that in tex, the macro is effectively not substituted "inline". I have not yet figured out what it actually does. It seems that macros act as state machines and they can be nested. The outer state for some things(such as inputlineno) is not the same as the inner state. I can see that this might be ok, but then the question is how to get the outer line number. I do not believe this is the problem though and I have ideas but nothing conclusive. I'll shut up now. Hopefully I've explained my thinking enough for you to help me.