My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Challenge! Multi-Language files

Marco Alka's photo
Marco Alka
·Apr 4, 2019

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?