语言设计:Goto 和函数的结合

发布于 2024-08-28 10:59:08 字数 569 浏览 4 评论 0原文

我正在设计并正在重新考虑一种与汇编语言相似的低级解释性编程语言。

我很快就遇到了函数/循环/goto 决策问题,并认为像 whilefor 这样的 while 循环会太高级且不合适,而 goto 会太低水平,无法维持并且再次普遍邪恶。 像您从大多数语言中知道的那样,具有返回值和参数的函数也不适合该语言的概念。

因此,我试图找出函数和 goto 之间能够实现

  • 递归
  • 高效循环的

东西经过一番思考,我想到了子例程的想法:

  • 它们像函数一样有开头和结尾
  • 它们有名称但没有参数,例如a goto
  • 你可以通过跳转进入一个,并在结束之前通过 return 再次退出它(不返回任何结果,仅停止子例程)
  • 就像普通代码一样处理 ->像 goto 这样的全局范围

所以我想知道:

  • 上面的想法好吗?有哪些(缺点)优点?
  • 是否会有更好的函数和 goto 组合,甚至是全新的想法?

I'm designing and currently rethinking a low-level interpreted programming language with similarities to assembler.

I very soon came across the functions/loops/gotos decision problem and thought that while loops like while and for would be too high-level and unfitting, gotos would be too low level, unmaintainable and generally evil again.
Functions like you know them from most languages that have return values and arguments aren't fitting in the language's concept either.

So I tried to figure out something between a function and a goto which is capable of

  • Recursion
  • Efficient loops

After some thinking I came up with the idea of subroutines:

  • They have a beginning and an end like a function
  • They have a name but no arguments like a goto
  • You can go into one with jump and go out of it again before its end with return (doesn't give back any result, only stops the subroutine)
  • Handled just like normal code -> Global scope like goto

So I wanted to know:

  • Is the idea above good? What are the (dis)advantages?
  • Would there be a better combination of function and goto or even a completely new idea?

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

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

发布评论

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

评论(3

再可℃爱ぅ一点好了 2024-09-04 10:59:08

上面的想法好不好?

不会。

或者至少在您通过设计新语言更好地解释您试图解决的问题之前不会。

有哪些(缺点)优点?

没有功能抽象,没有递归,仅通过共享可变状态进行编程。非常弱的组合原理。对于人类程序员来说很难使用,并且没有意义作为编译器目标。


想要完成这样的项目,你必须有一个目标。如果你的目标是学习一些东西,你最好学习一些优秀的语言,并尝试弄清楚你可以窃取什么,你可以组合什么,或者你可以实现什么。如果您有一个想要解决的实际问题,并且无法通过标准汇编语言解决,那就很有趣 - 告诉我们它是什么。

您可以尝试谷歌搜索“可移植汇编语言”,看看是否找到任何有趣的东西。

Is the idea above good?

No.

Or at least not until you give a much better explanation of what problems you are trying solve by designing a new language.

What are the (dis)advantages?

No functional abstraction, no recursion, programming only by sharing mutable state. Very weak composition principle. Difficult to use for a human programmer and having no point as a compiler target.


To get somewhere with a project like this, you have to have a goal. If your goal is to learn something, you're better off studying some great languages and trying to figure what you can steal, what you can combine, or what you can implement. If you have a real problem you're trying to solve, and it can't be solved by a standard assembly language, that's interesting—tell us what it is.

You might try Googling "portable assembly language" and see if you hit anything interesting.

Oo萌小芽oO 2024-09-04 10:59:08

我认为当你处于汇编级别时,GOTO 不再是邪恶的,而开始成为程序实际工作的方式

但无论如何,你的子例程是否带有参数?即使他们不这样做,你的商店如何注册状态以避免递归溢出?

I think when you're at assembly-level, GOTO stops being evil, and starts just being how programs actually work.

But anyway, do your sub-routines take parameters? Even if they don't, how do your store register state to avoid recursion overflow?

满身野味 2024-09-04 10:59:08

您正在实现的子例程与 Basic 中的 GOSUB 或汇编中的 CALL 相同。

仅当将递归用于递归算法时,递归才有意义。这需要带有参数、局部变量和返回值的函数。

然而,在一些只有全局变量的简单脚本语言中,有PUSHPOP指令用于将变量存储在堆栈中(就像汇编语言中寄存器的压入和弹出一样) 。这些可用于局部变量和递归的低级实现。

我在 Rosetta 代码中的一些 Vedit 宏语言示例中使用了该方法,例如参见 河内塔贝塞尔曲线

The subroutine you are implementing is just the same as GOSUB in Basic or CALL in Assembly.

Recursion is only meaningful if you are using it for recursive algorithms. That requires functions with parameters, local variables and a return-value.

However, in some simple scripting languages that only have global variables, there are PUSH and POP instructions for storing variables in stack (just like registers are pushed and popped in Assembly language). Those can be used for low-level implementation of local variables and recursion.

I have used that method in some Vedit Macro Language examples in Rosetta Code, see for example Towers of Hanoi and Bezier curve.

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