-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to generate lua code according to ast? #8
Comments
Basically, you need to traverse the AST producing the target code that corresponds to the AST format. What exactly do you want to do? Is Lua both your target and source code? |
Hello, I'm working on I planned to use the same util (ast-renderer.lua) to support more languages. My next targeted language is lua. Regards, |
@tst2005 I'm sorry for the delay, but I could not access the file |
I'm planning to use this as code changer. First I parse code to ast, then change ast but I need changed code back to Lua code. Are there some existing tools that convert AST back to Lua code (maybe in metalua project)? This could also be used as very good code beautifier. |
@stuta I'm not sure whether there are Lua code generators out there, but perhaps our code generator for Typed Lua can help: https://github.com/andremm/typedlua/blob/master/typedlua/tlcode.lua |
It seems somehow ok (with few bug fixes). There is line function code_block(): Lua-parse AST does not seem to contain .l -tag (I guess it is line number)? |
I tested lua-minify and metalua, none has good ast-to-source tools. I think it is best to use ast pos -tag and string.sub to generate fixed source from original source. If someone has example code of this kind of source generation I would like to see it. I want to use this tool to change parameter order and delete parameters in function calls. |
@stuta, did you get the code generation working for you? I got tlcode working and since I don't care for preserving the exact syntax/indentation, it should work for me (or at least I didn't come across any issues yet). It would be nice to be able to support a full round trip though. |
I created a new code generator by modifying typedlua/tlcode.lua heavly. I tried lua-parser, lua-minify and metalua as parser but luacheck was the best. It also gives errors on the same scan. I used is as a tool to change function parameters (add, remove or delete) and automatically add or remove require's to the top of the file. There are a lot of possibilities. It has been tested on my project that has 880 lua files, 265000 total lines and 208000 lines of code (not comments or blank). It still fails on some special cases like this: value = fn.iter(val):reduce(function(acc, rec)
if acc == "" then
return toStr(rec)
end
return acc..", "..toStr(rec)
end, "")
==>
value = fn.iter(val):reduce(function(acc, rec) ,
if acc == "" then
return toStr(rec)
end
return acc..", "..toStr(rec)
end"") I did it when I was sick and fixing non-working project bit by bit was not nice. Also, it is not well parametrized. So code is not pretty or easy to follow. I have been too busy to work on this. But if you want it I can give it to you. Maybe you could clean it an publish as open source. |
Does luacheck generate compatible AST? Its comment says "Returns AST (in almost MetaLua format)", but I'm not sure how much "almost" here makes it deviate from Metalua/lua-parser format.
Wouldn't this be a parser issue? Maybe the generated AST is wrong (and not ast2code conversion)?
Yes, I'd be interested in taking a look. You can email it to me to the address in the profile. Thank you. |
They are very similar, just a few minor differences, I can't remember what.
Ast is good, my ast2code logic is not.
I'll contact you later. I need to create first a standalone code without all possible libraries. |
Loot at https://github.com/Koihik/LuaFormatter/blob/master/docs/Style-Config.md. This project seems active and good. I tested with my hardest problems and it handles almost all of them. It also has quite a good configuration. I think I'll use it for formatting code. |
I have tested it more and it does not give enough freedom with options. I updated my own code for the latest Luacheck and it's almost complete now. I need some help for the ZeroBrane plugin, it works but it needs ui options and text select fixes. |
@stuta, send me an email or open ZBS ticket on github with details on what fixes are needed. |
No description provided.
The text was updated successfully, but these errors were encountered: