Back to index

supertuxkart  0.5+dfsg1
lisp.cpp
Go to the documentation of this file.
00001 //  $Id: lisp.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  TuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de>
00005 //  code in this file based on lispreader from Mark Probst
00006 //
00007 //  This program is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU General Public License
00009 //  as published by the Free Software Foundation; either version 2
00010 //  of the License, or (at your option) any later version.
00011 //
00012 //  This program is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU General Public License
00018 //  along with this program; if not, write to the Free Software
00019 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 #include "lisp.hpp"
00021 
00022 namespace lisp
00023 {
00024 
00025     Lisp::Lisp(LispType newtype)
00026             : m_type(newtype)
00027     {}
00028 
00029 //-----------------------------------------------------------------------------
00030 
00031     Lisp::~Lisp()
00032     {
00033         if(m_type == TYPE_SYMBOL || m_type == TYPE_STRING)
00034             delete[] m_v.m_string;
00035         if(m_type == TYPE_CONS)
00036         {
00037             delete m_v.m_cons.m_cdr;
00038             delete m_v.m_cons.m_car;
00039         }
00040     }
00041 
00042 //-----------------------------------------------------------------------------
00043 
00044     const Lisp*
00045     Lisp::getLisp(const char* name) const
00046     {
00047         const Lisp* P;
00048 
00049         for(P = getCdr(); P != 0; P = P->getCdr())
00050         {
00051             const Lisp* CHILD = P->getCar();
00052             // Also ignore if the child is not a CONS type, i.e.
00053             // a TYPE_INTEGER is found, for which car is not defined!
00054             if(!CHILD || CHILD->m_type!=TYPE_CONS) 
00055                 continue;
00056             if(!CHILD->getCar())
00057                 continue;
00058             std::string CHILDName;
00059             if(!CHILD->getCar()->get(CHILDName))
00060                 continue;
00061             if(CHILDName == name)
00062                 return CHILD;
00063         }
00064 
00065         return 0;
00066     }
00067 
00068 //-----------------------------------------------------------------------------
00069 
00070     //FIXME: is the boolean handled by this function? should the argument be
00071     //removed?
00072     void
00073     Lisp::print(int ) const
00074     {
00075         if(m_type == TYPE_CONS)
00076         {
00077             printf("(");
00078             if(m_v.m_cons.m_car)
00079                 m_v.m_cons.m_car->print();
00080             if(m_v.m_cons.m_cdr)
00081             {
00082                 printf(",");
00083                 if(m_v.m_cons.m_cdr)
00084                     m_v.m_cons.m_cdr->print();
00085             }
00086             printf(")");
00087         }
00088         if(m_type == TYPE_STRING)
00089         {
00090             printf("'%s' ", m_v.m_string);
00091         }
00092         if(m_type == TYPE_INTEGER)
00093         {
00094             printf("%d", m_v.m_integer);
00095         }
00096         if(m_type == TYPE_REAL)
00097         {
00098             printf("%f", m_v.m_real);
00099         }
00100         if(m_type == TYPE_SYMBOL)
00101         {
00102             printf("%s ", m_v.m_string);
00103         }
00104     }
00105 
00106 } // end of namespace lisp