News for hackers. Stuff that matters.
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.