lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


"Polyglot programs" are useful in many contexts, notably for sharing some common settings in a centralized place instead of having to edit multiple files and having one of them out of sync.

Thanks the Lua interpreter supports the hash-bang syntax on the first line, that's why it works.

Another way to do that is to transclude a code fragment (generally a set of variable assignments: this works simply like a basic .ini file or .csv file (you may need to add comment separators between lines to hide some additional separators that may be needed between them).

Generally, this approach is simpler as there's less "quirk" intro.

Many Lua projects exist within environments that also use other languages: Perl, Python, _javascript_, Java, shell (C-shell, Korn-shell, Bourne-shell, and its variants, often bash, but maybe also Powershell, NT .cmd), PHP, ASP, Ruby, or even HTML and XML and even C/C++ (which can also be interpreted or fast compiled and can as well coexist easily with C-shell scripts). Such coexistence is not exceptional and there are different languages for different purposes that do well what they are designed for and that you cannot fully replace in an actual environment.

The last alternative is to write tools that will generate config files for different languages from a source config (written in any input format suitable for that tool). But this requires more discipline and a chain of actions that must be followed scrupulously, otherwise, you'll get also out of sync and the results are unpredictable.

For some languages, allowing them to be "polyglot" is more difficult as they have stricter syntactic rules (e.g. Turtle data or RDF) and they require complex escaping mechanisms.
Also, not all of them support multiline block comments or arbitrarily long string literals.

Being "polyglot" does not mean you'll write and maintain a full software with this format as it would be very impractical, but at least you should be able to "package" your software with it. Historically it has been used since very long for file archives (consider the old "shar" format: shell archives on *nix)

Le mer. 12 août 2020 à 22:37, Marc Balmer <marc@msys.ch> a écrit :
You guys should really decide in which language to write your software....

> Am 12.08.2020 um 22:06 schrieb Sam Trenholme <lua@samiam.org>:
>
> Here is a related ugly Lua/Python polyglot:
>
> #!/usr/bin/env python
> _rem={}
> _rem={ #_rem --[=[
> }
> # Python code
> """ # ]=] }
> -- Lua code
> _rem={ #_rem --[=[
> """ # } ]=] }
>
> I use a simpler version of this so I can have code which declares a table in both Lua and Python with comments for both Lua and Python.
>
> — Sam
>
>> On 2020-08-12 11:19, Egor Skriptunoff wrote:
>> Nice idea with if-false-then-else!
>> Another way is to start with variable assignment:
>> #!/bin/sh
>> a=a--[[
>> ...shell code...
>> ]]
>> ...Lua code...