rewriteExprsInFunc
이 페이지에서

rewriteExprsInFunc()

함수, IR 분류

Applies rewrite to every expression in the body of f and returns the rewritten function.

import { rewriteExprsInFunc } from 'typeshade'

시그니처, 설명, 예제는 커밋 c66579bf의 컴파일러 소스에서 그대로 가져온 영어 원문입니다.

구문

function rewriteExprsInFunc(f: FuncDecl, rewrite: (e: Expr) => Expr): FuncDecl

매개변수

f FuncDecl

The function whose body is rewritten.

rewrite (e: Expr) => Expr

Called once per expression. Return the expression unchanged to keep it, or a new expression to replace it.

반환값

FuncDecl

A copy of f with the rewritten body, or f itself when no expression changed.

설명

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.

예제

예제

// Replace every literal `0` with `1`.
const g = rewriteExprsInFunc(f, (e) =>
e.op === 'lit' && e.value === 0 ? { ...e, value: 1 } : e,
)

소스

src/core/passes/rename-varrefs.ts, 71행, 커밋 c66579bf 기준

이 페이지 편집 문제 보고