Programs that print their own source code

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Re: Programs that print their own source code

Postby Meteorswarm » Sat May 08, 2010 6:48 pm UTC

Yeah, on Ubuntu Lucid it still fails:
Code: Select all
alec@Kongming:~$ echo '#!md5sum' > md5
alec@Kongming:~$ chmod +x md5
alec@Kongming:~$ ./md5
bash: ./md5: md5sum: bad interpreter: No such file or directory
alec@Kongming:~$
The same as the old Meteorswarm, now with fewer posts!
User avatar
Meteorswarm
 
Posts: 980
Joined: Sun Dec 27, 2009 12:28 am UTC
Location: Ithaca, NY

Re: Programs that print their own source code

Postby Goplat » Sat May 08, 2010 9:32 pm UTC

Use the absolute path. "#!/usr/bin/md5sum" or wherever it is. (If you don't know, run "which md5sum".)
Goplat
 
Posts: 490
Joined: Sun Mar 04, 2007 11:41 pm UTC

Re: Programs that print their own source code

Postby RoadieRich » Sat May 08, 2010 9:57 pm UTC

Works on OS-X/darwin too...
Code: Select all
$ echo '#!'`which md5sum` > md5
$ chmod +x md5
$ ./md5
3190365db110e56a2bc307f24e383685  ./md5
$
(I was kinda surprised that "$ echo '#!'`which md5sum`" worked as I expected it to, though.)
roband wrote:Mav is a cow.

UniJam 2012: Inter-university Games Jam hosted by Nottingham Trent University DevSoc.
nlug: Nottingham Linux User Group
DevSoc: The Nottingham Trent University Software Development Society
User avatar
RoadieRich
The Black Hand
 
Posts: 1030
Joined: Tue Feb 12, 2008 11:40 am UTC
Location: Somewhere only we know

Re: Programs that print their own source code

Postby TheChewanater » Sun May 09, 2010 1:39 am UTC

Works on iPhone/Darwin as well with a terminal emulator.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby ircmaxell » Tue May 11, 2010 1:54 pm UTC

A little bit of php that uses eval() (a slight cheat) and self-modifies its code. Each time, it generates random variable names... Run it as `php -q test.php | php -q | php -q | php -q | php -q` The -q flag is needed to suppress the content-type header that php wants to include normally...

Code: Select all
<?php

$var='
class Replace {

        public function __construct($string) {
                $this->source = $string;
        }

        public function __toString() {
                return $this->makeSource();
        }

        protected function makeSource() {
                $body = "<?php\n\n\$var=\'".addcslashes($this->source,"\'")."\';\n\necho eval(\$var);\n";
                if (preg_match_all("#\\\$(this->[a-zA-Z][a-zA-Z0-9_]*|[a-zA-Z][a-zA-Z0-9_]*)(\.|\s|\))#", $body, $matches, PREG_SET_ORDER)) {
                        $vars = array();
                        foreach ($matches as $match) {
                                $vars[] = $match[1];
                        }
                        array_unique($vars);
                        foreach ($vars as $var) {
                                if ($var == "this") continue;
                                $name = $this->createRandomName();
                                if (strpos($var, "->") !== false) {
                                        $replace = "\$this->".$name;
                                } else {
                                        $replace = "\$".$name;
                                }
                                $body = str_replace("\$".$var, $replace, $body);
                        }
                }
                return $body;
        }

        protected function createRandomName() {
                $returnLen = rand(5, 15);
                $chrs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";
                $maxchr = strlen($chrs) - 1;
                $return = "";
                for ($i = 0; $i < $returnLen; $i++) {
                        $return .= $chrs[rand(0, $maxchr)];
                }
                return $return;
        }
}

return new Replace($var);';

echo eval($var);

User avatar
ircmaxell
 
Posts: 44
Joined: Wed Dec 24, 2008 3:06 am UTC

Re: Programs that print their own source code

Postby Meteorswarm » Tue May 11, 2010 8:20 pm UTC

Goplat wrote:Use the absolute path. "#!/usr/bin/md5sum" or wherever it is. (If you don't know, run "which md5sum".)


Ah, yes, that does it.

Along the original lines, a shorter program to print its own source would be
Code: Select all
#!/bin/cat

possibly without the /bin, but I need the /bin on my system.
The same as the old Meteorswarm, now with fewer posts!
User avatar
Meteorswarm
 
Posts: 980
Joined: Sun Dec 27, 2009 12:28 am UTC
Location: Ithaca, NY

Re: Programs that print their own source code

Postby DharmaBum » Thu May 13, 2010 8:15 pm UTC

If anyone writes a quine in Brainfuck they win the internet. Shouldn't be impossible considering it's basically a language for writing Turing Machines.

Another one that would surely yield hilarity is LOLCODE.
DharmaBum
 
Posts: 32
Joined: Wed Nov 26, 2008 4:02 am UTC

Re: Programs that print their own source code

Postby hotaru » Thu May 13, 2010 9:12 pm UTC

DharmaBum wrote:If anyone writes a quine in Brainfuck they win the internet. Shouldn't be impossible considering it's basically a language for writing Turing Machines.

of course it isn't impossible.

DharmaBum wrote:Another one that would surely yield hilarity is LOLCODE.

something like this?
Code: Select all
#include <stdio.h>

int main()
{
 struct { unsigned a:3, b:3, c:2; } n = {0};
  do do printf("%hhu\n", *&n);
  while(!(n.a-- && !++n.b));
  while(++n.c);
  return 0; } 
User avatar
hotaru
 
Posts: 931
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Programs that print their own source code

Postby Pesto » Fri May 21, 2010 1:53 am UTC

This thread inspired me to implement my own programming language, called quine. In quine, every program is a quine. The syntax is quite simple, as any ASCII text file is a valid program.

You can install my quine interpreter on any Linux machine with the following command.

Code: Select all
alias quine='cat'

I have not yet had time to start work on porting this to Windows or other operating systems.
User avatar
Pesto
 
Posts: 729
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: Programs that print their own source code

Postby TheChewanater » Fri May 21, 2010 2:36 am UTC

You should continue work on ports to other platforms. However, I understand how hard it is writing a new language, which I would never even attempt myself. Hopefully popular libraries will start supporting it soon and it can take off.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.
User avatar
TheChewanater
 
Posts: 1261
Joined: Sat Aug 08, 2009 5:24 am UTC

Re: Programs that print their own source code

Postby hotaru » Fri May 21, 2010 4:45 am UTC

Pesto wrote:I have not yet had time to start work on porting this to Windows or other operating systems.

windows already includes an interpreter. it's called "type".
Code: Select all
#include <stdio.h>

int main()
{
 struct { unsigned a:3, b:3, c:2; } n = {0};
  do do printf("%hhu\n", *&n);
  while(!(n.a-- && !++n.b));
  while(++n.c);
  return 0; } 
User avatar
hotaru
 
Posts: 931
Joined: Fri Apr 13, 2007 6:54 pm UTC

Re: Programs that print their own source code

Postby skeptical scientist » Fri Apr 27, 2012 6:06 am UTC

PM 2Ring wrote:If you think using the repr() function is cheating, feel free to post an alternative.

It's totally cheating!

Here's a Python quine I just wrote. I didn't know the repr() function, so I wrote a function to duplicate enough of it's behavior for the quine. Unfortunately, that makes the code a fair bit longer.

Code: Select all
# Simple Python program to print its own source

def sanitize(s): #turns a string into python input representing the same string
    s=list(s)
    l=0
    while l<len(s):
        if s[l]=='\n':
            s=s[:l]+['\\','n']+s[l+1:]
            l+=2
        elif s[l]=='\\':
            s=s[:l]+['\\','\\']+s[l+1:]
            l+=2
        elif s[l]=='\"':
            s=s[:l]+['\\','\"']+s[l+1:]
            l+=2
        else: l+=1
    s=reduce(lambda x,y:x+y,s)
    return s

s1,s2="# Simple Python program to print its own source\n\ndef sanitize(s): #turns a string into python input representing the same string\n    s=list(s)\n    l=0\n    while l<len(s):\n        if s[l]=='\\n':\n            s=s[:l]+['\\\\','n']+s[l+1:]\n            l+=2\n        elif s[l]=='\\\\':\n            s=s[:l]+['\\\\','\\\\']+s[l+1:]\n            l+=2\n        elif s[l]=='\\\"':\n            s=s[:l]+['\\\\','\\\"']+s[l+1:]\n            l+=2\n        else: l+=1\n    s=reduce(lambda x,y:x+y,s)\n    return s\n\ns1,s2=\"","\"\ns=s1+sanitize(s1)+'\",\"'+sanitize(s2)+s2\nprint s"
s=s1+sanitize(s1)+'","'+sanitize(s2)+s2
print s
I'm looking forward to the day when the SNES emulator on my computer works by emulating the elementary particles in an actual, physical box with Nintendo stamped on the side.

"With math, all things are possible." —Rebecca Watson
User avatar
skeptical scientist
closed-minded spiritualist
 
Posts: 5920
Joined: Tue Nov 28, 2006 6:09 am UTC
Location: Madison, Wisconsin

Re: Programs that print their own source code

Postby thoughtfully » Fri Apr 27, 2012 4:50 pm UTC

Here's how to really cheat in Python.
Code: Select all
0 % cat cheat.py; python cheat.py
print open(__file__).read()
print open(__file__).read()
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery
User avatar
thoughtfully
 
Posts: 1917
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN

Re: Programs that print their own source code

Postby Cosmologicon » Fri Apr 27, 2012 6:53 pm UTC

We still doing this? Here's my python one-liner:
Code: Select all
print (u"*3)[46:-9].replace(chr(117),chr(117)+chr(34)) print (u"*3)[46:-9].replace(chr(117),chr(117)+chr(34))
User avatar
Cosmologicon
 
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA

Re: Programs that print their own source code

Postby PM 2Ring » Sat Apr 28, 2012 5:22 am UTC

Cosmologicon wrote:We still doing this?

I suspect that skeptical scientist has revived this thread because he's just started learning Python. If so, yay!

Here's my python one-liner:
Code: Select all
print (u"*3)[46:-9].replace(chr(117),chr(117)+chr(34)) print (u"*3)[46:-9].replace(chr(117),chr(117)+chr(34))

I'll pay that.
User avatar
PM 2Ring
 
Posts: 2593
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Mid north coast, NSW, Australia

Re: Programs that print their own source code

Postby skeptical scientist » Sat Apr 28, 2012 6:52 am UTC

PM 2Ring wrote:I suspect that skeptical scientist has revived this thread because he's just started learning Python. If so, yay!

Indeed. The academic job market is really tough right now, and I'm thinking about getting a job as a programmer. Any suggestions? (Probably not in this thread - if there's a relevant thread I should look at, a link would be nice.)

Cosmologicon wrote:Here's my python one-liner:
Code: Select all
print (u"*3)[46:-9].replace(chr(117),chr(117)+chr(34)) print (u"*3)[46:-9].replace(chr(117),chr(117)+chr(34))

That's cute. I like how you need basically two copies of your string, but you take three and slice to get the right start/stop point.
I'm looking forward to the day when the SNES emulator on my computer works by emulating the elementary particles in an actual, physical box with Nintendo stamped on the side.

"With math, all things are possible." —Rebecca Watson
User avatar
skeptical scientist
closed-minded spiritualist
 
Posts: 5920
Joined: Tue Nov 28, 2006 6:09 am UTC
Location: Madison, Wisconsin

Re: Programs that print their own source code

Postby WarDaft » Sat Apr 28, 2012 5:51 pm UTC

Job suggestions, language suggestions, or other?
All Shadow priest spells that deal Fire damage now appear green.
Big freaky cereal boxes of death.
User avatar
WarDaft
 
Posts: 1538
Joined: Thu Jul 30, 2009 3:16 pm UTC

Re: Programs that print their own source code

Postby skeptical scientist » Sat Apr 28, 2012 10:47 pm UTC

WarDaft wrote:Job suggestions, language suggestions, or other?

OT stuff taken here.
I'm looking forward to the day when the SNES emulator on my computer works by emulating the elementary particles in an actual, physical box with Nintendo stamped on the side.

"With math, all things are possible." —Rebecca Watson
User avatar
skeptical scientist
closed-minded spiritualist
 
Posts: 5920
Joined: Tue Nov 28, 2006 6:09 am UTC
Location: Madison, Wisconsin

Re: Programs that print their own source code

Postby tastelikecoke » Sun Apr 29, 2012 7:26 am UTC

I've been playing with Quines in python recently. The first quine I coded is not short at all, so I tried again.

Code: Select all
s = """s =
print s[:4]+chr(34)+chr(34)+chr(34)+s+chr(34)+chr(34)+chr(34)+s[4:]"""
print s[:4]+chr(34)+chr(34)+chr(34)+s+chr(34)+chr(34)+chr(34)+s[4:]
User avatar
tastelikecoke
 
Posts: 1118
Joined: Mon Feb 01, 2010 7:58 am UTC
Location: Antipode of Brazil

Re: Programs that print their own source code

Postby RAKtheUndead » Fri May 04, 2012 11:50 am UTC

As far as I'm concerned, quines have been uninteresting since 1994, when this was done:

http://www.ioccc.org/1994/smr.hint

I think he told Common Lisp to shove it.
"The outright rejection of technology cripples the otherwise educated mind." - RAK
RAKtheUndead
 
Posts: 72
Joined: Fri Mar 27, 2009 10:18 am UTC

Re: Programs that print their own source code

Postby sourmìlk » Mon May 07, 2012 7:06 am UTC

I was just about to bring that up :(
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
User avatar
sourmìlk
If I can't complain, can I at least express my fear?
 
Posts: 6405
Joined: Mon Dec 22, 2008 10:53 pm UTC
Location: permanently in the wrong

Previous

Return to Coding

Who is online

Users browsing this forum: Bing [Bot] and 9 guests