News for hackers. Stuff that matters.
Re:Why I wrote a book about interpreters by haberman on Thursday December 01, 2016 @03:19PM (#13083122)
The way I would sum up interpreters and compilers, trying to put it as concisely as possible:

Step 1. Write a program that reads the input program into a data structure that exactly represents the language. You just wrote a parser.

Step 2. Write a program that operates on the data structure from (1) to execute the program. You just wrote an interpreter.

Step 3 (Optional): Write a program that converts the data structure from (1) into a different data structure that you can interpret more efficiently. You just wrote an optimizer.

Step 4 (Optional): Keep iterating on (3) until the output of your optimizer is machine code. You just wrote a compiler.

Not saying the above obviates the need for a book like this one (at all), I just had never thought of it in quite this way and wanted to write it down. :)

Re:Why I wrote a book about interpreters by Arnavion on Thursday December 01, 2016 @03:43PM (#13083336)
Nit: The output does not have to be machine code to be considered a compiler.
Re:Why I wrote a book about interpreters by haberman on Thursday December 01, 2016 @06:12PM (#13084511)
I think in everyday usage, the most common thing that distinguishes the two is that a compiler saves its output in some standard format that some other piece of software is expected to understand.

So for example, something like Babel for JS is considered a compiler, because its output is JavaScript, which is a standardized language.

On the other hand, Python is not usually considered a compiler, even though it writes .pyc files to disk, because its output is bytecode that only Python itself is expected to understand.

Even this is a little bit grey though, because we would say that Python contains a bytecode compiler as one of its components, but I don't think that most people would say that the Python binary is a compiler.

Re:Why I wrote a book about interpreters by bretthoerner on Thursday December 01, 2016 @06:40PM (#13084668)
> Python is not usually considered a compiler, even though it writes .pyc files to disk, because its output is bytecode that only Python itself is expected to understand.

I'd disagree, simply because this is exactly what javac does too.

Re:Why I wrote a book about interpreters by haberman on Friday December 02, 2016 @10:40AM (#13089452)
Java bytecode is standardized. So any JVM implementing the spec should be able to interpret the output of javac:

http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.htm...

Python bytecode on the other hand is not standardized: its bytecode is considered an implementation detail of CPython and can vary from version to version.

Your example reinforces my point.

Re:Why I wrote a book about interpreters by z92 on Thursday December 01, 2016 @08:56PM (#13085342)
But javac doesn't run it. It's run by "java". Therefore javac is called the compiler.

If things were such that "java" runs all .java files and creates those .class files as bytecode cache, then java won't have been called a compiler.

And similarly, if python had two different programs, one to create *.pyc files and the other runs it, the former one would have been called a python compiler.

Re:Why I wrote a book about interpreters by dismantlethesun on Thursday December 01, 2016 @09:46PM (#13085554)
Colloquial usage really diverges strongly from technical usage.

For example, CPython could be considered to technically be a virtual machine because it does garbage collection on dead objects since v 2.4 at least, rather than being pure reference counting.

Yet most people would call CPython a straight interpreter.

Re:Why I wrote a book about interpreters by pjmlp on Friday December 02, 2016 @03:11AM (#13086978)
Memory management is an implementation detail, doesn't have anything to do with being an interpreter or virtual machine.
Re:Why I wrote a book about interpreters by clusmore on Thursday December 01, 2016 @05:56PM (#13084380)
Yes, it took me a while to "realise" (or perhaps "accept") this as well. What is machine code? Code that a machine can execute natively? What if I built a machine that had primitive instructions for JavaScript? Then JavaScript would be machine code, and an X->JavaScript "transpiler" could be considered a "compiler".

Now what if before building such a machine, I wanted to simulate one? I could write a "virtual" machine, which is really just a program that acts like a physical machine with native JavaScript instructions. Also known as a JavaScript interpreter.

Re:Why I wrote a book about interpreters by briansteffens on Thursday December 01, 2016 @08:54PM (#13085328)
Yeah, plus even when you get down to machine code, those instructions can be implemented as microcode in the CPU. Then you have things like the lisp machines. Or V8 compiling bits of javascript to machine code while it "interprets". What exactly defines a compiler gets pretty fuzzy.
Re:Why I wrote a book about interpreters by cestith on Friday December 02, 2016 @05:23PM (#13092862)
You could write something that compiles down to JavaScript opcodes then run them in your JavaScript virtual machine. This is actually pretty similar to what Perl5 does. You wouldn't have to do that to have an interpreter, though. So your implementation of your "interpreter" fuzzes the line moreso because it indeed technically contains a compiler.
Re:Why I wrote a book about interpreters by taneq on Thursday December 01, 2016 @05:57PM (#13084389)
What, then, is the difference between a compiler and an interpreter? Just that the compiler saves its output rather than executing it?
Re:Why I wrote a book about interpreters by justinpombrio on Thursday December 01, 2016 @09:01PM (#13085357)
They have very different outputs. Compilers produce programs, while interpreters produce answers. More specifically, compilers produce programs in another language (stereotypically machine code), while interpreters run your program to get an answer.

If all you have are compilers, you can never run a program. All you can do is convert it between different languages. Fortunately your computer comes with an interpreter built in: you can think of your cpu as an interpreter for machine code, implemented in hardware.

Re:Why I wrote a book about interpreters by taneq on Thursday December 01, 2016 @09:58PM (#13085618)
So by this definition, an interpreter is a compiler plus a virtual machine, bundled together?
Re:Why I wrote a book about interpreters by cestith on Friday December 02, 2016 @05:26PM (#13092884)
It can be but needn't be. You can evaluate straight from walking your AST. You can also transform from your AST into an output then run the output. Either one is commonly called an interpreter but the former is further to that end of a spectrum.
Re:Why I wrote a book about interpreters by Patient0 on Thursday December 01, 2016 @06:55PM (#13084755)
I would define it as: when a compiler operates on its own source code, the resultant compiled compiler executes as fast as the original. When an interpreter evaluates its own source code, the resultant interpreted interpreter is an order of magnitude slower. This definition allows for things like "just in time" compilation, and rules out silly tricks like bundling an interpreter and some source code into an executable image.
Re:Why I wrote a book about interpreters by SixSigma on Thursday December 01, 2016 @11:08PM (#13085931)
Until you get to the Forth family
Re:Why I wrote a book about interpreters by bogomipz on Friday December 02, 2016 @12:41AM (#13086310)
Why does that change with going to Forth?
Re:Why I wrote a book about interpreters by SixSigma on Friday December 02, 2016 @07:08AM (#13087932)
The speed differential. Your basically making a list of jumps into machine code and then jumping in a loop. The interpreter takes your input and turns it into a jump list which it pumps into the loop. You can reuse a jump list by defining it as a new word.
Re:Why I wrote a book about interpreters by taneq on Thursday December 01, 2016 @09:34PM (#13085492)
So compilation is idempotent? That's pithy, I like it.
Re:Why I wrote a book about interpreters by kevin_thibedeau on Thursday December 01, 2016 @07:47PM (#13085044)
An interpreter controls the entire state of its execution environment. A compiler generates code, passes it over the wall, and hopes for the best.
Re:Why I wrote a book about interpreters by dougws on Thursday December 01, 2016 @07:26PM (#13084952)
This is a pretty good blog post (based on a really good paper) that describes the fundamental differences between interpreters and compilers: http://blog.sigfpe.com/2009/05/three-projections-of-doctor-f...
Re:Why I wrote a book about interpreters by yoz-y on Thursday December 01, 2016 @06:09PM (#13084492)
For me, the compiler takes input program in language A and spits out the same program in language B. An interpreter goes (more or less) line by line and executes what it reads.

In theory, compilation is reversible, execution is not.

Re:Why I wrote a book about interpreters by nv-vn on Thursday December 01, 2016 @08:20PM (#13085197)
Minor nit-pick: an interpreter need not work line-by-line, but rather (more commonly?) AST-node-by-AST-node. This isn't necessarily true for BASIC interpreters or something resembling assembly, but in general the code is represented as a tree of parsed values by the time the interpreter starts running your code.
Re:Why I wrote a book about interpreters by nickpsecurity on Thursday December 01, 2016 @05:06PM (#13084008)
It can even be source code: source-to-source compiler.
Re:Why I wrote a book about interpreters by cestith on Friday December 02, 2016 @05:31PM (#13092906)
That's technically true. Those are also known as translators, although they are technically no different from compilers.

Something that compiles to an executable format for your environment is most likely to be actually called a compiler. Some "interpreters" are actually compiling to opcodes for a virtual machine then running on the virtual machine.

Re:Why I wrote a book about interpreters by theoh on Thursday December 01, 2016 @05:45PM (#13084289)
There's a lot of mystique and prestige around the idea of compilers. It doesn't help that parsing is a fairly untidy, complex process. You're definitely right in trying to present the overall functions in a much, much simpler way.

My personal recommendation in this field is nanopass compiler architecture. It's a more manageable approach to the task of implementing a compiler, through the use of lots of incremental passes rather than a few heroic ones.

http://andykeep.com/pubs/np-preprint.pdf

NB "When a paper on the first nanopass framework was accepted for ICFP 2004, however, the program committee required the authors to refocus the paper on education because they did not believe the nanopass methodology was suitable for commercial compilers. They were principally concerned with the compile-time overhead of repeated traversals. This concern was impossible to refute at the time because the prototype infrastructure and implementation were not mature enough to put to the test."

Re:Why I wrote a book about interpreters by philiplu on Thursday December 01, 2016 @07:21PM (#13084923)
Interesting. My first compiler experience, 40 years ago, could reasonably be considered nanopass. This was the IBM Fortran IV compiler on an 11/30 clone. IIRC, the code to compile was kept in the minuscule memory, while dozens of passes were pulled in from disk to move slowly from source to machine code. As a bonus, you could usually gauge how far you'd progressed by watching the pattern of the blinkenlights.
Re:Why I wrote a book about interpreters by benibela on Thursday December 01, 2016 @05:59PM (#13084401)
>Step 2. Write a program that operates on the data structure from (1) to execute the program. You just wrote an interpreter.

I did just that. The data structure matches the language exactly.

It is also slow.

The plan was to benchmark it and optimize the slowest parts, but they turn out to be all equally slow.

It is better to skip this step and directly create some byte-code or something. Probably less work than changing it later

Re:Why I wrote a book about interpreters by haberman on Thursday December 01, 2016 @06:17PM (#13084552)
Definitely. This wasn't meant to be an actual guide as much as a way of concisely explaining the terms. You'll almost always want byte-code of some kind if you want good performance.