[Mono-list] Mixing languages within

Michal Moskal Michal Moskal <michal.moskal@gmail.com>
Fri, 10 Dec 2004 15:07:20 +0100


On Thu, 9 Dec 2004 15:38:14 -0800 (PST), Kelly Leahy
<kellyleahy@swbell.net> wrote:
> That being said, the question was about mixing code in
> a single source file.  I don't think this should be or
> would ever be possible.

It is easy to mix source languages in one file -- you just need to
write a simple preprocessor, that separates the content to files. This
has been done with various literate programming systems.

Then, if all the compilers involved support IL output, then it is easy
to merge -- even if they cannot produce .netmodule, you can ildasm
dlls produced, cat them together and ilasm.

The problem is cross references -- that is if part in language A needs
part in language B and vice versa. Then the compilers need to agree
much more -- upon some internal APIs, which seems quite difficult in
general.

However there is also a possibility to generate language A from
specialized languages B and C (that is C# from the string processing
languages), and then just run the output through the A compiler. If B
and C do not need much information about parts written in A, then it
is quite easy -- most of the semantic checks are done by the A
compiler. The drawbacks are the need for separate program, but also,
which is more important, misleading error messages from the A
compiler, for errors in B or C parts.

Yet another possibility is to host the B and C compilers inside the A
compiler -- they would use A internal representations, produce proper
error messages and so on. The hard part is the embedding -- you need
to know a lot about the compiler you're going to use. In most cases
you'll also need the sources for it.

<adv>However there are some compilers, like the Nemerle compiler, that
support easy embedding of such stuff within separate plugins. I refer
you to
http://nemerle.org/macrouse.html#embedded-languages for some more
info.</adv> :-)

-- 
: Michal Moskal ::: http://nemerle.org/~malekith/ :: GCS !tv h e>+++ b++
: ::: Logic is a nice contrast to the Real World. :: UL++++$ C++ E--- a?