Shader: Add import statement (not doing anything for now)

This commit is contained in:
Jérôme Leclercq
2022-03-04 18:27:37 +01:00
parent ca83f363a3
commit b6cd85d6fe
23 changed files with 101 additions and 0 deletions

View File

@@ -215,6 +215,14 @@ namespace Nz::ShaderAst
return clone;
}
StatementPtr AstCloner::Clone(ImportStatement& node)
{
auto clone = std::make_unique<ImportStatement>();
clone->modulePath = node.modulePath;
return clone;
}
StatementPtr AstCloner::Clone(MultiStatement& node)
{
auto clone = std::make_unique<MultiStatement>();

View File

@@ -185,6 +185,11 @@ namespace Nz::ShaderAst
node.statement->Visit(*this);
}
void AstRecursiveVisitor::Visit(ImportStatement& /*node*/)
{
/* nothing to do */
}
void AstRecursiveVisitor::Visit(MultiStatement& node)
{
for (auto& statement : node.statements)

View File

@@ -301,6 +301,13 @@ namespace Nz::ShaderAst
Node(node.statement);
}
void AstSerializerBase::Serialize(ImportStatement& node)
{
Container(node.modulePath);
for (auto& path : node.modulePath)
Value(path);
}
void AstSerializerBase::Serialize(MultiStatement& node)
{
Container(node.statements);

View File

@@ -1308,6 +1308,11 @@ namespace Nz::ShaderAst
}
}
StatementPtr SanitizeVisitor::Clone(ImportStatement& node)
{
return static_unique_pointer_cast<ImportStatement>(AstCloner::Clone(node));
}
StatementPtr SanitizeVisitor::Clone(MultiStatement& node)
{
auto clone = std::make_unique<MultiStatement>();

View File

@@ -954,6 +954,20 @@ namespace Nz
ScopeVisit(*node.statement);
}
void LangWriter::Visit(ShaderAst::ImportStatement& node)
{
bool first = true;
for (const std::string& path : node.modulePath)
{
if (!first)
Append("/");
Append(path);
first = false;
}
}
void LangWriter::Visit(ShaderAst::IntrinsicExpression& node)
{
bool method = false;

View File

@@ -49,6 +49,7 @@ namespace Nz::ShaderLang
{ "fn", TokenType::FunctionDeclaration },
{ "for", TokenType::For },
{ "if", TokenType::If },
{ "import", TokenType::Import },
{ "in", TokenType::In },
{ "let", TokenType::Let },
{ "module", TokenType::Module },

View File

@@ -649,14 +649,23 @@ namespace Nz::ShaderLang
return { parameterName, std::move(parameterType) };
}
ShaderAst::StatementPtr Parser::ParseImportStatement()
{
Expect(Advance(), TokenType::Import);
std::vector<std::string> modulePath;
modulePath.push_back(ParseIdentifierAsName());
while (Peek().type == TokenType::Divide) //< /
{
Consume();
modulePath.push_back(ParseIdentifierAsName());
}
Expect(Advance(), TokenType::Semicolon);
return ShaderBuilder::Import(std::move(modulePath));
}
ShaderAst::StatementPtr Parser::ParseOptionDeclaration()
@@ -715,6 +724,12 @@ namespace Nz::ShaderLang
case TokenType::External:
return ParseExternalBlock(std::move(attributes));
case TokenType::Import:
if (!attributes.empty())
throw UnexpectedToken{};
return ParseImportStatement();
case TokenType::OpenSquareBracket:
assert(attributes.empty());
return ParseRootStatement(ParseAttributes());

View File

@@ -726,6 +726,11 @@ namespace Nz
PopResultId();
}
void SpirvAstVisitor::Visit(ShaderAst::ImportStatement& node)
{
/* nothing to do */
}
void SpirvAstVisitor::Visit(ShaderAst::IntrinsicExpression& node)
{
switch (node.intrinsic)