views:

232

answers:

16

Is there a programming language, having usable interactive interpreter, even as it can be compiled to machine code?

+1  A: 

Not exactly machine code, but Java can be compiled and also used via BeanShell.

Bill Karwin
The Symantec Java IDE (forgot its name) used to have a Java compiler.
Vitor Py
+1  A: 

I've used Ruby with an interpreter, and there seems to be a compiler here.

Charlie Salts
I'm fairly certain that's actually just a tool that bundles your ruby code with an interpreter, it doesn't actually compile down to machine code. Calling it a compiler is misleading and incorrect.
Benson
I'm just reading the site as advertised. Good point, though, as most people would not know the difference unless they dug into the implementation details.
Charlie Salts
+4  A: 

Many flavors of Lisp offer both options, including Clojure.

Greg Harman
Note that Clojure is compiled to JVM bytecode.
Greg Harman
Clojure is compiled to bytecode, not machine code (unless you're counting the jitter in some Java implementations). But true on the other languages.
Chuck
@Chuck Yes, he hadn't added the comment ruling out bytecode when I wrote the original answer...
Greg Harman
+3  A: 

Two come to my mind : ocaml and scala (~= java), but I'm sure there must be a lot more out there.

Shautieh
BTW : the command to compile ocaml into native code is "ocamlopt" (ocamlc will compile to bytecode). As for scala, I am not sure whether gcj can compile its java compatible bytecode or not yet, so if someone has more info..?
Shautieh
+1  A: 

Icon used to have a compiler, but it falls in and out of maintenence. It may still work.

staticsan
+1  A: 

Python can be compiled to windows executables.

Riateche
As with the case of "RubyScript2Exe" the tool you're referring to actually just bundles the python code with a python interpreter. This is not compiling; calling it a compiler is a misuse of the term. However, it *is* incredibly useful, so from a practical standpoint that might not matter. :-)
Benson
+1  A: 

C# can be compiled by using SnippetCompiler, maybe this would act as an interactive interpreter for you?

mverardo
linqpad will also work...
Alan
+12  A: 

Haskell, using the Glasgow Haskell Compiler which has an interactive "shell" called GHCi.

Artelius
+1 for actually compiling to machine code. :-)
Benson
Same with QuickBasic actually, but that produces 16-bit machine code so this note is for historical interest.
Artelius
@Artelius i can't remember qbasic's interactive shell :)
mykhal
@mykhal: It's called the Immediate window. But note that QBasic couldn't compile (only QuickBasic could).
Artelius
Well well well, isn't Haskell the popular kid on the quad tonight? :-)
Owen S.
+14  A: 

Compilation vs. "interpretation" is essentially a matter of implementation, not the language itself. For example, MRI Ruby 1.8 is interpreted, while MacRuby is compiled to native machine code. Both include an interactive REPL. All the languages I know that have at least one machine-code compiler and at least one REPL:

  • Ruby
  • Python
  • Almost all Lisps (Lisp was the language that pioneered this technique, AFAIK)
  • OCaml
  • Haskell
  • Forth

If we're counting compilation to bytecode as well as machine code, it's true of the vast majority of popular bytecode-compiled languages:

  • Java
  • Scala
  • Groovy
  • Erlang
  • C#
  • F#
  • Smalltalk
Chuck
Smalltalk have REPL
mathk
That would be why Smalltalk is on the list of bytecode-compiled languages with a REPL.
Chuck
+2  A: 

Lua has an interactive mode for one-liners and experimentation. It normally compiles to bytecode for its VM for execution. LuaJIT is an independent implementation of a Lua VM that also does just-in-time compilation to 32-bit x86. Support for 64-bit is underway, and support for ARM is frequently requested.

Compilation to a bytecode is often a reasonable compromise between a pure interpreter and a pure compiler. The VM can be tuned to the needs of the language, and JIT techniques can analyze the VM code as it executes and concentrate on frequently executed code paths and inner loops.

RBerteig
+1  A: 

Your question is a bit vague. Even Java would fit it:

by interactive interpreter, i mean shell-like environment, where you can work in the runtime interactively.

Java has this, e.g. in the Eclipse "scrapbook pages", where you can enter Java expressions and have them evaluated right away. Java is of course also a compiled language (and while it's usually compiled to bytecode, there are various compilers that output machine code).

So what are you looking for? Maybe you could explain your problem or interest.

sleske
@sleske i'm expecting a wide variety of answers mentioning languges both capable of being compiled to an executable file and having full-fledged interactive interpreter
mykhal
+1  A: 

I tried using mono/.net for a bit and found random GC pauses to be disagreeable (at least on my crusty old laptop). I looked at using gambit-c an implementation of scheme that can compile to C but it seemed difficult to work with because the docs were somewhat limited and the packages where not very easy to install and use.

I usually just stick to having an interpreted language such as python bound to C/C++ which is more painful but at least I know what I am in for.

fuzzy-waffle
+2  A: 

As others have mentioned, OCaml.

If managed code (.NET CLI) is close enough to machine code, F# would be a candidate as well. There are probably other .NET/Mono languages which meet the requirement as well.

Michael E
+2  A: 

You may regret you asked:

C and C++.

Why?

and there are probably others out there as well.

Owen S.
+2  A: 

Plenty of languages offer an implementation that both interacts and compiles to machine code, but it's rare to do both at once. Standard ML of New Jersey is one that has an interactive loop but no bytecode: it simply compiles to machine code in memory and then branches to it.

Norman Ramsey
+2  A: 

And here's another one to burn your house down:

x86 Assembly

Yup, there are interpreters for this as well.

At this point you're really in emulator land, but it does meet the requirements you state.

I'm wondering if it's easier to name compiled languages that someone hasn't cobbled up a working interpreter for. :-)

Owen S.