add: 操作数的种类

This commit is contained in:
jackfiled 2024-03-10 11:14:52 +08:00
parent 8d30b1626b
commit 410593f326
3 changed files with 110 additions and 22 deletions

View File

@ -19,7 +19,9 @@ public enum DelimiterType
LeftParenthesis,
RightParenthesis,
LeftSquareBracket,
RightSquareBracket
RightSquareBracket,
SingleQuotation,
DoubleQuotation
}
public enum KeywordType
@ -40,3 +42,21 @@ public enum KeywordType
To,
Do,
}
public enum OperatorType
{
Equal,
NotEqual,
Less,
LessEqual,
Greater,
GreaterEqual,
Plus,
Minus,
Multiply,
Divide,
Mod,
And,
Or,
Assign
}

View File

@ -8,38 +8,68 @@ public abstract class TerminatorBase
}
/// <summary>
/// A terminator in grammar and it always represents a semantic token.
/// 语法中的一个终结符
/// 终结符标识词法分析中得到的一个记号
/// </summary>
public class Terminator : TerminatorBase, IEquatable<Terminator>
{
public override bool IsTerminated => true;
private readonly bool _isKeyword;
private readonly SemanticTokenType _terminatorType;
private readonly KeywordType _keywordType;
private readonly DelimiterType _delimiterType;
private readonly OperatorType _operatorType;
public Terminator(KeywordType keywordType)
{
_isKeyword = true;
_terminatorType = SemanticTokenType.Keyword;
_keywordType = keywordType;
}
public Terminator(DelimiterType delimiterType)
{
_isKeyword = false;
_terminatorType = SemanticTokenType.Delimiter;
_delimiterType = delimiterType;
}
public Terminator(OperatorType operatorType)
{
_terminatorType = SemanticTokenType.Operator;
_operatorType = operatorType;
}
private Terminator(SemanticTokenType type)
{
_terminatorType = type;
}
/// <summary>
/// 标识符终结符单例
/// 鉴于在语法中不关心标识符具体内容,因此可以使用单例对象
/// </summary>
public static Terminator IdentifierTerminator => new(SemanticTokenType.Identifier);
/// <summary>
/// 字符终结符单例
/// 鉴于在语法中不关心具体字符,因此可以使用单例对象
/// </summary>
public static Terminator CharacterTerminator => new(SemanticTokenType.Character);
public override int GetHashCode()
{
if (_isKeyword)
int hash = _terminatorType.GetHashCode();
switch (_terminatorType)
{
return _keywordType.GetHashCode();
}
else
{
return _delimiterType.GetHashCode();
case SemanticTokenType.Keyword:
return hash ^ _keywordType.GetHashCode();
case SemanticTokenType.Delimiter:
return hash ^ _delimiterType.GetHashCode();
case SemanticTokenType.Operator:
return hash ^ _operatorType.GetHashCode();
default:
return hash;
}
}
@ -50,18 +80,21 @@ public class Terminator : TerminatorBase, IEquatable<Terminator>
return false;
}
if (_isKeyword != other._isKeyword)
if (_terminatorType != other._terminatorType)
{
return false;
}
if (_isKeyword)
switch (_terminatorType)
{
return _keywordType == other._keywordType;
}
else
{
return _delimiterType == other._delimiterType;
case SemanticTokenType.Keyword:
return _keywordType == other._keywordType;
case SemanticTokenType.Delimiter:
return _delimiterType == other._delimiterType;
case SemanticTokenType.Operator:
return _operatorType == other._operatorType;
default:
return true;
}
}
@ -87,7 +120,7 @@ public class Terminator : TerminatorBase, IEquatable<Terminator>
}
/// <summary>
/// A non-terminator in grammar like the 'ProgramStruct'.
/// 语法中的非终结符
/// </summary>
public class NonTerminator : TerminatorBase, IEquatable<NonTerminator>
{

View File

@ -2,28 +2,47 @@
using Enums;
/// <summary>
/// 词法记号基类
/// </summary>
public abstract class SemanticToken
{
public abstract SemanticTokenType TokenType { get; }
/// <summary>
/// 记号出现的行号
/// </summary>
public required uint LinePos { get; init; }
/// <summary>
/// 记号出现的列号
/// </summary>
public required uint CharacterPos { get; init; }
/// <summary>
/// 记号的字面值
/// </summary>
public required string LiteralValue { get; init; }
}
/// <summary>
/// 字符类型记号
/// </summary>
public class CharacterSemanticToken : SemanticToken
{
public override SemanticTokenType TokenType => SemanticTokenType.Character;
public static bool TryParse(uint linePos, uint characterPos, LinkedListNode<char> now, out CharacterSemanticToken? token)
public static bool TryParse(uint linePos, uint characterPos, LinkedListNode<char> now,
out CharacterSemanticToken? token)
{
token = null;
return false;
}
}
/// <summary>
/// 分隔符类型记号
/// </summary>
public class DelimiterSemanticToken : SemanticToken
{
public override SemanticTokenType TokenType => SemanticTokenType.Delimiter;
@ -42,7 +61,9 @@ public class DelimiterSemanticToken : SemanticToken
{ '(', DelimiterType.LeftParenthesis },
{ ')', DelimiterType.RightParenthesis },
{ '[', DelimiterType.LeftSquareBracket },
{ ']', DelimiterType.RightSquareBracket }
{ ']', DelimiterType.RightSquareBracket },
{ '\'', DelimiterType.SingleQuotation },
{ '\"', DelimiterType.DoubleQuotation }
};
if (!delimiterMap.TryGetValue(now.Value, out DelimiterType value))
@ -62,6 +83,9 @@ public class DelimiterSemanticToken : SemanticToken
}
}
/// <summary>
/// 关键字类型记号
/// </summary>
public class KeywordSemanticToken : SemanticToken
{
public override SemanticTokenType TokenType => SemanticTokenType.Keyword;
@ -75,7 +99,7 @@ public class KeywordSemanticToken : SemanticToken
if (now.Next is null)
{
// As there is now keyword shorter than 2 characters.
// 没有比两个字符更短的关键字
token = null;
return false;
}
@ -119,10 +143,15 @@ public class KeywordSemanticToken : SemanticToken
}
}
/// <summary>
/// 操作数类型记号
/// </summary>
public class OperatorSemanticToken : SemanticToken
{
public override SemanticTokenType TokenType => SemanticTokenType.Operator;
public required OperatorType OperatorType { get; init; }
public static bool TryParse(uint linePos, uint characterPos, LinkedListNode<char> now,
out OperatorSemanticToken? token)
{
@ -131,6 +160,9 @@ public class OperatorSemanticToken : SemanticToken
}
}
/// <summary>
/// 数值类型记号
/// </summary>
public class NumberSemanticToken : SemanticToken
{
public override SemanticTokenType TokenType => SemanticTokenType.Number;
@ -143,6 +175,9 @@ public class NumberSemanticToken : SemanticToken
}
}
/// <summary>
/// 标识符类型记号
/// </summary>
public class IdentifierSemanticToken : SemanticToken
{
public override SemanticTokenType TokenType => SemanticTokenType.Identifier;