No Quine Built In? No Problem!

-3

Are you tired of languages with built in quining techniques?

Your challenge, should you choose to accept it, is to add a built-in quine feature to the language of your choice.

Task

Given some programming language X, you should define

  1. A language X′ that is a subset of X.
  2. A quine command string Q. (This is a string of your choice; it need not literally be “Q”.)

Now you have a new language Y, consisting of programs that are in X′ except that they may also contain copies of Q as if Q were a new built-in statement or function. Your job is to implement this language Y, by writing

  1. A compiler, written in X′, producing an output program B in X for each input program A in Y.

When B is run, it should behave like A would if all its copies of Q had been replaced with a statement in X that prints the source code of B.

You’ve therefore created a language Y that contains a quining builtin Q.

Rules

  • Your chosen language subset X′ must still satisfy the definition of a programming language.
  • The output programs B of your compiler must be proper quines. You may not use quining builtins that may already be in X.
  • This is code golf, so the shortest code in bytes (defined as your language's default native encoding) wins.

Rohan Jhunjhunwala

Posted 2016-07-07T01:07:05.467

Reputation: 2 569

Question was closed 2018-03-17T19:33:11.197

1What if the language has a builtin quine command, but we designate a different string as a new quine command? – Conor O'Brien – 2016-07-07T01:33:12.810

It seems dubious, but what language do you have in mind? – Rohan Jhunjhunwala – 2016-07-07T01:43:37.937

Languages like Jolf, Actually, etc. – Conor O'Brien – 2016-07-07T01:44:11.267

Is it clear what I am asking? – Rohan Jhunjhunwala – 2016-07-07T01:44:13.097

Yes, it is, for the most part – Conor O'Brien – 2016-07-07T01:44:29.470

Ok, Thanks, I feel like an esolang like that with a built in quine may not be in the spirit of the challenge. A simple implementation would just find and replace the string for a new quine command into the old one. That being said if the implementation of such a thing is not too trivial I would accept it. Feel free to write one up, and message it to me and I can take a look. – Rohan Jhunjhunwala – 2016-07-07T01:46:16.780

@CᴏɴᴏʀO'Bʀɪᴇɴ Upon further review I have decided that you are permitted to define a new quine character, except the new quine operator must be implemented without use of the old one. – Rohan Jhunjhunwala – 2016-07-07T02:17:53.837

Is a quine flag cheating? – NoOneIsHere – 2016-07-07T03:15:24.910

I would say yes – Rohan Jhunjhunwala – 2016-07-07T08:10:38.217

Totally confused, can you define it in terms of functions? i.e. if a quinine is (Compile(Source))() = Source what are you after? – weston – 2016-07-07T10:34:12.553

The Q comand should output the compiled program – Rohan Jhunjhunwala – 2016-07-07T12:32:37.963

"... the compiled source of language Y" doesn't even make sense. I'm not sure whether the edited explanation is worse than the previous one, but it's certainly still far from clear. – Peter Taylor – 2016-07-07T13:42:02.800

I slightly tweaked it again. I however feel that while the challenge is hard to understand it certainly is doable as there are two answers to this question. @Peter Taylor – Rohan Jhunjhunwala – 2016-07-07T13:54:59.873

In that case maybe you can enlist the aid of Anders to write a clear spec - although I'm not convinced that he's understood what you want either. At least his CJam program doesn't behave as I would expect. – Peter Taylor – 2016-07-07T14:55:31.703

This got less clear with recent edits. The problem as I understand it is as follows. Given some programming language X, you are to define (1) a quine command string Q; (2) a language X′ that is a subset of X; (3) a compiler, written in X′, and producing an output program B in X for any input program A that is in X′ except in that A may also contain copies of Q. When B is run, it should behave like A would if all its copies of Q had been replaced with a statement in X that prints the source code of B. We’ve thus created a language Y (X′ augmented with Q) that contains a quining builtin Q. – Anders Kaseorg – 2016-07-07T17:38:26.887

@RohanJhunjhunwala, does that match your intention? If so you are welcome to edit it into the question, or I can. – Anders Kaseorg – 2016-07-07T17:39:08.557

@Anders Kaesorg yes that matches my intention – Rohan Jhunjhunwala – 2016-07-07T18:08:19.180

Is it now clear enough? – Rohan Jhunjhunwala – 2016-07-07T18:08:57.787

@RohanJhunjhunwala Since one can hide a fair amount of logic within the quine command string, this might be more interesting if the score were something like the sum of the length of the compiler and the length of the quine command string, rather than just the length of the compiler—what do you think? – Anders Kaseorg – 2016-07-07T18:37:28.153

yes if this question gets off of hold I would consider that. Do you think it deserves to be taken off of hold? – Rohan Jhunjhunwala – 2016-07-07T19:18:49.147

@RohanJhunjhunwala It’s on hold because people thought it was unclear, but I think the new wording will satisfy them. I voted to reopen. – Anders Kaseorg – 2016-07-07T20:36:09.793

@busukxuan are the new rule clarifications more accepatable and clear enough to warrant reopening? – Rohan Jhunjhunwala – 2016-07-08T01:51:21.900

@Connor O' Brien are the new rules more appropriate? I am interested in reopening this question – Rohan Jhunjhunwala – 2016-07-08T14:19:09.110

Answers

3

CJam, 7 bytes

qp":Q~"

The quine command string is Qp":Q~"o. The language subset is programs that do not use the variable Q except as part of the quine command string.

Demo

Input program:

"I am a quine and my source is:
"oQp":Q~"o"
Have a nice day."o

Try it online

Output program:

"\"I am a quine and my source is:
\"oQp\":Q~\"o\"
Have a nice day.\"o"
:Q~

Try it online

Output of output program:

I am a quine and my source is:
"\"I am a quine and my source is:
\"oQp\":Q~\"o\"
Have a nice day.\"o"
:Q~
Have a nice day.

Anders Kaseorg

Posted 2016-07-07T01:07:05.467

Reputation: 29 242

See comments on question: if you understand what OP is asking, perhaps you can edit the question to make it intelligible for the rest of us. Although note that I can't find any interpretation of the question which makes this answer correct: if I pass 1Q to your cross-compiler, the output is a program which prints 11Q, and I don't think "output" in the context of a full program can validly be interpreted as "place on the stack". – Peter Taylor – 2016-07-07T15:00:13.517

@PeterTaylor The quine command string I defined is Qp":Q~"o, not Q. 1Q is not valid in my defined language subset, because it uses the variable Q in a way that’s not part of the quine command string Qp":Q~"o.

If you pass 1Qp":Q~"o to the compiler, then as requested, you will get a program that pushes 1 onto the stack, then outputs its own source, then implicitly outputs the contents of the stack. – Anders Kaseorg – 2016-07-07T17:13:36.723

1

Java "only" 498 Bytes

The following java program takes in input as command line arguments.

class Q{
public static void main(String[]a){
System.out.println(a[0]);System.out.print("static char[]s={");a[2]+="}static void q(){System.out.print(\""+a[0]+"\\n\");System.out.print(\"static char[]s={\");for(char c:s)System.out.print((int) c+\",\");System.out.print(\"\\b};\\n"+a[1]+"\\n\");for(char c:s)System.out.print(c);System.out.print(\"\\n}\");}";for(char c:a[2].toCharArray())System.out.print((int) c+",");System.out.println("\b};");System.out.println(a[1]);System.out.print(a[2]+"\n}");
}} 

In this case my language "X" is (a subset of) Java, and my Quine Command string "Q" is q();for each instance of q(); the output program will print its source. Input is restricted to only four lines, and any more or less results in undefined behavior. My golfed program technically follows these same restrictions. So my subset is programs that fit on four lines as shown and do not have a function called q or use any variable named s.

Sample Input

class sampleInput{//class def on this line
public static void main(String[]argument){//method header here and optional code although avoid putting code here
System.out.println("Hello World Of Quines");q();System.out.println("\n------------");q();/*Avoid comments on this line unless you use this format YOU MUST! put two braces on the next line*/
}}

Sample Output

class sampleInput{//class def on this line
static char[]s={83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,108,110,40,34,72,101,108,108,111,32,87,111,114,108,100,32,79,102,32,81,117,105,110,101,115,34,41,59,113,40,41,59,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,108,110,40,34,92,110,45,45,45,45,45,45,45,45,45,45,45,45,34,41,59,113,40,41,59,47,42,65,118,111,105,100,32,99,111,109,109,101,110,116,115,32,111,110,32,116,104,105,115,32,108,105,110,101,32,117,110,108,101,115,115,32,121,111,117,32,117,115,101,32,116,104,105,115,32,102,111,114,109,97,116,32,112,117,116,32,116,119,111,32,98,114,97,99,101,115,32,111,110,32,116,104,101,32,110,101,120,116,32,108,105,110,101,42,47,125,115,116,97,116,105,99,32,118,111,105,100,32,113,40,41,123,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,99,108,97,115,115,32,115,97,109,112,108,101,73,110,112,117,116,123,47,47,99,108,97,115,115,32,100,101,102,32,111,110,32,116,104,105,115,32,108,105,110,101,92,110,34,41,59,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,115,116,97,116,105,99,32,99,104,97,114,91,93,115,61,123,34,41,59,102,111,114,40,99,104,97,114,32,99,58,115,41,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,40,105,110,116,41,32,99,43,34,44,34,41,59,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,92,98,125,59,92,110,112,117,98,108,105,99,32,115,116,97,116,105,99,32,118,111,105,100,32,109,97,105,110,40,83,116,114,105,110,103,91,93,97,114,103,117,109,101,110,116,41,123,47,47,109,101,116,104,111,100,32,104,101,97,100,101,114,32,104,101,114,101,32,97,110,100,32,111,112,116,105,111,110,97,108,32,99,111,100,101,32,97,108,116,104,111,117,103,104,32,97,118,111,105,100,32,112,117,116,116,105,110,103,32,99,111,100,101,32,104,101,114,101,92,110,34,41,59,102,111,114,40,99,104,97,114,32,99,58,115,41,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,99,41,59,83,121,115,116,101,109,46,111,117,116,46,112,114,105,110,116,40,34,92,110,125,34,41,59,125};
public static void main(String[]argument){//method header here and optional code although avoid putting code here
System.out.println("Hello World Of Quines");q();System.out.println("\n------------");q();/*Avoid comments on this line unless you use this format put two braces on the next line*/}static void q(){System.out.print("class sampleInput{//class def on this line\n");System.out.print("static char[]s={");for(char c:s)System.out.print((int) c+",");System.out.print("\b};\npublic static void main(String[]argument){//method header here and optional code although avoid putting code here\n");for(char c:s)System.out.print(c);System.out.print("\n}");}
}

Rohan Jhunjhunwala

Posted 2016-07-07T01:07:05.467

Reputation: 2 569

1

Haskell, 35 bytes

main=interact(\s->s++"q="++show s)

Includes a trailing newline. The quine command string is (putStr$q++"q="++show q). The language subset is programs with a trailing newline that do not use the variable q except as part of the quine command string.

Demo

Input program:

main = do
  putStrLn "I am a quine and my source is:"
  (putStr$q++"q="++show q)
  putStrLn "\nHave a nice day."

Output program:

main = do
  putStrLn "I am a quine and my source is:"
  (putStr$q++"q="++show q)
  putStrLn "\nHave a nice day."
q="main = do\n  putStrLn \"I am a quine and my source is:\"\n  (putStr$q++\"q=\"++show q)\n  putStrLn \"\\nHave a nice day.\"\n"

Output of output program:

I am a quine and my source is:
main = do
  putStrLn "I am a quine and my source is:"
  (putStr$q++"q="++show q)
  putStrLn "\nHave a nice day."
q="main = do\n  putStrLn \"I am a quine and my source is:\"\n  (putStr$q++\"q=\"++show q)\n  putStrLn \"\\nHave a nice day.\"\n"
Have a nice day.

Anders Kaseorg

Posted 2016-07-07T01:07:05.467

Reputation: 29 242