Skip to content

Commit c6ee71e

Browse files
authored
TSL Transpiler: Introduce Linker and improvements (#1717)
* TSL Transpiler: Introduce Linker and improvements * Update * Update three.js
1 parent 43b3cf6 commit c6ee71e

File tree

3 files changed

+62
-42
lines changed

3 files changed

+62
-42
lines changed
Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1+
export class Comment {
2+
constructor(comment: string);
3+
4+
comment: string;
5+
6+
readonly isComment: true;
7+
}
8+
19
export class Program {
2-
constructor();
10+
constructor(body?: Statement[]);
311

412
body: Statement[];
513

6-
isProgram: true;
14+
readonly isProgram: true;
715
}
816

917
// Boolean seems to be handled as a Unary
@@ -23,11 +31,16 @@ export type Statement =
2331
| FunctionCall
2432
| Return
2533
| Discard
34+
| Continue
35+
| Break
2636
| Accessor
2737
| StaticElement
2838
| DynamicElement
2939
| AccessorElements
3040
| For
41+
| While
42+
| Switch
43+
| SwitchCase
3144
| null;
3245

3346
export class VariableDeclaration {
@@ -46,7 +59,7 @@ export class VariableDeclaration {
4659

4760
immutable: boolean;
4861

49-
isVariableDeclaration: true;
62+
readonly isVariableDeclaration: true;
5063
}
5164

5265
export class Uniform {
@@ -55,7 +68,7 @@ export class Uniform {
5568
type: string;
5669
name: string;
5770

58-
isUniform: true;
71+
readonly isUniform: true;
5972
}
6073

6174
export class Varying {
@@ -64,7 +77,7 @@ export class Varying {
6477
type: string;
6578
name: string;
6679

67-
isVarying: true;
80+
readonly isVarying: true;
6881
}
6982

7083
export class FunctionParameter {
@@ -75,26 +88,26 @@ export class FunctionParameter {
7588
qualifier: string | null;
7689
immutable: boolean;
7790

78-
isFunctionParameter: true;
91+
readonly isFunctionParameter: true;
7992
}
8093

8194
export class FunctionDeclaration {
82-
constructor(type: string, name: string, params?: FunctionParameter[]);
95+
constructor(type: string, name: string, params?: FunctionParameter[], body?: Statement[]);
8396

8497
type: string;
8598
name: string;
8699
params: FunctionParameter[];
87100
body: Statement[];
88101

89-
isFunctionDeclaration: true;
102+
readonly isFunctionDeclaration: true;
90103
}
91104

92105
export class Expression {
93106
constructor(expression: string);
94107

95108
expression: string;
96109

97-
isExpression: true;
110+
readonly isExpression: true;
98111
}
99112

100113
export class Ternary {
@@ -104,7 +117,7 @@ export class Ternary {
104117
left: Statement;
105118
right: Statement;
106119

107-
isTernary: true;
120+
readonly isTernary: true;
108121
}
109122

110123
export class Operator {
@@ -114,7 +127,7 @@ export class Operator {
114127
left: Statement;
115128
right: Statement;
116129

117-
isOperator: true;
130+
readonly isOperator: true;
118131
}
119132

120133
export class Unary {
@@ -124,7 +137,7 @@ export class Unary {
124137
expression: Statement;
125138
after: boolean;
126139

127-
isUnary: true;
140+
readonly isUnary: true;
128141
}
129142

130143
export class Number {
@@ -133,26 +146,25 @@ export class Number {
133146
type: string;
134147
value: string;
135148

136-
isNumber: true;
149+
readonly isNumber: true;
137150
}
138151

139152
export class String {
140153
constructor(value: string);
141154

142155
value: string;
143156

144-
isString: true;
157+
readonly isString: true;
145158
}
146159

147160
export class Conditional {
148-
constructor(cond?: Conditional | null);
161+
constructor(cond?: Conditional | null, body?: Statement[]);
149162

150163
cond: Conditional | null;
151-
152164
body: Statement[];
153165
elseConditional: Conditional | null;
154166

155-
isConditional: true;
167+
readonly isConditional: true;
156168
}
157169

158170
export class FunctionCall {
@@ -161,57 +173,57 @@ export class FunctionCall {
161173
name: string;
162174
params: Statement[];
163175

164-
isFunctionCall: true;
176+
readonly isFunctionCall: true;
165177
}
166178

167179
export class Return {
168180
constructor(value: Statement);
169181

170182
value: Statement;
171183

172-
isReturn: true;
184+
readonly isReturn: true;
173185
}
174186

175187
export class Discard {
176188
constructor();
177189

178-
isDiscard: true;
190+
readonly isDiscard: true;
179191
}
180192

181193
export class Continue {
182194
constructor();
183195

184-
isContinue: true;
196+
readonly isContinue: true;
185197
}
186198

187199
export class Break {
188200
constructor();
189201

190-
isBreak: true;
202+
readonly isBreak: true;
191203
}
192204

193205
export class Accessor {
194206
constructor(property: string);
195207

196208
property: string;
197209

198-
isAccessor: true;
210+
readonly isAccessor: true;
199211
}
200212

201213
export class StaticElement {
202214
constructor(value: Statement);
203215

204216
value: Statement;
205217

206-
isStaticElement: true;
218+
readonly isStaticElement: true;
207219
}
208220

209221
export class DynamicElement {
210222
constructor(value: Statement);
211223

212224
value: Statement;
213225

214-
isDynamicElement: true;
226+
readonly isDynamicElement: true;
215227
}
216228

217229
export class AccessorElements {
@@ -220,40 +232,44 @@ export class AccessorElements {
220232
object: FunctionCall | Accessor;
221233
elements: (StaticElement | DynamicElement)[];
222234

223-
isAccessorElements: true;
235+
readonly isAccessorElements: true;
224236
}
225237

226238
export class For {
227-
constructor(initialization: Statement, condition: Statement, afterthought: Statement);
239+
constructor(initialization: Statement, condition: Statement, afterthought: Statement, body?: Statement[]);
228240

229241
initialization: Statement;
230242
condition: Statement;
231243
afterthought: Statement;
232-
233244
body: Statement[];
234245

235-
isFor: true;
246+
readonly isFor: true;
236247
}
237248

238-
export class Switch {
239-
constructor(discriminant: Statement);
249+
export class While {
250+
constructor(condition: Statement, body?: Statement[]);
240251

252+
condition: Statement;
241253
body: Statement[];
242254

255+
readonly isWhile: true;
256+
}
257+
258+
export class Switch {
259+
constructor(discriminant: Statement, cases: Statement[]);
260+
243261
discriminant: Statement;
244-
case: Statement | null;
245-
isSwitch: true;
262+
cases: Statement[];
263+
264+
readonly isSwitch: true;
246265
}
247266

248267
export class SwitchCase {
249-
constructor(caseCondition: Statement);
250-
251-
caseCondition: Statement;
268+
constructor(body: Statement, conditions?: Statement[] | null);
252269

253270
body: Statement[];
254-
255-
nextCase: SwitchCase | null;
271+
conditions: Statement[];
256272

257273
isDefault: boolean;
258-
isSwitchCase: true;
274+
readonly isSwitchCase: true;
259275
}

types/three/examples/jsm/transpiler/TSLEncoder.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
Comment,
23
Conditional,
34
Expression,
45
For,
@@ -10,6 +11,7 @@ import {
1011
Uniform,
1112
VariableDeclaration,
1213
Varying,
14+
While,
1315
} from "./AST.js";
1416

1517
export default class TSLEncoder {
@@ -25,19 +27,21 @@ export default class TSLEncoder {
2527

2628
addImport(name: string): void;
2729
emitUniform(node: Uniform): string;
28-
emitExpression(node: Expression): string;
30+
emitExpression(node: Expression, output?: string | null): string;
2931
emitBody(body: Statement[]): string;
3032
emitTernary(node: Ternary): string;
3133
emitConditional(node: Conditional): string;
3234
emitLoop(node: For): string;
3335
emitSwitch(switchNode: Switch): string;
3436
emitFor(node: For): string;
3537
emitForWhile(node: For): string;
38+
emitWhile(node: While): string;
3639
emitVariables(node: VariableDeclaration, isRoot?: boolean): string;
3740
emitVarying(node: Varying): string;
3841
emitOverloadingFunction(nodes: FunctionDeclaration[]): string;
3942
emitFunction(node: FunctionDeclaration): string;
4043
setLastStatement(statement: Statement | null): void;
41-
emitExtraLine(statement: Statement): string;
44+
emitComment(statement: Comment, body: Statement[]): string;
45+
emitExtraLine(statement: Statement, body: Statement[]): string;
4246
emit(ast: Program): string;
4347
}

0 commit comments

Comments
 (0)