-
-
Notifications
You must be signed in to change notification settings - Fork 48
feat: Ternary operator #2069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aleksanderkatan
wants to merge
8
commits into
main
Choose a base branch
from
feat/ternary-operator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: Ternary operator #2069
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6712504
Add basic ternary operator functionality
f4d283f
Add more tests
3fa8574
Better error message
992fc8c
Add docs
9bc3bc4
Handle undefined in TGSL
3b5ed33
Add a test for undefined
e8d9a61
smol
f1ddb10
Optimize semicolons
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -160,6 +160,7 @@ function operatorToType< | |
|
|
||
| const unaryOpCodeToCodegen = { | ||
| '-': neg[$internal].gpuImpl, | ||
| 'void': () => snip('', wgsl.Void, 'constant'), | ||
| } satisfies Partial< | ||
| Record<tinyest.UnaryOperator, (...args: never[]) => unknown> | ||
| >; | ||
|
|
@@ -770,6 +771,21 @@ ${this.ctx.pre}}`; | |
| ); | ||
| } | ||
|
|
||
| if (expression[0] === NODE.conditionalExpr) { | ||
| // ternary operator | ||
| const [_, test, consequent, alternative] = expression; | ||
| const testExpression = this.expression(test); | ||
| if (isKnownAtComptime(testExpression)) { | ||
| return testExpression.value | ||
| ? this.expression(consequent) | ||
| : this.expression(alternative); | ||
| } else { | ||
| throw new Error( | ||
| `Ternary operator is only supported for comptime-known checks (used with '${testExpression.value}'). For runtime checks, please use 'std.select' or if/else statements.`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (expression[0] === NODE.stringLiteral) { | ||
| return snip(expression[1], UnknownData, /* origin */ 'constant'); | ||
| } | ||
|
|
@@ -791,9 +807,8 @@ ${this.ctx.pre}}`; | |
| statement: tinyest.Statement, | ||
| ): string { | ||
| if (typeof statement === 'string') { | ||
| return `${this.ctx.pre}${ | ||
| this.ctx.resolve(this.identifier(statement).value).value | ||
| };`; | ||
| const resolved = this.ctx.resolve(this.expression(statement).value).value; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does changing it from "identifier" to "expression" do? Was it intentional? |
||
| return resolved.length === 0 ? '' : `${this.ctx.pre}${resolved};`; | ||
| } | ||
|
|
||
| if (typeof statement === 'boolean') { | ||
|
|
@@ -1066,9 +1081,8 @@ ${this.ctx.pre}else ${alternate}`; | |
| return `${this.ctx.pre}break;`; | ||
| } | ||
|
|
||
| return `${this.ctx.pre}${ | ||
| this.ctx.resolve(this.expression(statement).value).value | ||
| };`; | ||
| const resolved = this.ctx.resolve(this.expression(statement).value).value; | ||
| return resolved.length === 0 ? '' : `${this.ctx.pre}${resolved};`; | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| import { describe, expect } from 'vitest'; | ||
| import { it } from '../utils/extendedIt.ts'; | ||
| import * as d from '../../src/data/index.ts'; | ||
| import * as std from '../../src/std/index.ts'; | ||
| import tgpu from '../../src/index.ts'; | ||
|
|
||
| describe('ternary operator', () => { | ||
| it('should resolve to one of the branches', () => { | ||
| const mySlot = tgpu.slot<boolean>(); | ||
| const myFn = tgpu.fn([], d.u32)(() => { | ||
| return mySlot.$ ? 10 : 20; | ||
| }); | ||
|
|
||
| expect( | ||
| tgpu.resolve([ | ||
| myFn.with(mySlot, true).$name('trueFn'), | ||
| myFn.with(mySlot, false).$name('falseFn'), | ||
| ]), | ||
| ) | ||
| .toMatchInlineSnapshot(` | ||
| "fn falseFn() -> u32 { | ||
| return 10u; | ||
| } | ||
|
|
||
| fn falseFn_1() -> u32 { | ||
| return 20u; | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should work for different comptime known expressions', () => { | ||
| const condition = true; | ||
| const comptime = tgpu['~unstable'].comptime(() => true); | ||
| const slot = tgpu.slot(true); | ||
| const derived = tgpu['~unstable'].derived(() => slot.$); | ||
|
|
||
| const myFn = tgpu.fn([])(() => { | ||
| // biome-ignore lint/correctness/noConstantCondition: it's a test | ||
| const a = true ? 1 : 0; | ||
| const b = std.allEq(d.vec2f(1, 2), d.vec2f(1, 2)) ? 1 : 0; | ||
| const c = condition ? 1 : 0; | ||
| const dd = comptime() ? 1 : 0; | ||
| const e = slot.$ ? 1 : 0; | ||
| const f = derived.$ ? 1 : 0; | ||
| }); | ||
|
|
||
| expect(tgpu.resolve([myFn])).toMatchInlineSnapshot(` | ||
| "fn myFn() { | ||
| const a = 1; | ||
| const b = 1; | ||
| const c = 1; | ||
| const dd = 1; | ||
| const e = 1; | ||
| const f = 1; | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should resolve nested operators', () => { | ||
| const mySlot = tgpu.slot<number>(0); | ||
| const myFn = tgpu.fn([], d.u32)(() => { | ||
| return mySlot.$ === 1 | ||
| ? 10 | ||
| : mySlot.$ === 2 | ||
| ? 20 | ||
| : mySlot.$ === 3 | ||
| ? 30 | ||
| : -1; | ||
| }); | ||
|
|
||
| expect( | ||
| tgpu.resolve([ | ||
| myFn, | ||
| myFn.with(mySlot, 1).$name('oneFn'), | ||
| myFn.with(mySlot, 2).$name('twoFn'), | ||
| myFn.with(mySlot, 3).$name('threeFn'), | ||
| ]), | ||
| ) | ||
| .toMatchInlineSnapshot(` | ||
| "fn threeFn() -> u32 { | ||
| return -1u; | ||
| } | ||
|
|
||
| fn threeFn_1() -> u32 { | ||
| return 10u; | ||
| } | ||
|
|
||
| fn threeFn_2() -> u32 { | ||
| return 20u; | ||
| } | ||
|
|
||
| fn threeFn_3() -> u32 { | ||
| return 30u; | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should not include unused dependencies', ({ root }) => { | ||
| const mySlot = tgpu.slot<boolean>(); | ||
| const myUniform = root.createUniform(d.u32); | ||
| const myReadonly = root.createReadonly(d.u32); | ||
|
|
||
| const myFn = tgpu.fn([], d.u32)(() => { | ||
| return mySlot.$ ? myUniform.$ : myReadonly.$; | ||
| }); | ||
|
|
||
| expect(tgpu.resolve([myFn.with(mySlot, true).$name('trueFn')])) | ||
| .toMatchInlineSnapshot(` | ||
| "@group(0) @binding(0) var<uniform> myUniform: u32; | ||
|
|
||
| fn trueFn() -> u32 { | ||
| return myUniform; | ||
| }" | ||
| `); | ||
|
|
||
| expect(tgpu.resolve([myFn.with(mySlot, false).$name('falseFn')])) | ||
| .toMatchInlineSnapshot(` | ||
| "@group(0) @binding(0) var<storage, read> myReadonly: u32; | ||
|
|
||
| fn falseFn() -> u32 { | ||
| return myReadonly; | ||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should handle undefined', ({ root }) => { | ||
| const counter = root.createMutable(d.u32); | ||
|
|
||
| const myFunction = tgpu.fn([])(() => { | ||
| // biome-ignore lint/correctness/noConstantCondition: it's a test | ||
| false ? counter.$++ : undefined; | ||
| }); | ||
| expect(tgpu.resolve([myFunction])).toMatchInlineSnapshot(` | ||
| "fn myFunction() { | ||
|
|
||
| }" | ||
| `); | ||
| }); | ||
|
|
||
| it('should throw when test is not comptime known', () => { | ||
| const myFn = tgpu.fn([d.u32], d.u32)((n) => { | ||
| return n > 0 ? n : -n; | ||
| }); | ||
|
|
||
| expect(() => tgpu.resolve([myFn])).toThrowErrorMatchingInlineSnapshot(` | ||
| [Error: Resolution of the following tree failed: | ||
| - <root> | ||
| - fn:myFn: Ternary operator is only supported for comptime-known checks (used with '(n > 0u)'). For runtime checks, please use 'std.select' or if/else statements.] | ||
| `); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should call it "conditional" if we want to follow the pattern