TinyBF Hidden in C
12/01/2022
Overview
A while ago, there was a challenge on CodeGolf where you write a program that outputs “Hello, World!” and contestants had to guess which lang it was written in. One of the contestants submitted this piece of code that outputs TinyBF when compiled with C but “Hello, World!” when ran with TinyBF.
I, c, o, d, e, C;
i;
main()
{
i++;
for (i++; i ^ 9; i++)
putchar(((i + 69) * !(i + 2 * ~0) | (9 != 9 + (I = ((i - 1) >> (i + 2 * ~0)) + ~(!(i + 2 * ~0) + ~0))) * 111 | 115 * (6 > i + 1) * (i > 3) + ~(i > (10 + (9 > i) + ~i + (i > 9))) + (5 < i) + (i < 5) + 1 | (c = (i > 6)) | (o = (i >= 7 + !i)) | (d = (i >> 1 > 3) * (i * ((i + ~0 > 5) << 2) + (i > ~2 + i))) | (e = (i - ~0 > (i | 5) && 32 >> i) * 99) | (C = (i >> (i >> 2 + i / 7) > 0) * (i + (i << 1) + (i << 2) + (i << 3) + (i << 4) >= (i != 6) * (5 >= i) * (i + (i << 5))) * (i + (i * i) + 62) * ((i == 6) != !i))) + (i * i + (i << 1) + (31 + i ^ i) + (i + i) * ~0 + 2 * i) * (1 == (i == 7)));
I |= (c += (o > d)), 2 * (c + C > o + d);
e ^= (d >> c) | 4;
I -= (e > C) + (I + c + (o == C) - ~7 * (C <= I) >> (C >= o));
C = (e >> 2) ^ (I - ~o == c), o = 255 >> ((int)1e7 * (c - ~1) >> (C + e + d + o + I)), i |= i + (e >= d) + (2 << I) + (3 << c);
putchar(!(I + d + c >= (C | e)) ? (I > o) + (d = (20 * (I == c)) >> (1 == ~I + d + e + (C == (1 >= (I == C))))) : (I + o + C) * ((C + e) / 5 + C + I + 20 + I + I == 1 >> (o >> (d >= (C != I)))));
}
What is TinyBF?
TinyBF is Brainf**k with reduced character set. Simply put, TinyBF uses the =
character to switch between directions, which eliminates the need for >,<
in Brainf**k. It also eliminates [,]
characters with just one |
character. Finally, it replaces ,
with |=|
and .
with ==
. All other characters are ignored in TinyBF.
This motivated me and my friend to build a transpiler that could merge C and TinyBF code into one file. It was a fun exercise and we needed an idea for the LangJam.
Process
Our approach was to scan C code for uses of TinyBF character set, which are =,|,>,+
. Valid C tokens that uses these characers are the following
TinyBF Character | C Tokens |
---|---|
+ |
+, ++, += |
= |
=, ==, !=, +=, -=, ^=, /=, &=, >=, <=, >>=, <<=, %=, |= |
| |
|, ||, |= |
> |
>, >>, >>= |