by breuleux · 1y ago · 0 comments
transform = Replace(pattern="$x + $y", replacement="($x + 1) + $y")
transform("1 + 1") # "(1 + 1) + 1
transform("3 + 4 + 5") # "(((3 + 1) + 4) + 1) + 5
transform("10 * 1 + 1") # no change, because the expression is actually (10 + 1) + 1
If you give me a primitive like that in Lisp, I'm going to use it. It's a lot more trustworthy than "replace item in list by index". Because it's nice and all that everything is a list in Lisp, but manipulating code as lists (or maps or whatever) like you describe isn't going to get you very far. If you want to do something as simple as replacing all calls to (f x) by (f x 1), you better hope you don't come across code like (let ((f 123)) ...), for example. Outside of toy cases and examples, you need robust primitives that understand the code's semantic structure. ast-grep --pattern 'var code = $PAT' --rewrite 'let code = $PAT' --lang js
https://ast-grep.github.io/guide/introduction.html