rewriteExprsInFunc()
Function in IR
Applies rewrite to every expression in the body of f and returns the rewritten function.
import { rewriteExprsInFunc } from 'typeshade'
The signature, the description and the examples come from the compiler's own source at commit 26de7be8.
Syntax
function rewriteExprsInFunc(f: FuncDecl, rewrite: (e: Expr) => Expr): FuncDeclParameters
Return value
A copy of f with the rewritten body, or f itself when no
expression changed.
Description
The walk visits every statement and expression shape in the body. Each
expression is passed to rewrite after its children have been rewritten,
and whatever rewrite returns is taken as finished output: it is not
walked again.
Object identity is preserved wherever possible. A subtree that rewrite
leaves alone comes back as the original object, and f itself is returned
when nothing in the body changed. An expression object that appears at
several places in the body is rewritten once, and the same new object is
used at every occurrence, so passes that correlate a variable’s
declaration, assignments and reads by object identity keep working on the
result.
Examples
Example
// Replace every literal `0` with `1`.const g = rewriteExprsInFunc(f, (e) => e.op === 'lit' && e.value === 0 ? { ...e, value: 1 } : e,)Source
src/core/passes/rename-varrefs.ts, line 71, at commit 26de7be8