Files
Convention-CSharp/Convention/[Symbolization]/Detail/Structure.cs

88 lines
3.0 KiB
C#
Raw Normal View History

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
2025-07-05 16:57:59 +08:00
private Structure(string symbolName, int lineIndex,int wordIndex, Namespace parentNamespace) : base(symbolName, lineIndex, wordIndex)
2025-07-02 13:11:26 +08:00
{
this.ParentNamespace = parentNamespace;
}
2025-07-05 16:57:59 +08:00
public static Structure Create(VariableSymbol symbol, Namespace parent)
2025-07-02 13:11:26 +08:00
{
2025-07-05 16:57:59 +08:00
Structure result = new(symbol.SymbolName, symbol.LineIndex, symbol.WordIndex, parent);
2025-07-02 13:11:26 +08:00
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;
2025-07-05 16:57:59 +08:00
public StructureInstance(string symbolName, int lineIndex, int wordIndex, Structure structureType)
: base(symbolName, lineIndex, wordIndex)
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;
}
2025-07-05 16:57:59 +08:00
public override StructureInstance CloneVariable(string targetSymbolName, int lineIndex, int wordIndex)
2025-07-02 13:11:26 +08:00
{
2025-07-05 16:57:59 +08:00
return new StructureInstance(targetSymbolName, lineIndex, wordIndex, 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