2024-03-13 23:58:06 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
2024-04-11 22:41:43 +08:00
|
|
|
|
using Canon.Core.Enums;
|
|
|
|
|
using Canon.Core.GrammarParser;
|
2024-04-07 16:47:28 +08:00
|
|
|
|
using Canon.Core.LexicalParser;
|
|
|
|
|
using Canon.Core.SyntaxNodes;
|
2024-04-08 19:46:24 +08:00
|
|
|
|
using Canon.Tests.GeneratedParserTests;
|
2024-03-13 23:58:06 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Tests.GrammarParserTests;
|
|
|
|
|
|
2024-04-07 16:47:28 +08:00
|
|
|
|
public class PascalGrammarTests
|
2024-03-13 23:58:06 +08:00
|
|
|
|
{
|
2024-04-12 19:01:37 +08:00
|
|
|
|
private readonly IGrammarParser _parser = GeneratedGrammarParser.Instance;
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void DoNothingTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program DoNothing;
|
|
|
|
|
begin
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
Lexer lexer = new(program);
|
|
|
|
|
List<SemanticToken> tokens = lexer.Tokenize();
|
|
|
|
|
tokens.Add(SemanticToken.End);
|
|
|
|
|
|
2024-04-12 19:01:37 +08:00
|
|
|
|
ProgramStruct root = _parser.Analyse(tokens);
|
2024-04-07 16:47:28 +08:00
|
|
|
|
Assert.Equal("DoNothing", root.Head.ProgramName.LiteralValue);
|
2024-04-12 19:01:37 +08:00
|
|
|
|
Assert.Equal(15, root.Count());
|
2024-04-07 16:47:28 +08:00
|
|
|
|
}
|
2024-04-11 22:41:43 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AddTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program Add;
|
|
|
|
|
var a : Integer;
|
|
|
|
|
begin
|
|
|
|
|
a := 1 + 1
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
Lexer lexer = new(program);
|
|
|
|
|
List<SemanticToken> tokens = lexer.Tokenize();
|
|
|
|
|
tokens.Add(SemanticToken.End);
|
|
|
|
|
|
2024-04-12 19:01:37 +08:00
|
|
|
|
ProgramStruct root = _parser.Analyse(tokens);
|
2024-04-11 22:41:43 +08:00
|
|
|
|
Assert.Equal("Add", root.Head.ProgramName.LiteralValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void WriteLnTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program exFunction;
|
|
|
|
|
const str = 'result is : ';
|
|
|
|
|
var a, b : Integer;
|
|
|
|
|
begin
|
|
|
|
|
writeln( str, ret );
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
Lexer lexer = new(program);
|
|
|
|
|
List<SemanticToken> tokens = lexer.Tokenize();
|
|
|
|
|
tokens.Add(SemanticToken.End);
|
|
|
|
|
|
2024-04-12 19:01:37 +08:00
|
|
|
|
ProgramStruct root = _parser.Analyse(tokens);
|
2024-04-11 22:41:43 +08:00
|
|
|
|
Assert.Equal("exFunction", root.Head.ProgramName.LiteralValue);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 12:37:17 +08:00
|
|
|
|
[Fact]
|
|
|
|
|
public void SubprogramTest()
|
2024-04-11 22:41:43 +08:00
|
|
|
|
{
|
2024-04-13 12:37:17 +08:00
|
|
|
|
const string program = """
|
|
|
|
|
program main;
|
|
|
|
|
procedure test;
|
|
|
|
|
begin
|
|
|
|
|
end;
|
|
|
|
|
begin
|
|
|
|
|
end.
|
|
|
|
|
""";
|
2024-04-11 22:41:43 +08:00
|
|
|
|
|
2024-04-13 12:37:17 +08:00
|
|
|
|
Lexer lexer = new(program);
|
|
|
|
|
List<SemanticToken> tokens = lexer.Tokenize();
|
|
|
|
|
tokens.Add(SemanticToken.End);
|
|
|
|
|
|
|
|
|
|
ProgramStruct root = _parser.Analyse(tokens);
|
|
|
|
|
Assert.Equal("main", root.Head.ProgramName.LiteralValue);
|
2024-04-11 22:41:43 +08:00
|
|
|
|
}
|
2024-03-13 23:58:06 +08:00
|
|
|
|
}
|