代码:
with Ada.Text_IO; use Ada.Text_IO;
procedure Ada_Quine is
type String_Access is access String;
Prefix : constant String := (1 .. 8 => ' ') & ('n', 'e', 'w', ' ', 'S', 't', 'r', 'i', 'n', 'g', ''', '(', Character'Val(34));
Suffix : constant String := (Character'Val(34), ')', ',');
NullStr: constant String := (1 .. 8 => ' ') & ('n', 'u', 'l', 'l');
Myself: constant array (1 .. 27) of String_Access := (
new String'("with Ada.Text_IO; use Ada.Text_IO;"),
new String'(""),
new String'("procedure Ada_Quine is"),
new String'(" type String_Access is access String;"),
new String'(" Prefix : constant String := (1 .. 8 => ' ') & ('n', 'e', 'w', ' ', 'S', 't', 'r', 'i', 'n', 'g', ''', '(', Character'Val(34));"),
new String'(" Suffix : constant String := (Character'Val(34), ')', ',');"),
new String'(" NullStr: constant String := (1 .. 8 => ' ') & ('n', 'u', 'l', 'l');"),
new String'(" Myself: constant array (1 .. 27) of String_Access := ("),
new String'(" );"),
new String'("begin"),
new String'(" for I in 1 .. 8 loop"),
new String'(" Put_Line(Myself(I).all);"),
new String'(" end loop;"),
new String'(""),
new String'(" for S of Myself loop"),
new String'(" if S /= null then"),
new String'(" Put_Line(Prefix & S.all & Suffix);"),
new String'(" else"),
new String'(" Put_Line(NullStr);"),
new String'(" end if;"),
new String'(" end loop;"),
new String'(""),
new String'(" for I in 9 .. Myself'Last - 1 loop"),
new String'(" Put_Line(Myself(I).all);"),
new String'(" end loop;"),
new String'("end Ada_Quine;"),
null
);
begin
for I in 1 .. 8 loop
Put_Line(Myself(I).all);
end loop;
for S of Myself loop
if S /= null then
Put_Line(Prefix & S.all & Suffix);
else
Put_Line(NullStr);
end if;
end loop;
for I in 9 .. Myself'Last - 1 loop
Put_Line(Myself(I).all);
end loop;
end Ada_Quine;