开始使用不熟悉的语言编写新代码

发布于 2024-08-15 18:22:55 字数 122 浏览 4 评论 0原文

我正在用一种我不太熟悉的语言(FORTRAN)开始一个新项目,并且处于“发现”阶段。通常,阅读并弄清楚代码是一项相当简单的任务,但是,该代码相当大并且结构不那么好。是否有任何方法/技巧/技巧/工具来映射 50k 行相当密集的代码?

I'm starting a new project in a language I'm less familiar with (FORTRAN) and am in the 'discovery' phase. Normally reading through and figuring out code is a fairly simple task, however, this code is rather large and not so structured. Are there any methods/tips/tricks/tools to mapping out 50k lines of rather dense code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

自此以后,行同陌路 2024-08-22 18:22:56

@ldigas:“Fortran 要求在程序/子例程开始时声明所有内容...”

不,除非 IMPLICIT NONE 出现在例程开始时,否则 Fortran 使用隐式类型。因此,以 AH 和 OZ 开头的变量名称为 REAL,而 IN 为 INTEGER。 (这使得 GOD 成为 REAL 变量,而 INT,幸运的是,隐含地成为 INTEGER。)记住,Fortran(实际上当时是 FORTRAN)是由科学家和数学家设计的,为他们设计了 i、j、k、l、m 和n 始终是整数。

使用 IMPLICIT NONE,您必须显式键入所有变量,就像在 C、C++ 或 Java 中一样。所以你可以拥有“INTEGER GOD”和“REAL INT”。

@ldigas: "Fortran requires everything to be declared at the start of program/subroutine ... "

No, unless IMPLICIT NONE appears at the start of a routine, Fortran uses implicit typing. So variable names starting with A-H and O-Z are typed REAL, and I-N are INTEGER. (Which makes GOD a REAL variable, and INT, luckily, an INTEGER, implicitly.) Remember, Fortran (actually back then it was FORTRAN) was designed by scientists and mathematicians, for whom i, j, k, l, m, and n were ALWAYS integers.

With IMPLICIT NONE, you are forced to explicitly type all variables, as you would in C, C++, or Java. So you could have INTEGER GOD, and REAL INT.

沧桑㈠ 2024-08-22 18:22:55

是 Fortran IV(不太可能)、77 还是 90/95?这些修订使语言发生了很大变化。 High-Performance Mark 列出的一些问题在 Fortran IV 中很常见,但在 77 中不常见,而其他问题在 77 中仍然很常见。用最大编译器警告检查代码的建议非常好 - 甚至使用两个编译器。

我首先绘制子例程结构图,并从总体上了解它们的功能。然后更深入地了解需要改变的领域。

有一些用于分析甚至改进 Fortran 代码的工具,例如 http://www.polyhedron.com/pf -plusfort0htmlhttp://www.crescentbaysoftware.com/vast_77to90.html

Is it Fortran IV (unlikely), 77, or 90/95? The language changed a lot with these revisions. Some of the gotchas that High-Performance Mark listed were common in Fortran IV but uncommon in 77, while others were still common in 77. The suggestion to check the code with maximum compiler warnings is excellent -- even use two compilers.

I'd start by diagramming the subroutine structure, and getting a high-level view of what they do. Then get a deeper understanding of the areas that need to be changed.

There are tools for analyzing and even improving Fortran code, e.g., http://www.polyhedron.com/pf-plusfort0html or http://www.crescentbaysoftware.com/vast_77to90.html.

烏雲後面有陽光 2024-08-22 18:22:55

当我三十年前编写 Fortran (F77) 代码时(哎呀!),我们只有有限的设施来自动绘制未知代码库的流程图。它很丑陋,而且仅限于绘图仪床可以提供的空间。正如 @Simon 提到的,您还可以(并且在某些版本中可以)也可以使用调试器。

现在,交互式探索变得更加容易。此外,您还可以尝试使用 IDE。我没有亲自尝试过,因为 Fortran 不再是我的主要开发语言,但是 Photran 是一种Fortran 的 Eclipse 插件,似乎正在积极开发中(最后一次发布是本月)。

When I coded Fortran (F77) thirty (yikes!) years ago, we had limited facilities to automatically flowchart an unknown codebase. It was ugly, and limited to the real-estate that a plotter bed could supply. As @Simon mentions, you can (and could back then, with some versions) also use a debugger.

Now, interactive exploration is easier. Additionally, you can experiment with IDEs. I have not personally tried it, as Fortran is no longer my key development language, but Photran is an Eclipse plug-in for Fortran, and appears to be under active development (last release was this month).

你丑哭了我 2024-08-22 18:22:55

调试器是您的朋友——如果您有 Fortran 调试器的话。

在您进一步深入之前,我会先熟悉该语言的基本语法以及任何缺点,例如根据变量名称和声明位置等对变量类型进行假设。如果您不明白这些内容,那么您可能会迷失方向即使有一个有用的调试器。

还要记住,结构有时依赖于语言。您正在查看的代码对于您习惯的语言来说可能结构很差,但对于 Fortran 来说可能结构很好,因为 Fortran 有自己的一套特性。我想我只是说要以开放的心态开始,否则你会带着不必要的倾向,认为你正在查看的代码很糟糕。可能是这样,但这可能只是你不习惯的事情。

祝你好运。大约 20 年前,当我以 Fortran 编程为生时,我非常喜欢 Fortran,并且由于某些平台上的计算速度,它仍然是某些应用程序的首选语言。学术界还是不少。

The debugger is your friend - if you have one for Fortran.

Before you go too much further I would familiarise yourself with the basic syntax of the language plus any foibles like assumptions about variable types from their names and positions of declarations etc. If you don't get that stuff then you are likely to get very lost even with a helpful debugger.

Remember as well that structure is sometimes language dependent. The code you are looking at may be badly structured for the languages you are used to but may be very well structured for Fortran, which has its own set of peculiarities. I think I am just saying have an open mind to start with otherwise you'll be carrying around the unnecessary predisposition that the code you are looking at is bad. It may be, but it may just be something you are not used to.

Best of luck. I rather liked Fortran when I programmed in it for a living about 20 years ago, and it is still the language of choice for some applications because of computation speeds on some platforms. Still quite a lot of it in academia.

帅气尐潴 2024-08-22 18:22:55

我总是找到执行的起点,或者其他代码(我没有处理)调用我正在检查的代码的地方。然后我从那里开始阅读它,根据需要跟踪方法调用以弄清楚发生了什么。

I always find the starting point of execution, or where other code (that I'm not working on) calls the code I'm examining. Then I just start reading through it from there, following method calls as necessary to figure out what's going on.

抠脚大汉 2024-08-22 18:22:55

振作起来。 Fortran 的优点之一是它非常简单。除非你找到一个被编程为利用“聪明”技巧的代码。我建议您要做的第一件事是通过编译器运行您的程序,并将语法检查和标准合规性调到最大。旧版(Fortran 90 之前的)FORTRAN 因其人们用来绕过语言限制的巧妙技巧而臭名昭著。对于更熟悉现代语言的程序员来说,一些陷阱是:

-- 公共块;以及其他全球状态机制;尤其糟糕的是用于重命名和重新定义变量的公共块;

-- 等价(可怕,但你可能会被它们绊倒);

-- 固定格式源形式;

-- CONTINUE 语句的使用,以及以同一个 CONTINUE 语句结束的多个循环的做法;

-- 变量的隐式声明(要对这些变量进行排序,请在 PROGRAM、MODULE、SUBROUTINE 或 FUNCTION 语句出现的所有位置后立即插入行 IMPLICIT NONE);

-- 子程序的多个入口点;

——还有其他一些我很熟悉但记不起来的人。

如果这些对你毫无意义,那么它们很快就会变得毫无意义。最后,您可能想看看 Understanding for Fortran。虽然很贵,但是非常有用。

问候

马克

Take heart. One of Fortran's virtues is that it is very simple. Unless you find a code which has been programmed to take advantage of 'clever' tricks. I suggest that the first thing you do is to run your program through a compiler with syntax-checking and standards-compliance turned up to the max. Old (pre-Fortran 90) FORTRAN is notorious for the clever tricks that people used to get round the language's limitations. Some of the gotchas for programmers more familiar with modern languages:

-- common blocks; and other mechanisms for global state; especially bad are common blocks which are used to rename and redefine variables;

-- equivalences (horrid, but you might trip over them);

-- fixed-format source form;

-- use of CONTINUE statement, and the practice of having multiple loops ending at the same CONTINUE statement;

-- implicit declaration of variables (to sort these out, insert the line IMPLICIT NONE at immediately after the PROGRAM, MODULE, SUBROUTINE or FUNCTION statement everywhere they occur);

-- multiple entry points into sub-programs;

-- and a few others I'm so familiar with I can't recall them.

If these mean nothing to you, they soon will. And finally, you might want to look at Understand for Fortran. It costs, but it's very useful.

Regards

Mark

寄居人 2024-08-22 18:22:55

您在 Linux 或 OpenSolaris 上运行吗?如果是这样,Sun Studio Fortran 编译器就是最好的编译器之一。 Sun Studio IDE 可以理解 Fortran,并带有调试器。 http://developers.sun.com/sunstudio

Are you running on Linux or OpenSolaris? If so, the Sun Studio Fortran compiler is one of the best. And the Sun Studio IDE understands Fortran and comes with a debugger. http://developers.sun.com/sunstudio

独孤求败 2024-08-22 18:22:55

我很困,所以我会简短地说:)

首先 grep 出(或您使用的任何工具)程序语句、子例程语句、函数语句等。如果使用 f90,可能是模块等。在此基础上绘制一种图表,在该图表上您将看到什么调用什么(什么使用什么子例程、函数等)。

对情况有了总体了解后,就可以获取数据了。 Fortran 要求在程序/子例程开始时声明所有内容...因此第一行应该给出声明。当你把这些放入你刚刚制作的图表中之后,到时候你应该已经有一个非常清晰的情况了。

现在,下一步实际上取决于您想用它做什么。

I'm sleepy so I'll be short :)

Start by grepping out (or whatever tool you use) program statements, subroutine statements, function statements and the like. Maybe modules and such if f90 is used. Draw a kind of diagram based on that, on which you'll see what calls what (what uses what subroutines, functions and the like).

After you've got a general view of the situation, get to data. Fortran requires everything to be declared at the start of program/subroutine ... so the first lines should give you declarations. After you've putted those in the diagram you just made, you should have a very clear situation by that time.

Now, the next step depends really on what you want to do with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文