为什么 BASIC 有编号行?

发布于 2024-08-24 22:23:27 字数 347 浏览 8 评论 0原文

可能的重复:
我们为什么要为行号烦恼?

我很好奇为什么 BASIC 编程语言的早期版本有行编号,如下所示:

42 PRINT "Hello world!"

当时的文本编辑器没有行编号?

编辑:是的,我知道它们用于 GOTO,但为什么呢?我的意思是使用标签的计算成本太高?

Possible Duplicate:
Why did we bother with line numbers at all?

I'm curious about why early versions of the BASIC programming language had line numbering like in:

42 PRINT "Hello world!"

The text editors back then had no line numbering?

EDIT: Yes, I know they are used for GOTOs, but why? I mean having labels was too computationally expensive?

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

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

发布评论

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

评论(15

谜兔 2024-08-31 22:23:27

许多微型计算机在 ROM 中都有一个 BASIC 解释器,它会在启动时启动。问题是没有文本编辑器或文件系统可言。您有一个交互式提示来完成所有操作。如果您想插入一行代码,只需从行号开始键入即可。它将把它插入到代码中的正确位置。例如:(

>10 print "hello"
>30 goto 10
>20 print "world"
>list
10 PRINT "hello"
20 PRINT "world"
30 GOTO 10
>

在该示例中,> 是 BASIC 提示符)

如果您想删除一行,您可以输入类似 ERASE 20 的内容。
一些非常奇特的系统为您提供了行编辑器(即EDIT 10
如果你没有计划你的行号并且用完了(我如何在 10 和 11 之间插入一行?),一些系统会给你一个 RENUM 命令,它会重新编号你的代码(并调整 GOTOsGOSUBs 适当)。

欢乐时光!

Many microcomputers had a BASIC interpreter in ROM that would start upon bootup. The problem was that there was no text editor or file system to speak of. You had an interactive prompt to do everything through. If you wanted to insert a line of code, you just typed it, starting with the line number. It would insert it into the correct spot in you code. e.g:

>10 print "hello"
>30 goto 10
>20 print "world"
>list
10 PRINT "hello"
20 PRINT "world"
30 GOTO 10
>

(In that example > is the BASIC prompt)

If you wanted to erase a line, you would type something like ERASE 20.
Some really fancy systems gave you a line editor (i.e. EDIT 10)
And if you didn't plan your line numbers and ran out (how do I insert a line between 10 and 11?) some systems gave you a RENUM command which would renumber your code (and adjust GOTOs and GOSUBs appropriately).

Fun Times!

素衣风尘叹 2024-08-31 22:23:27

最初的 BASIC 行编号实际上是该语言的一个组成部分,用于控制流。

GOTO 和 GOSUB 命令将获取该行,并将其用于控制​​流。这在当时很常见(尽管现在不鼓励这样做)。

The original BASIC line numbering was actually an integral part of the language, and used for control flow.

The GOTO and GOSUB commands would take the line, and use it for control flow. This was common then (even though it's discouraged now).

扶醉桌前 2024-08-31 22:23:27

它们被用作 GOTO 和 GOSUB 的标签,

如下所示:

10 PRINT "HELLO WORLD"
20 GOTO 10

在某些早期的 BASIC 版本中没有命名标签

如果您想在现有的 2 行代码之间插入一行,那么它们也是必需的,因为在早期,您没有完整的标签文本编辑器。一切都必须在“交互式”解释器中输入。

因此,如果您输入:

15 PRINT "AND THE UNIVERSE"

程序将变为:

10 PRINT "HELLO WORLD"
15 PRINT "AND THE UNIVERSE"
20 GOTO 10

当您用完行号时,您可以运行“重新编号”工具来对程序中的所有行重新编号,但在 Commodore 64 和其他家用计算机的早期,我们连这个都没有,所以你必须手动重新编号。这就是为什么您必须在行号中留出 10 个或更多的间隙,以便您可以轻松地在行号之间添加行。

如果您想尝试 Commodore 64 解释器,请查看这个用 Flash 编写的 C64 模拟器:http:// /codeazur.com.br/stuff/fc64_final/(无需安装)

They were used as labels for GOTO and GOSUB

Like this:

10 PRINT "HELLO WORLD"
20 GOTO 10

There were no named labels in some early BASIC versions

They were also required if you wanted to insert a line between 2 existing lines of code, because in the early days, you had no full text editors. Everything had to be typed in the "interactive" interpreter.

So if you typed:

15 PRINT "AND THE UNIVERSE"

The program would become:

10 PRINT "HELLO WORLD"
15 PRINT "AND THE UNIVERSE"
20 GOTO 10

When you ran out of line numbers, you could run a "renumbering" tool to renumber all lines in your program, but in the very early days of the Commodore 64 and other home computers, we didn't even have that, so you'd have to renumber manually. That's why you had to leave gaps of 10 or more in the line numbers, so you could easily add lines in between.

If you want to try out the Commodore 64 interpreter, check out this C64 emulator written in Flash: http://codeazur.com.br/stuff/fc64_final/ (no install required)

山有枢 2024-08-31 22:23:27

一个简单的谷歌就揭示了维基百科对此的说法

<块引用>

行号是一些较旧的编程语言(例如 GW-BASIC)中必需的语法元素。[2]造成这种情况的主要原因是当时大多数操作系统缺乏交互式文本编辑器。由于程序员的界面通常仅限于行编辑器,因此行号提供了一种机制,通过该机制可以引用源代码中的特定行进行编辑,并且程序员可以通过该机制在特定点插入新行。行号还提供了一种方便的方法来区分要输入到程序中的代码和用户输入后立即执行的命令(没有行号)。

A simple google reveals what wikipedia has to say about it:

Line numbers were a required element of syntax in some older programming languages such as GW-BASIC.[2] The primary reason for this is that most operating systems at the time lacked interactive text editors; since the programmer's interface was usually limited to a line editor, line numbers provided a mechanism by which specific lines in the source code could be referenced for editing, and by which the programmer could insert a new line at a specific point. Line numbers also provided a convenient means of distinguishing between code to be entered into the program and commands to be executed immediately when entered by the user (which do not have line numbers).

Smile简单爱 2024-08-31 22:23:27

在 BASIC 中,行号表示顺序。

此外,许多旧的编辑器不用于文件,而只是用于行(“行编辑器”,例如 ed,标准编辑器)。通过以这种方式对它们进行编号,您就知道您正在处理哪条生产线。

In BASIC, the line numbers indicated sequence.

Also, many older editors weren't for files, but simply lines ("line editors", e.g. ed, the standard editor). By numbering them this way, you knew which line you were working on.

一向肩并 2024-08-31 22:23:27

过去,所有语言都有序列号,一切都在打孔卡上。
每张卡有一行。
一副牌组成了你的程序。

当你丢下卡片时,你会将它们放入使用这些序列号的卡片分类器中。

当然,它们被控制流结构引用。

Back in the day all languages had sequence numbers, everything was on punched cards.
There was one line per card.
Decks of cards made up your program.

When you dropped the cards, you'd put them in a card sorter that used those sequence numbers.

And of course, they were referenced by control flow constructs.

裸钻 2024-08-31 22:23:27

在 C64 上,甚至没有真正的编辑器(至少是内置的)。要编辑程序的一部分,您可以执行类似 LIST 100-200 的操作,然后您只能编辑当前显示在屏幕上的那些行(不能向上滚动!)

On the C64, there wasn't even a real editor (built-in at least). To edit a part of the program, you'd do something like LIST 100-200, and then you'd only be able to edit those lines that were currently displayed on the screen (no scrolling upwards!)

離人涙 2024-08-31 22:23:27

它们是语句的标签,因此您可以GOTO行号。语句的数量不一定必须与文件中的物理行号相匹配。

They were labels for statements, so that you could GOTO the line number. The number of the statements did not necessarily have to match the physical line numbers in the file.

拒绝两难 2024-08-31 22:23:27

行号用于控制流程。没有命名的子例程。例如,您必须使用 GOSUB 60 来调用从第 60 行开始的子例程。


在您的更新中,并非所有语言都有标签,但所有语言都同时具有行号。曾经,一切都是打孔卡。 BASIC 是最早的交互式语言之一,您可以在其中实际键入内容并立即得到响应。行号仍然是当前的技术。

标签一项额外费用。您必须跟踪符号标签与其引用的代码或数据之间的相关性。但是,如果每一行都有一个行号(并且如果所有控制流语句的传输始终引用行的开头),那么您不需要单独的符号表。

还要记住,最初的 BASIC 解释器不需要变量的符号表:有 26 个名为 AZ 的变量。有些很复杂并且含有 An-Zn。有些人非常奇特,通过在变量后面添加“$”或“%”来区分字符串、整数和浮点。但不需要符号表。

The line numbers were used in control flow. There were no named subroutines. You had to use GOSUB 60, for instance, to call the subroutine starting at line 60.


On your update, not all languages had labels, but all languages had line numbers at one time. At one time, everything was punch cards. BASIC was one of the very first interactive languages, where you could actually type something and have a response immediately. Line numbers were still the current technology.

Labels are an extra expense. You have to keep track of the correlation between the symbolic label and the code or data to which it refers. But if every line has a line number (and if all transfer of control flow statements always refer to the beginning of a line), then you don't need a separate symbol table.

Also keep in mind that original BASIC interpreters didn't need a symbol table for variables: There were 26 variables named A-Z. Some were sophisticated and had An-Zn. Some got very fancy and added a distinction between string, integer and floating point by adding "$" or "%" after the variable. But no symbol table was required.

没︽人懂的悲伤 2024-08-31 22:23:27

IIRC,行号主要用作 GOTO 和 GOSUB 语句的标签,因为在某些(大多数?) BASIC 风格中,无法标记一段代码。

IIRC, line numbers were mostly used as labels for GOTO and GOSUB statements, since in some (most?) flavors of BASIC there was no way to label a section of code.

山有枢 2024-08-31 22:23:27

它们也被编辑器使用 - 即你说:

edit 100

编辑第 100 行。

They were also used by the editor - ie you said:

edit 100

to edit line 100.

垂暮老矣 2024-08-31 22:23:27

正如其他人指出的那样,这些行号被用作子例程的一部分。

当然,现在不再这样做是有原因的。想象一下,如果您在第 10 行上说 GOTO 20,然后意识到您需要在第 10 行之后再编写 10 行代码。突然间,您会遇到 20 行代码,因此您需要将子例程移得更远(更高的数字)并更改您的 GOTO 值,或者您需要编写另一个在代码中跳转得更远的子例程。

换句话说,它变成了真正的意大利面条代码的噩梦,并且维护起来并不有趣。

As others have pointed out, these line numbers were used as part of subroutines.

Of course, there's a reason that this isn't done anymore. Imagine if you say GOTO 20 on line 10, and then later realize you need to write 10 more lines of code after line 10. All of a sudden, you're smashing up against 20 so you either need to shift your subroutine farther away (higher numbers) and change your GOTO value, or you need to write another subroutine that jumps farther in the code.

In other words, it became a nightmare of true spaghetti code and is not fun to maintain.

爱要勇敢去追 2024-08-31 22:23:27

在许多情况下,它是在命令行中输入的(或者是在我的旧 Commodore 64 上),因此可能并不总是有文本编辑器,或者即使有,它也是非常基本的。

此外,您还需要执行 GOTO 等操作,以及在其他行之间插入行。

即:

10 PRINT "HELLO"
20 GOTO 10
15 PRINT " WORLD"

它在逻辑 10 15 20 中的位置

It was entered in on the command-line in many instances (or was, on my old Commodore 64) so there might not always have been a text editor, or if there was, it was quite basic.

In addition, you would need to do GOTOs and the like, as well as inserting lines in between others.

ie:

10 PRINT "HELLO"
20 GOTO 10
15 PRINT " WORLD"

where it would go in the logical 10 15 20

家住魔仙堡 2024-08-31 22:23:27

有些编辑器只有“覆盖”模式,没有“插入”模式。这使得编辑现有代码变得极其痛苦。但是,通过添加行号功能,您可以从文件中的任何位置修补现有代码:

100 PRINT "Broken Code"
200 PRINT "Foobar"
...
101 patch the broken code
102 patch more broken code

因为行号不必在文件中排序。

Some editors only had an "overwrite" mode and no "insert" mode. This made editing of existing code extremely painful. By adding that line-number feature, you could however patch existing code from anywhere within the file:

100 PRINT "Broken Code"
200 PRINT "Foobar"
...
101 patch the broken code
102 patch more broken code

Because line numbers didn't have to be ordered within the file.

路弥 2024-08-31 22:23:27

行号是语言的一部分,在一些非常早期的语言中,甚至操作系统也只是这些简单的行。你所需要的只是一次一行操作。尝试使用 1-4k 程序文件编写会计系统,并按大小对其进行分段以完成工作。要进行编辑,您可以使用行号来告诉您正在编辑的内容。因此,如果您输入如下内容:

10 PRINT "howdy"
20 GOTO 10
10 PRINT "WOOPS"
15 PRINT "MORE WOOPS"
20
RUN

您将得到:

WOOPS
MORE WHOOPS

空白 20 将有效删除该行。

Line numbers were a PART of the language, in some VERY early ones, even the OS was just these simple lines. ALL you had was one line at a time to manipulate. Try writing an accounting system using 1-4k program files and segmenting it by size to get stuff done. To edit you used the line numbers to tell what you were editing. So, if you entered like:

10 PRINT "howdy"
20 GOTO 10
10 PRINT "WOOPS"
15 PRINT "MORE WOOPS"
20
RUN

YOU WOULD GET:

WOOPS
MORE WHOOPS

The blank 20 would effectivly delete that line.

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