2025-06-30 21:49:33 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Convention.Symbolization.Internal
|
|
|
|
|
{
|
2025-07-02 13:11:26 +08:00
|
|
|
|
public sealed class Structure : Variable<Structure>, ICanFindVariable
|
2025-06-30 21:49:33 +08:00
|
|
|
|
{
|
2025-07-02 13:11:26 +08:00
|
|
|
|
private Dictionary<string, Variable> MemberFields;
|
|
|
|
|
public readonly Namespace ParentNamespace;
|
2025-06-30 21:49:33 +08:00
|
|
|
|
|
2025-07-02 13:11:26 +08:00
|
|
|
|
public Dictionary<string, Variable> CloneMemberFields()
|
2025-06-30 21:49:33 +08:00
|
|
|
|
{
|
2025-07-02 13:11:26 +08:00
|
|
|
|
return new(from pair in MemberFields
|
|
|
|
|
select new KeyValuePair<string, Variable>(
|
|
|
|
|
pair.Key,
|
|
|
|
|
pair.Value is ICloneable cloneable ? (cloneable.Clone() as Variable) : pair.Value)
|
|
|
|
|
);
|
2025-06-30 21:49:33 +08:00
|
|
|
|
}
|
2025-07-02 13:11:26 +08:00
|
|
|
|
|
|
|
|
|
private Structure(string symbolName, Namespace parentNamespace) : base(symbolName)
|
|
|
|
|
{
|
|
|
|
|
this.ParentNamespace = parentNamespace;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Structure Create(string structureName, Namespace parent)
|
|
|
|
|
{
|
|
|
|
|
Structure result = new(structureName, parent);
|
|
|
|
|
parent.AddStructure(result);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(Structure other)
|
2025-06-30 21:49:33 +08:00
|
|
|
|
{
|
2025-07-02 13:11:26 +08:00
|
|
|
|
return this == other;
|
2025-06-30 21:49:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 13:11:26 +08:00
|
|
|
|
public Variable[] Find(string symbolName)
|
2025-06-30 21:49:33 +08:00
|
|
|
|
{
|
2025-07-02 13:11:26 +08:00
|
|
|
|
if (MemberFields.TryGetValue(symbolName, out var variable))
|
2025-06-30 21:49:33 +08:00
|
|
|
|
{
|
2025-07-02 13:11:26 +08:00
|
|
|
|
return new[] { variable };
|
2025-06-30 21:49:33 +08:00
|
|
|
|
}
|
2025-07-02 13:11:26 +08:00
|
|
|
|
return Array.Empty<Variable>();
|
2025-06-30 21:49:33 +08:00
|
|
|
|
}
|
2025-07-02 13:11:26 +08:00
|
|
|
|
}
|
2025-07-01 20:34:09 +08:00
|
|
|
|
|
2025-07-02 13:11:26 +08:00
|
|
|
|
public sealed class StructureInstance : CloneableVariable<StructureInstance>,ICanFindVariable
|
|
|
|
|
{
|
|
|
|
|
public readonly Structure StructureType;
|
|
|
|
|
private readonly Dictionary<string, Variable> MemberFields;
|
|
|
|
|
public StructureInstance(string symbolName, Structure structureType)
|
|
|
|
|
: base(symbolName)
|
2025-07-01 20:34:09 +08:00
|
|
|
|
{
|
2025-07-02 13:11:26 +08:00
|
|
|
|
this.StructureType = structureType;
|
|
|
|
|
this.MemberFields = structureType.CloneMemberFields();
|
|
|
|
|
}
|
|
|
|
|
public override bool Equals(StructureInstance other)
|
|
|
|
|
{
|
|
|
|
|
return this == other;
|
|
|
|
|
}
|
|
|
|
|
public override StructureInstance CloneVariable(string targetSymbolName)
|
|
|
|
|
{
|
|
|
|
|
return new StructureInstance(targetSymbolName, this.StructureType);
|
2025-07-01 20:34:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 13:11:26 +08:00
|
|
|
|
public Variable GetField(string memberName)
|
2025-07-01 20:34:09 +08:00
|
|
|
|
{
|
2025-07-02 13:11:26 +08:00
|
|
|
|
if (MemberFields.TryGetValue(memberName, out var result))
|
|
|
|
|
return result;
|
|
|
|
|
else
|
|
|
|
|
throw new KeyNotFoundException($"Member '{memberName}' not found in structure '{StructureType.SymbolInfo.SymbolName}'.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Variable[] Find(string symbolName)
|
|
|
|
|
{
|
|
|
|
|
if (MemberFields.TryGetValue(symbolName, out var variable))
|
|
|
|
|
{
|
|
|
|
|
return new[] { variable };
|
|
|
|
|
}
|
|
|
|
|
return Array.Empty<Variable>();
|
2025-07-01 20:34:09 +08:00
|
|
|
|
}
|
2025-06-30 21:49:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-07-02 13:11:26 +08:00
|
|
|
|
|