From 580f2d505a94fca7c841f605e5da074778b5e975 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Tue, 14 May 2024 14:10:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E5=AF=B9=E4=BA=8Econst=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=A3=80=E6=9F=A5=E9=94=99=E8=AF=AF=20(#85)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewed-on: https://git.rrricardo.top/PostGuard/Canon/pulls/85 --- Canon.Core/SemanticParser/CodeGeneratorVisitor.cs | 2 +- Canon.Core/SemanticParser/PascalType.cs | 2 +- Canon.Core/SemanticParser/SymbolTable.cs | 11 ++++++++++- Canon.Core/SemanticParser/TypeCheckVisitor.cs | 2 +- Canon.Tests/SemanticTests/SymbolTableTests.cs | 10 ++++++++++ 5 files changed, 23 insertions(+), 4 deletions(-) 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})); + } }