[Avida-SVN] r2422 - in development: source/script tests/_asl_good_012_multiple_level_scope/config

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Tue Mar 4 13:25:58 PST 2008


Author: brysonda
Date: 2008-03-04 16:25:57 -0500 (Tue, 04 Mar 2008)
New Revision: 2422

Modified:
   development/source/script/cSemanticASTVisitor.cc
   development/source/script/cSemanticASTVisitor.h
   development/source/script/cSymbolTable.cc
   development/source/script/cSymbolTable.h
   development/tests/_asl_good_012_multiple_level_scope/config/main.asl
Log:
AS:
Truly test embedded scope symbol shadowing and implement support for it.

Modified: development/source/script/cSemanticASTVisitor.cc
===================================================================
--- development/source/script/cSemanticASTVisitor.cc	2008-03-04 20:45:18 UTC (rev 2421)
+++ development/source/script/cSemanticASTVisitor.cc	2008-03-04 21:25:57 UTC (rev 2422)
@@ -72,7 +72,7 @@
 
 cSemanticASTVisitor::cSemanticASTVisitor(cASLibrary* lib, cSymbolTable* global_symtbl, cASTNode* main)
   : m_library(lib), m_global_symtbl(global_symtbl), m_parent_scope(global_symtbl), m_fun_id(0), m_cur_symtbl(global_symtbl)
-  , m_success(true), m_fun_def(false)
+  , m_success(true), m_fun_def(false), m_top_level(true)
 {
   // Add internal definition of the global function
   int fun_id = -1;
@@ -106,6 +106,14 @@
 {
   tListIterator<cASTNode> it = node.Iterator();
   
+  bool should_pop = false;
+  if (m_top_level) {
+    m_top_level = false;
+  } else {
+    m_cur_symtbl->PushScope();
+    should_pop = true;
+  }
+  
   bool has_return = false;
   bool warn_unreach = false;
   cASTNode* stmt = NULL;
@@ -118,6 +126,13 @@
       warn_unreach = true;
     }
   }
+  
+  if (should_pop) {
+    // Check all functions in the current scope level and make sure they have been defined
+    cSymbolTable::cFunctionIterator fit = m_cur_symtbl->ActiveFunctionIterator();
+    while (fit.Next()) if (!fit.HasCode()) SEMANTIC_ERROR(FUNCTION_UNDEFINED, (const char*)fit.GetName());
+    m_cur_symtbl->PopScope();    
+  }
 }
 
 
@@ -133,6 +148,7 @@
   node.GetVariable()->Accept(*this);
   
   // Check the code
+  m_top_level = true;
   node.GetCode()->Accept(*this);
   
   // Check all functions in the current scope level and make sure they have been defined
@@ -148,14 +164,7 @@
   node.GetCondition()->Accept(*this);
   checkCast(node.GetCondition()->GetType(), TYPE(BOOL));
   
-  m_cur_symtbl->PushScope();
-  
   node.GetCode()->Accept(*this);
-
-  // Check all functions in the current scope level and make sure they have been defined
-  cSymbolTable::cFunctionIterator fit = m_cur_symtbl->ActiveFunctionIterator();
-  while (fit.Next()) if (!fit.HasCode()) SEMANTIC_ERROR(FUNCTION_UNDEFINED, (const char*)fit.GetName());
-  m_cur_symtbl->PopScope();
   
   // Check all elseif blocks
   tListIterator<cASTIfBlock::cElseIf> it = node.ElseIfIterator();
@@ -163,28 +172,11 @@
   while ((ei = it.Next())) {
     ei->GetCondition()->Accept(*this);
     checkCast(ei->GetCondition()->GetType(), TYPE(BOOL));
-    
-    m_cur_symtbl->PushScope();
-    
     ei->GetCode()->Accept(*this);
-
-    // Check all functions in the current scope level and make sure they have been defined
-    cSymbolTable::cFunctionIterator fit = m_cur_symtbl->ActiveFunctionIterator();
-    while (fit.Next()) if (!fit.HasCode()) SEMANTIC_ERROR(FUNCTION_UNDEFINED, (const char*)fit.GetName());
-    m_cur_symtbl->PopScope();
   }
   
   // Check else block if there is one
-  if (node.GetElseCode()) {
-    m_cur_symtbl->PushScope();
-    
-    node.GetElseCode()->Accept(*this);
-
-    // Check all functions in the current scope level and make sure they have been defined
-    cSymbolTable::cFunctionIterator fit = m_cur_symtbl->ActiveFunctionIterator();
-    while (fit.Next()) if (!fit.HasCode()) SEMANTIC_ERROR(FUNCTION_UNDEFINED, (const char*)fit.GetName());
-    m_cur_symtbl->PopScope();
-  }
+  if (node.GetElseCode()) node.GetElseCode()->Accept(*this);
 }
 
 
@@ -192,15 +184,7 @@
 {
   node.GetCondition()->Accept(*this);
   checkCast(node.GetCondition()->GetType(), TYPE(BOOL));
-
-  m_cur_symtbl->PushScope();
-  
   node.GetCode()->Accept(*this);
-
-  // Check all functions in the current scope level and make sure they have been defined
-  cSymbolTable::cFunctionIterator fit = m_cur_symtbl->ActiveFunctionIterator();
-  while (fit.Next()) if (!fit.HasCode()) SEMANTIC_ERROR(FUNCTION_UNDEFINED, (const char*)fit.GetName());
-  m_cur_symtbl->PopScope();
 }
 
 
@@ -273,6 +257,7 @@
   // If this is the definition process the code
   if (node.GetCode()) {
     if (!m_parent_scope->GetFunctionDefinition(fun_id)) {
+      m_top_level = true;
       node.GetCode()->Accept(*this);
       
       if (node.GetType() != TYPE(VOID) && !m_cur_symtbl->ScopeHasReturn()) SEMANTIC_WARNING(NO_RETURN);

Modified: development/source/script/cSemanticASTVisitor.h
===================================================================
--- development/source/script/cSemanticASTVisitor.h	2008-03-04 20:45:18 UTC (rev 2421)
+++ development/source/script/cSemanticASTVisitor.h	2008-03-04 21:25:57 UTC (rev 2422)
@@ -58,6 +58,7 @@
   
   bool m_success;
   bool m_fun_def;
+  bool m_top_level;
 
   
   cSemanticASTVisitor(); // @not_implemented

Modified: development/source/script/cSymbolTable.cc
===================================================================
--- development/source/script/cSymbolTable.cc	2008-03-04 20:45:18 UTC (rev 2421)
+++ development/source/script/cSymbolTable.cc	2008-03-04 21:25:57 UTC (rev 2422)
@@ -33,8 +33,8 @@
 
 bool cSymbolTable::AddVariable(const cString& name, ASType_t type, int& var_id)
 {
+  bool found = LookupVariable(name, var_id);
   int shadow = var_id;
-  bool found = LookupVariable(name, var_id);
   if (found && m_sym_tbl[var_id]->scope == m_scope && !m_sym_tbl[var_id]->deactivate) return false;
   
   var_id = m_sym_tbl.GetSize();

Modified: development/source/script/cSymbolTable.h
===================================================================
--- development/source/script/cSymbolTable.h	2008-03-04 20:45:18 UTC (rev 2421)
+++ development/source/script/cSymbolTable.h	2008-03-04 21:25:57 UTC (rev 2422)
@@ -45,7 +45,7 @@
 
     int scope;
     int shadow;
-    bool deactivate;
+    int deactivate;
     
     sSymbolEntry(const cString& in_name, ASType_t in_type, int in_scope)
       : name(in_name), type(in_type), scope(in_scope), shadow(-1), deactivate(0) { ; }

Modified: development/tests/_asl_good_012_multiple_level_scope/config/main.asl
===================================================================
--- development/tests/_asl_good_012_multiple_level_scope/config/main.asl	2008-03-04 20:45:18 UTC (rev 2421)
+++ development/tests/_asl_good_012_multiple_level_scope/config/main.asl	2008-03-04 21:25:57 UTC (rev 2422)
@@ -5,6 +5,7 @@
 		int z = 12 - x;
 		{
 			int x = 1;
+			int z = x + 1;
 		}
 		x = x - 1;
 	}




More information about the Avida-cvs mailing list