Implement If blocks in scripts

This commit is contained in:
Yoshi Askharoun
2025-07-18 14:04:06 -05:00
parent 15b050a343
commit 044615088d
6 changed files with 199 additions and 24 deletions
+50
View File
@@ -0,0 +1,50 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
namespace Microsoft.Iris.DecompXml;
internal class CodeBlockInfo
{
public CodeBlockInfo(uint startOffset, uint endOffset, SyntaxKind kind, ExpressionSyntax expression = null)
{
StartOffset = startOffset;
EndOffset = endOffset;
Kind = kind;
Expression = expression;
}
public uint StartOffset { get; init; }
public uint EndOffset { get; init; }
public SyntaxKind Kind { get; init; }
public ExpressionSyntax Expression { get; init; }
public List<StatementSyntax> Statements { get; } = [];
public void FinalizeBlock(CodeBlockInfo parentBlock)
{
var blockBody = Block(Statements);
switch (Kind)
{
case SyntaxKind.IfStatement:
var ifStatement = IfStatement(Expression, blockBody);
parentBlock.Statements.Add(ifStatement);
break;
case SyntaxKind.ElseClause:
var elseClause = ElseClause(blockBody);
var ifElseBlock = (IfStatementSyntax)parentBlock.Statements[^1];
parentBlock.Statements[^1] = ifElseBlock.WithElse(elseClause);
break;
default:
throw new System.NotImplementedException($"Unrecognized code block kind '{Kind}'");
}
}
}