2024-04-07 16:10:34 +08:00
|
|
|
|
using Canon.Core.SemanticParser;
|
|
|
|
|
|
|
|
|
|
namespace Canon.Tests.SemanticTests;
|
|
|
|
|
|
|
|
|
|
public class SymbolTableTests
|
|
|
|
|
{
|
|
|
|
|
[Fact]
|
|
|
|
|
public void BasicTypeTest()
|
|
|
|
|
{
|
|
|
|
|
SymbolTable table = new();
|
|
|
|
|
|
2024-05-12 14:29:18 +08:00
|
|
|
|
Assert.True(table.TryGetType("Integer", out PascalType? integer));
|
2024-04-07 16:10:34 +08:00
|
|
|
|
Assert.Equal(PascalBasicType.Integer, integer);
|
2024-05-12 14:29:18 +08:00
|
|
|
|
Assert.True(table.TryGetType("Real", out PascalType? real));
|
2024-04-07 16:10:34 +08:00
|
|
|
|
Assert.Equal(PascalBasicType.Real, real);
|
2024-05-12 14:29:18 +08:00
|
|
|
|
Assert.True(table.TryGetType("Boolean", out PascalType? boolean));
|
2024-04-07 16:10:34 +08:00
|
|
|
|
Assert.Equal(PascalBasicType.Boolean, boolean);
|
2024-05-12 14:29:18 +08:00
|
|
|
|
Assert.True(table.TryGetType("Character", out PascalType? character));
|
2024-04-07 16:10:34 +08:00
|
|
|
|
Assert.Equal(PascalBasicType.Character, character);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void SingleTableInsertAndFindTest()
|
|
|
|
|
{
|
|
|
|
|
SymbolTable table = new();
|
|
|
|
|
|
|
|
|
|
Assert.True(table.TryAddSymbol(new Symbol { SymbolName = "a", SymbolType = PascalBasicType.Integer }));
|
2024-05-12 14:29:18 +08:00
|
|
|
|
Assert.True(table.TryAddSymbol(new Symbol { SymbolName = "temperature", SymbolType = PascalBasicType.Real }));
|
2024-04-07 16:10:34 +08:00
|
|
|
|
|
|
|
|
|
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 }));
|
2024-05-12 14:29:18 +08:00
|
|
|
|
Assert.True(table.TryAddSymbol(new Symbol { SymbolName = "temperature", SymbolType = PascalBasicType.Real }));
|
2024-04-07 16:10:34 +08:00
|
|
|
|
|
|
|
|
|
SymbolTable child = table.CreateChildTable();
|
|
|
|
|
|
2024-05-12 14:29:18 +08:00
|
|
|
|
Assert.True(child.TryAddSymbol(new Symbol { SymbolName = "a", SymbolType = PascalBasicType.Real }));
|
|
|
|
|
Assert.True(child.TryAddSymbol(new Symbol { SymbolName = "level2", SymbolType = PascalBasicType.Boolean }));
|
2024-04-07 16:10:34 +08:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2024-05-14 14:10:42 +08:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void DuplicatedTest()
|
|
|
|
|
{
|
|
|
|
|
SymbolTable table = new();
|
|
|
|
|
|
|
|
|
|
Assert.True(table.TryAddSymbol(
|
|
|
|
|
new Symbol{SymbolName = "a", SymbolType = PascalBasicType.Integer, Const = true}));
|
|
|
|
|
Assert.False(table.TryAddSymbol(new Symbol{SymbolName = "a", SymbolType = PascalBasicType.Boolean}));
|
|
|
|
|
}
|
2024-04-07 16:10:34 +08:00
|
|
|
|
}
|