refact: 重构类型和符号表部分 (#25)

Reviewed-on: PostGuard/Canon#25
This commit is contained in:
2024-04-07 16:10:34 +08:00
parent 1a0d3c37db
commit c0a8e25d45
14 changed files with 537 additions and 1064 deletions

View File

@@ -0,0 +1,50 @@
using Canon.Core.SemanticParser;
namespace Canon.Tests.SemanticTests;
public class PascalTypeTests
{
[Fact]
public void PascalBasicTypeTests()
{
PascalType integer = PascalBasicType.Integer;
PascalType boolean = PascalBasicType.Boolean;
PascalType character = PascalBasicType.Character;
PascalType real = PascalBasicType.Real;
PascalType voidType = PascalBasicType.Void;
Assert.Equal(integer, PascalBasicType.Integer);
Assert.Equal(boolean, PascalBasicType.Boolean);
Assert.NotEqual(integer, character);
Assert.NotEqual(boolean, real);
Assert.NotEqual(character, voidType);
}
[Fact]
public void PascalArrayTypeTests()
{
PascalType array1 = new PascalArrayType(PascalBasicType.Integer, 0, 10);
PascalType array2 = new PascalArrayType(PascalBasicType.Integer, 0, 10);
Assert.Equal(array1, array2);
PascalType array3 = new PascalArrayType(PascalBasicType.Integer, -9, -3);
Assert.NotEqual(array1, array3);
}
[Fact]
public void PascalFunctionTypeTests()
{
PascalType function1 = new PascalFunctionType([new PascalParameterType(PascalBasicType.Integer, false)],
PascalBasicType.Void);
PascalType function2 = new PascalFunctionType([new PascalParameterType(PascalBasicType.Integer, false)],
PascalBasicType.Void);
Assert.Equal(function1, function2);
PascalType function3 = new PascalFunctionType([new PascalParameterType(PascalBasicType.Real, true)],
PascalBasicType.Integer);
Assert.NotEqual(function1, function3);
}
}

View File

@@ -0,0 +1,66 @@
using Canon.Core.SemanticParser;
namespace Canon.Tests.SemanticTests;
public class SymbolTableTests
{
[Fact]
public void BasicTypeTest()
{
SymbolTable table = new();
Assert.True(table.TryGetType("integer", out PascalType? integer));
Assert.Equal(PascalBasicType.Integer, integer);
Assert.True(table.TryGetType("real", out PascalType? real));
Assert.Equal(PascalBasicType.Real, real);
Assert.True(table.TryGetType("boolean", out PascalType? boolean));
Assert.Equal(PascalBasicType.Boolean, boolean);
Assert.True(table.TryGetType("char", out PascalType? character));
Assert.Equal(PascalBasicType.Character, character);
}
[Fact]
public void SingleTableInsertAndFindTest()
{
SymbolTable table = new();
Assert.True(table.TryAddSymbol(new Symbol { SymbolName = "a", SymbolType = PascalBasicType.Integer }));
Assert.True(table.TryAddSymbol(new Symbol
{
SymbolName = "temperature", SymbolType = PascalBasicType.Real, Const = true
}));
Assert.True(table.TryGetSymbol("a", out Symbol? a));
Assert.Equal(PascalBasicType.Integer, a.SymbolType);
Assert.False(table.TryGetSymbol("notExist", out a));
}
[Fact]
public void NestedTableInsertAndFindTest()
{
SymbolTable table = new();
Assert.True(table.TryAddSymbol(new Symbol { SymbolName = "a", SymbolType = PascalBasicType.Integer }));
Assert.True(table.TryAddSymbol(new Symbol
{
SymbolName = "temperature", SymbolType = PascalBasicType.Real, Const = true
}));
SymbolTable child = table.CreateChildTable();
Assert.True(child.TryAddSymbol(new Symbol{SymbolName = "a", SymbolType = PascalBasicType.Real}));
Assert.True(child.TryAddSymbol(new Symbol
{
SymbolName = "level2", SymbolType = PascalBasicType.Boolean, Reference = true
}));
Assert.True(child.TryGetSymbol("a", out Symbol? a));
Assert.Equal(PascalBasicType.Real, a.SymbolType);
Assert.True(table.TryGetSymbol("a", out a));
Assert.Equal(PascalBasicType.Integer, a.SymbolType);
Assert.True(table.TryGetSymbol("temperature", out Symbol? temp));
Assert.Equal(PascalBasicType.Real, temp.SymbolType);
}
}