Commit 07354241 by John Kessenich

HLSL: Grammar: Recognize { } style initializers for composites.

parent b0a63f57
float4 a1 = float4(1, 0.5, 0, 1), b1 = float4(2.0, 2.5, 2.1, 2.2); float4 a1 = float4(1, 0.5, 0, 1), b1 = float4(2.0, 2.5, 2.1, 2.2);
float4 a1i = {1, 0.5, 0, 1}, b1i = {2.0, 2.5, 2.1, 2.2};
float a2 = 0.2, b2; float a2 = 0.2, b2;
float a3, b3 = 0.3; float a3, b3 = 0.3;
float a4, b4 = 0.4, c4; float a4, b4 = 0.4, c4;
...@@ -7,6 +8,17 @@ float a5 = 0.5, b5, c5 = 1.5; ...@@ -7,6 +8,17 @@ float a5 = 0.5, b5, c5 = 1.5;
float4 ShaderFunction(float4 input) : COLOR0 float4 ShaderFunction(float4 input) : COLOR0
{ {
float4 a2 = float4(0.2, 0.3, 0.4, 0.5); float4 a2 = float4(0.2, 0.3, 0.4, 0.5);
struct S1 {
float f;
int i;
};
struct S2 {
int j;
float g;
S1 s1;
};
S2 s2i = { 9, a5, { (a3,a4), 12} }, s2 = S2(9, a5, S1((a3,a4), 12));
float a8 = (a2, b2), a9 = a5;
return input * a1; return input * a1;
} }
...@@ -1173,28 +1173,77 @@ bool HlslGrammar::acceptExpression(TIntermTyped*& node) ...@@ -1173,28 +1173,77 @@ bool HlslGrammar::acceptExpression(TIntermTyped*& node)
} while (true); } while (true);
} }
// initializer
// : LEFT_BRACE initializer_list RIGHT_BRACE
//
// initializer_list
// : assignment_expression COMMA assignment_expression COMMA ...
//
bool HlslGrammar::acceptInitializer(TIntermTyped*& node)
{
// LEFT_BRACE
if (! acceptTokenClass(EHTokLeftBrace))
return false;
// initializer_list
TSourceLoc loc = token.loc;
node = nullptr;
do {
// assignment_expression
TIntermTyped* expr;
if (! acceptAssignmentExpression(expr)) {
expected("assignment expression in initializer list");
return false;
}
node = intermediate.growAggregate(node, expr, loc);
// COMMA
if (acceptTokenClass(EHTokComma))
continue;
// RIGHT_BRACE
if (acceptTokenClass(EHTokRightBrace))
return true;
expected(", or }");
return false;
} while (true);
}
// Accept an assignment expression, where assignment operations // Accept an assignment expression, where assignment operations
// associate right-to-left. This is, it is implicit, for example // associate right-to-left. That is, it is implicit, for example
// //
// a op (b op (c op d)) // a op (b op (c op d))
// //
// assigment_expression // assigment_expression
// : binary_expression op binary_expression op binary_expression ... // : binary_expression op binary_expression op binary_expression ...
// | initializer
// //
bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node) bool HlslGrammar::acceptAssignmentExpression(TIntermTyped*& node)
{ {
// initializer
if (peekTokenClass(EHTokLeftBrace)) {
if (acceptInitializer(node))
return true;
expected("initializer");
return false;
}
// binary_expression
if (! acceptBinaryExpression(node, PlLogicalOr)) if (! acceptBinaryExpression(node, PlLogicalOr))
return false; return false;
// assignment operation?
TOperator assignOp = HlslOpMap::assignment(peek()); TOperator assignOp = HlslOpMap::assignment(peek());
if (assignOp == EOpNull) if (assignOp == EOpNull)
return true; return true;
// ... op // assignment op
TSourceLoc loc = token.loc; TSourceLoc loc = token.loc;
advanceToken(); advanceToken();
// ... binary_expression // binary_expression
// But, done by recursing this function, which automatically // But, done by recursing this function, which automatically
// gets the right-to-left associativity. // gets the right-to-left associativity.
TIntermTyped* rightNode = nullptr; TIntermTyped* rightNode = nullptr;
......
...@@ -73,6 +73,7 @@ namespace glslang { ...@@ -73,6 +73,7 @@ namespace glslang {
bool acceptFunctionDefinition(TFunction&, TIntermNode*&); bool acceptFunctionDefinition(TFunction&, TIntermNode*&);
bool acceptParenExpression(TIntermTyped*&); bool acceptParenExpression(TIntermTyped*&);
bool acceptExpression(TIntermTyped*&); bool acceptExpression(TIntermTyped*&);
bool acceptInitializer(TIntermTyped*&);
bool acceptAssignmentExpression(TIntermTyped*&); bool acceptAssignmentExpression(TIntermTyped*&);
bool acceptBinaryExpression(TIntermTyped*&, PrecedenceLevel); bool acceptBinaryExpression(TIntermTyped*&, PrecedenceLevel);
bool acceptUnaryExpression(TIntermTyped*&); bool acceptUnaryExpression(TIntermTyped*&);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment