Expr
타입, IR 분류
중간 표현(IR)이 담을 수 있는 식 형태의 전부입니다. Node와 ReadonlyNode가 만들고, WGSL, GLSL, CPU 백엔드가 순회하며 생성하거나 평가하는 닫힌 집합입니다.
import type { Expr } from 'typeshade'import type { Expr } from 'typeshade/core/ir'
설명은 커밋 af54ddad5의 검토된 컴파일러 문서를 번역한 것입니다. 시그니처와 예제 코드는 원문을 따르며, 필요한 경우 설명에 편집상 수정이 반영됩니다.
구문
type Expr = | { readonly op: 'lit'; readonly type: ShaderType; readonly value: number | boolean } | { readonly op: 'constref'; readonly type: ShaderType; readonly name: string } | { readonly op: 'overrideref'; readonly type: ShaderType; readonly name: string } | { readonly op: 'externref'; readonly type: ShaderType; readonly name: string } | { readonly op: 'param'; readonly type: ShaderType; readonly name: string; /** Where this was written, when it is the TARGET of an assignment — the lvalue a * statement is about to write, so a debugger can highlight what changes rather than the * whole line. A read of the same name carries none in this increment. Read it with * {@link sourceSpanOf}. */ readonly span?: SourceSpan; } | { readonly op: 'varref'; readonly type: ShaderType; readonly name: string; /** Where this was written, when it is the TARGET of an assignment — the lvalue a * statement is about to write, so a debugger can highlight what changes rather than the * whole line. An authored workgroup-variable read also carries its span for stage * diagnostics; it adds no debugger step. Read it with {@link sourceSpanOf}. */ readonly span?: SourceSpan; } | { readonly op: 'binop'; readonly type: ShaderType; readonly bop: BinOp; readonly a: Expr; readonly b: Expr; } | { readonly op: 'unop'; readonly type: ShaderType; readonly a: Expr } | { readonly op: 'compare'; readonly type: ShaderType; readonly cop: CmpOp; readonly a: Expr; readonly b: Expr; } | { readonly op: 'logical'; readonly type: ShaderType; readonly lop: LogOp; readonly a: Expr; readonly b: Expr; } | { readonly op: 'call'; readonly type: ShaderType; readonly fn: string; readonly args: readonly Expr[]; readonly declRef?: FuncDecl; /** Where this call was written, when it is the outermost call node lowered from a * `ts.CallExpression`. Stepping into a helper has to tell two calls in one statement * apart, which is why this expression kind carries a span at all. A call the front end * synthesises while expanding one (the `random` hash, an array higher-order function, * a `Math.*` expansion, a numeric cast) carries none, because it was written nowhere. * Read it with {@link sourceSpanOf}. */ readonly span?: SourceSpan; /** On a `console.*` call whose author wrote a string literal among the arguments: every * argument in the order written, a string for a label and a number for the index of a * value in `args`. A label has no GPU representation and is never an `Expr`; it lives * here, on the host's side of the call (Rule 7.8, surface §66). Absent when every * argument is a value, which is then `args` in order. */ readonly labels?: readonly (string | number)[]; } | { readonly op: 'member'; readonly type: ShaderType; readonly base: Expr; readonly field: string; } | { readonly op: 'construct'; readonly type: ShaderType; readonly args: readonly Expr[] } | { readonly op: 'select'; readonly type: ShaderType; readonly cond: Expr; readonly ifTrue: Expr; readonly ifFalse: Expr; } | { readonly op: 'index'; readonly type: ShaderType; readonly base: Expr; readonly idx: Expr; /** Where this was written, when it is the TARGET of an assignment — the lvalue a * statement is about to write, so a debugger can highlight what changes rather than the * whole line. A read of the same name carries none in this increment. Read it with * {@link sourceSpanOf}. */ readonly span?: SourceSpan; } | { readonly op: 'matchExpr'; readonly type: ShaderType; readonly scrutinee: Expr; readonly cases: ReadonlyArray<readonly [number, Expr]>; readonly default: Expr; };설명
IR이 담을 수 있는 식 형태의 전부입니다. Node와 ReadonlyNode가 만들고, WGSL, GLSL, CPU 백엔드가 순회하며 생성하거나 평가하는 닫힌 집합입니다. op를 판별자로 쓰는 판별 공용체이며 메서드가 없습니다. 그래서 패스는 모든 변형을 패턴 매칭할 수 있고(백엔드의 switch에서 case가 빠지면 tsc가 보고한다), 하위 트리를 자유롭게 공유할 수 있습니다. 모든 변형은 자신의 type: ShaderType을 가집니다. IR은 만들어질 때 타입이 완전히 정해지므로, 백엔드는 생성하는 동안 타입을 다시 추론하지 않습니다. 이 객체 리터럴은 직접 작성할 필요가 없습니다. Node의 체이닝 메서드(.add(), .mul() 등)와 vec4, mod 같은 독립 함수가 type을 채우고, 형태를 만들기 전에 피연산자를 검증합니다.
함께 보기
소스
src/core/ir/nodes.ts, 36행, 커밋 af54ddad5 기준