mirror of
https://github.com/ZuneDev/ZuneUIXTools.git
synced 2026-07-27 13:11:59 -07:00
[WIP] Support for basic foreach loops
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
@@ -8,21 +7,18 @@ namespace Microsoft.Iris.DecompXml;
|
||||
|
||||
internal record CodeBlockInfo
|
||||
{
|
||||
public CodeBlockInfo(uint startOffset, uint endOffset, SyntaxKind kind, ExpressionSyntax expression = null)
|
||||
public CodeBlockInfo(uint startOffset, uint endOffset, ICodeBlockAdditionalInfo additionalInfo = null)
|
||||
{
|
||||
StartOffset = startOffset;
|
||||
EndOffset = endOffset;
|
||||
Kind = kind;
|
||||
Expression = expression;
|
||||
AdditionalInfo = additionalInfo;
|
||||
}
|
||||
|
||||
public uint StartOffset { get; init; }
|
||||
|
||||
public uint EndOffset { get; init; }
|
||||
|
||||
public SyntaxKind Kind { get; init; }
|
||||
|
||||
public ExpressionSyntax Expression { get; init; }
|
||||
public ICodeBlockAdditionalInfo AdditionalInfo { get; init; }
|
||||
|
||||
public List<StatementSyntax> Statements { get; } = [];
|
||||
|
||||
@@ -30,21 +26,45 @@ internal record CodeBlockInfo
|
||||
{
|
||||
var blockBody = Block(Statements);
|
||||
|
||||
switch (Kind)
|
||||
switch (AdditionalInfo)
|
||||
{
|
||||
case SyntaxKind.IfStatement:
|
||||
var ifStatement = IfStatement(Expression, blockBody);
|
||||
case IfBlockInfo ifBlockInfo:
|
||||
var ifStatement = IfStatement(ifBlockInfo.Condition, blockBody);
|
||||
parentBlock.Statements.Add(ifStatement);
|
||||
break;
|
||||
|
||||
case SyntaxKind.ElseClause:
|
||||
case ElseBlockInfo _:
|
||||
var elseClause = ElseClause(blockBody);
|
||||
var ifElseBlock = (IfStatementSyntax)parentBlock.Statements[^1];
|
||||
parentBlock.Statements[^1] = ifElseBlock.WithElse(elseClause);
|
||||
break;
|
||||
|
||||
case ForEachBlockInfo forEachBlockInfo:
|
||||
var foreachStatement = ForEachStatement(forEachBlockInfo.Type, forEachBlockInfo.Identifier,
|
||||
forEachBlockInfo.Source, blockBody);
|
||||
parentBlock.Statements.Add(foreachStatement);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new System.NotImplementedException($"Unrecognized code block kind '{Kind}'");
|
||||
throw new System.NotImplementedException($"Unrecognized code block kind '{AdditionalInfo.GetType().Name}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal interface ICodeBlockAdditionalInfo;
|
||||
|
||||
internal class IfBlockInfo(ExpressionSyntax condition = null) : ICodeBlockAdditionalInfo
|
||||
{
|
||||
public ExpressionSyntax Condition { get; set; } = condition;
|
||||
}
|
||||
|
||||
internal class ElseBlockInfo : ICodeBlockAdditionalInfo;
|
||||
|
||||
internal class ForEachBlockInfo : ICodeBlockAdditionalInfo
|
||||
{
|
||||
public ExpressionSyntax Source { get; set; }
|
||||
|
||||
public string Identifier { get; set; }
|
||||
|
||||
public TypeSyntax Type { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user