2

I have large codebase written in HSP(wikipedia article - think "BASIC", but japanese).

By "large" I mean it has 151352 lines of code, 60 source files with total code size of 4.5 megabytes. Also, it has plenty of spaghetti code, no comments and badly needs refactoring. The good thing is that it has a lot of text messages, so not all of those lines represent actual program logic.

I'd like to convert this codebase to C++, while retaining my sanity. "I'd like" means that I'm not required to do it, but I'd strongly prefer to find a method to do it.

What's a good way to do it? Obviously, I can't just rewrite it all in C++ (Well, I could do it in theory, but it would take up to 2 years, and I would introduce many bugs in process), so (I think) a reasonable decision would be to implement code recompiler/preprocessor that would allow me to convert source code into messy C++ (HSP is much simpler than C++, so it should be possible) and then start refactoring/documenting the result.

Unfortunately, i'm not entirely sure how to approach building the recompiler efficiently. While I know there are Lex/Yacc/Bison/Boost::spirit, I haven't used them personally.

So can you recommend a good way perform such conversion? Any free tool ("free" as in "free beer") that is available on windows platform is allowed, as long as it doesn't affect license of original source code.

4

3 回答 3

1

Yacc 它的目标是有效地处理更复杂的任务,而且学习起来很复杂,我认为它是矫枉过正的。

Spirit 应该是更好的选择,如果您已经知道使用它,我个人会使用 Prolog 来完成这项任务。

Prolog 具有内置的语法分析,即所谓的 DCG。对于像 Basic 这样简单的语言,我很确定语法中没有实际问题,并且现代 Prologs(我认为 SWI-Prolog 有效)可以很好地处理源代码中的复杂字符编码。

此外,在 Prolog 中,您可以尝试应用一些天真来展开意大利面条代码。一般来说,这是一项复杂的任务,但如果你只有少量的模式,重复多次,这可能很容易。

模式匹配是此类问题的关键......

于 2012-05-19T13:44:18.147 回答
1

好吧,如果你真的想走这条路而忘记评论中的建议,你可能应该好好看看 openhsp 编译器,主要是 codegen 文件:

并且在你的眼睛下面有令牌:

看来HSP并没有那么复杂,可以跳过AST这一步。不过,您可以从中获得很好的优化。不要忘记准备一个 C++ 库来嵌入您生成的代码,这样您就可以管理 HSP 怪异(如全局变量和动态类型)。

如果你能从中破解一些东西,你还必须删除这个编译器所做的大部分工作(创建可执行文件、链接和东西)。不要忘记,这是一项非常漫长而艰巨的任务,可能不会比完全重写更快或更容易。但如果你准备好了,你会发现它很困难:)

于 2012-05-19T13:50:00.647 回答
0

根据代码库的原始所有者,从版本 3 开始的 HSP 包括 HSP 到 C 代码转换器。由于时间不够,信息没有得到验证,但这篇博客文章记录了名为 hspcnv 的工具,该工具旨在将 HSP 代码转换为 C 代码。文章是日文的。

于 2012-05-23T23:31:21.607 回答