为 C 的子集实现基于堆栈的虚拟机

发布于 2024-09-10 21:07:52 字数 408 浏览 2 评论 0原文

大家好,我目前正在实现一种简单的编程语言来学习经验,但我需要一些建议。目前我正在设计我的解释器,但遇到了问题。

我的语言是 C 的子集,并且我在堆栈解释器实现方面遇到问题。在该语言中,将编译以下内容:

somefunc ()
{
    1 + 2;
}

main ()
{
    somefunc ();
}

现在这是可以的,但是当计算“1+2”时,结果被推送到堆栈上,然后函数返回,但堆栈上仍然有一个数字,而不应该有。我该如何解决这个问题?

我考虑过在函数调用之前保存堆栈的“状态”并在函数调用之后恢复“状态”。例如,将元素数量保存在堆栈上,然后执行函数代码,返回,然后从堆栈中弹出,直到元素数量与之前相同(如果函数返回某些内容,则可能+1)。

有什么想法吗?感谢您的任何提示!

Hello everyone I'm currently implementing a simple programming language for learning experience but I'm in need of some advice. Currently I'm designing my Interpreter and I've come into a problem.

My language is a subset of C and I'm having a problem regarding the stack interpreter implementation. In the language the following will compile:

somefunc ()
{
    1 + 2;
}

main ()
{
    somefunc ();
}

Now this is alright but when "1+2" is computed the result is pushed onto a stack and then the function returns but there's still a number on the stack, and there shouldn't be. How can I get around this problem?

I've thought about saving a "state" of the stack before a function call and restoring the "state" after the function call. For example saving the number of elements on the stack, then execute the function code, return, and then pop from the stack until we have the same number of elements as before (or maybe +1 if the function returned something).

Any ideas? Thanks for any tips!

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

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

发布评论

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

评论(3

调妓 2024-09-17 21:07:52

好问题!我的爱好之一是为玩具语言编写编译器,感谢您出色的编程品味。

表达式语句是语句中的代码只是一个表达式的语句。这意味着 形式的任何内容。 ;,其中包括赋值和函数调用等内容,但不包括 ifwhilereturn。任何表达式语句最后都会在堆栈上留下剩余值,您应该将其丢弃。

1 + 2 是一个表达式语句,但这些也是:

  • x = 5;
    赋值表达式将值5留在堆栈上,因为赋值的结果是左侧操作数的值。 语句完成后,您将弹出未使用的值 5。

  • printf("hello world!\n");
    printf() 返回输出的字符数。该值将保留在堆栈中,因此当语句完成时将其弹出。

实际上,每个表达式语句都会在堆栈上留下一个值,除非表达式的类型为 void。在这种情况下,您可以使用特殊情况的 void 语句并且之后不弹出任何内容,或者将假装的“void”值推入堆栈,以便您始终可以弹出一个值。

Great question! One of my hobbies is writing compilers for toy languages, so kudos for your excellent programming taste.

An expression statement is one where the code in the statement is simply an expression. This means anything of the form <expression> ;, which includes things like assignments and function calls, but not ifs, whiles, or returns. Any expression statement will have a left over value on the stack at the end, which you should discard.

1 + 2 is an expression statement, but so are these:

  • x = 5;
    The assignment expression leaves the value 5 on the stack since the result of an assignment is the value of the left-hand operand. After the statement is finished you pop off the unused value 5.

  • printf("hello world!\n");
    printf() returns the number of characters output. You will have this value left over on the stack, so pop it when the statement finishes.

Effectively every expression statement will leave a value on the stack unless the expression's type is void. In that case you either special-case void statements and don't pop anything afterwards, or push a pretend "void" value onto the stack so you can always pop a value.

森末i 2024-09-17 21:07:52

您将需要一个更智能的解析器。当您看到其值未被使用的表达式时,您需要发出 POP。

You'll need a smarter parser. When you see an expression whose value isn't being used then you need to emit a POP.

东风软 2024-09-17 21:07:52

这是学习优化的一个重要机会。你有一个执行数字但整数数学的函数, int 数学结果甚至不以任何方式、形状或形式使用。

让编译器优化该函数将减少大量生成和执行的字节码!

This is an important opportunity on learning optimization. you have a function that does number but integer math, the int math result isn't even used in any way, shape, or form.

Having your compiler optimize the function away would reduce alot of bytecode being generated and executed for nothing!

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