r/cpp • Factorio Developer • Feb 15 '18

Lua written in modern C++ (>= 14)?

I work on Factorio (written in C++) and we use Lua as the script language for mods. So, from time to time I interact with the C implementation of Lua and my experience with it hasn't been the most pleasant.

It got me wondering - has anyone re-written it in modern C++? Everything I find online is "how to use Lua in C++" or "C++ wrapper for Lua" and they all use the basic C implementation provided from the official Lua website.

I've had quite a lot of experience with the internals of the C Lua implementation and from what I've seen it wouldn't be hard to re-write it using modern C++ - maybe 2~ weeks time. But from what I can find nobody has done it - which also makes me wonder if I'm missing something.

Part of me wants to do it just to say I did it and the other part of me wants to do it to see if I can make it faster than the C version: I think I can - not because "C++ is faster" but because I can mutate/experiment with the C++ version far easier and know changes I make are safe with how the rest of the implementation operates.

231 Upvotes

127 comments sorted by

View all comments

17

u/ColinPP PEGTL | taocpp Feb 15 '18

A while ago I wrote a PEGTL grammar that corresponds to the Lua 5.3 lexer and parser. If you get the PEGTL you can find it in src/example/pegtl/lua53_parse.cpp.

Should you decide to go ahead with a rewrite, and try to use this grammar, I'd be happy to help get you started with the PEGTL, and to iron out any bugs the grammar might still have. (It parses the official Lua test-suite, which is a good sign, but without any semantic actions it's not enough to be sure it's correct.)

9

u/Rseding91 Factorio Developer Feb 15 '18

If I do end up doing it, it will start as a carbon copy of the C implementation into C++ format so unless I mess up some copy-paste (which is likely) it will function identically to how the C version does.

Once that's done then I would start changing pieces of the logic to use C++ 1 step at a time making sure things still function correctly as I did it.

1

u/Dexterous_ C+++Ↄ Feb 16 '18

I just downloaded the Lua code from here, and basically search and replaced a bunch of stuff, and it just kinda worked, no errors not even warnings.. That was really surprising to me, here's a Github link so you can see what I did.

Only src/Makefile has some manual adjustments.

3

u/Rseding91 Factorio Developer Feb 17 '18

Yeah I started on it yesterday and this is what I have as of last night: https://github.com/Rseding91/LuaPlusPlus