fix:对于const重复定义检查错误 (#85)
Some checks failed
Integration Test / Open-Set-Test (push) Has been cancelled
Test canon project / Test-Canon (push) Has been cancelled

Reviewed-on: PostGuard/Canon#85
This commit is contained in:
jackfiled 2024-05-14 14:10:42 +08:00
parent 20e82c6f4f
commit 580f2d505a
5 changed files with 23 additions and 4 deletions

View File

@ -31,7 +31,7 @@ public class CodeGeneratorVisitor(ICompilerLogger? logger = null) : TypeCheckVis
public override void PostVisit(ConstDeclaration constDeclaration) public override void PostVisit(ConstDeclaration constDeclaration)
{ {
base.PreVisit(constDeclaration); base.PostVisit(constDeclaration);
(IdentifierSemanticToken token, ConstValue constValue) = constDeclaration.ConstValue; (IdentifierSemanticToken token, ConstValue constValue) = constDeclaration.ConstValue;

View File

@ -31,7 +31,7 @@ public abstract class PascalType : IEquatable<PascalType>
return false; return false;
} }
return IsReference == other.IsReference; return true;
} }
public T Convert<T>() where T : PascalType public T Convert<T>() where T : PascalType

View File

@ -44,7 +44,16 @@ public class SymbolTable
/// </summary> /// </summary>
/// <param name="symbol">欲添加的符号</param> /// <param name="symbol">欲添加的符号</param>
/// <returns>是否添加成功</returns> /// <returns>是否添加成功</returns>
public bool TryAddSymbol(Symbol symbol) => _symbols.TryAdd(symbol.SymbolName, symbol); public bool TryAddSymbol(Symbol symbol)
{
if (_symbols.ContainsKey(symbol.SymbolName))
{
return false;
}
_symbols.Add(symbol.SymbolName, symbol);
return true;
}
/// <summary> /// <summary>
/// 尝试从符号表极其父符号表查找符号 /// 尝试从符号表极其父符号表查找符号

View File

@ -22,7 +22,7 @@ public class TypeCheckVisitor(ICompilerLogger? logger = null) : SyntaxNodeVisito
public override void PreVisit(ConstDeclaration constDeclaration) public override void PreVisit(ConstDeclaration constDeclaration)
{ {
base.PostVisit(constDeclaration); base.PreVisit(constDeclaration);
(IdentifierSemanticToken token, ConstValue constValue) = constDeclaration.ConstValue; (IdentifierSemanticToken token, ConstValue constValue) = constDeclaration.ConstValue;

View File

@ -54,4 +54,14 @@ public class SymbolTableTests
Assert.True(table.TryGetSymbol("temperature", out Symbol? temp)); Assert.True(table.TryGetSymbol("temperature", out Symbol? temp));
Assert.Equal(PascalBasicType.Real, temp.SymbolType); Assert.Equal(PascalBasicType.Real, temp.SymbolType);
} }
[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}));
}
} }