Shader: Add support for while loops
This commit is contained in:
@@ -193,6 +193,15 @@ namespace Nz::ShaderAst
|
||||
return clone;
|
||||
}
|
||||
|
||||
StatementPtr AstCloner::Clone(WhileStatement& node)
|
||||
{
|
||||
auto clone = std::make_unique<WhileStatement>();
|
||||
clone->condition = CloneExpression(node.condition);
|
||||
clone->body = CloneStatement(node.body);
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
ExpressionPtr AstCloner::Clone(AccessIdentifierExpression& node)
|
||||
{
|
||||
auto clone = std::make_unique<AccessIdentifierExpression>();
|
||||
|
||||
@@ -175,4 +175,13 @@ namespace Nz::ShaderAst
|
||||
if (node.returnExpr)
|
||||
node.returnExpr->Visit(*this);
|
||||
}
|
||||
|
||||
void AstRecursiveVisitor::Visit(WhileStatement& node)
|
||||
{
|
||||
if (node.condition)
|
||||
node.condition->Visit(*this);
|
||||
|
||||
if (node.body)
|
||||
node.body->Visit(*this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,6 +318,12 @@ namespace Nz::ShaderAst
|
||||
Node(node.returnExpr);
|
||||
}
|
||||
|
||||
void AstSerializerBase::Serialize(WhileStatement& node)
|
||||
{
|
||||
Node(node.condition);
|
||||
Node(node.body);
|
||||
}
|
||||
|
||||
void ShaderAstSerializer::Serialize(StatementPtr& shader)
|
||||
{
|
||||
m_stream << s_magicNumber << s_currentVersion;
|
||||
|
||||
@@ -460,12 +460,11 @@ namespace Nz::ShaderAst
|
||||
{
|
||||
case Identifier::Type::Constant:
|
||||
{
|
||||
// Replace IdentifierExpression by ConstantExpression
|
||||
auto constantExpr = std::make_unique<ConstantExpression>();
|
||||
constantExpr->cachedExpressionType = GetExpressionType(m_context->constantValues[identifier->index]);
|
||||
constantExpr->constantId = identifier->index;
|
||||
// Replace IdentifierExpression by Constant(Value)Expression
|
||||
ConstantExpression constantExpr;
|
||||
constantExpr.constantId = identifier->index;
|
||||
|
||||
return constantExpr;
|
||||
return Clone(constantExpr); //< Turn ConstantExpression into ConstantValueExpression
|
||||
}
|
||||
|
||||
case Identifier::Type::Variable:
|
||||
@@ -951,6 +950,19 @@ namespace Nz::ShaderAst
|
||||
return clone;
|
||||
}
|
||||
|
||||
StatementPtr SanitizeVisitor::Clone(WhileStatement& node)
|
||||
{
|
||||
MandatoryExpr(node.condition);
|
||||
MandatoryStatement(node.body);
|
||||
|
||||
auto clone = static_unique_pointer_cast<WhileStatement>(AstCloner::Clone(node));
|
||||
|
||||
if (GetExpressionType(*clone->condition) != ExpressionType{ PrimitiveType::Boolean })
|
||||
throw AstError{ "expected a boolean value" };
|
||||
|
||||
return clone;
|
||||
}
|
||||
|
||||
auto SanitizeVisitor::FindIdentifier(const std::string_view& identifierName) const -> const Identifier*
|
||||
{
|
||||
auto it = std::find_if(m_context->identifiersInScope.rbegin(), m_context->identifiersInScope.rend(), [&](const Identifier& identifier) { return identifier.name == identifierName; });
|
||||
|
||||
Reference in New Issue
Block a user