能够自我复制并且有用的程序——不是 quine
我有一个执行有用任务的程序。现在,除了执行原始任务之外,我还想在编译的可执行文件运行时生成纯文本源代码。这不是奎因,但可能是相关的。
此功能通常很有用,但我的特定程序是用 Fortran 90 编写的并使用 Mako 模板。编译后,它可以访问原始源代码文件,但我希望能够确保用户运行可执行文件时源存在。
这有可能实现吗?
下面是一个执行简单任务的简单 Fortran 90 示例。
program exampl
implicit none
write(*,*) 'this is my useful output'
end program exampl
是否可以修改此程序,使其执行相同的任务(编译时输出字符串)并输出包含源代码的 Fortran 90 文本文件?
提前致谢
I have a program which performs a useful task. Now I want to produce the plain-text source code when the compiled executable runs, in addition to performing the original task. This is not a quine, but is probably related.
This capability would be useful in general, but my specific program is written in Fortran 90 and uses Mako Templates. When compiled it has access to the original source code files, but I want to be able to ensure that the source exists when a user runs the executable.
Is this possible to accomplish?
Here is an example of a simple Fortran 90 which does a simple task.
program exampl
implicit none
write(*,*) 'this is my useful output'
end program exampl
Can this program be modified such that it performs the same task (outputs a string when compiled) and outputs a Fortran 90 text file containing the source?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经很久没有接触过 Fortran(而且我从未处理过 Fortran 90),所以我不确定,但我看到了一种基本方法,只要该语言支持代码中的字符串文字,就应该有效。
将整个程序包含在一个文字块中。显然,您不能在其中包含文字,而是需要某种标记来告诉您的程序包含文字块。
显然,这意味着您有源代码的两份副本,一份位于另一份内部。因为这很丑陋,所以我不会这样做,而是将源代码与 include_me 令牌一起存储在其中,并通过在编译之前构建嵌套文件的程序运行它。请注意,该程序将与从文字块重新创建代码的例程共享大量代码。如果您打算走这条路,我也会让程序吐出该程序的源代码,这样任何试图修改文件的人都不需要处理两个副本。
It's been so long since I have touched Fortran (and I've never dealt with Fortran 90) that I'm not certain but I see a basic approach that should work so long as the language supports string literals in the code.
Include your entire program inside itself in a block of literals. Obviously you can't include the literals within this, instead you need some sort of token that tells your program to include the block of literals.
Obviously this means you have two copies of the source, one inside the other. As this is ugly I wouldn't do it that way, but rather store your source with the include_me token in it and run it through a program that builds the nested files before you compile it. Note that this program will share a decent amount of code with the routine that recreates the code from the block of literals. If you're going to go this route I would also make the program spit out the source for this program so whoever is trying to modify the files doesn't need to deal with the two copies.
我的原始程序(参见问题)已编辑:添加一个包含语句
调用此文件“exampl.f90”
然后另一个程序(在本例中用Python编写)读取该源
运行此python脚本会生成一个可执行文件。当可执行文件运行时,它执行原始任务并打印源代码。
My original program (see question) is edited: add an include statement
Call this file "exampl.f90"
Then another program (written in Python in this case) reads that source
Running this python script produces an executable. When the executable is run, it performs the original task AND prints the source code.