diff --git a/Canon.Core/SemanticParser/CodeGeneratorVisitor.cs b/Canon.Core/SemanticParser/CodeGeneratorVisitor.cs index 3e58f1c..0285373 100644 --- a/Canon.Core/SemanticParser/CodeGeneratorVisitor.cs +++ b/Canon.Core/SemanticParser/CodeGeneratorVisitor.cs @@ -31,7 +31,7 @@ public class CodeGeneratorVisitor(ICompilerLogger? logger = null) : TypeCheckVis public override void PostVisit(ConstDeclaration constDeclaration) { - base.PreVisit(constDeclaration); + base.PostVisit(constDeclaration); (IdentifierSemanticToken token, ConstValue constValue) = constDeclaration.ConstValue; diff --git a/Canon.Core/SemanticParser/PascalType.cs b/Canon.Core/SemanticParser/PascalType.cs index 19673d7..dc7e3e5 100644 --- a/Canon.Core/SemanticParser/PascalType.cs +++ b/Canon.Core/SemanticParser/PascalType.cs @@ -31,7 +31,7 @@ public abstract class PascalType : IEquatable return false; } - return IsReference == other.IsReference; + return true; } public T Convert() where T : PascalType diff --git a/Canon.Core/SemanticParser/SymbolTable.cs b/Canon.Core/SemanticParser/SymbolTable.cs index cb38e95..eb80130 100644 --- a/Canon.Core/SemanticParser/SymbolTable.cs +++ b/Canon.Core/SemanticParser/SymbolTable.cs @@ -44,7 +44,16 @@ public class SymbolTable /// /// 欲添加的符号 /// 是否添加成功 - 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; + } /// /// 尝试从符号表极其父符号表查找符号 diff --git a/Canon.Core/SemanticParser/TypeCheckVisitor.cs b/Canon.Core/SemanticParser/TypeCheckVisitor.cs index 4770f41..bd21320 100644 --- a/Canon.Core/SemanticParser/TypeCheckVisitor.cs +++ b/Canon.Core/SemanticParser/TypeCheckVisitor.cs @@ -22,7 +22,7 @@ public class TypeCheckVisitor(ICompilerLogger? logger = null) : SyntaxNodeVisito public override void PreVisit(ConstDeclaration constDeclaration) { - base.PostVisit(constDeclaration); + base.PreVisit(constDeclaration); (IdentifierSemanticToken token, ConstValue constValue) = constDeclaration.ConstValue; diff --git a/Canon.Tests/SemanticTests/SymbolTableTests.cs b/Canon.Tests/SemanticTests/SymbolTableTests.cs index af8ab8b..e89038d 100644 --- a/Canon.Tests/SemanticTests/SymbolTableTests.cs +++ b/Canon.Tests/SemanticTests/SymbolTableTests.cs @@ -54,4 +54,14 @@ public class SymbolTableTests Assert.True(table.TryGetSymbol("temperature", out Symbol? temp)); 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})); + } }