James Kanze said:
Michael Doubez wrote:
[...]
Any language will eventually need to be translated into some kind of
pseudo-code or byte code (even if interpreted) or else into machine
language, if it is to be executed at all. Machines can only understand
machine language. This applies to C++ as well as Java or (shudder)
Visual Basic.
Yes, but the interpreter can interpret a textual representation
without converting it into an itermediate format. I'm not
saying that it's a good solution, but I've used one or two that
worked that way.
yes...
and I know of a very trivial example:
using characters to directly drive the logic code behind such an
interpreter.
for example, the interpreter can be a sort of state machine, where each
character is dispatched via a switch and potentially either mutates state,
or switches to an alternate character dispatch loop, such that "parsing" and
"execution" are equivalent.
granted, this tends not to result in exactly "human readable" code (unless
one likes gobeledygook...), but allows for very simple, as well as
relatively fast, interpreters.
examples of where I have used this type of thing:
generating machine code (in my assembler);
driving instruction decoding (in my x86 interpreter, which uses the same
strings as the assembler);
generating special ASM fragments (various places, where I have both command
strings and FSM's (Finite State Machine) to convert these into ASM, to be
fed into the assembler);
....
another example:
I had used it before in basic genetic-programming tests, mostly as it had
allowed me to directly see the algos the GP evolver was devising, and
allowed allmost bytecode-like execution speeds.
granted, one would not design a general-purpose programming language this
way, or likely even attempt to implement a general-purpose compiler as a big
mass of FSM's...
Sure it can. Some of these, like memory management, are purely
runtime. Things like lexical scope are considerably trickier,
but they can be done.
yeah...
there is lots which can be done at runtime, and infact, many tasks are much
easier at runtime than compile time...
we just like compile-time, as it is usually faster...
anyways, namespaces and scope are hardly a problem for runtime resolution.
Lisp and Scheme have been doing lexical scoping (at runtime) for decades;
similarly, pretty much all JavaScript interpreters can do lexical scoping.
this is not the problem...
actually, there would not seem to be anything in that list which would
likely, actually, pose a problem for an interpreter.
(that or the Scheme REPL is actually just some sort of mass delusion, and
lambda's don't really exist...).
granted, if the comment were something like:
an interpreter can't really (directly) make use of statically compiled
classes and methods, I would be more inclined to agree.
it is bridging the gap between statically compiled machine code, and
high-level logic and interpretation, which is where most of the nastiness
lies...
(granted, even this can be addressed, granted it involves digging solutions
out of an "evil bag of horrors"...).
the big challenge for the average C or C++ programmer:
if given a textual string declaring a function to be called, and the
arguments to pass to it, how will you accomplish calling this function
without hard-coding any information about the function(s) to be called (the
only information available is what can be gained from standard OS API's, or
from reading in the source files).
(IOW: "if(!strcmp(name, "foo")) foo(atoi(arg1), ...);", ..., is an instant
fail).
now, consider this moment...
now, consider expanding it to support arbitrary expressions, functions,
entire compilation units, ...
can this be done? yes...
the question is to consider how to approach this.