# [Clean], 386 bytes There's almost certainly huge savings I can get out of the helper function, probably by using another one instead of `case` and `until`. Working on that. import StdEnv,Data.List i=isPrefixOf $['if(':s]=g')'s\x h=h++['?':x] $['else if(':s]=g')'s\x h=[':':h]++['?':x] $['else ':s]=g'='s\x _=[':':x] $[]=[] g c s f#(h,t)=span((<>)c)s #(v,x,y)=case(until(\(_,k)=k==[]||i['if']k||i['else']k)(\q=:(x,y)|y>[]=(x++[hd y],tl y)=q)([],tl t))of([v,'=':x],y)=([v,'='],x,y);(x,y)=([],x,y) #z= $y #x=f(init x)h |z%(1,1)==['=']=z%(0,1)++x++drop 2z=v++x++z [Try it online!][TIO-jn2fwsbr] [Clean]: https://github.com/Ourous/curated-clean-linux [TIO-jn2fwsbr]: https://tio.run/##ZVHLbsIwELznKyyFyrZiKtJjYOmFHiohFanHYCE3D2KRB8QmSqJ8e1M7QC@cPKOZ3RmtozwR5VhU8TVPUCFkOcriXNUafev4o2zYRmjxupVKOxKk2tVJKtuv1JmFWKYEB4rDEVOs9i3KIPO8EL/joOVWT3KVoGdTiAMcZPzZevfB5DvcfJPMIeTOEUVIodQlGdMU1FmUhKzWNKLKcUnDWtZRiIRKyLXUMid7cmAnCicws8MgbVvMTxOyYQZTsr9AQOzg0K1NCGlNpyxGHWc6R2bdhZJwwprSKiVhw0w5U8lG3RmfgpfTFrBuCxy3BzTrHLeFlMhSatTSzBn6F@Izn5pGdhAMXRjqeSY2rqszeuuhmVg/fmthfsAsQdOZy5W/oOYtVuaBxfJ2WfAf6KHM/X9pifn4G6W5OKpx/rkdN10pChndyC4XOq3qYpz//AE "Clean – Try It Online"