Writing a triple polyglot script

In the previous post I've described how to write a script that's both valid Ruby and Python. Then I came up with the idea of adding Perl on top if it.

So, the basic form of the polyglot code (with slight modifications) is this:

(0 and eval("puts 1") or 1) or eval(compile("print(2)", "", "exec"))

Now, if I could find a value that sets Perl apart from Ruby & Python and add it to the expression above, I'd be able to run some code via eval. Luckily, quick google search revealed that "0" is falsey in Perl. After a bit of tinkering I've managed to construct something like this:

(0 and (eval("puts 1") or 1)) or ("0" and (eval(compile("print(2)", "", "exec")) or 1)) or eval("print 3;")

That's quite a lot of brackets to make non-lisper's eyes bleed, so here's the brief summary of what's going on:

(0 and (<ruby_expr> or 1))
  or ("0" and (<python_expr> or 1))
    or <perl_expr>

making code "readable"

Again, this is still not enough for me, since I wanted placeholders that would let me write more "readable" code inside eval calls (read: multiline strings).

And this is where I encountered a small problem. Python supports multiline strings only via triple-quotes. Perl (unlike Ruby) raises syntax error on that. So I needed some workaround.

The solution was to use q "function" in Perl:

substr(q("""
<perl_stmts>
"""), 3, -3)

Syntax is still valid in 3 languages. But there's a catch - q function does not exist in Ruby and Python, but defining it should be easy using the same techniques described earlier:

(0 and eval("def q(x) x end", binding)) or ("0" and (eval(compile("def q(x):\n return x", "", "exec")) or 1));

Now we can wrap the rest of the strings in q identity function, and we're done.

creating a "practical" application

I didn't spend so much time just for nothing, so here's a working fizzbuzz example:

(0 and eval("def q(x) x end", binding)) or ("0" and (eval(compile("def q(x):\n return x", "", "exec")) or 1));

(0 and (eval(q("""
1.upto(100){|i|puts ('fizzbuzz '[n=i**4%-15,n+13] || i.to_s).rstrip}
""")) or 1)) or ("0" and (eval(compile(q("""
for i in range(1,101): print('fizz'*(i%3==0) + 'buzz'*(i%5==0) or i)
"""), "", "exec")) or 1)) or eval(substr(q('''
print "fizz"x!($_ % 3) . "buzz"x!($_ % 5) || $_ , "\n" for 1 .. 100;
'''), 3, -3))

Triple-hirable.

January 28, 2020 · code

Writing a polyglot script

So here's the thing - I'm relatively good at Python. I somewhat know Ruby. Not good enough to write something complex, but enough to understand that it's "a bit" different from Python. Yet both of the languages have similar syntaxes, which made me think whether I could come up with a script that's valid in both languages.

The shortest valid script could be:

print("hello world")

I thought that was too easy, so I set a goal to write a script that would print the exact language used. After a bit of tinkering, I came up with this:

print(["ruby", "python"][(0 or 1)])

I've remembered that one of the main surprises the developers who switched from one language to another encountered was the fact that 0 is falsey in Python, whereas in Ruby it is truthy. So the expression 0 or 1 evaluates to 0 and 1 in Ruby and Python, accordingly. And if we use just that to get the value at the corresponding index of the specially crafted list, we can achieve the given task. As a side note, we would need to enclose the boolean expression in parentheses, because otherwise it'll throw a syntax error in Ruby.

I could have settled here, but I decided to raise the bar higher. What if I try to come up with a script that could do more sophisticated things than just printing some values? Now that's where things get more complicated, since you can't just do anything in one languages that would be also correct in another. But I've finally managed to come up with a somewhat decent solution with a few neat tricks:

(0 and eval("""
3.times {
  puts 'ruby code goes here'
}
true
""")) or eval(compile("""
for i in range(3):
    print('python code goes here')
""", "", "exec"))

So what's happening in here:

  1. The code above is a single statement of the form (0 and <expr1>) or <expr2>. Both Python and Ruby support short-circuiting for boolean expressions, therefore expr1 evaluates in Ruby and expr2 in Python only.
  2. It heavily relies on metaprogramming, by evaluating only a single function that could contain more complex code inside a string. Ruby's eval needs to have a truthy value as the last statement, which is returned as the function's value. If we fail to do so, Ruby will try to execute expr2. For Python we could have used expr, but I realised that it's not available in Python 2 (which had not reached the EOL at the time when I was experimenting, so as a well-mannered Pythonista I had to support it as well).
  3. Using triple-quote strings is a nice addition to write more readable code without providing escape sequences for newlines where necessary. In Python it's the only way to create multiline strings. Ruby, on the other hand, supports multiline strings by default with single and double quoted strings, and treats """foo""" as three separate strings "" "foo" "" which then get concatenated to one. This, however, makes using double (or single) quote characters tricky in the code placeholders.
January 08, 2020 · code

Writing an ouroboros quine

Smart people write C. Pragmatic people write Go. Only a fool would write C code that outputs Go code that outputs the original C code.

So I made one. Below is the code:

#include <stdio.h>
int main(){
char*c="#include <stdio.h>%cint main(){%cchar*c=%c%s%c,o[1000],g[1000];%csprintf(g,%cpackage main;import%ccfmt%cc;func main(){fmt.Print(%cc%ccs%cc)}%c,34,34,96,37,96);%csprintf(o,c,10,10,34,c,34,10,34,37,37,37,37,37,34,10,10,10,10,10);%cfprintf(stdout,g,o);%creturn 0;%c}%c",o[1000],g[1000];
sprintf(g,"package main;import%cfmt%c;func main(){fmt.Print(%c%cs%c)}",34,34,96,37,96);
sprintf(o,c,10,10,34,c,34,10,34,37,37,37,37,37,34,10,10,10,10,10);
fprintf(stdout,g,o);
return 0;
}

If you run it, you should get the result below:

$ cc ouroboros.c -o ouroboros
$ ./ouroboros > ouroboros.go
$ go run ouroboros.go > ouroboros2.c
$ diff ouroboros.c ouroboros2.c && cowsay "wtf?"
 ______ 
< wtf? >
 ------ 
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

How

One of the simplest quines in C looks like this:

#include <stdio.h>
int main(){
char*c="#include <stdio.h>%cint main(){%cchar*c=%c%s%c;%cprintf(c,10,10,34,c,34,10,10,10,10);%creturn 0;%c}%c";
printf(c,10,10,34,c,34,10,10,10,10);
return 0;
}

I'm not going to dive into the details - there's a detailed explanation on the tricks and examples for writing quines available in Rosetta Code's wiki page.

Since I wasn't going to waste my time hand-crafting the string that should contain the original source code, I wrote Python code which generates the C code above:

quine = r'''#include <stdio.h>
int main(){
char*c="?";
printf(c,!);
return 0;
}
'''

quinestr = r''
args = []
for i, ch in enumerate(quine):
    if ch in '\n"':
        quinestr += '%c'
        args.append(ord(ch))
    elif ch == '?':
        quinestr += '%s'
        args.append('c')
    else:
        quinestr += ch

args = ','.join(map(str, args))

print(
    quine\
        .replace('?',
            quinestr\
                .replace('?', '%s')\
                .replace('!', args))\
        .replace('!', args),
    end='')

To go further than just a simple quine, I had to make the quine self-aware, storing the output string in a variable. To do so I updated the template C code to the one below:

#include <stdio.h>
int main(){
char*c="?",o[1000];
sprintf(o,c,!);
fputs(o, stdout);
return 0;
}

The variable o now holds the value of the source code. Next, I came up with Go code snippet that should output it:

package main;import"fmt";func main(){fmt.Print(`<your_quine_here>`)})

I used back-quoted string since they can contain any characters, even newlines, which will be present in the o.

And the final tricky part - I needed to add the Go code as a C string:

#include <stdio.h>
int main(){
char*c="?",o[1000],g[1000];
sprintf(g,"package main;import%cfmt%c;func main(){fmt.Print(%c%cs%c)}",34,34,96,37,96);
sprintf(o,c,!);
fprintf(stdout,g,o);
return 0;
}

The variable g now contains the template Go code. The trick here is making sure that the variable for Go code contains %s and `, but implicitly. The former needs to be used in the next sprintf call to do the substitution, and the latter can't be explicit because back-quoted strings in Go cannot contain back-quote characters.

The final Python code which outputs the C code which outputs the Go code which outputs the C... you got it, is available here.

Why

Why not?

November 20, 2019 · code · unix

Brief history of CSS colors

I've decided to create a personal blog. After a few hours of searching for a perfect template, I went with Rosa theme. The only thing I changed (apart from minor cosmetic HTML tweaks) was the link color. I set it to steelblue, which is visible when you hover over a link. I don't have time for choosing the perfect color, because I have more important things to spend my time on.

Which is - finding out where did steelblue color come from to CSS. The search of color origins revealed a lot of interesting backstory, forgotten memes and even a tragic loss of the people behind the computing history.

History of colors in CSS

In early 90's Tim Berners-Lee publishes the first prototype of what would become WWW. Internet becomes a new hot thing, HTML becomes the language of the Web, and in 1994 Tim Berners-Lee founds W3C - organization behind standardization of the Web.

People start building websites, and one of the most requested features is document customization. Web developers want to be able to layout and add colors to their HTML. So CSS1 recommendation came in 1996 to address this. It specifies a lot of niceties, among which is ability to set colors, both using RGB codes and keywords. Color name keywords were taken from MS Windows 16 color palette. orange was added later on to CSS2.

CSS3 came around not long after. For the reasons unknown to me, 2001 draft spec added even more color keywords, this time from X11 (windowing system for Unix-like operating systems). It had a fair amount of criticism, but ends up in a spec anyways. This spec mentions steelblue for the first time.

In 2014 rebeccapurple was proposed and added to CSS4 spec, in memoriam of Eric A. Meyer's daughter Rebecca, the man who played major role in CSS advocacy.

History of colors in X11

So steelblue came from X11, it turns out. And to answer the initial question we need to go further back in history.

Xerox releases Alto in 1973 - the first computer with what would become today's GUI. During late 70's and early 80's GUI becomes a new hot thing. Microsoft and Apple present operating systems which offer GUIs.

In the meantime enthusiasts from MIT come up with X Window System in 1984. The next year X adds color support. Jim Gettys creates a list of color names, with "steelblue" among them.

Paul Raveling updates the list in 1989. Being a software engineer with a total lack of control, he ends up adding a bunch of new colors (dodgerblue?), changing the existing ones during the course. This update, which includes final "tuned" version of steelblue, ends up in CSS spec along with others' contributions.

The End.

August 10, 2019 · web · unix

Hello World

This is the first post. That's about it.

August 08, 2019