godawful sh/Lua polyglot

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

godawful sh/Lua polyglot

Norman Ramsey
I'm working on a file that is simultaneously a ksh script and a pandoc
Lua filter.  In case someone else finds it useful to have a shell script
and a Lua script be the same file, here's how it opens:

#!/bin/ksh

if false
then  --[[
else

  case $# in
    0) pandoc --lua-filter="$0" ;;
    *) pandoc --lua-filter="$0" "$@" ;;
  esac

  exit $?

fi

]]

end

... Lua code follows ...
Reply | Threaded
Open this post in threaded view
|

Re: godawful sh/Lua polyglot

Egor Skriptunoff-2
Nice idea with if-false-then-else!
Another way is to start with variable assignment:

#!/bin/sh
a=a--[[
...shell code...
]]
...Lua code...

Reply | Threaded
Open this post in threaded view
|

Re: godawful sh/Lua polyglot

Sam Trenholme
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...
Reply | Threaded
Open this post in threaded view
|

Re: godawful sh/Lua polyglot

Marc Balmer
You guys should really decide in which language to write your software....

> Am 12.08.2020 um 22:06 schrieb Sam Trenholme <[hidden email]>:
>
> 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...
Reply | Threaded
Open this post in threaded view
|

Re: godawful sh/Lua polyglot

Philippe Verdy-2
"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 <[hidden email]> a écrit :
You guys should really decide in which language to write your software....

> Am 12.08.2020 um 22:06 schrieb Sam Trenholme <[hidden email]>:
>
> 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...
Reply | Threaded
Open this post in threaded view
|

Re: godawful sh/Lua polyglot

Egor Skriptunoff-2
In reply to this post by Sam Trenholme
On Wed, Aug 12, 2020 at 11:06 PM Sam Trenholme wrote:
Here is a related ugly Lua/Python polyglot:

#!/usr/bin/env python
_rem={}
_rem={ #_rem --[=[
}
# Python code
""" # ]=] }
-- Lua code
_rem={ #_rem --[=[
""" # } ]=] }



Let me rewrite it a bit:

#!/usr/bin/env python
_rem=(#''--[=[
);del _rem

# Python code
print"Hello from Python!"

"""]=]and _rem)

-- Lua code
print"Hello from Lua"

--"""

Reply | Threaded
Open this post in threaded view
|

Re: godawful sh/Lua polyglot

Sam Trenholme
In reply to this post by Philippe Verdy-2
On 2020-08-12 14:47, Philippe Verdy wrote:
> "Polyglot programs" are useful in many contexts, notably for
> sharing some common settings in a centralized place

The way I use it is to store table data in a way which is both Lua and
Python compatible, and have comments in both Lua and Python. For
example:
https://github.com/samboy/covid-19-html/blob/master/stateNameAbbr.lua

— Sam
Reply | Threaded
Open this post in threaded view
|

Re: godawful sh/Lua polyglot

Egor Skriptunoff-2
On Thu, Aug 13, 2020 at 4:00 AM Sam Trenholme wrote:
The way I use it is to store table data in a way which is both Lua and
Python compatible, and have comments in both Lua and Python.

rem=(#''--[=[
);"""

COMMENT

"""#]=])

print"Hello from both Python and Lua"
 
Reply | Threaded
Open this post in threaded view
|

Re: godawful sh/Lua polyglot

Jonathan Goble
On Wed, Aug 12, 2020 at 9:20 PM Egor Skriptunoff <[hidden email]> wrote:

rem=(#''--[=[
);"""

COMMENT

"""#]=])

print"Hello from both Python and Lua"


That print call will only work on Python 2.x, which has been EOL since January. To be compatible with Python 3 (where print is a builtin function rather than a keyword statement), you need parentheses around the argument.