File tree Expand file tree Collapse file tree 7 files changed +43
-0
lines changed
Expand file tree Collapse file tree 7 files changed +43
-0
lines changed Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments