spoon icon indicating copy to clipboard operation
spoon copied to clipboard

WIP(general feedback appreciated): fix/refactor(control-flow): Switch rework and multi-node statement changes

Open Mr-Pine opened this issue 1 year ago • 0 comments

In the process of handling switch expressions and enhanced switch statements I ended up rewriting the whole switch code. Notable Changes to the results:

  • Switch expressions and enhanced switches are now properly supported and exhaustiveness for properly compiled java 21+ code is complete
  • Statements in the cases of a switch are no longer wrapped in separate blocks. Instead, the all expressions are now wrapped in one block and when appropriate a block case in an enhanced switch is of course also wrapped in a separate block.
  • default cases don't have a dummy statement any more.
  • Probably most important: New layout for statements that require multiple nodes: The construct starts with a node that is assigned the entire statement, then the subnodes follow and the construct is finished with a STATEMENT_END node that is tagged with the start node (See details below). I intend to adapt this to other multi-node constructs as well and some support for this is already implemented, but the rest is probably better placed in another PR. I think this change enables a more complete representation of multi-node constructs, especially of multi-node expressions like conditionals (currently conditionals in returns are not handled at all, for example) or maybe even nested method calls. The design of this was based on the way conditionals are handled when assigned to local variables. I added the end marker mainly as a jump point for skipping the entire statement and for easier support with return statements.

I'd appreciate some feedback for these changes, especially on the last point as it has a pretty big impact

image

for

public int switchTest(int a) {
	int b = 0;
	switch (a) {
		case 1:
			return 0;
		case 2:
			b = a * 2;
			break;
		case 3:
		case 4:
			b = a * 9;
			break;
		default:
			return 1;
	}
	return b;
}

Mr-Pine avatar Apr 06 '24 22:04 Mr-Pine