I originally wrote to my friend Tim and asked this
question:
Have you ever thought of how this works?
<%
var a = {"qweqwe", "asdasd"}, i;
for (i in a)
{
%>
<%= i %> <%= a[i] %>
<%
}
%>
I can't see how it could be the case that the host
of this code passes over the first part (up to the
for's opening brace) to the scripting engine, then the
two variable prints, then the closing brace. For one
thing, how would the script host (ASP, XCODE, etc.)
regain control at the print statements when the script
language returns to the top of the loop?
I can see how this might be altered by the host to
this: ...everything up to '{'
Response.Write (i);
Response.Write (" ");
Response.Write (a[i]);
Response.Write ("\n");
}
and then the translated block is sent to the
scripting engine.
Put it this way: how could it work for the scripting
language to stop and start (as the code is written)? I
think that the host has control and translates the
text, the scripting engine executes the translated
text, and the host emits the text as written by the
scripting engine.
This led to a later formulation of the problem:
There are many problems, BTW, in doing inclusion as
an operation within the language an include file
does not have to be a complete translation unit, it can
call methods that appear in later include files, etc.
These observations about complete translation units
underline why I don't think that you can change
languages inside a block written in a different
scripting language. The scripting engine needs to work
on a complete block in its language. And that's why I
think that this is very odd:
<% for (i = 1; i <= 10; i++)
{ %>
Tim has <%= i %> big warts.
<% } %>
To parse this, a scripting engine needs a completely
syntactic statement. The host can't pass the engine
"for (i = 0; i < 10; i++) {" and then expect that
the engine will execute it once, pass control back to
the host, back and forth ten times and then the host
knows that the scripting engine is done. I think that
the host has to translate this immediately into an
equivalent block that uses printf-like function calls
for the plain text output, like this:
<% for (i = 1; i <= 10; i++)
{
Response.Write ("Tim has ");
Response.Write (i);
Response.Write (" big warts.");
} %>
But I have problems even with that because then the
host has to know how to write the method call, and the
main problem is that VBScript and JavaScript (and
probably every other language) have different rules for
terminating the statement and quoting text and other
syntactic issues.