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, LeftParenthesis,
RightParenthesis, RightParenthesis,
LeftSquareBracket, LeftSquareBracket,
RightSquareBracket RightSquareBracket,
SingleQuotation,
DoubleQuotation
} }
public enum KeywordType public enum KeywordType
@ -40,3 +42,21 @@ public enum KeywordType
To, To,
Do, 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> /// <summary>
/// A terminator in grammar and it always represents a semantic token. /// 语法中的一个终结符
/// 终结符标识词法分析中得到的一个记号
/// </summary> /// </summary>
public class Terminator : TerminatorBase, IEquatable<Terminator> public class Terminator : TerminatorBase, IEquatable<Terminator>
{ {
public override bool IsTerminated => true; public override bool IsTerminated => true;
private readonly bool _isKeyword; private readonly SemanticTokenType _terminatorType;
private readonly KeywordType _keywordType; private readonly KeywordType _keywordType;
private readonly DelimiterType _delimiterType; private readonly DelimiterType _delimiterType;
private readonly OperatorType _operatorType;
public Terminator(KeywordType keywordType) public Terminator(KeywordType keywordType)
{ {
_isKeyword = true; _terminatorType = SemanticTokenType.Keyword;
_keywordType = keywordType; _keywordType = keywordType;
} }
public Terminator(DelimiterType delimiterType) public Terminator(DelimiterType delimiterType)
{ {
_isKeyword = false; _terminatorType = SemanticTokenType.Delimiter;
_delimiterType = delimiterType; _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() public override int GetHashCode()
{ {
if (_isKeyword) int hash = _terminatorType.GetHashCode();
switch (_terminatorType)
{ {
return _keywordType.GetHashCode(); case SemanticTokenType.Keyword:
} return hash ^ _keywordType.GetHashCode();
else case SemanticTokenType.Delimiter:
{ return hash ^ _delimiterType.GetHashCode();
return _delimiterType.GetHashCode(); case SemanticTokenType.Operator:
return hash ^ _operatorType.GetHashCode();
default:
return hash;
} }
} }
@ -50,18 +80,21 @@ public class Terminator : TerminatorBase, IEquatable<Terminator>
return false; return false;
} }
if (_isKeyword != other._isKeyword) if (_terminatorType != other._terminatorType)
{ {
return false; return false;
} }
if (_isKeyword) switch (_terminatorType)
{ {
case SemanticTokenType.Keyword:
return _keywordType == other._keywordType; return _keywordType == other._keywordType;
} case SemanticTokenType.Delimiter:
else
{
return _delimiterType == other._delimiterType; 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> /// <summary>
/// A non-terminator in grammar like the 'ProgramStruct'. /// 语法中的非终结符
/// </summary> /// </summary>
public class NonTerminator : TerminatorBase, IEquatable<NonTerminator> public class NonTerminator : TerminatorBase, IEquatable<NonTerminator>
{ {

View File

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