4 ms·
I wonder if something like this exists for other languages? Most rewriting/refactoring tools make too many assumptions about the surrounding project. Something
by bmc7505 5y ago
I wonder if something like this exists for other languages? Most rewriting/refactoring tools make too many assumptions about the surrounding project. Something simple which supports pattern matching against a CST subtree and invertible parsing/unparsing would be the ideal polyglot macro-system. Suppose I want to find and rename all variables with a certain type or rewrite an expression of a certain shape in multiple languages at once (e.g. Java/JavaScript/C/C++). Something between a regular expressions (too brittle) and a full parser would be great a great solution to have for this use case.
- erezsh 5y agoI actually have a prototype of something like that, built on top of Lark. It's still far from being complete or user friendly, but I'm already successfully transpiling a subset of Python to Javascript. It still needs a lot of handholding, but at the same time I get to use gems like - ... = TemplateTranslator({ 'isinstance($a, str)': "typeof($a) == 'string'", 'isinstance($a, bool)': "typeof($a) == 'boolean'", 'len($a)': '$a.length', 'str($a)': '$a.toString()', 'getattr($a, $b)': '$a and $a[$b]', 'getattr($a, $b, $c)': '($a and $a[$b]) or $c', 'hasattr($a, $b)': '$b in $a', '$a.join($b)': '$b.join($a)', ... })
- bmc7505 5y agoNice! I like the syntax, this reminds me of the structural search and replace feature from IntelliJ IDEA. Is this the project you were referring to? https://github.com/erezsh/py2js https://github.com/erezsh/py2js