Skip to content

Commit 2a40228

Browse files
committed
Fixed issue #42
1 parent 6083c06 commit 2a40228

File tree

7 files changed

+43
-0
lines changed

7 files changed

+43
-0
lines changed

compiler/ast.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3028,6 +3028,14 @@ class VarDef : public AST
30283028
{
30293029
for (char *id : ids)
30303030
{
3031+
std::string varName(id);
3032+
if (st.getCurrentScope().isThereNameInLocals(varName))
3033+
{
3034+
std::ostringstream oss;
3035+
oss << "Duplicate variable definition: variable \"" << varName << "\" is already defined in the current scope.";
3036+
semantic_error(line, column, oss.str());
3037+
}
3038+
30313039
STEntry *entry = new STEntry();
30323040
if (!type->getParenthesisList().empty())
30333041
{

compiler/symbol.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ class Scope {
165165
return locals;
166166
}
167167

168+
bool isThereNameInLocals(const std::string& name) const {
169+
return locals.find(name) != locals.end();
170+
}
171+
168172
STEntry* searchPreviousDeclaration(const std::string& name) const {
169173
if(locals.find(name) == locals.end())
170174
return nullptr;

llvm/ast.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,6 +2983,14 @@ class VarDef : public AST
29832983
{
29842984
for (char *id : ids)
29852985
{
2986+
std::string varName(id);
2987+
if (st.getCurrentScope().isThereNameInLocals(varName))
2988+
{
2989+
std::ostringstream oss;
2990+
oss << "Duplicate variable definition: variable \"" << varName << "\" is already defined in the current scope.";
2991+
semantic_error(line, column, oss.str());
2992+
}
2993+
29862994
STEntry *entry = new STEntry();
29872995
if (!type->getParenthesisList().empty())
29882996
{

llvm/symbol.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ class Scope {
131131
locals[name] = entry;
132132
}
133133

134+
bool isThereNameInLocals(const std::string& name) const {
135+
return locals.find(name) != locals.end();
136+
}
137+
134138
STEntry* lookup(const std::string& name) {
135139
if (locals.find(name) == locals.end()) {
136140
return nullptr;

semantic/ast.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,14 @@ class VarDef : public AST
16841684
{
16851685
for (char *id : ids)
16861686
{
1687+
std::string varName(id);
1688+
if (st.getCurrentScope().isThereNameInLocals(varName))
1689+
{
1690+
std::ostringstream oss;
1691+
oss << "Duplicate variable definition: variable \"" << varName << "\" is already defined in the current scope.";
1692+
semantic_error(line, column, oss.str());
1693+
}
1694+
16871695
STEntry *entry = new STEntry();
16881696
if (!type->getParenthesisList().empty())
16891697
{

semantic/symbol.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ class Scope {
131131
locals[name] = entry;
132132
}
133133

134+
bool isThereNameInLocals(const std::string& name) const {
135+
return locals.find(name) != locals.end();
136+
}
137+
134138
STEntry* lookup(const std::string& name) {
135139
if (locals.find(name) == locals.end()) {
136140
return nullptr;

testing/issues/dup_var.dana

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def main
2+
var a b a is int
3+
begin
4+
a := 1
5+
writeInteger: a
6+
writeString: "\nthis program is wrong!!!\n"
7+
end

0 commit comments

Comments
 (0)