diff options
Diffstat (limited to 'content/html/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.html')
| m--------- | content/html | 6 | ||||
| -rw-r--r-- | content/html/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.html | 146 |
2 files changed, 6 insertions, 146 deletions
diff --git a/content/html b/content/html new file mode 160000 +Subproject 27ed46c9129db86669052d4e7211340da1081d8 diff --git a/content/html/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.html b/content/html/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.html deleted file mode 100644 index 09b89bd9..00000000 --- a/content/html/gemfeed/2014-03-24-the-fibonacci.pl.c-polyglot.html +++ /dev/null @@ -1,146 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> -<head> -<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> -<title>The fibonacci.pl.c Polyglot</title> -<link rel="shortcut icon" type="image/gif" href="/favicon.ico" /> -<style type="text/css"> -body { - margin: auto; - max-width: 900px; - background-color: #FFFFEF; - border: 1px dashed #880000; - border-radius: 8px; - padding: 5px; -} -img { - display:block; - max-width: 80%; -} -a.textlink:before { - content: " ⇒ "; - padding-left: 2px; -} -a.textlink { - text-decoration: none; - color: #FF0000; -} -a.textlink:hover { - text-decoration: underline; -} -i { - color: #48AAAD; -} -pre { - background-color: #F1F8E9; - border: 1px dashed #BB0000; - border-radius: 8px; - padding: 5px; - font-family: "Lucida Console", "Courier New", monospace; -} -h1 { - text-align: center; - color: #880000; -} -h2, h3 { - color: #BB0000; -} -</style> -</head> -<body> -<h1>The fibonacci.pl.c Polyglot</h1> -<p class="quote"><i>Written by Paul Buetow 2014-03-24</i></p> -<p>In computing, a polyglot is a computer program or script written in a valid form of multiple programming languages, which performs the same operations or output independent of the programming language used to compile or interpret it</p> -<a class="textlink" href="https://en.wikipedia.org/wiki/Polyglot_(computing)">https://en.wikipedia.org/wiki/Polyglot_(computing)</a><br /> -<h2>The Fibonacci numbers</h2> -<p>For fun, I programmed my own Polyglot, which is both, valid Perl and C code. The interesting part about C is, that $ is a valid character to start variable names with:</p> -<pre> -#include <stdio.h> - -#define $arg function_argument -#define my int -#define sub int -#define BEGIN int main(void) - -my $arg; - -sub hello() { - printf("Hello, welcome to Perl-C!\n"); - printf("This program is both, valid C and Perl code!\n"); - printf("It calculates all fibonacci numbers from 0 to 9!\n\n"); - return 0; -} - -sub fibonacci() { - my $n = $arg; - - if ($n < 2) { - return $n; - } - - $arg = $n - 1; - my $fib1 = fibonacci(); - $arg = $n - 2; - my $fib2 = fibonacci(); - - return $fib1 + $fib2; -} - -BEGIN { - hello(); - my $i = 0; - - for ($i = 0; $i <= 10; ++$i) { - $arg = $i; - printf("fib(%d) = %d\n", $i, fibonacci()); - } - - return 0; -} -</pre> -<p>You can find the whole source code at GitHub:</p> -<a class="textlink" href="https://github.com/snonux/perl-c-fibonacci">https://github.com/snonux/perl-c-fibonacci</a><br /> -<h3>Let's run it with Perl:</h3> -<pre> -❯ perl fibonacci.pl.c -Hello, welcome to Perl-C! -This program is both, valid C and Perl code! -It calculates all fibonacci numbers from 0 to 9! - -fib(0) = 0 -fib(1) = 1 -fib(2) = 1 -fib(3) = 2 -fib(4) = 3 -fib(5) = 5 -fib(6) = 8 -fib(7) = 13 -fib(8) = 21 -fib(9) = 34 -fib(10) = 55 -</pre> -<h3>Let's compile it as C and run the binary:</h3> -<pre> -❯ gcc fibonacci.pl.c -o fibonacci -❯ ./fibonacci -Hello, welcome to Perl-C! -This program is both, valid C and Perl code! -It calculates all fibonacci numbers from 0 to 9! - -fib(0) = 0 -fib(1) = 1 -fib(2) = 1 -fib(3) = 2 -fib(4) = 3 -fib(5) = 5 -fib(6) = 8 -fib(7) = 13 -fib(8) = 21 -fib(9) = 34 -fib(10) = 55 -</pre> -<p>It's really fun to play with :-).</p> -<p>E-Mail me your thoughts at comments@mx.buetow.org!</p> -<a class="textlink" href="../">Go back to the main site</a><br /> -</body> -</html> |
