2025-07-18 00:23:23 -05:00
|
|
|
using Microsoft.CodeAnalysis.CSharp;
|
|
|
|
|
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|
|
|
|
using Microsoft.Iris.Asm;
|
|
|
|
|
using Microsoft.Iris.Markup;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2025-07-17 14:07:44 -05:00
|
|
|
using System;
|
|
|
|
|
using System.Linq.Expressions;
|
2025-07-17 00:46:05 -05:00
|
|
|
|
2025-07-18 00:23:23 -05:00
|
|
|
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
|
|
|
|
|
2025-07-17 00:46:05 -05:00
|
|
|
namespace Microsoft.Iris.DecompXml.Mock;
|
|
|
|
|
|
2025-07-18 00:23:23 -05:00
|
|
|
internal abstract class IrisExpression : Expression
|
2025-07-17 00:46:05 -05:00
|
|
|
{
|
2025-07-18 00:23:23 -05:00
|
|
|
public virtual string Decompile(DecompileContext context) => ToSyntax(context).ToString();
|
|
|
|
|
|
|
|
|
|
public abstract ExpressionSyntax ToSyntax(DecompileContext context);
|
2025-07-17 14:07:44 -05:00
|
|
|
|
2025-07-17 17:52:57 -05:00
|
|
|
public static Expression Wrap(object p)
|
2025-07-17 14:07:44 -05:00
|
|
|
{
|
|
|
|
|
return p switch
|
|
|
|
|
{
|
|
|
|
|
null => Constant(null),
|
|
|
|
|
Expression expr => expr,
|
|
|
|
|
Disassembler.RawConstantInfo constantInfo => new IrisConstantExpression(constantInfo.Value, constantInfo.Type),
|
2025-07-18 00:23:23 -05:00
|
|
|
SymbolReference symbolRef => Constant(symbolRef),
|
2025-07-17 14:07:44 -05:00
|
|
|
|
2025-07-17 17:52:57 -05:00
|
|
|
_ => throw new NotImplementedException($"Unable to wrap '{p}' in an expression")
|
2025-07-17 14:07:44 -05:00
|
|
|
};
|
|
|
|
|
}
|
2025-07-17 17:52:57 -05:00
|
|
|
|
|
|
|
|
public static string Decompile(Expression expr, DecompileContext context)
|
|
|
|
|
{
|
|
|
|
|
return expr is IrisExpression irisExpr
|
|
|
|
|
? irisExpr.Decompile(context)
|
|
|
|
|
: expr.ToString();
|
|
|
|
|
}
|
2025-07-18 00:23:23 -05:00
|
|
|
|
|
|
|
|
public static ExpressionSyntax ToSyntax(object obj, DecompileContext context)
|
|
|
|
|
{
|
|
|
|
|
return obj switch
|
|
|
|
|
{
|
|
|
|
|
null => LiteralExpression(SyntaxKind.NullLiteralExpression),
|
|
|
|
|
int intValue => LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(intValue)),
|
|
|
|
|
string strValue => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(strValue)),
|
|
|
|
|
IStringEncodable strEnc => ParseExpression(strEnc.EncodeString()),
|
|
|
|
|
|
|
|
|
|
Disassembler.RawConstantInfo constantInfo => new IrisConstantExpression(constantInfo.Value, constantInfo.Type).ToSyntax(context),
|
|
|
|
|
SymbolReference symbolRef => IdentifierName(symbolRef.Symbol),
|
|
|
|
|
TypeSchema typeSchema => IdentifierName(context.GetQualifiedName(typeSchema).ToString()),
|
|
|
|
|
|
|
|
|
|
IrisExpression irisExpr => irisExpr.ToSyntax(context),
|
|
|
|
|
Expression expr => ParseExpression(expr.ToString()),
|
|
|
|
|
ExpressionSyntax exprSyn => exprSyn,
|
|
|
|
|
|
|
|
|
|
_ => IdentifierName(obj.ToString())
|
|
|
|
|
};
|
|
|
|
|
}
|
2025-07-17 00:46:05 -05:00
|
|
|
}
|