lua-parser icon indicating copy to clipboard operation
lua-parser copied to clipboard

How to generate lua code according to ast?

Open ipengyo opened this issue 8 years ago • 15 comments
trafficstars

ipengyo avatar Jun 09 '17 12:06 ipengyo

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?

andremm avatar Jun 14 '17 14:06 andremm

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,

tst2005 avatar Jul 04 '17 11:07 tst2005

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

andremm avatar Sep 15 '17 19:09 andremm

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 avatar Jan 05 '18 15:01 stuta

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

andremm avatar Jan 06 '18 14:01 andremm

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 avatar Jan 10 '18 01:01 stuta

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 avatar Jan 10 '18 02:01 stuta

@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 avatar Jan 23 '20 20:01 pkulchenko

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?

pkulchenko avatar Jan 23 '20 20:01 pkulchenko

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.

stuta avatar Jan 24 '20 13:01 stuta

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.

pkulchenko avatar Jan 24 '20 16:01 pkulchenko

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 avatar Jan 24 '20 20:01 stuta

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 avatar Jan 25 '20 15:01 stuta

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 avatar Mar 25 '20 20:03 stuta

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

pkulchenko avatar Mar 25 '20 21:03 pkulchenko