graphitemaster / glsl-parser

A GLSL parser
MIT License
260 stars 30 forks source link

Parser ignores parenthisis #8

Closed bkaradzic closed 8 years ago

bkaradzic commented 8 years ago

Code:

float a, b, c;
float d = (a + b) * (b - c) * 3.0;

Becomes:

float a;
float b;
float c;
float d = a + b * b - c * 3.0;

I wouldn't mind if fixed AST becomes (each operation separate line / temporary):

float a;
float b;
float c;
float tmp0 = a + b;
float tmp1 = b - c;
float tmp2 = tmp0 * tmp1;
float d = tmp2 * 3.0;
graphitemaster commented 8 years ago

The parser doesn't ignore parentheses. It understands them and operator precedence. The AST represents the correct order of operations always. Otherwise the AST would be ambigious. The problem is the outputter which walks the AST forgets to add parens around binary expressions. I fixed this in 8f8ec27

graphitemaster commented 8 years ago

The technique you're describing where each operation gets its own variable like that is called SSA (Single Static Assignment) that is an optional pass that occurs after AST. Typically during code generation. glsl-parser doesn't concern itself with that ;) The word parser is in the name for a reason. I would like to do a compiler eventually targeting other output shader languages (HLSL, ESSL, and maybe SPIRV) but I doubt the effort is worth it.

bkaradzic commented 8 years ago

Yeah, I know what SSA is, it was my cunning plan attempt to word it in the way to get you to do it, otherwise I have to do it... :)