Skip to content
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

Open
ipengyo opened this issue Jun 9, 2017 · 15 comments
Open

How to generate lua code according to ast? #8

ipengyo opened this issue Jun 9, 2017 · 15 comments
Labels

Comments

@ipengyo
Copy link

ipengyo commented Jun 9, 2017

No description provided.

@andremm
Copy link
Owner

andremm commented Jun 14, 2017

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?

@tst2005
Copy link
Contributor

tst2005 commented Jul 4, 2017

Hello,

I'm working on sh to ast and ast to sh (see lua-sh-parser).

I planned to use the same util (ast-renderer.lua) to support more languages. My next targeted language is lua.
Let me know if I must release sooner as planned ;)

Regards,

@andremm
Copy link
Owner

andremm commented Sep 15, 2017

@tst2005 I'm sorry for the delay, but I could not access the file ast-render.lua. Do you need any help from the lua-parser side or can I close this?

@stuta
Copy link

stuta commented Jan 5, 2018

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.

@andremm
Copy link
Owner

andremm commented Jan 6, 2018

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

@stuta
Copy link

stuta commented Jan 10, 2018

It seems somehow ok (with few bug fixes).

There is line function code_block():
if block[1] and block[1].l and block[1].l > firstline then
fmt.indent = fmt.indent + 1

Lua-parse AST does not seem to contain .l -tag (I guess it is line number)?

@stuta
Copy link

stuta commented Jan 10, 2018

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.

@pkulchenko
Copy link

@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.

@pkulchenko
Copy link

Actually, given the discussion in #12, it seems like tlcode needs to be extended to cover ne, gt, and ge operators.

@andremm, any plans to include tlcode logic as one of pp outputs?

@stuta
Copy link

stuta commented Jan 24, 2020

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.

@pkulchenko
Copy link

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.

It still fails on some special cases like this:

Wouldn't this be a parser issue? Maybe the generated AST is wrong (and not ast2code conversion)?

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.

Yes, I'd be interested in taking a look. You can email it to me to the address in the profile. Thank you.

@stuta
Copy link

stuta commented Jan 24, 2020

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.

They are very similar, just a few minor differences, I can't remember what.

It still fails on some special cases like this:

Wouldn't this be a parser issue? Maybe the generated AST is wrong (and not ast2code conversion)?

Ast is good, my ast2code logic is not.

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.

Yes, I'd be interested in taking a look. You can email it to me to the address in the profile. Thank you.

I'll contact you later. I need to create first a standalone code without all possible libraries.

@stuta
Copy link

stuta commented Jan 25, 2020

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.

@stuta
Copy link

stuta commented Mar 25, 2020

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.

@pkulchenko
Copy link

@stuta, send me an email or open ZBS ticket on github with details on what fixes are needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants