Challenge! Multi-Language files

I just pulled out an old script of mine, which is amazing, because it uses several programming languages in one file, while at the same time having next to zero dependencies and being able to run on anything Windows, as long as it is at least XP. So I just wondered, if it is possible to do something similar with other languages in other situations.

Let me challenge you to find convenient programming language pairs one can put into a single file and leverage the advantages of each of them! Also describe how the coupling works. Note: HTML is not a programming language 😉

As for my example: CMD ❤ JScript - just save as hello.cmd and execute on Windows

@if (@CodeSection == @Batch) @then
    @echo off

    rem This is CMD

    echo Hello from CMD
    cscript //E:JScript //nologo "%~f0"
    exit /b
@end

// This is JScript

WScript.Echo("Hello from JScript");
WScript.Quit(0);

This works thanks to CMD and JScript both understanding the first line, but interpreting it differently.

JScript's multiline if-statements need to begin with an @, so we can write several lines of stuff into an if-block which evals to false in JScript, hence the CMD inside is never interpreted by JScript and it jumps straight to @end.

CMD can put an @ before statements in order to execute them, but not write the output to the console, so we can write @if in CMD, too. Since CMD also evals the if-condition to false, it will not run the following statement, which is @then. Yeah, CMD has single-line if statements in this case. It will, though, just go on executing code in the next line, so the CMD code has to be at the top and needs to exit before @end. The CMD also calls the JScript interpreter and passes its own file to it for execution, so the JScript part is evaluated, too.

There is a second way which uses a similar trick to put CMD and JScript into the same file, however I'll leave that to you 😉

What kind of languages will you put together to get things done?

Comments (4)

Add a comment
Mark's photo

One obvious extra language is Whitespace with anything that doesn't require indenting.

And maybe Fortran's fixed form could also be useful: first 5 chars are labels and anything after 72 is ignored ( info ).

Combining Javascript and Typescript, or even Python 2 and 3 or pypy, is considered cheating I guess?

I guess you could do something with //, which is a comment in some languages but integer division in others...

I don't have time to make something right now though...

Show all replies
Mark's photo

Currently focussed on [every programming languages and all of the projects]

And legit to put code in git history.

Alex Stanford's photo

Wow, thanks a lot for sharing this. I was searching for this information for my research with https://www.masterpapers.com/article_writing and thankfully I found here all that I needed.