03-06-2023, 11:38 AM
之前见过好几种语言的quine,但是自己没写过,今天试着用Ada写一个,模仿wikipedia上Java的版本。
我觉得主要难点有两个,一个是引号的处理,我可以使用 Character'Val(34) 代替引号;另一个是Ada数组最后一个元素后面不能加逗号,于是我在数组最后加一个null元素。写了一上午终于拼了一个出来。
我觉得主要难点有两个,一个是引号的处理,我可以使用 Character'Val(34) 代替引号;另一个是Ada数组最后一个元素后面不能加逗号,于是我在数组最后加一个null元素。写了一上午终于拼了一个出来。
代码:
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;