Christian Weisgerber <
naddy@mips.inka.de> writes:
On 2024-10-12, Rainer Weikusat <rweikusat@talktalk.net> wrote:
Indeed. As far as I know the term, an interpreter is something which
reads text from a file, parses it an checks it for syntax errors
and then executes the code as soon as enough of it has been gathered to
allow for execution of something, ie, a complete statement. This read,
check and parse, execute cycle is repeated until the program
terminates.
>
I don't really want to participate in this discussion, but what
you're saying there is that all those 1980s home computer BASIC
interpreters, which read and tokenized a program before execution,
were actually compilers.
If they contained something which compiled all of the source code prior
to execution in order to transform it some actually executable
intermediate representation whose execution didn't require future access
to the source code and thus, also didn't include checking the source
code for syntactical correctness, this something can be called a
compiler and the execution engine some sort of virtual machine which
could principally execute programs compiled from source code in any
programming language.
But judging from Wikipedia, Murkysoft Basic stored programs as linked
list of preprocessed lines and interpreted these, ie, doing string
lookups of keywords from the source code at run time in order to
determine what code to execute. Insofar I vaguely remember this from
Apple //c BASIC (has been a while) syntax errors would also be found at
runtime, ie, once execution reached the line with the error. This would
make it an interpreter.
In constrast to this, this somewhat amusing small Perl program:
while (<>) {
while (length) {
s/^(\w+)// and print(scalar reverse($1));
s/^(\W+)// and print($1);
}
}
[reads lines from stdin and prints them with each word reversed]
gets translated into an op tree whose textual representation (perl
-MO=Concise,-basic) looks like
this:
y <@> leave[1 ref] vKP/REFC ->(end)
1 <0> enter v ->2
2 <;> nextstate(main 1 a.pl:1) v:{ ->3
x <2> leaveloop vKP/2 ->y
3 <{> enterloop(next->r last->x redo->4) v ->s
- <1> null vK/1 ->x
w <|> and(other->4) vK/1 ->x
v <1> defined sK/1 ->w
- <1> null sK/2 ->v
- <1> ex-rv2sv sKRM*/1 ->t
s <#> gvsv[*_] s ->t
u <1> readline[t2] sKS/1 ->v
t <#> gv[*ARGV] s ->u
- <@> lineseq vKP ->-
4 <;> nextstate(main 3 a.pl:2) v:{ ->5
q <2> leaveloop vKP/2 ->r
5 <{> enterloop(next->m last->q redo->6) v ->n
- <1> null vK/1 ->q
p <|> and(other->6) vK/1 ->q
o <1> length[t4] sK/BOOL,1 ->p
- <1> ex-rv2sv sK/1 ->o
n <#> gvsv[*_] s ->o
- <@> lineseq vKP ->-
6 <;> nextstate(main 5 a.pl:3) v:{ ->7
- <1> null vK/1 ->f
9 <|> and(other->a) vK/1 ->f
8 </> subst(/"^(\w+)"/) sK/BOOL ->9
7 <$> const[PV ""] s ->8
e <@> print vK ->f
a <0> pushmark s ->b
- <1> scalar sK/1 ->e
d <@> reverse[t6] sK/1 ->e
b <0> pushmark s ->c
- <1> ex-rv2sv sK/1 ->d
c <#> gvsv[*1] s ->d
f <;> nextstate(main 5 a.pl:4) v:{ ->g
- <1> null vK/1 ->m
i <|> and(other->j) vK/1 ->m
h </> subst(/"^(\W+)"/) sK/BOOL ->i
g <$> const[PV ""] s ->h
l <@> print vK ->m
j <0> pushmark s ->k
- <1> ex-rv2sv sK/1 ->l
k <#> gvsv[*1] s ->l
m <0> unstack v ->n
r <0> unstack v ->s
Each line represents a node on this tree and the names refer to builtin
'ops'. In the actual tree, they're pointers to C functions and execution
happens as preorder traversal of this tree and invoking the op functions
from the leaves to root to produce the arguments necessary for invoking
op functions residing at a higher level in this tree.
Modules for writing this internal representation to a file and loading
it back from there and even for translating it into C exist. They're
just not part of the core distribution anymore.