Last active
January 13, 2024 23:42
-
-
Save nathanhleung/8eebf17c1021322dd11e654c7ceade50 to your computer and use it in GitHub Desktop.
runnable at https://replit.com/@nathanhleung/quine, based on https://youtu.be/N-_XmLanPYg?t=1150. no self-reference!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.youtube.com/watch?v=N-_XmLanPYg | |
const BACKSLASH = String.fromCharCode(92); | |
const BACKTICK = String.fromCharCode(96); | |
function A() { | |
const program = `function B(str: string) { | |
function escapeProgram(str: string) { | |
return str | |
.replaceAll("\`", BACKSLASH + "\`") | |
.replaceAll("\${", BACKSLASH + "\${"); | |
} | |
function escapeStr(str: string) { | |
return str | |
.replaceAll( | |
BACKTICK + "\${escapeProgram(str)}" + BACKTICK, | |
BACKSLASH + BACKTICK + "\${escapeProgram(str)}" + | |
BACKSLASH + BACKTICK | |
); | |
} | |
function q(str: string) { | |
return \`// https://www.youtube.com/watch?v=N-_XmLanPYg | |
const BACKSLASH = String.fromCharCode(92); | |
const BACKTICK = String.fromCharCode(96); | |
function A() { | |
const program = \`\${escapeProgram(str)}\`; | |
return program; | |
}\` | |
} | |
const A = q(str); | |
console.log(A); | |
console.log(); | |
console.log(escapeStr(str)); | |
console.log(); | |
console.log("B(A());") | |
}`; | |
return program; | |
} | |
function B(str: string) { | |
function escapeProgram(str: string) { | |
return str | |
.replaceAll("`", BACKSLASH + "`") | |
.replaceAll("${", BACKSLASH + "${"); | |
} | |
function escapeStr(str: string) { | |
return str | |
.replaceAll( | |
BACKTICK + "${escapeProgram(str)}" + BACKTICK, | |
BACKSLASH + BACKTICK + "${escapeProgram(str)}" + | |
BACKSLASH + BACKTICK | |
); | |
} | |
function q(str: string) { | |
return `// https://www.youtube.com/watch?v=N-_XmLanPYg | |
const BACKSLASH = String.fromCharCode(92); | |
const BACKTICK = String.fromCharCode(96); | |
function A() { | |
const program = \`${escapeProgram(str)}\`; | |
return program; | |
}` | |
} | |
const A = q(str); | |
console.log(A); | |
console.log(); | |
console.log(escapeStr(str)); | |
console.log(); | |
console.log("B(A());") | |
} | |
B(A()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment