Skip to content

Instantly share code, notes, and snippets.

@tiesmaster
Created December 2, 2012 11:16
Show Gist options
  • Save tiesmaster/4188259 to your computer and use it in GitHub Desktop.
Save tiesmaster/4188259 to your computer and use it in GitHub Desktop.
Magik Quine
_block
_local quote_char << character.from_value(34)
_local s << {
"_block",
" _local quote_char << character.from_value(34)",
" _local s << {",
" }",
" _for line _over s.slice(1, 3).fast_elements()",
" _loop",
" write(line)",
" _endloop",
" _for line _over s.slice(1, s.size-1).fast_elements()",
" _loop",
" write(tab_char, quote_char, line, quote_char, %,)",
" _endloop",
" write(tab_char, quote_char, quote_char)",
" _for line _over s.slice_to_end(4).fast_elements()",
" _loop",
" write(line)",
" _endloop",
"_endblock",
""
}
_for line _over s.slice(1, 3).fast_elements()
_loop
write(line)
_endloop
_for line _over s.slice(1, s.size-1).fast_elements()
_loop
write(tab_char, quote_char, line, quote_char, %,)
_endloop
write(tab_char, quote_char, quote_char)
_for line _over s.slice_to_end(4).fast_elements()
_loop
write(line)
_endloop
_endblock
_block
_local actual, expected, line, ret, pos,
st << internal_text_output_stream.new(),
quine_source << system.pathname_down(system.pathname_up(!source_file!), "quine.magik")
_block
_dynamic !output! << st
load_file(quine_source)
_endblock
actual << st.string
_over 1.upto(3)
_loop
actual << actual.slice_to_end(actual.index_of(%newline)+1)
_endloop
_over 1.upto(2)
_loop
actual << actual.slice(1, actual.rindex_of(%newline)-1)
_endloop
st << external_text_input_stream.new(quine_source)
_protect
_local internal_stream << internal_text_output_stream.new()
_loop
_if (line << st.get_line()) _is _unset _then _leave _endif
internal_stream.write(line, %newline)
_endloop
expected << internal_stream.string
_protection
st.close()
_endprotect
_if actual = expected
_then
write("OK!")
_else
write("FAIL!!!")
(ret, pos) << actual.cf_subseq(expected)
write("position: ", pos, " (line ", actual.slice(1, pos).occurrences_of(%newline), ")")
write("expected:")
write(expected.slice(
expected.rindex_of(%newline, pos).default(1),
expected.index_of(%newline, pos).default(expected.size)-1))
write("actual:")
write(actual.slice(
actual.rindex_of(%newline, pos).default(1),
actual.index_of(%newline, pos).default(actual.size)-1))
_endif
_endblock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment