Created
August 11, 2016 15:07
-
-
Save md2perpe/5ccc6af159dc96704ef92b8d342ec6c2 to your computer and use it in GitHub Desktop.
Program outputting a program that outputs itself
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"regexp" | |
) | |
func main() { | |
in := ` | |
package main | |
import "fmt" | |
func main() { | |
s := "$" | |
fmt.Printf(s, @) | |
} | |
` | |
re, _ := regexp.Compile("\n|\t|\"|\\$|@|.") | |
args := re.ReplaceAllStringFunc(in, func (s string) string { | |
switch s { | |
case "\n": return "10, " | |
case "\t": return "9, " | |
case "\"": return "34, " | |
case "$": return "s, " | |
case "@": return "" | |
default: return "" | |
} | |
}) | |
fmts := re.ReplaceAllStringFunc(in, func (s string) string { | |
switch s { | |
case "\n": return "%c" | |
case "\t": return "%c" | |
case "\"": return "%c" | |
case "$": return "%s" | |
case "@": return args | |
default: return s | |
} | |
}) | |
code := re.ReplaceAllStringFunc(in, func (s string) string { | |
switch s { | |
case "$": return fmts | |
case "@": return args | |
default: return s | |
} | |
}) | |
fmt.Print(code) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment