News for hackers. Stuff that matters.
Re:Abstract Syntax Tree for Patching Code and Assessing Code... by 1337shadow on Monday June 07, 2021 @02:42AM (#27419723)
Python's ast module is fantastic, we use it both to compile Python to Michelson[0], an assembly language for the Tezos blockchain, and Python to JavaScript[1]. However, for refactoring one might want to consider redbaron[2].

[0] https://yourlabs.io/pyratzlabs/pymich [1] https://yourlabs.io/oss/ryzom/#javascript [2] https://redbaron.readthedocs.io/en/latest/

Re:Abstract Syntax Tree for Patching Code and Assessing Code... by erezsh on Monday June 07, 2021 @04:55AM (#27420450)
> Python to JavaScript

That's cool. I'm working on something very similar, with the goal of generating idiomatic Javascript, which seems to be a goal for your code as well.

I did make a few different choices.

- Instead of worrying about formatting and indent, I just output whatever I feel like, and run Prettier afterwards. That way I don't have to worry about whitespace, indents, or superfluous parenthesis. - I integrated it with type analysis, so that operators like `+=` will produce different code for each type (ints vs lists, for example). - I also include the comments from the python source in the target javascript

But these things aside, I can see a lot of similarities!

Re:Abstract Syntax Tree for Patching Code and Assessing Code... by 1337shadow on Monday June 07, 2021 @07:31AM (#27421306)
Interesting! can you link your repo please? Life without webpack has been only pure joy TBH ;)
Re:Abstract Syntax Tree for Patching Code and Assessing Code... by erezsh on Monday June 07, 2021 @11:13AM (#27423523)
Sorry, no repo yet.
Re:Abstract Syntax Tree for Patching Code and Assessing Code... by acka on Monday June 07, 2021 @09:07AM (#27422090)
Please do realize that if your project ever ends up under source code management, reading diffs and merging changes may cause headaches for anyone who wishes to contribute to your code. SCM and liberal use of code reformatting tools usually don't mix well, unless you're very careful while configuring them.
Re:Abstract Syntax Tree for Patching Code and Assessing Code... by erezsh on Monday June 07, 2021 @10:52AM (#27423297)
> SCM and liberal use of code reformatting tools usually don't mix well

Care to elaborate about that?

It seems to me like the resulting code will be very consistent, and only change when I either improve the transpiler, or when Prettier issue an update to their formatting. But that's unlikely, and anyway I can just use a fixed version.

Re:Abstract Syntax Tree for Patching Code and Assessing Code... by heinrichhartman on Monday June 07, 2021 @02:57AM (#27419813)
> However, for refactoring one might want to consider redbaron[2].

> [2] https://redbaron.readthedocs.io/en/latest/

Thanks for the link. This looks really cool!

Re:Abstract Syntax Tree for Patching Code and Assessing Code... by codethief on Monday June 07, 2021 @06:46AM (#27421048)
It does! But it also looks really… dead?

> For now, RedBaron can be considered in alpha, the core is quite stable but it is not battle tested yet and is still a bit rough.

> RedBaron [supports] up to python 3.7 grammar

> Last commit: 11 Aug 2019

Does anyone know what's going on with the project?

Re:Abstract Syntax Tree for Patching Code and Assessing Code... by stevekemp on Monday June 07, 2021 @10:03AM (#27422727)
Agreed. I've not used it much, but I've done some fun things with it - mostly relating to adding linting checks for pipelines.

For example we use troposphere to generate cloudformation templates, so I wrote a linter which would ensure that every time we tried to create an elastic container registry on AWS we explicitly setup "security scanning" to be "on".

That kind of thing is trivial to write when you have the AST to manipulate:

* Find all calls to function "foo(...)".

* Error out if there is is no parameter "scan_on_push = True".

Re:Abstract Syntax Tree for Patching Code and Assessing Code... by anitil on Monday June 07, 2021 @03:03AM (#27419841)
I love it as well. I'm working on a Python AST viewer tool at the moment [0] so I've been spending a bit of time playing with it lately.

I'm thinking of calling it Pasta (Python AST view....A? I don't have a word that starts with A).

[0] I only really have a gif - https://imgur.com/a/4YYdfmZ

Re:Abstract Syntax Tree for Patching Code and Assessing Code... by joshuamorton on Monday June 07, 2021 @03:41AM (#27420060)
Re:Abstract Syntax Tree for Patching Code and Assessing Code... by anitil on Monday June 07, 2021 @07:06PM (#27428470)
Nooooo I guess it was pretty low hanging food.
Re:Abstract Syntax Tree for Patching Code and Assessing Code... by austinjp on Monday June 07, 2021 @05:43AM (#27420695)
Oh, nice. Based on the diagrams and the sibbling comment about Google's pasta, perhaps name it spaghetti? :)
Re:Abstract Syntax Tree for Patching Code and Assessing Code... by anitil on Monday June 07, 2021 @07:06PM (#27428476)
Good idea, thanks!