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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023-2024 Cody Bennett
3
+ Copyright (c) 2023-2025 Cody Bennett
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -14,32 +14,53 @@ Tools and IntelliSense for GLSL and WGSL.
14
14
  - [Parse](#parse)
15
15
  - [Generate](#generate)
16
16
  - [AST](#ast)
17
- - [Literal](#literal)
17
+ - [Node Objects](#node-objects)
18
18
  - [Identifier](#identifier)
19
- - [Type](#type)
20
- - [VariableDeclaration](#variabledeclaration)
21
- - [VariableDeclarator](#variabledeclarator)
22
- - [StructDeclaration](#structdeclaration)
23
- - [FunctionDeclaration](#functiondeclaration)
24
- - [UnaryExpression](#unaryexpression)
25
- - [BinaryExpression](#binaryexpression)
26
- - [TernaryExpression](#ternaryexpression)
27
- - [CallExpression](#callexpression)
28
- - [MemberExpression](#memberexpression)
29
- - [ArrayExpression](#arrayexpression)
30
- - [BlockStatement](#blockstatement)
31
- - [IfStatement](#ifstatement)
32
- - [ForStatement](#forstatement)
33
- - [WhileStatement](#whilestatement)
34
- - [DoWhileStatement](#dowhilestatement)
35
- - [SwitchStatement](#switchstatement)
36
- - [SwitchCase](#switchcase)
37
- - [ReturnStatement](#returnstatement)
38
- - [PreprocessorStatement](#preprocessorstatement)
39
- - [PrecisionStatement](#precisionstatement)
40
- - [ContinueStatement](#continuestatement)
41
- - [BreakStatement](#breakstatement)
42
- - [DiscardStatement](#discardstatement)
19
+ - [Literal](#literal)
20
+ - [ArraySpecifier](#arrayspecifier)
21
+ - [Program](#program)
22
+ - Statements
23
+ - [ExpressionStatement](#expressionstatement)
24
+ - [BlockStatement](#blockstatement)
25
+ - [DiscardStatement](#discardstatement)
26
+ - [PreprocessorStatement](#preprocessorstatement)
27
+ - [PrecisionStatement](#precisionstatement)
28
+ - Control Flow
29
+ - [ReturnStatement](#returnstatement)
30
+ - [BreakStatement](#breakstatement)
31
+ - [ContinueStatement](#continuestatement)
32
+ - Choice
33
+ - [IfStatement](#ifstatement)
34
+ - [SwitchStatement](#switchstatement)
35
+ - [SwitchCase](#switchcase)
36
+ - Loops
37
+ - [WhileStatement](#whilestatement)
38
+ - [DoWhileStatement](#dowhilestatement)
39
+ - [ForStatement](#forstatement)
40
+ - Declarations
41
+ - [FunctionDeclaration](#functiondeclaration)
42
+ - [FunctionParameter](#functionparameter)
43
+ - [VariableDeclaration](#variabledeclaration)
44
+ - [VariableDeclarator](#variabledeclarator)
45
+ - [UniformDeclarationBlock](#uniformdeclarationblock)
46
+ - [StructDeclaration](#structdeclaration)
47
+ - Expressions
48
+ - [ArrayExpression](#arrayexpression)
49
+ - Unary operations
50
+ - [UnaryExpression](#unaryexpression)
51
+ - [UnaryOperator](#unaryoperator)
52
+ - [UpdateExpression](#updateexpression)
53
+ - [UpdateOperator](#updateoperator)
54
+ - Binary Operations
55
+ - [BinaryExpression](#binaryexpression)
56
+ - [BinaryOperator](#binaryoperator)
57
+ - [AssignmentExpression](#assignmentexpression)
58
+ - [AssignmentOperator](#assignmentoperator)
59
+ - [LogicalExpression](#logicalexpression)
60
+ - [LogicalOperator](#logicaloperator)
61
+ - [MemberExpression](#memberexpression)
62
+ - [ConditionalExpression](#conditionalexpression)
63
+ - [CallExpression](#callexpression)
43
64
 
44
65
  ## Installation
45
66
 
@@ -231,7 +252,7 @@ minify(`#version 300 es\nin vec2 c;out vec4 data[gl_MaxDrawBuffers];void main(){
231
252
  Parses a string of GLSL (WGSL is WIP) code into an [AST](#ast).
232
253
 
233
254
  ```ts
234
- const ast: AST[] = parse(code: string)
255
+ const ast: Program = parse(code: string)
235
256
  ```
236
257
 
237
258
  ## Generate
@@ -239,7 +260,7 @@ const ast: AST[] = parse(code: string)
239
260
  Generates a string of GLSL (WGSL is WIP) code from an [AST](#ast).
240
261
 
241
262
  ```ts
242
- const code: string = generate(ast: AST[], {
263
+ const code: string = generate(program: Program, {
243
264
  target: 'GLSL' // | 'WGSL'
244
265
  })
245
266
  ```
@@ -248,188 +269,184 @@ const code: string = generate(ast: AST[], {
248
269
 
249
270
  An [Abstract Syntax Tree](https://en.wikipedia.org/wiki/Abstract_syntax_tree) loosely based on [ESTree](https://github.com/estree/estree) for GLSL and WGSL grammars.
250
271
 
251
- ### Literal
272
+ ### Node Objects
252
273
 
253
- A shader literal representing a `bool`, `float`, `int`, or `uint` type.
274
+ AST nodes extend `Node` objects which implement the following abstract interface:
254
275
 
255
276
  ```ts
256
- class Literal {
257
- value: string
277
+ interface Node {
278
+ type: string
258
279
  }
259
280
  ```
260
281
 
282
+ The `type` field is a string representing the AST variant type which can determine the interface a node implements.
283
+
261
284
  ### Identifier
262
285
 
263
286
  A variable identifier.
264
287
 
265
288
  ```ts
266
- class Identifier {
267
- value: string
289
+ interface Identifier extends Node {
290
+ type: 'Identifier'
291
+ name: string
268
292
  }
269
293
  ```
270
294
 
271
- ### Type
295
+ ### Literal
272
296
 
273
- Represents a type specifier and its parameters (WGSL specific).
297
+ A shader literal representing a `bool`, `float`, `int`, or `uint` type.
274
298
 
275
299
  ```ts
276
- class Type {
277
- name: string
278
- parameters: (Type | Literal | Identifier)[] | null
300
+ interface Literal extends Node {
301
+ type: 'Literal'
302
+ value: string
279
303
  }
280
304
  ```
281
305
 
282
- ### VariableDeclaration
306
+ ### ArraySpecifier
283
307
 
284
- A variable declaration with an optional binding layout, type qualifiers, kind (WGSL only), and declarators (e.g. a comma-separated list).
308
+ An array and its dimensions.
285
309
 
286
310
  ```ts
287
- class VariableDeclaration {
288
- layout: Record<string, string | boolean> | null
289
- qualifiers: string[]
290
- kind: 'var' | 'let' | 'const' | null
291
- type: Type | Identifier
292
- declarations: VariableDeclarator[]
311
+ interface ArraySpecifier extends Node {
312
+ type: 'ArraySpecifier'
313
+ typeSpecifier: Identifier
314
+ dimensions: (Literal | Identifier | null)[]
293
315
  }
294
316
  ```
295
317
 
296
- #### VariableDeclarator
318
+ ### Program
297
319
 
298
- A single named declarator as part of a `VariableDeclaration`.
320
+ Represents the root of an AST.
299
321
 
300
322
  ```ts
301
- class VariableDeclarator {
302
- name: string
303
- value: AST | null
323
+ interface Program extends Node {
324
+ type: 'Program'
325
+ body: Statement[]
304
326
  }
305
327
  ```
306
328
 
307
- ### StructDeclaration
329
+ ### ExpressionStatement
308
330
 
309
- A struct declaration. Can be used as a type or constructor.
331
+ An expression as a standalone statement.
310
332
 
311
333
  ```ts
312
- class StructDeclaration {
313
- name: string
314
- members: VariableDeclaration[]
334
+ interface ExpressionStatement extends Node {
335
+ type: 'ExpressionStatement'
336
+ expression: Expression
315
337
  }
316
338
  ```
317
339
 
318
- ### FunctionDeclaration
340
+ ### BlockStatement
319
341
 
320
- A function declaration with an optional type qualifier and arguments.
342
+ A block statement.
321
343
 
322
344
  ```ts
323
- class FunctionDeclaration {
324
- name: string
325
- type: Type | Identifier
326
- qualifiers: string[]
327
- args: VariableDeclaration[]
328
- body: BlockStatement | null
345
+ interface BlockStatement extends Node {
346
+ type: 'BlockStatement'
347
+ body: Statement[]
329
348
  }
330
349
  ```
331
350
 
332
- ### UnaryExpression
351
+ ### DiscardStatement
333
352
 
334
- A unary expression with a left or right handed operator.
353
+ A discard statement in fragment shaders.
335
354
 
336
355
  ```ts
337
- class UnaryExpression {
338
- operator: string
339
- left: AST | null
340
- right: AST | null
356
+ interface DiscardStatement extends Node {
357
+ type: 'DiscardStatement'
341
358
  }
342
359
  ```
343
360
 
344
- ### BinaryExpression
361
+ ### PreprocessorStatement
345
362
 
346
- A binary expression with a left and right operand.
363
+ A GLSL preprocessor statement with an optional value.
347
364
 
348
365
  ```ts
349
- class BinaryExpression {
350
- operator: string
351
- left: AST
352
- right: AST
366
+ interface PreprocessorStatement extends Node {
367
+ type: 'PreprocessorStatement'
368
+ name: string
369
+ value: Expression[] | null
353
370
  }
354
371
  ```
355
372
 
356
- ### TernaryExpression
373
+ ### PrecisionStatement
357
374
 
358
- A ternary or conditional expression.
375
+ A GLSL precision statement.
359
376
 
360
377
  ```ts
361
- class TernaryExpression {
362
- test: AST
363
- consequent: AST
364
- alternate: AST
378
+ interface PrecisionStatement extends Node {
379
+ type: 'PrecisionStatement'
380
+ precision: PrecisionQualifier
381
+ typeSpecifier: Identifier
365
382
  }
366
383
  ```
367
384
 
368
- ### CallExpression
385
+ ### ReturnStatement
369
386
 
370
- A call expression.
387
+ A return statement with an optional argument.
371
388
 
372
389
  ```ts
373
- class CallExpression {
374
- callee: AST
375
- args: AST[]
390
+ interface ReturnStatement extends Node {
391
+ type: 'ReturnStatement'
392
+ argument: Expression | null
376
393
  }
377
394
  ```
378
395
 
379
- ### MemberExpression
396
+ ### BreakStatement
380
397
 
381
- A member expression.
398
+ A break statement.
382
399
 
383
400
  ```ts
384
- class MemberExpression {
385
- object: AST
386
- property: AST
401
+ interface BreakStatement extends Node {
402
+ type: 'BreakStatement'
387
403
  }
388
404
  ```
389
405
 
390
- ### ArrayExpression
406
+ ### ContinueStatement
391
407
 
392
- An array expression. `members` can be empty if uninitialized.
408
+ A continue statement.
393
409
 
394
410
  ```ts
395
- class ArrayExpression {
396
- type: Type
397
- members: AST[]
411
+ interface ContinueStatement extends Node {
412
+ type: 'ContinueStatement'
398
413
  }
399
414
  ```
400
415
 
401
- ### BlockStatement
416
+ ### IfStatement
402
417
 
403
- A block statement.
418
+ An if-else statement.
404
419
 
405
420
  ```ts
406
- class BlockStatement {
407
- body: AST[]
421
+ interface IfStatement extends Node {
422
+ type: 'IfStatement'
423
+ test: Expression
424
+ consequent: Statement
425
+ alternate: Statement | null
408
426
  }
409
427
  ```
410
428
 
411
- ### IfStatement
429
+ ### SwitchStatement
412
430
 
413
- An if statement.
431
+ A switch statement.
414
432
 
415
433
  ```ts
416
- class IfStatement {
417
- test: AST
418
- consequent: AST
419
- alternate: AST | null
434
+ interface SwitchStatement extends Node {
435
+ type: 'SwitchStatement'
436
+ discriminant: Expression
437
+ cases: SwitchCase[]
420
438
  }
421
439
  ```
422
440
 
423
- ### ForStatement
441
+ #### SwitchCase
424
442
 
425
- A for statement.
443
+ A switch-case statement. `test` is null for a `default` case.
426
444
 
427
445
  ```ts
428
- class ForStatement {
429
- init: AST | null
430
- test: AST | null
431
- update: AST | null
432
- body: AST
446
+ interface SwitchCase extends Node {
447
+ type: 'SwitchCase'
448
+ test: Expression | null
449
+ consequent: Statement[]
433
450
  }
434
451
  ```
435
452
 
@@ -438,9 +455,10 @@ class ForStatement {
438
455
  A while statement.
439
456
 
440
457
  ```ts
441
- class WhileStatement {
442
- test: AST
443
- body: AST
458
+ interface WhileStatement extends Node {
459
+ type: 'WhileStatement'
460
+ test: Expression
461
+ body: Statement
444
462
  }
445
463
  ```
446
464
 
@@ -449,86 +467,265 @@ class WhileStatement {
449
467
  A do-while statement.
450
468
 
451
469
  ```ts
452
- class DoWhileStatement {
453
- test: AST
454
- body: AST
470
+ interface DoWhileStatement extends Node {
471
+ type: 'DoWhileStatement'
472
+ body: Statement
473
+ test: Expression
455
474
  }
456
475
  ```
457
476
 
458
- ### SwitchStatement
477
+ ### ForStatement
459
478
 
460
- A switch statement.
479
+ A for statement.
461
480
 
462
481
  ```ts
463
- class SwitchStatement {
464
- discriminant: AST
465
- cases: SwitchCase[]
482
+ interface ForStatement extends Node {
483
+ type: 'ForStatement'
484
+ init: VariableDeclaration | Expression | null
485
+ test: Expression | null
486
+ update: Expression | null
487
+ body: Statement
466
488
  }
467
489
  ```
468
490
 
469
- #### SwitchCase
491
+ ### FunctionDeclaration
470
492
 
471
- A switch-case statement. `test` is null for a `default` case.
493
+ A function declaration. `body` is null for overloads.
472
494
 
473
495
  ```ts
474
- class SwitchCase {
475
- test: AST | null
476
- consequent: AST[]
496
+ interface FunctionDeclaration extends Node {
497
+ type: 'FunctionDeclaration'
498
+ id: Identifier
499
+ qualifiers: PrecisionQualifier[]
500
+ typeSpecifier: Identifier | ArraySpecifier
501
+ params: FunctionParameter[]
502
+ body: BlockStatement | null
477
503
  }
478
504
  ```
479
505
 
480
- ### ReturnStatement
506
+ #### FunctionParameter
481
507
 
482
- A return statement with an optional argument.
508
+ A function parameter within a function declaration.
483
509
 
484
510
  ```ts
485
- class ReturnStatement {
486
- argument: Literal | Identifier | UnaryExpression | null
511
+ interface FunctionParameter extends Node {
512
+ type: 'FunctionParameter'
513
+ id: Identifier
514
+ qualifiers: (ConstantQualifier | ParameterQualifier | PrecisionQualifier)[]
515
+ typeSpecifier: Identifier | ArraySpecifier
487
516
  }
488
517
  ```
489
518
 
490
- ### PreprocessorStatement
519
+ ### VariableDeclaration
491
520
 
492
- A GLSL preprocessor statement with an optional value.
521
+ A variable declaration.
493
522
 
494
523
  ```ts
495
- class PreprocessorStatement {
496
- name: string
497
- value: AST[] | null
524
+ interface VariableDeclaration extends Node {
525
+ type: 'VariableDeclaration'
526
+ declarations: VariableDeclarator[]
498
527
  }
499
528
  ```
500
529
 
501
- ### PrecisionStatement
530
+ #### VariableDeclarator
502
531
 
503
- A GLSL precision statement.
532
+ A variable declarator within a variable declaration.
504
533
 
505
534
  ```ts
506
- class PrecisionStatement {
507
- precision: 'lowp' | 'mediump' | 'highp'
508
- type: Type
535
+ interface VariableDeclarator extends Node {
536
+ type: 'VariableDeclarator'
537
+ id: Identifier
538
+ qualifiers: (ConstantQualifier | InterpolationQualifier | StorageQualifier | PrecisionQualifier)[]
539
+ typeSpecifier: Identifier | ArraySpecifier
540
+ layout: Record<string, string | boolean> | null
541
+ init: Expression | null
509
542
  }
510
543
  ```
511
544
 
512
- ### ContinueStatement
545
+ ### UniformDeclarationBlock
513
546
 
514
- A continue statement.
547
+ A uniform declaration block with optional layout and qualifiers.
515
548
 
516
549
  ```ts
517
- class ContinueStatement {}
550
+ interface UniformDeclarationBlock extends Node {
551
+ type: 'UniformDeclarationBlock'
552
+ id: Identifier | null
553
+ qualifiers: LayoutQualifier[]
554
+ typeSpecifier: Identifier | ArraySpecifier
555
+ layout: Record<string, string | boolean> | null
556
+ members: VariableDeclaration[]
557
+ }
518
558
  ```
519
559
 
520
- ### BreakStatement
560
+ ### StructDeclaration
521
561
 
522
- A break statement.
562
+ A struct declaration. Can be used as a type or constructor.
523
563
 
524
564
  ```ts
525
- class BreakStatement {}
565
+ interface StructDeclaration extends Node {
566
+ type: 'StructDeclaration'
567
+ id: Identifier
568
+ members: VariableDeclaration[]
569
+ }
526
570
  ```
527
571
 
528
- ### DiscardStatement
572
+ ### ArrayExpression
573
+
574
+ An array initialization expression.
575
+
576
+ ```ts
577
+ interface ArrayExpression extends Node {
578
+ type: 'ArrayExpression'
579
+ typeSpecifier: ArraySpecifier
580
+ elements: Expression[]
581
+ }
582
+ ```
583
+
584
+ ### UnaryExpression
585
+
586
+ A unary expression with a left or right handed operator.
587
+
588
+ ```ts
589
+ interface UnaryExpression extends Node {
590
+ type: 'UnaryExpression'
591
+ operator: UnaryOperator
592
+ prefix: boolean
593
+ argument: Expression
594
+ }
595
+ ```
596
+
597
+ #### UnaryOperator
598
+
599
+ ```ts
600
+ type UnaryOperator = '-' | '+' | '!' | '~'
601
+ ```
602
+
603
+ ### UpdateExpression
604
+
605
+ An update expression with an optionally prefixed operator.
606
+
607
+ ```ts
608
+ interface UpdateExpression extends Node {
609
+ type: 'UpdateExpression'
610
+ operator: UpdateOperator
611
+ argument: Expression
612
+ prefix: boolean
613
+ }
614
+ ```
615
+
616
+ #### UpdateOperator
617
+
618
+ ```ts
619
+ type UpdateOperator = '++' | '--'
620
+ ```
621
+
622
+ ### BinaryExpression
623
+
624
+ A binary expression with a left and right operand.
625
+
626
+ ```ts
627
+ interface BinaryExpression extends Node {
628
+ type: 'BinaryExpression'
629
+ operator: BinaryOperator
630
+ left: Expression
631
+ right: Expression
632
+ }
633
+ ```
529
634
 
530
- A discard statement.
635
+ #### BinaryOperator
531
636
 
532
637
  ```ts
533
- class DiscardStatement {}
638
+ type BinaryOperator =
639
+ | '=='
640
+ | '!='
641
+ | '<'
642
+ | '<='
643
+ | '>'
644
+ | '>='
645
+ | '<<'
646
+ | '>>'
647
+ | '+'
648
+ | '-'
649
+ | '*'
650
+ | '/'
651
+ | '%'
652
+ | '|'
653
+ | '^'
654
+ | '&'
655
+ ```
656
+
657
+ ### AssignmentExpression
658
+
659
+ An assignment expression.
660
+
661
+ ```ts
662
+ interface AssignmentExpression extends Node {
663
+ type: 'AssignmentExpression'
664
+ operator: AssignmentOperator
665
+ left: Expression
666
+ right: Expression
667
+ }
668
+ ```
669
+
670
+ #### AssignmentOperator
671
+
672
+ ```ts
673
+ type AssignmentOperator = '=' | '+=' | '-=' | '*=' | '/=' | '%=' | '<<=' | '>>=' | '>>>=' | '|=' | '^=' | '&='
674
+ ```
675
+
676
+ ### LogicalExpression
677
+
678
+ A logical operation between two expressions.
679
+
680
+ ```ts
681
+ interface LogicalExpression extends Node {
682
+ type: 'LogicalExpression'
683
+ operator: LogicalOperator
684
+ left: Expression
685
+ right: Expression
686
+ }
687
+ ```
688
+
689
+ #### LogicalOperator
690
+
691
+ ```ts
692
+ type LogicalOperator = '||' | '&&' | '^^'
693
+ ```
694
+
695
+ ### MemberExpression
696
+
697
+ A member expression.
698
+
699
+ ```ts
700
+ interface MemberExpression extends Node {
701
+ type: 'MemberExpression'
702
+ object: Expression
703
+ property: Expression
704
+ computed: boolean
705
+ }
706
+ ```
707
+
708
+ ### ConditionalExpression
709
+
710
+ A conditional expression or ternary.
711
+
712
+ ```ts
713
+ interface ConditionalExpression extends Node {
714
+ type: 'ConditionalExpression'
715
+ test: Expression
716
+ alternate: Expression
717
+ consequent: Expression
718
+ }
719
+ ```
720
+
721
+ ### CallExpression
722
+
723
+ A function call expression or struct initialization.
724
+
725
+ ```ts
726
+ interface CallExpression extends Node {
727
+ type: 'CallExpression'
728
+ callee: Expression
729
+ arguments: Expression[]
730
+ }
534
731
  ```
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export * from '../src'
1
+ export * from '../src/index.ts'