Skip to content

Instantly share code, notes, and snippets.

@docwhat
Last active April 21, 2021 19:15
Show Gist options
  • Save docwhat/ebce251dbb14c034049538cc62ab93cd to your computer and use it in GitHub Desktop.
Save docwhat/ebce251dbb14c034049538cc62ab93cd to your computer and use it in GitHub Desktop.
Quines
# http://editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[Makefile]
indent_style = tab
# Use 4 spaces for the Python files
[*.py]
indent_size = 4
max_line_length = 80
# The JSON files contain newlines inconsistently
[*.json]
insert_final_newline = ignore
# Minified JavaScript files shouldn't be changed
[*.md]
trim_trailing_whitespace = false
;(function quine() {
console.log(";(" + quine.toString() + ")()")
})()
CODE = "def foo(s) \"\"; end\n\nputs \"CODE = \#{foo(CODE)}\\n\\n\#{CODE}\""
def foo(s) s.gsub('\\', '\\\\\\').gsub(/\n/, '\n').gsub(/"/, '\"').gsub(/#/, '\#').gsub(/\A|\Z/, '"'); end
puts "CODE = #{foo(CODE)}\n\n#{CODE}"
#!/usr/bin/make --warn-undefined-variables
QUINES := $(wildcard *.py *.js *.go *.bash *.c *.rb)
OUTPUTS := $(addprefix output/,$(QUINES))
QUIET := true
.phony: all
all: $(OUTPUTS)
.phony: diff
diff: $(OUTPUTS)
@for quine in $(QUINES); do diff -us "$$quine" "output/$$quine"; done
.phony: clean
clean:
@rm -f $(OUTPUTS)
.phony: files
files:
@ls -1 Makefile $(QUINES)
.phony: watch
watch:
@while ! (make files | entr -d make diff); do sleep 0.1; done
output/.stamp:
echo narf $@ narf
mkdir -p $(dir $@)
date > output/.stamp
$(OUTPUTS): output/.stamp
output/%.py: %.py
python3 $< > $@
output/%.rb: %.rb
ruby $< > $@
output/%.js: %.js
node $< > $@
output/%.go: %.go
go run $< > $@
output/%.bash: %.bash
bash $< > $@
output/%.c: %.c
cc $< -o output/$(basename $<)
output/$(basename $<) > $@
@rm -f output/$(basename $<)
#include <stdio.h>
static char sym[] = "\n\t\\\"";
int main(void) {
const char *code = "#include <stdio.h>%c%cstatic char sym[] = %c%cn%ct%c%c%c%c%c;%c%cint main(void) {%c%cconst char *code = %c%s%c;%c%cprintf(code, sym[0], sym[0], sym[3], sym[2], sym[2], sym[2], sym[2], sym[2], sym[3], sym[3], sym[0], sym[0], sym[0], sym[1], sym[3], code, sym[3], sym[0], sym[1], sym[0], sym[0], sym[1], sym[0], sym[0]);%c%c%creturn 0;%c}%c";
printf(code, sym[0], sym[0], sym[3], sym[2], sym[2], sym[2], sym[2], sym[2], sym[3], sym[3], sym[0], sym[0], sym[0], sym[1], sym[3], code, sym[3], sym[0], sym[1], sym[0], sym[0], sym[1], sym[0], sym[0]);
return 0;
}
package main
import "fmt"
const strStart = 71
func main() {
code := `package main
import "fmt"
const strStart = 71
func main() {
code :=
fmt.Print(code[:strStart] + " \u0060" + code + "\u0060" + code[strStart:])
}
`
fmt.Print(code[:strStart] + " \u0060" + code + "\u0060" + code[strStart:])
}
a = 'print("a = %r" % a)\nprint(a)'
print("a = %r" % a)
print(a)
code = "code = %s\n\nputs format(code, code.inspect)"
puts format(code, code.inspect)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment