[Avida-SVN] r1818 - in development: Avida.xcodeproj source/script source/tools

brysonda at myxo.css.msu.edu brysonda at myxo.css.msu.edu
Wed Jul 18 11:59:29 PDT 2007


Author: brysonda
Date: 2007-07-18 14:59:29 -0400 (Wed, 18 Jul 2007)
New Revision: 1818

Added:
   development/source/tools/tAutoRelease.h
Modified:
   development/Avida.xcodeproj/project.pbxproj
   development/source/script/ASTree.h
   development/source/script/cASTDumpVisitor.cc
   development/source/script/cASTDumpVisitor.h
   development/source/script/cParser.cc
Log:
AS: WhileBlock nodes.

Add tAutoRelease, a lightweight pointer wrapper that will automatically delete its contents on replacement or destruction.  The Release method will destructively pass control of the pointer to the caller.

Modified: development/Avida.xcodeproj/project.pbxproj
===================================================================
--- development/Avida.xcodeproj/project.pbxproj	2007-07-18 15:08:57 UTC (rev 1817)
+++ development/Avida.xcodeproj/project.pbxproj	2007-07-18 18:59:29 UTC (rev 1818)
@@ -811,6 +811,7 @@
 		70F7DE76092967A8009E311D /* cGenotypeBatch.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = cGenotypeBatch.h; sourceTree = "<group>"; };
 		70F9FC0E0C469DC10083B788 /* cASTDumpVisitor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cASTDumpVisitor.h; sourceTree = "<group>"; };
 		70F9FC0F0C469DC10083B788 /* cASTDumpVisitor.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cASTDumpVisitor.cc; sourceTree = "<group>"; };
+		70F9FD990C4E89C40083B788 /* tAutoRelease.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tAutoRelease.h; sourceTree = "<group>"; };
 		70FB86A908BFAFEC00BDF589 /* CMakeLists.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = "<group>"; };
 		DCC30FCF0762539D008F7A48 /* Doxyfile */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = Doxyfile; sourceTree = "<group>"; };
 		DCC3109C0762539E008F7A48 /* avida.cc */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; path = avida.cc; sourceTree = "<group>"; };
@@ -1555,6 +1556,7 @@
 				70BCB21C0AB7ADA6003FF331 /* cArgContainer.cc */,
 				70BCB2470AB7B634003FF331 /* cArgSchema.h */,
 				703D4D6D0ABA374A0032C8A0 /* cArgSchema.cc */,
+				70F9FD990C4E89C40083B788 /* tAutoRelease.h */,
 			);
 			path = tools;
 			sourceTree = "<group>";

Modified: development/source/script/ASTree.h
===================================================================
--- development/source/script/ASTree.h	2007-07-18 15:08:57 UTC (rev 1817)
+++ development/source/script/ASTree.h	2007-07-18 18:59:29 UTC (rev 1818)
@@ -152,10 +152,15 @@
 class cASTWhileBlock : public cASTNode
 {
 private:
+  cASTNode* m_expr;
+  cASTNode* m_code;
   
 public:
-  cASTWhileBlock() { ; }
+  cASTWhileBlock(cASTNode* expr, cASTNode* code) : m_expr(expr), m_code(code) { ; }
   
+  inline cASTNode* GetCondition() { return m_expr; }
+  inline cASTNode* GetCode() { return m_code; }
+  
   void Accept(cASTVisitor& visitor);
 };
 

Modified: development/source/script/cASTDumpVisitor.cc
===================================================================
--- development/source/script/cASTDumpVisitor.cc	2007-07-18 15:08:57 UTC (rev 1817)
+++ development/source/script/cASTDumpVisitor.cc	2007-07-18 18:59:29 UTC (rev 1818)
@@ -29,6 +29,12 @@
 using namespace std;
 
 
+cASTDumpVisitor::cASTDumpVisitor() : m_depth(0)
+{
+  cout << "main:" << endl;
+  m_depth++;
+}
+
 inline void cASTDumpVisitor::indent()
 {
   for (int i = 0; i < m_depth; i++) cout << "  ";
@@ -110,15 +116,10 @@
 {
   tListIterator<cASTNode> it(node.Iterator());
   
-  indent();
-  cout << "stmtlist:" << endl;
-
-  m_depth++;
   cASTNode* stmt = NULL;
   while ((stmt = it.Next())) {
     stmt->Accept(*this);
   }
-  m_depth--;
 }
 
 
@@ -137,7 +138,25 @@
 
 void cASTDumpVisitor::visitWhileBlock(cASTWhileBlock& node)
 {
+  indent();
+  cout << "while:" << endl;
   
+  m_depth++;
+  indent();
+  cout << "condition:" << endl;
+  
+  m_depth++;
+  node.GetCondition()->Accept(*this);
+  m_depth--;
+  
+  indent();
+  cout << "do:" << endl;
+  
+  m_depth++;
+  node.GetCode()->Accept(*this);
+  m_depth--;
+  
+  m_depth--;
 }
 
 

Modified: development/source/script/cASTDumpVisitor.h
===================================================================
--- development/source/script/cASTDumpVisitor.h	2007-07-18 15:08:57 UTC (rev 1817)
+++ development/source/script/cASTDumpVisitor.h	2007-07-18 18:59:29 UTC (rev 1818)
@@ -36,7 +36,7 @@
   int m_depth;
   
 public:
-  cASTDumpVisitor() : m_depth(0) { ; }
+  cASTDumpVisitor();
   
   void visitAssignment(cASTAssignment&);
   

Modified: development/source/script/cParser.cc
===================================================================
--- development/source/script/cParser.cc	2007-07-18 15:08:57 UTC (rev 1817)
+++ development/source/script/cParser.cc	2007-07-18 18:59:29 UTC (rev 1818)
@@ -26,6 +26,7 @@
 
 #include "AvidaScript.h"
 #include "cFile.h"
+#include "tAutoRelease.h"
 
 /*
  The following represents the grammar for AvidaScript in BNF, adjusted so that it is compatible with recursive descent
@@ -316,6 +317,8 @@
   PARSE_TRACE("parseAssignment");
   cASTAssignment* an = new cASTAssignment(currentText());
   
+  nextToken(); // consume '='
+
   nextToken();
   cASTNode* expr = parseExpression();
   an->SetExpression(expr);
@@ -767,7 +770,7 @@
     return NULL;
   }
   
-  cASTNode* args = NULL;
+  tAutoRelease<cASTNode> args;
   if (nextToken() != TOKEN(PREC_CLOSE)) {
     if (declare) {
       args = parseVarDeclareList();
@@ -783,7 +786,7 @@
   
   nextToken();
   
-  return new cASTFunctionDefinition(type, name, args);
+  return new cASTFunctionDefinition(type, name, args.Release());
 }
 
 cASTNode* cParser::parseIDStatement()
@@ -1046,29 +1049,30 @@
 cASTNode* cParser::parseWhileStatement()
 {
   PARSE_TRACE("parseWhileStatement");
-  cASTNode* ws = NULL;
   
   if (nextToken() != TOKEN(PREC_OPEN)) {
     PARSE_UNEXPECT();
-    return ws;
+    return NULL;
   }
   
   nextToken();
-  parseExpression();
+  tAutoRelease<cASTNode> cond(parseExpression());
   
   if (currentToken() != TOKEN(PREC_CLOSE)) {
     PARSE_UNEXPECT();
-    return ws;
+    return NULL;
   }
+  nextToken();
   
   bool loose = false;
-  parseCodeBlock(loose);
+  tAutoRelease<cASTNode> code(parseCodeBlock(loose));
   if (!loose && currentToken() != TOKEN(CMD_ENDWHILE)) {
     PARSE_UNEXPECT();
-    return ws;
+    return NULL;
   }
+  nextToken();
   
-  return ws;
+  return new cASTWhileBlock(cond.Release(), code.Release());
 }
 
 
@@ -1110,6 +1114,7 @@
       break;
     case AS_PARSE_ERR_NULL_EXPR:
       std::cerr << "expected expression, found '" << currentText() << "'" << ERR_ENDL;
+      break;
     case AS_PARSE_ERR_EOF:
       if (!m_err_eof) {
         std::cerr << "unexpected end of file" << ERR_ENDL;

Added: development/source/tools/tAutoRelease.h
===================================================================
--- development/source/tools/tAutoRelease.h	                        (rev 0)
+++ development/source/tools/tAutoRelease.h	2007-07-18 18:59:29 UTC (rev 1818)
@@ -0,0 +1,51 @@
+/*
+ *  tAutoRelease.h
+ *  Avida
+ *
+ *  Created by David on 7/18/07.
+ *  Copyright 2007 Michigan State University. All rights reserved.
+ *
+ *
+ *  This program is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public License
+ *  as published by the Free Software Foundation; version 2
+ *  of the License.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#ifndef tAutoRelease_h
+#define tAutoRelease_h
+
+//! A lightweight pointer wrapper class that automatically deletes the contents on destruction.
+template <class T> class tAutoRelease
+{
+private:
+  T* m_value;
+  
+  tAutoRelease(const tAutoRelease&); // @not_implemented
+  tAutoRelease& operator=(const tAutoRelease&); // @not_implemented
+
+  
+public:
+  explicit inline tAutoRelease() : m_value(NULL) { ; }
+  explicit inline tAutoRelease(T* value) : m_value(value) { ; }
+  inline ~tAutoRelease() { delete m_value; }
+  
+  inline void Set(T* value) { delete m_value; m_value = value; }
+  inline tAutoRelease<T>& operator=(T* value) { delete m_value; m_value = value; return *this; }
+  
+  //! Take control of the contents
+  inline T* Release() { T* value = m_value; m_value = NULL; return value; }
+};
+
+
+#endif




More information about the Avida-cvs mailing list