jblog
toc

Fire Photographies

2017-11-18, post № 185

art, haiku, poetry,

A tiny spark flies;
Ignites a crumbled paper.
Thus fire is born.

fire-photographies-43.jpg
fire-photographies-50.jpg
fire-photographies-64.jpg

Generic C / Python Polyglot

2017-11-04, post № 184

C, programming, Python, #recreational

A polyglot — coming from the Greek words πολύς (many) and γλώττα (tongue) — is a piece of source code which can run in multiple languages, often performing language-dependent tasks.
Even though such a source code’s feature may not seem particularily useful or possible with certain language combinations, trying to bend not one but multiple language’s syntactic rules and overall behavior is still interesting.

An example of a rather simple polyglot would be a Python 2 / Python 3 polyglot — if one counts those as two separate languages. Because of their enormous similarities, one can pick out differences and use those to determine which language runs the source code.

if 1/2>0:print("Python 3")
else:print("Python 2")

Utilizing Python’s version difference regarding integer division and real division is one of the most common ways to tell them apart, as it can also be used to only control a specific part of a program instead of having to write two nearly identical programs (increases the program’s style and cuts on bytes — an important consideration if one golfs a polyglot).

However, polyglots combining languages that are not as similar as Python 2 and Python 3 require more effort. The following is a general Python 2 / C polyglot, meaning that nearly all C and Python 2 programs can be mixed using this template (there are a few rules both languages need to abide which will come apparent later).

#define _\
"""
main(){printf("C");}
#define _"""
#define/*
print"Python 2"
#*/_f

In the above code, main(){printf("C");} can be nearly any C code and print"Python 2" can be nearly any Python 2 code.
Language determination is exclusively done via syntax. A C compiler [1] sees a #define statement and a line continuation \, another two #define statements with a block comment in between and actual compilable C source code (view first emphasis).Python, on the other hand, treats all octothorps, #, as comments, ignoring the line continuation, and triple-quoted strings, """...""", as strings rather than statements and thus only sees the Python code (view second emphasis).

My first ever polyglot used this exact syntactical language differentiation and solved a task titled Life and Death of Trees (link to my answer).

Halloween MMXVII

2017-10-31, post № 183

art, poetry, #dark, #iambic pentameter, #poem, #Shakespearean sonnet, #sonnet

Gentle whispers wake my worldly being,
for long have I been laying here asleep.
My eye I open, though without seeing;
a world possessed by darkness — far and deep.
I want to try and move but must I not.
An every thought consumed by this abyss.
I try unraveling this psychic knot,
though does this fight seem to be meaningless.
Alas, before my mind is swallowed whole,
a square of light appears in front of me.
A cat inside the light; it wants my soul,
strips me of my heart so I will not be.
Without its core, my outer shell lies here,
the inner power used to fuel all fear.

— Jonathan Frech, 30th of October 2017
Manuscript.

Jonathan Frech's blog; built 2021/04/16 20:21:20 CEST