-2
\$\begingroup\$

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.
\$\endgroup\$
23
  • 1
    \$\begingroup\$ What if the language has a builtin quine command, but we designate a different string as a new quine command? \$\endgroup\$ Commented Jul 7, 2016 at 1:33
  • \$\begingroup\$ It seems dubious, but what language do you have in mind? \$\endgroup\$ Commented Jul 7, 2016 at 1:43
  • \$\begingroup\$ Languages like Jolf, Actually, etc. \$\endgroup\$ Commented Jul 7, 2016 at 1:44
  • \$\begingroup\$ Is it clear what I am asking? \$\endgroup\$ Commented Jul 7, 2016 at 1:44
  • \$\begingroup\$ Yes, it is, for the most part \$\endgroup\$ Commented Jul 7, 2016 at 1:44

3 Answers 3

3
\$\begingroup\$

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.
\$\endgroup\$
2
  • \$\begingroup\$ 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". \$\endgroup\$ Commented Jul 7, 2016 at 15:00
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Jul 7, 2016 at 17:13
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

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}");}
}
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.