Skip to content

TSL Transpiler - Add Support for Switch Statements #31272

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

Merged
merged 11 commits into from
Jun 18, 2025

Conversation

cmhhelgeson
Copy link
Contributor

@cmhhelgeson cmhhelgeson commented Jun 15, 2025

Related issue: #30900, #30849

Description

Adds transpiler support for switches.

  • Generic Support
  • Default Statements
  • Extraneous edge cases which people would like to support
  • Add support for Break
  • Add support for Continue
  • Ensure new code works with comments and comment blocks

@cmhhelgeson
Copy link
Contributor Author

cmhhelgeson commented Jun 15, 2025

In lieu of having a custom block function for cases it might be better/possible to have case as one of the terminators in parseBlock/getGroupDelta. @sunag What do you think? I've moved to the suggested approach, as the previous approach was preventing users from creating new variables within the switch statement. parseBody() will also return the block delimiting token, which can help in cases where blocks need to be chained together.

@cmhhelgeson cmhhelgeson marked this pull request as ready for review June 15, 2025 20:24
@cmhhelgeson cmhhelgeson marked this pull request as draft June 15, 2025 20:51
@cmhhelgeson cmhhelgeson marked this pull request as ready for review June 15, 2025 23:18
@cmhhelgeson cmhhelgeson changed the title Transpiler - Add Support for Switch Statements GLSLDecoder/TSLTranspiler - Add Support for Switch Statements Jun 16, 2025
@cmhhelgeson cmhhelgeson changed the title GLSLDecoder/TSLTranspiler - Add Support for Switch Statements TSL Transpiler - Add Support for Switch Statements Jun 16, 2025
@sunag
Copy link
Collaborator

sunag commented Jun 17, 2025

I tested with this example but it doesn't seem to be working yet.

int switchTest(int x) {
	int result = 0;
	switch (x) {
		case 0:
			result = 10;
			break;
		case 1:
			result = 20;
			break;
		case 2:
			result = 30;
			break;
		/*default: // its break the transpiler
			result = -1;
			break;*/
	}
	return result;
}

Result

Switch(x).Case(int( 0 ), () => {

	result.assign( int( 10 ) );
	break;

} ).Default( () => {

	result.assign( int( 20 ) );
	break;

} ).Default( () => {

	result.assign( int( 30 ) );
	break;

} );

@cmhhelgeson
Copy link
Contributor Author

cmhhelgeson commented Jun 17, 2025

I tested with this example but it doesn't seem to be working yet.

I will look at it later today and convert to draft for now. I'm not sure how comments work but it seems like the Decoder just ignores them in most instances.

@cmhhelgeson cmhhelgeson marked this pull request as draft June 17, 2025 15:53
@cmhhelgeson
Copy link
Contributor Author

cmhhelgeson commented Jun 18, 2025

Output seems to be good now, seemingly just a readToken order of operations issue (parseSwitchCaseBlock was erroneously reading/skipping case token again after already being skipped).

image

@cmhhelgeson cmhhelgeson marked this pull request as ready for review June 18, 2025 04:43
@cmhhelgeson
Copy link
Contributor Author

cmhhelgeson commented Jun 18, 2025

Is this how we generally want to style cases for switch statements in future TSL examples? (code output from latest commit)

Switch( x )
	.Case( int( 0 ), () => {
		result.assign( int( 10 ) );
		Break();
	} ).Case( int( 1 ), () => {
		result.assign( int( 20 ) );
		Break();
	} ).Case( int( 2 ), () => {
		result.assign( int( 30 ) );
		Break();
	} );
return result;

Also, though I do appreciate getting these changes in as soon as possible, I would appreciate some communication about requested code style, indentation changes, or missing/expected features in a code review rather than seeing them in a commit later on before I can attempt to address the issues myself. I largely use these PRs as an opportunity to improve myself as a programmer, and I would like to be involved in finishing and fully understanding the projects I start. If there's anything I can do on my end in terms of improving communication or my own code style to align it more with the project, please let me know.

@sunag
Copy link
Collaborator

sunag commented Jun 18, 2025

Also, though I do appreciate getting these changes in as soon as possible, I would appreciate some communication about requested code style or indentation changes in a code review so that I can attempt to address the issues myself.

@cmhhelgeson Sure, we can do it like that in the next PR's and thanks for collaborating once again.

About the style, I wouldn't say it's definitive, it seems easier to read, we can continue like this for now.

@sunag sunag added this to the r178 milestone Jun 18, 2025
@sunag sunag merged commit 4ec743d into mrdoob:dev Jun 18, 2025
11 checks passed
@cmhhelgeson cmhhelgeson deleted the cmh/switch_transpiler branch June 18, 2025 16:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants