lotabout / write-a-C-interpreter

Write a simple interpreter of C. Inspired by c4 and largely based on it.

Repository from Github https://github.comlotabout/write-a-C-interpreterRepository from Github https://github.comlotabout/write-a-C-interpreter

重复的LEV

mmclkv opened this issue · comments

在使用return语句时,生成了一句LEV,而在function_body()函数的最后,当parse到右花括号时,又会生成一句LEV。

@mmclkv 两个LEV都是必要的,虽然有些不会执行,例如下面的例子:

int hello(int x) {
    if (x < 0) {
        return 0;
    }
}

不谈它的功能,只说应该生成怎样的代码。第一句 return 必需生成一句 LEV 用来退出函数。这时函数的最后 } 也需要加一句 LEV 用来退出函数。而当函数里的最后一句语句是 return 时就出现了你说的情况。

当然,也可以增加判断机制来减少这个额外的指令,只是这会增加额外的复杂度。

嗯,感谢楼主的回答。其实我的本意只是觉得这种翻译方法与一般的翻译方法不太一样,不过在楼主的代码中,LEV的功能也和平常RET的功能不一样,所以在楼主的代码中,这个问题也不是问题了 :)