add: basic grammar parser including expression and program.
This commit is contained in:
10
CanonSharp.Pascal/SyntaxTree/AssignNode.cs
Normal file
10
CanonSharp.Pascal/SyntaxTree/AssignNode.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public sealed class AssignNode(VariableNode variable, SyntaxNodeBase expression) : SyntaxNodeBase
|
||||
{
|
||||
public override SyntaxNodeType NodeType => SyntaxNodeType.Assign;
|
||||
|
||||
public VariableNode Variable => variable;
|
||||
|
||||
public SyntaxNodeBase Expression => expression;
|
||||
}
|
33
CanonSharp.Pascal/SyntaxTree/BinaryOperatorNode.cs
Normal file
33
CanonSharp.Pascal/SyntaxTree/BinaryOperatorNode.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public enum BinaryOperatorType
|
||||
{
|
||||
Add,
|
||||
Subtract,
|
||||
Multiply,
|
||||
|
||||
// Pascal特有的整数除法关键词div
|
||||
IntegerDivide,
|
||||
Divide,
|
||||
Mod,
|
||||
And,
|
||||
Or,
|
||||
Equal,
|
||||
NotEqual,
|
||||
Greater,
|
||||
GreaterEqual,
|
||||
Less,
|
||||
LessEqual
|
||||
}
|
||||
|
||||
public sealed class BinaryOperatorNode(BinaryOperatorType operatorType, SyntaxNodeBase left, SyntaxNodeBase right)
|
||||
: SyntaxNodeBase
|
||||
{
|
||||
public override SyntaxNodeType NodeType => SyntaxNodeType.BinaryOperator;
|
||||
|
||||
public BinaryOperatorType OperatorType => operatorType;
|
||||
|
||||
public SyntaxNodeBase Left => left;
|
||||
|
||||
public SyntaxNodeBase Right => right;
|
||||
}
|
8
CanonSharp.Pascal/SyntaxTree/BlockNode.cs
Normal file
8
CanonSharp.Pascal/SyntaxTree/BlockNode.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public sealed class BlockNode(IEnumerable<SyntaxNodeBase> statements) : SyntaxNodeBase
|
||||
{
|
||||
public override SyntaxNodeType NodeType => SyntaxNodeType.Block;
|
||||
|
||||
public IList<SyntaxNodeBase> Statements => statements.ToList();
|
||||
}
|
8
CanonSharp.Pascal/SyntaxTree/BooleanValueNode.cs
Normal file
8
CanonSharp.Pascal/SyntaxTree/BooleanValueNode.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public sealed class BooleanValueNode(bool value) : SyntaxNodeBase
|
||||
{
|
||||
public override SyntaxNodeType NodeType => SyntaxNodeType.BooleanValue;
|
||||
|
||||
public bool Value => value;
|
||||
}
|
8
CanonSharp.Pascal/SyntaxTree/FloatValueNode.cs
Normal file
8
CanonSharp.Pascal/SyntaxTree/FloatValueNode.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public sealed class FloatValueNode(double value) : SyntaxNodeBase
|
||||
{
|
||||
public override SyntaxNodeType NodeType => SyntaxNodeType.FloatValue;
|
||||
|
||||
public double Value => value;
|
||||
}
|
8
CanonSharp.Pascal/SyntaxTree/IntegerValueNode.cs
Normal file
8
CanonSharp.Pascal/SyntaxTree/IntegerValueNode.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public sealed class IntegerValueNode(int value) : SyntaxNodeBase
|
||||
{
|
||||
public override SyntaxNodeType NodeType => SyntaxNodeType.IntegerValue;
|
||||
|
||||
public int Value => value;
|
||||
}
|
10
CanonSharp.Pascal/SyntaxTree/Program.cs
Normal file
10
CanonSharp.Pascal/SyntaxTree/Program.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public sealed class Program(ProgramHead head, ProgramBody body) : SyntaxNodeBase
|
||||
{
|
||||
public override SyntaxNodeType NodeType => SyntaxNodeType.Program;
|
||||
|
||||
public ProgramHead Head => head;
|
||||
|
||||
public ProgramBody Body => body;
|
||||
}
|
8
CanonSharp.Pascal/SyntaxTree/ProgramBody.cs
Normal file
8
CanonSharp.Pascal/SyntaxTree/ProgramBody.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public sealed class ProgramBody(BlockNode block) : SyntaxNodeBase
|
||||
{
|
||||
public override SyntaxNodeType NodeType => SyntaxNodeType.ProgramBody;
|
||||
|
||||
public BlockNode MainBlock => block;
|
||||
}
|
10
CanonSharp.Pascal/SyntaxTree/ProgramHead.cs
Normal file
10
CanonSharp.Pascal/SyntaxTree/ProgramHead.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using CanonSharp.Pascal.Scanner;
|
||||
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public sealed class ProgramHead(LexicalToken programName) : SyntaxNodeBase
|
||||
{
|
||||
public override SyntaxNodeType NodeType => SyntaxNodeType.ProgramHead;
|
||||
|
||||
public LexicalToken ProgramName => programName;
|
||||
}
|
31
CanonSharp.Pascal/SyntaxTree/SyntaxNodeBase.cs
Normal file
31
CanonSharp.Pascal/SyntaxTree/SyntaxNodeBase.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public enum SyntaxNodeType
|
||||
{
|
||||
BinaryOperator,
|
||||
UnaryOperator,
|
||||
IntegerValue,
|
||||
FloatValue,
|
||||
BooleanValue,
|
||||
Variable,
|
||||
Assign,
|
||||
Block,
|
||||
ProgramBody,
|
||||
ProgramHead,
|
||||
Program
|
||||
}
|
||||
|
||||
public abstract class SyntaxNodeBase
|
||||
{
|
||||
public abstract SyntaxNodeType NodeType { get; }
|
||||
|
||||
public T Convert<T>() where T : SyntaxNodeBase
|
||||
{
|
||||
if (this is not T result)
|
||||
{
|
||||
throw new InvalidCastException($"Can't convert {NodeType} to target node.");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
17
CanonSharp.Pascal/SyntaxTree/UnaryOperatorNode.cs
Normal file
17
CanonSharp.Pascal/SyntaxTree/UnaryOperatorNode.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public enum UnaryOperatorType
|
||||
{
|
||||
Plus,
|
||||
Minus,
|
||||
Not
|
||||
}
|
||||
|
||||
public sealed class UnaryOperatorNode(UnaryOperatorType operatorType, SyntaxNodeBase node) : SyntaxNodeBase
|
||||
{
|
||||
public override SyntaxNodeType NodeType => SyntaxNodeType.UnaryOperator;
|
||||
|
||||
public UnaryOperatorType OperatorType => operatorType;
|
||||
|
||||
public SyntaxNodeBase Node => node;
|
||||
}
|
8
CanonSharp.Pascal/SyntaxTree/VariableNode.cs
Normal file
8
CanonSharp.Pascal/SyntaxTree/VariableNode.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace CanonSharp.Pascal.SyntaxTree;
|
||||
|
||||
public sealed class VariableNode(string name) : SyntaxNodeBase
|
||||
{
|
||||
public override SyntaxNodeType NodeType => SyntaxNodeType.Variable;
|
||||
|
||||
public string IdentifierName => name;
|
||||
}
|
Reference in New Issue
Block a user