2025-06-30 20:20:26 +08:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace Convention.Symbolization.Internal
|
|
|
|
|
{
|
2025-07-01 22:21:30 +08:00
|
|
|
|
public abstract class Variable : IEquatable<Variable>
|
2025-06-30 20:20:26 +08:00
|
|
|
|
{
|
2025-07-01 20:34:09 +08:00
|
|
|
|
public readonly VariableSymbol SymbolInfo;
|
|
|
|
|
|
2025-07-01 22:21:30 +08:00
|
|
|
|
protected Variable(VariableSymbol symbolInfo)
|
2025-07-01 20:34:09 +08:00
|
|
|
|
{
|
2025-07-01 22:21:30 +08:00
|
|
|
|
this.SymbolInfo = symbolInfo;
|
2025-07-01 20:34:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract bool Equals(Variable other);
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
2025-07-01 22:21:30 +08:00
|
|
|
|
return Equals(obj as Variable);
|
2025-07-01 20:34:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 22:21:30 +08:00
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return base.GetHashCode();
|
|
|
|
|
}
|
2025-07-02 18:46:09 +08:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return SymbolInfo.SymbolName;
|
|
|
|
|
}
|
2025-07-01 22:21:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public abstract class Variable<T> : Variable, IEquatable<T>
|
|
|
|
|
{
|
2025-07-05 16:57:59 +08:00
|
|
|
|
protected Variable(string symbolName, int lineIndex, int wordIndex) : base(new VariableSymbol(symbolName, typeof(T), lineIndex, wordIndex)) { }
|
2025-07-01 22:21:30 +08:00
|
|
|
|
public abstract bool Equals(T other);
|
|
|
|
|
|
|
|
|
|
public override bool Equals(Variable other)
|
|
|
|
|
{
|
|
|
|
|
return other is T variable && Equals(variable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override bool Equals(object obj)
|
|
|
|
|
{
|
|
|
|
|
return obj is T variable && Equals(variable);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual int GetVariableHashCode()
|
|
|
|
|
{
|
|
|
|
|
return base.GetHashCode();
|
|
|
|
|
}
|
2025-07-01 20:34:09 +08:00
|
|
|
|
|
|
|
|
|
public override sealed int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return GetVariableHashCode();
|
|
|
|
|
}
|
2025-06-30 20:20:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-07-02 13:11:26 +08:00
|
|
|
|
public interface ICanFindVariable
|
|
|
|
|
{
|
|
|
|
|
public Variable[] Find(string symbolName);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-01 22:21:30 +08:00
|
|
|
|
public abstract class CloneableVariable<T> : Variable<T>, ICloneable
|
|
|
|
|
{
|
2025-07-05 16:57:59 +08:00
|
|
|
|
protected CloneableVariable(string symbolName, int lineIndex, int wordIndex) : base(symbolName, lineIndex, wordIndex)
|
2025-07-01 22:21:30 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-05 16:57:59 +08:00
|
|
|
|
public object Clone() => CloneVariable(SymbolInfo.SymbolName, 0, 0);
|
2025-07-02 13:11:26 +08:00
|
|
|
|
|
2025-07-05 16:57:59 +08:00
|
|
|
|
public abstract T CloneVariable(string targetSymbolName, int lineIndex, int wordIndex);
|
2025-07-02 13:11:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public sealed class NullVariable : CloneableVariable<NullVariable>
|
|
|
|
|
{
|
2025-07-05 16:57:59 +08:00
|
|
|
|
public NullVariable(string symbolName, int lineIndex, int wordIndex) : base(symbolName, lineIndex, wordIndex)
|
2025-07-01 22:21:30 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-05 16:57:59 +08:00
|
|
|
|
public override NullVariable 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(targetSymbolName, lineIndex, wordIndex);
|
2025-07-02 13:11:26 +08:00
|
|
|
|
}
|
2025-07-01 22:21:30 +08:00
|
|
|
|
|
2025-07-02 13:11:26 +08:00
|
|
|
|
public override bool Equals(NullVariable other)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2025-07-01 22:21:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-06-30 20:20:26 +08:00
|
|
|
|
public readonly struct VariableSymbol
|
|
|
|
|
{
|
2025-07-05 16:57:59 +08:00
|
|
|
|
public readonly int LineIndex;
|
|
|
|
|
public readonly int WordIndex;
|
2025-06-30 20:20:26 +08:00
|
|
|
|
public readonly string SymbolName;
|
|
|
|
|
public readonly Type VariableType;
|
|
|
|
|
|
2025-07-05 16:57:59 +08:00
|
|
|
|
public VariableSymbol(string symbolName, Type variableType, int lineIndex,int wordIndex)
|
2025-06-30 20:20:26 +08:00
|
|
|
|
{
|
|
|
|
|
this.SymbolName = symbolName;
|
|
|
|
|
this.VariableType = variableType;
|
2025-07-05 16:57:59 +08:00
|
|
|
|
this.LineIndex = lineIndex;
|
|
|
|
|
this.WordIndex = wordIndex;
|
2025-06-30 20:20:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Equals(VariableSymbol other)
|
|
|
|
|
{
|
|
|
|
|
return other.SymbolName.Equals(SymbolName) && other.VariableType.Equals(VariableType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return new Tuple<string, Type>(SymbolName, VariableType).GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|