Skip to content

Commit

Permalink
Parse else branch
Browse files Browse the repository at this point in the history
  • Loading branch information
AndresTraks committed Jan 20, 2025
1 parent e709b35 commit 6a83bfc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Hlsl/FlowControl/IfStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public IfStatement(HlslTreeNode comparison, Closure closure)
public HlslTreeNode Comparison { get; }

public StatementSequence TrueBody { get; }
public Closure EndClosure { get; set; }
public StatementSequence FalseBody { get; set; }
public Closure TrueEndClosure { get; set; }
public Closure FalseEndClosure { get; set; }
}
}
16 changes: 14 additions & 2 deletions Hlsl/HlslAstWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,21 @@ private void WriteIfStatement(IfStatement ifStatement)
WriteLine($"if ({comparison}) {{");
indent += "\t";
WriteStatement(ifStatement.TrueBody);
WriteTempVariableAssignments(ifStatement.EndClosure);
WriteTempVariableAssignments(ifStatement.TrueEndClosure);
indent = indent.Substring(0, indent.Length - 1);
WriteLine("}");
if (ifStatement.FalseBody != null)
{
WriteLine("} else {");
indent += "\t";
WriteStatement(ifStatement.FalseBody);
WriteTempVariableAssignments(ifStatement.FalseEndClosure);
indent = indent.Substring(0, indent.Length - 1);
WriteLine("}");
}
else
{
WriteLine("}");
}
}

private void WriteReturnStatement(ReturnStatement returnStatement)
Expand Down
25 changes: 24 additions & 1 deletion Hlsl/InstructionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ private void ParseControlInstruction(D3D9Instruction instruction)
{
InsertIfStatement(instruction);
}
else if (instruction.Opcode == Opcode.Else)
{
SwitchToElseBranch();
}
else if (instruction.Opcode == Opcode.Endif)
{
EndIf(instruction);
Expand Down Expand Up @@ -392,6 +396,18 @@ private void InsertIfStatement(D3D9Instruction instruction)
_currentStatements.Push(ifStatement.TrueBody);
}

private void SwitchToElseBranch()
{
Closure closure = GetCurrentClosure();

_currentStatements.Pop();
IfStatement ifStatement = _currentStatements.Peek() as IfStatement;
_activeOutputs = ifStatement.Closure.Outputs;
ifStatement.TrueEndClosure = closure;
ifStatement.FalseBody = new StatementSequence(ifStatement.Closure);
_currentStatements.Push(ifStatement.FalseBody);
}

private void EndIf(D3D9Instruction instruction)
{
Closure closure = GetCurrentClosure();
Expand All @@ -401,7 +417,14 @@ private void EndIf(D3D9Instruction instruction)
while ((ifStatement = _currentStatements.Pop() as IfStatement) == null)
{
}
ifStatement.EndClosure = closure;
if (ifStatement.FalseBody != null)
{
ifStatement.FalseEndClosure = closure;
}
else
{
ifStatement.TrueEndClosure = closure;
}
}

private void InsertReturnStatement()
Expand Down

0 comments on commit 6a83bfc

Please sign in to comment.