## Embedding one language in another

Assembly code can be embedded in C code. Are there examples of a higher level programming language which could embed another high level programming language. HTML and javascipt does not count ;-)

## Comment viewing options

### Here is one

C is embedded in my language Kogut similarly to assembler being embedded in C. Example:

### Inline::*

Perl has a package that handles writing code in another language, calling its compiler/interpreter, and making that code available. I'm pretty sure it's usually used for C.

### Critcl

Tcl has a similar tool called Critcl:

http://www.equi4.com/critcl.html

http://dedasys.com/davidw/

Ada is very interesting when it comes to this sort of thing. Here's the RM reference. Other texts give more detailed examples.

### HTML and javascipt does not count?

Why not? Seems a variation on the theme of Turing completeness: any language that can be represented as text can be emitted from any other language for subsequent execution/interpretation.
I admit with some shame ;) that I've written some ascii-disasters that generated garbled bits of JavaScript and some SQL within VBScript, in the name of spreading the work across client, web-, and database-server.
The J2EE people are embedding a lot of Java in XML, using reflection to skirt the issue that Java is a compiled language, not a scripting language. Ahem.
Conceptually, I think that the latest C++ template metaprogramming developments over on boost.org might be considered to be implementing a functional language within a compiled one, but I'm nowhere near cool enough to grasp the fullness of all that.
Then there is this gem:
fxsl, about which I am highly curious; can anyone relate experience with it?

### some work on foreign interfaces

ML

Nick Benton: Embedded Interpreters

PLT Scheme

Eli Barzilay, Dmitry Orlovsky: Foreign Interface for PLT Scheme

More

### A rather obscure example

Do you recall cayenne, the dependently typed programming language? It was implemented in a rather awkward language called Lazy ML or just lml for short. lml was a precursor to Haskell and the hbc compiler used the lml compiler as a backend. Well, you could embed lml into your cayenne code. So that's embedding a very high level language into and even higher level language :-)

### Lush

Lush, a Lisp for numerical computing, allows you to mix Lisp and C in functions.

### Interpreters and preprocessors

Naturally, any language can be embedded in any other via one of the following mechanisms:

* Writing an interpreter for the embedded language in the host language. The host language treats the embdded source in the embedded language as data; non-trivial analysis of the embedded code is deferred until the interpreter executes.

* Use of a preprocessor/macro system to translate the embedded language into the host language. This case covers use of standalone preprocessors, macro preprocessors specified by the host language (like C preprocessor), or integrated macro systems (like Lisp/scheme has). SQL used to be done this way. Since we allow use of standalone preprocessors, any language can be obviously metaprogrammed in this fashion.

* Use of directives defined within the host language to embed code fragments of a different language. This is, I suspect, what the original poster meant--the "asm" block is a separate syntactic construct in C, and thus a fundamental part of the language. (Of course, asm directives do very little checking on the specified opcodes, merely passing them down to the assembler).

Two other common ways of mixing code (whether these constitute "embedding", as the source written in different languages is segregated in these last cases) are:

* Use of foreign function interfaces--whether simple things like extern "C" or JNI, or complicated beasts like RMI

* Use of compenent frameworks/architectures designed for inter-language use.

### Quines and polyglots

Somewhat related topics are quines (programs that output themselves) and polyglots (programs that behave observationally identically in different PLs).

### Not to be a wet blanket...

...but is there any real importance to quines and polyglots, or are tese in the category of amusing/cool hacks that are otherwise uninteresting from a theoretical and/or practical viewpoint? Especially for PL designers?

### Well, if your PL that you designed is HQ9+

then quines are a large consideration. :-)

Personally I'd rather see an eval/apply contest wherein the source code becomes self compiling.

### Metaprogramming

I think of quines as a kind of fixed points of meta-programming.

Both them and polyglots are good for exploring the PL in a bit unusual perspective, but that's a personal opinion. I don't see any formal importance, but then again I don't believe in the ideal of pure formality.

### Sort of

Polyglots are primarily a curio, although they *may* be instructive with regards to ways of embedding one language inside another.

Quines, on the other hand, give some insight into the metaprogramming ability of a language. It is no coincidence that lisp (or, in fact, lambda calculus) has one of the smallest quines around:
((LAMBDA (X) (,X ',X)) '(LAMBDA (X) (,X ',X)))

### Biology

...is there any real importance to quines and polyglots...

The best example of a quine I can think of is the DNA program running on your personal "cell" hardware. And I suppose a cross-species virus is somewhat similar to a polyglot.

### There are real uses...

I actually did a quine that had a real, practical use. I was writing a script that performed a random drawing of people to win a prize. In order to create a paper trail and make sure that everything was above-board, I had the program print itself out as part of the listing.

### Linking to Quines and polyglots

Hi Andreas,

Just a note: best to link to Wikipedia itself, not a syndicate like answers.com unless you have a particular reason. So Quines and polyglots, version #2.

### Metaprogramming example

http://dslengine.sourceforge.net/

http://arxiv.org/abs/cs.PL/0409016

### Another example

Oracle preprocessors like Pro*C, Pro*Fortran, ...

### Embedding one language in another

Is this related to integration between langauges in different language "paradigms"?

An example of integrating a functional language to a logical language is the programming language Mercury. Mercury achieves this by being a logical language at base, and providing a little sugaring that gives a functional notation that exhibits indeed functional semantics.

An opposite approach seems possible, althogh perhaps not so easy, of having a language be functional at base and implementing logic within it and providing any syntactic sugaring that would be necessary to make the logic convenient to write in.

Is there any reason to prefer the approach that puts functional programming at the base over the approach that puts logic programming at the base?

Would an integration where each language thinks it is simulating the other be a worthwhile goal? Would achieving it require a conceptual infinite tower of interpreters?

### Functional/Logic integration

Is there any reason to prefer the approach that puts functional programming at the base over the approach that puts logic programming at the base?

The fact that functional programs are relational but relational programs aren't functional. But there are functional logic languages that emphasize the functional aspect over the logic aspect, e.g. Curry

Would an integration where each language thinks it is simulating the other be a worthwhile goal?

Probably not, I don't see what it buys the user. A language where there doesn't appear to be two (or more) languages seems more worthwhile.

Would achieving it require a conceptual infinite tower of interpreters?

Refci!