shaderkit 0.2.1 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ast.ts CHANGED
@@ -1,137 +1,403 @@
1
- export class Literal {
2
- constructor(public value: string /*| number | boolean*/) {}
1
+ /**
2
+ * A position in the source code.
3
+ */
4
+ // export interface Position {
5
+ // line: number // >= 1
6
+ // column: number // >= 0
7
+ // }
8
+
9
+ /**
10
+ * Represents the source location of a node.
11
+ */
12
+ // export interface SourceLocation {
13
+ // source: string | null
14
+ // start: Position
15
+ // end: Position
16
+ // }
17
+
18
+ /**
19
+ * Base interface for all AST nodes.
20
+ */
21
+ export interface Node {
22
+ type: string
23
+ // loc: SourceLocation | null
3
24
  }
4
25
 
5
- export class Identifier {
6
- constructor(public value: string) {}
26
+ /**
27
+ * Represents the root of an AST.
28
+ */
29
+ export interface Program extends Node {
30
+ type: 'Program'
31
+ body: Statement[]
7
32
  }
8
33
 
9
- export class Type {
10
- constructor(public name: string, public parameters: (Type | Literal | Identifier)[] | null) {}
34
+ /**
35
+ * A variable identifier.
36
+ */
37
+ export interface Identifier extends Node {
38
+ type: 'Identifier'
39
+ name: string
11
40
  }
12
41
 
13
- export class VariableDeclarator {
14
- constructor(public name: string, public value: AST | null) {}
42
+ /**
43
+ * A shader literal representing a `bool`, `float`, `int`, or `uint` type.
44
+ */
45
+ export interface Literal extends Node {
46
+ type: 'Literal'
47
+ value: string /*| number | boolean*/
15
48
  }
16
49
 
17
- export class VariableDeclaration {
18
- constructor(
19
- public layout: Record<string, string | boolean> | null,
20
- public qualifiers: string[],
21
- public kind: 'var' | 'let' | 'const' | null,
22
- public type: Type | Identifier,
23
- public declarations: VariableDeclarator[],
24
- ) {}
50
+ /**
51
+ * An array and its dimensions.
52
+ */
53
+ export interface ArraySpecifier extends Node {
54
+ type: 'ArraySpecifier'
55
+ typeSpecifier: Identifier
56
+ dimensions: (Literal | Identifier | null)[]
25
57
  }
26
58
 
27
- export class StructDeclaration {
28
- constructor(public name: string, public members: VariableDeclaration[]) {}
59
+ /**
60
+ * An array initialization expression.
61
+ */
62
+ export interface ArrayExpression extends Node {
63
+ type: 'ArrayExpression'
64
+ typeSpecifier: ArraySpecifier
65
+ elements: Expression[]
29
66
  }
30
67
 
31
- export class FunctionDeclaration {
32
- constructor(
33
- public name: string,
34
- public type: Type | Identifier,
35
- public qualifiers: string[],
36
- public args: VariableDeclaration[],
37
- public body: BlockStatement | null,
38
- ) {}
68
+ export type UnaryOperator = '-' | '+' | '!' | '~'
69
+
70
+ /**
71
+ * A unary expression with a left or right handed operator.
72
+ */
73
+ export interface UnaryExpression extends Node {
74
+ type: 'UnaryExpression'
75
+ operator: UnaryOperator
76
+ prefix: boolean
77
+ argument: Expression
39
78
  }
40
79
 
41
- export class UnaryExpression {
42
- constructor(public operator: string, public left: AST | null, public right: AST | null) {}
80
+ /**
81
+ * An update expression with an optionally prefixed operator.
82
+ */
83
+ export interface UpdateExpression extends Node {
84
+ type: 'UpdateExpression'
85
+ operator: UpdateOperator
86
+ argument: Expression
87
+ prefix: boolean
43
88
  }
44
89
 
45
- export class BinaryExpression {
46
- constructor(public operator: string, public left: AST, public right: AST) {}
90
+ export type UpdateOperator = '++' | '--'
91
+
92
+ /**
93
+ * A binary expression with a left and right operand.
94
+ */
95
+ export interface BinaryExpression extends Node {
96
+ type: 'BinaryExpression'
97
+ operator: BinaryOperator
98
+ left: Expression
99
+ right: Expression
47
100
  }
48
101
 
49
- export class TernaryExpression {
50
- constructor(public test: AST, public consequent: AST, public alternate: AST) {}
102
+ export type BinaryOperator =
103
+ | '=='
104
+ | '!='
105
+ | '<'
106
+ | '<='
107
+ | '>'
108
+ | '>='
109
+ | '<<'
110
+ | '>>'
111
+ | '+'
112
+ | '-'
113
+ | '*'
114
+ | '/'
115
+ | '%'
116
+ | '|'
117
+ | '^'
118
+ | '&'
119
+
120
+ /**
121
+ * An assignment expression.
122
+ */
123
+ export interface AssignmentExpression extends Node {
124
+ type: 'AssignmentExpression'
125
+ operator: AssignmentOperator
126
+ left: Expression
127
+ right: Expression
51
128
  }
52
129
 
53
- export class CallExpression {
54
- constructor(public callee: AST, public args: AST[]) {}
130
+ export type AssignmentOperator = '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '<<=' | '>>=' | '>>>=' | '|=' | '^=' | '&='
131
+
132
+ /**
133
+ * A logical operation between two expressions.
134
+ */
135
+ export interface LogicalExpression extends Node {
136
+ type: 'LogicalExpression'
137
+ operator: LogicalOperator
138
+ left: Expression
139
+ right: Expression
55
140
  }
56
141
 
57
- export class MemberExpression {
58
- constructor(public object: AST, public property: AST) {}
142
+ export type LogicalOperator = '||' | '&&' | '^^'
143
+
144
+ /**
145
+ * A member expression.
146
+ */
147
+ export interface MemberExpression extends Node {
148
+ type: 'MemberExpression'
149
+ object: Expression
150
+ property: Expression
151
+ computed: boolean
59
152
  }
60
153
 
61
- export class ArrayExpression {
62
- constructor(public type: Type, public members: AST[]) {}
154
+ /**
155
+ * A conditional expression or ternary.
156
+ */
157
+ export interface ConditionalExpression extends Node {
158
+ type: 'ConditionalExpression'
159
+ test: Expression
160
+ alternate: Expression
161
+ consequent: Expression
63
162
  }
64
163
 
65
- export class BlockStatement {
66
- constructor(public body: AST[]) {}
164
+ /**
165
+ * A function call expression or struct initialization.
166
+ */
167
+ export interface CallExpression extends Node {
168
+ type: 'CallExpression'
169
+ callee: Expression
170
+ arguments: Expression[]
67
171
  }
68
172
 
69
- export class IfStatement {
70
- constructor(public test: AST, public consequent: AST, public alternate: AST | null) {}
173
+ /**
174
+ * An expression as a standalone statement.
175
+ */
176
+ export interface ExpressionStatement extends Node {
177
+ type: 'ExpressionStatement'
178
+ expression: Expression
71
179
  }
72
180
 
73
- export class ForStatement {
74
- constructor(public init: AST | null, public test: AST | null, public update: AST | null, public body: AST) {}
181
+ /**
182
+ * A block statement.
183
+ */
184
+ export interface BlockStatement extends Node {
185
+ type: 'BlockStatement'
186
+ body: Statement[]
75
187
  }
76
188
 
77
- export class WhileStatement {
78
- constructor(public test: AST, public body: AST) {}
189
+ /**
190
+ * A return statement with an optional argument.
191
+ */
192
+ export interface ReturnStatement extends Node {
193
+ type: 'ReturnStatement'
194
+ argument: Expression | null
79
195
  }
80
196
 
81
- export class DoWhileStatement {
82
- constructor(public test: AST, public body: AST) {}
197
+ /**
198
+ * A break statement.
199
+ */
200
+ export interface BreakStatement extends Node {
201
+ type: 'BreakStatement'
83
202
  }
84
203
 
85
- export class SwitchCase {
86
- constructor(public test: AST | null, public consequent: AST[]) {}
204
+ /**
205
+ * A continue statement.
206
+ */
207
+ export interface ContinueStatement extends Node {
208
+ type: 'ContinueStatement'
87
209
  }
88
210
 
89
- export class SwitchStatement {
90
- constructor(public discriminant: AST, public cases: SwitchCase[]) {}
211
+ /**
212
+ * A discard statement in fragment shaders.
213
+ */
214
+ export interface DiscardStatement extends Node {
215
+ type: 'DiscardStatement'
91
216
  }
92
217
 
93
- export class ReturnStatement {
94
- constructor(public argument: Literal | Identifier | UnaryExpression | null) {}
218
+ /**
219
+ * An if-else statement.
220
+ */
221
+ export interface IfStatement extends Node {
222
+ type: 'IfStatement'
223
+ test: Expression
224
+ consequent: Statement
225
+ alternate: Statement | null
95
226
  }
96
227
 
97
- export class PreprocessorStatement {
98
- constructor(public name: string, public value: AST[] | null) {}
228
+ /**
229
+ * A switch statement.
230
+ */
231
+ export interface SwitchStatement extends Node {
232
+ type: 'SwitchStatement'
233
+ discriminant: Expression
234
+ cases: SwitchCase[]
99
235
  }
100
236
 
101
- export class PrecisionStatement {
102
- constructor(public precision: 'lowp' | 'mediump' | 'highp', public type: Type) {}
237
+ /**
238
+ * A switch-case statement. `test` is null for a `default` case.
239
+ */
240
+ export interface SwitchCase extends Node {
241
+ type: 'SwitchCase'
242
+ test: Expression | null
243
+ consequent: Statement[]
103
244
  }
104
245
 
105
- export class ContinueStatement {}
246
+ /**
247
+ * A while statement.
248
+ */
249
+ export interface WhileStatement extends Node {
250
+ type: 'WhileStatement'
251
+ test: Expression
252
+ body: Statement
253
+ }
106
254
 
107
- export class BreakStatement {}
255
+ /**
256
+ * A do-while statement.
257
+ */
258
+ export interface DoWhileStatement extends Node {
259
+ type: 'DoWhileStatement'
260
+ body: Statement
261
+ test: Expression
262
+ }
263
+
264
+ /**
265
+ * A for statement.
266
+ */
267
+ export interface ForStatement extends Node {
268
+ type: 'ForStatement'
269
+ init: VariableDeclaration | Expression | null
270
+ test: Expression | null
271
+ update: Expression | null
272
+ body: Statement
273
+ }
108
274
 
109
- export class DiscardStatement {}
275
+ export type ConstantQualifier = 'const'
276
+ export type ParameterQualifier = 'in' | 'out' | 'inout'
277
+ export type StorageQualifier = 'uniform' | 'in' | 'out'
110
278
 
111
- export type AST =
279
+ export type InterpolationQualifier = 'centroid' | 'smooth' | 'flat' | 'invariant'
280
+ export type LayoutQualifier = 'location' | 'std140' | 'packed' | 'shared'
281
+ export type PrecisionQualifier = 'highp' | 'mediump' | 'lowp'
282
+
283
+ /**
284
+ * A function declaration. `body` is null for overloads.
285
+ */
286
+ export interface FunctionDeclaration extends Node {
287
+ type: 'FunctionDeclaration'
288
+ id: Identifier
289
+ qualifiers: PrecisionQualifier[]
290
+ typeSpecifier: Identifier | ArraySpecifier
291
+ params: FunctionParameter[]
292
+ body: BlockStatement | null
293
+ }
294
+
295
+ /**
296
+ * A function parameter within a function declaration.
297
+ */
298
+ export interface FunctionParameter extends Node {
299
+ type: 'FunctionParameter'
300
+ id: Identifier
301
+ qualifiers: (ConstantQualifier | ParameterQualifier | PrecisionQualifier)[]
302
+ typeSpecifier: Identifier | ArraySpecifier
303
+ }
304
+
305
+ /**
306
+ * A variable declaration.
307
+ */
308
+ export interface VariableDeclaration extends Node {
309
+ type: 'VariableDeclaration'
310
+ declarations: VariableDeclarator[]
311
+ }
312
+
313
+ /**
314
+ * A variable declarator within a variable declaration.
315
+ */
316
+ export interface VariableDeclarator extends Node {
317
+ type: 'VariableDeclarator'
318
+ id: Identifier
319
+ qualifiers: (ConstantQualifier | InterpolationQualifier | StorageQualifier | PrecisionQualifier)[]
320
+ typeSpecifier: Identifier | ArraySpecifier
321
+ layout: Record<string, string | boolean> | null
322
+ init: Expression | null
323
+ }
324
+
325
+ /**
326
+ * A uniform declaration block with optional layout and qualifiers.
327
+ */
328
+ export interface UniformDeclarationBlock extends Node {
329
+ type: 'UniformDeclarationBlock'
330
+ id: Identifier | null
331
+ qualifiers: LayoutQualifier[]
332
+ typeSpecifier: Identifier | ArraySpecifier
333
+ layout: Record<string, string | boolean> | null
334
+ members: VariableDeclaration[]
335
+ }
336
+
337
+ /**
338
+ * A struct declaration. Can be used as a type or constructor.
339
+ */
340
+ export interface StructDeclaration extends Node {
341
+ type: 'StructDeclaration'
342
+ id: Identifier
343
+ members: VariableDeclaration[]
344
+ }
345
+
346
+ /**
347
+ * A GLSL preprocessor statement with an optional value.
348
+ */
349
+ export interface PreprocessorStatement extends Node {
350
+ type: 'PreprocessorStatement'
351
+ name: string
352
+ value: Expression[] | null
353
+ }
354
+
355
+ /**
356
+ * A GLSL precision statement.
357
+ */
358
+ export interface PrecisionStatement extends Node {
359
+ type: 'PrecisionStatement'
360
+ precision: PrecisionQualifier
361
+ typeSpecifier: Identifier
362
+ }
363
+
364
+ export type Expression =
112
365
  | Literal
113
366
  | Identifier
114
- | Type
115
- | VariableDeclarator
116
- | VariableDeclaration
117
- | StructDeclaration
118
- | FunctionDeclaration
367
+ | ArrayExpression
119
368
  | UnaryExpression
369
+ | UpdateExpression
120
370
  | BinaryExpression
121
- | TernaryExpression
122
- | CallExpression
371
+ | AssignmentExpression
372
+ | LogicalExpression
123
373
  | MemberExpression
124
- | ArrayExpression
374
+ | ConditionalExpression
375
+ | CallExpression
376
+
377
+ export type Statement =
378
+ | ExpressionStatement
125
379
  | BlockStatement
380
+ | ReturnStatement
381
+ | BreakStatement
382
+ | ContinueStatement
383
+ | DiscardStatement
126
384
  | IfStatement
127
- | ForStatement
385
+ | SwitchStatement
128
386
  | WhileStatement
129
387
  | DoWhileStatement
130
- | SwitchCase
131
- | SwitchStatement
132
- | ReturnStatement
388
+ | ForStatement
389
+ | FunctionDeclaration
390
+ | VariableDeclaration
391
+ | UniformDeclarationBlock
392
+ | StructDeclaration
133
393
  | PreprocessorStatement
134
394
  | PrecisionStatement
135
- | ContinueStatement
136
- | BreakStatement
137
- | DiscardStatement
395
+
396
+ export type AST =
397
+ | Program
398
+ | ArraySpecifier
399
+ | SwitchCase
400
+ | FunctionParameter
401
+ | VariableDeclarator
402
+ | Expression
403
+ | Statement
package/src/constants.ts CHANGED
@@ -99,7 +99,7 @@ export const WGSL_KEYWORDS = [
99
99
  'discard',
100
100
  'else',
101
101
  'enable',
102
- 'false',
102
+ // 'false',
103
103
  'fn',
104
104
  'for',
105
105
  'if',
@@ -109,7 +109,7 @@ export const WGSL_KEYWORDS = [
109
109
  'return',
110
110
  'struct',
111
111
  'switch',
112
- 'true',
112
+ // 'true',
113
113
  'var',
114
114
  'while',
115
115
 
@@ -501,8 +501,8 @@ export const GLSL_KEYWORDS = [
501
501
  'int',
502
502
  'void',
503
503
  'bool',
504
- 'true',
505
- 'false',
504
+ // 'true',
505
+ // 'false',
506
506
  'invariant',
507
507
  'discard',
508
508
  'return',