Skip to content
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

Transpiler: Bidirectional associative #28710

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 46 additions & 4 deletions examples/jsm/transpiler/GLSLDecoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ const precedenceOperators = [
','
].reverse();

const associativityRightToLeft = [
'=',
'+=', '-=', '*=', '/=', '%=', '^=', '&=', '|=', '<<=', '>>=',
',',
'?',
':'
];

const spaceRegExp = /^((\t| )\n*)+/;
const lineRegExp = /^\n+/;
const commentRegExp = /^\/\*[\s\S]*?\*\//;
Expand Down Expand Up @@ -297,13 +305,13 @@ class GLSLDecoder {

for ( const operator of precedenceOperators ) {

for ( let i = 0; i < tokens.length; i ++ ) {
const parseToken = ( i, inverse = false ) => {

const token = tokens[ i ];

groupIndex += getGroupDelta( token.str );

if ( ! token.isOperator || i === 0 || i === tokens.length - 1 ) continue;
if ( ! token.isOperator || i === 0 || i === tokens.length - 1 ) return;

if ( groupIndex === 0 && token.str === operator ) {

Expand All @@ -330,9 +338,43 @@ class GLSLDecoder {

}

if ( groupIndex < 0 ) {
if ( inverse ) {

if ( groupIndex > 0 ) {

return this.parseExpressionFromTokens( tokens.slice( i ) );

}

} else {

if ( groupIndex < 0 ) {

return this.parseExpressionFromTokens( tokens.slice( 0, i ) );

}

}

};

if ( associativityRightToLeft.includes( operator ) ) {

for ( let i = 0; i < tokens.length; i ++ ) {

const result = parseToken( i );

if ( result ) return result;

}

} else {

for ( let i = tokens.length - 1; i >= 0; i -- ) {

const result = parseToken( i, true );

return this.parseExpressionFromTokens( tokens.slice( 0, i ) );
if ( result ) return result;

}

Expand Down