2024-04-26 10:18:49 +08:00
|
|
|
|
using Canon.Core.SyntaxNodes;
|
2024-04-18 16:34:32 +08:00
|
|
|
|
using Canon.Tests.Utils;
|
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-07 16:47:28 +08:00
|
|
|
|
[Fact]
|
|
|
|
|
public void DoNothingTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
2024-05-01 21:06:27 +08:00
|
|
|
|
program DoNothing;
|
|
|
|
|
begin
|
|
|
|
|
end.
|
|
|
|
|
""";
|
2024-04-07 16:47:28 +08:00
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
ProgramStruct root = CompilerHelpers.Analyse(program);
|
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.
|
|
|
|
|
""";
|
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
ProgramStruct root = CompilerHelpers.Analyse(program);
|
2024-04-11 22:41:43 +08:00
|
|
|
|
Assert.Equal("Add", root.Head.ProgramName.LiteralValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void WriteLnTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program exFunction;
|
2024-05-04 13:57:14 +08:00
|
|
|
|
const str = 'a';
|
2024-04-11 22:41:43 +08:00
|
|
|
|
var a, b : Integer;
|
|
|
|
|
begin
|
|
|
|
|
writeln( str, ret );
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
2024-04-26 10:18:49 +08:00
|
|
|
|
ProgramStruct root = CompilerHelpers.Analyse(program);
|
2024-04-11 22:41:43 +08:00
|
|
|
|
Assert.Equal("exFunction", root.Head.ProgramName.LiteralValue);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-13 12:37:17 +08:00
|
|
|
|
[Fact]
|
2024-04-28 15:13:09 +08:00
|
|
|
|
public void CharacterTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program varTest;
|
|
|
|
|
var a : integer;
|
|
|
|
|
begin
|
|
|
|
|
a := 9 div 1;
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
ProgramStruct root = CompilerHelpers.Analyse(program);
|
|
|
|
|
Assert.Equal("vartest", root.Head.ProgramName.IdentifierName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ArrayTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program arrayTest;
|
|
|
|
|
var a : array [0..10] of integer;
|
|
|
|
|
begin
|
|
|
|
|
a[0] := 1;
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
CompilerHelpers.Analyse(program);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void MultiplyArrayTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program arrayTest;
|
|
|
|
|
var a : array [10..100, 0..10] of integer;
|
|
|
|
|
begin
|
|
|
|
|
a[10,0] := 1;
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
CompilerHelpers.Analyse(program);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ProcedureTest()
|
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-28 15:13:09 +08:00
|
|
|
|
CompilerHelpers.Analyse(program);
|
2024-04-11 22:41:43 +08:00
|
|
|
|
}
|
2024-04-20 22:23:29 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
2024-04-28 15:13:09 +08:00
|
|
|
|
public void FunctionTest()
|
2024-04-20 22:23:29 +08:00
|
|
|
|
{
|
|
|
|
|
const string program = """
|
2024-04-28 15:13:09 +08:00
|
|
|
|
program main;
|
|
|
|
|
function test(a, b : integer) : integer;
|
|
|
|
|
begin
|
|
|
|
|
test := 1;
|
|
|
|
|
end;
|
2024-04-20 22:23:29 +08:00
|
|
|
|
begin
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
2024-04-28 15:13:09 +08:00
|
|
|
|
CompilerHelpers.Analyse(program);
|
2024-04-20 22:23:29 +08:00
|
|
|
|
}
|
2024-04-26 10:18:49 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
2024-04-28 15:13:09 +08:00
|
|
|
|
public void ForLoopTest()
|
2024-04-26 10:18:49 +08:00
|
|
|
|
{
|
|
|
|
|
const string program = """
|
2024-04-28 15:13:09 +08:00
|
|
|
|
program main;
|
|
|
|
|
var i : integer;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
begin
|
2024-04-28 15:13:09 +08:00
|
|
|
|
for i := 1 to 100 do
|
|
|
|
|
begin
|
|
|
|
|
doSomething(i);
|
|
|
|
|
end;
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
CompilerHelpers.Analyse(program);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void IfConditionTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program main;
|
|
|
|
|
begin
|
|
|
|
|
if 1 = 2 then
|
|
|
|
|
begin
|
|
|
|
|
test1;
|
|
|
|
|
test2 := a;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
doSomething;
|
|
|
|
|
end;
|
2024-04-26 10:18:49 +08:00
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
CompilerHelpers.Analyse(program);
|
|
|
|
|
}
|
2024-05-01 21:06:27 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ProcedureCallTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program main;
|
|
|
|
|
var a : integer;
|
|
|
|
|
function test : integer;
|
|
|
|
|
begin
|
|
|
|
|
end;
|
|
|
|
|
begin
|
|
|
|
|
test;
|
|
|
|
|
a := test;
|
|
|
|
|
test();
|
|
|
|
|
a := test();
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
CompilerHelpers.Analyse(program);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ProcedureDefinitionTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program main;
|
|
|
|
|
procedure test();
|
|
|
|
|
begin
|
|
|
|
|
end;
|
|
|
|
|
begin
|
|
|
|
|
test();
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
CompilerHelpers.Analyse(program);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void FactorTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program main;
|
|
|
|
|
var a : integer;
|
|
|
|
|
begin
|
|
|
|
|
a := 1 + +1;
|
|
|
|
|
a := 1 - -1;
|
|
|
|
|
a := 1 + -1;
|
|
|
|
|
a := 1 - +1;
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
CompilerHelpers.Analyse(program);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void TrueFalseTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program main;
|
|
|
|
|
const a = true; b = false;
|
|
|
|
|
var c, d : boolean;
|
|
|
|
|
begin
|
|
|
|
|
c := true;
|
|
|
|
|
d := false;
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
CompilerHelpers.Analyse(program);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void ProcedureAndVariableTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program main;
|
|
|
|
|
begin
|
|
|
|
|
test
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
CompilerHelpers.Analyse(program);
|
|
|
|
|
}
|
2024-05-10 13:52:11 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void WhileLoopTest()
|
|
|
|
|
{
|
|
|
|
|
const string program = """
|
|
|
|
|
program main;
|
|
|
|
|
var i : integer;
|
|
|
|
|
begin
|
|
|
|
|
while i < 10 do
|
|
|
|
|
i := i + 1;
|
|
|
|
|
end.
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
|
|
CompilerHelpers.Analyse(program);
|
|
|
|
|
}
|
2024-03-13 23:58:06 +08:00
|
|
|
|
}
|