src/lisp/lisp.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include "lisp/lisp.hpp"
00018 
00019 #include <stdio.h>
00020 
00021 namespace lisp {
00022 
00023 Lisp::Lisp(LispType newtype) :
00024   type(newtype),
00025   v()
00026 {
00027 }
00028 
00029 Lisp::~Lisp()
00030 {
00031   // resources should be on parser obstack, so no need to delete anything
00032 }
00033 
00034 const Lisp*
00035 Lisp::get_lisp(const char* name) const
00036 {
00037   for(const Lisp* p = this; p != 0; p = p->get_cdr()) {
00038     const Lisp* child = p->get_car();
00039     if(!child || child->get_type() != TYPE_CONS)
00040       continue;
00041     const Lisp* childname = child->get_car();
00042     if(!childname)
00043       continue;
00044     std::string childName;
00045     if(!childname->get(childName))
00046       continue;
00047     if(childName == name) {
00048       return child->get_cdr();
00049     }
00050   }
00051 
00052   return 0;
00053 }
00054 
00055 void
00056 Lisp::print(int indent) const
00057 {
00058   for(int i = 0; i < indent; ++i)
00059     printf(" ");
00060 
00061   if(type == TYPE_CONS) {
00062     printf("(\n");
00063     const Lisp* lisp = this;
00064     while(lisp) {
00065       if(lisp->v.cons.car)
00066         lisp->v.cons.car->print(indent + 1);
00067       lisp = lisp->v.cons.cdr;
00068     }
00069     for(int i = 0; i < indent; ++i)
00070       printf(" ");
00071     printf(")");
00072   }
00073   if(type == TYPE_STRING) {
00074     printf("'%s' ", v.string);
00075   }
00076   if(type == TYPE_INTEGER) {
00077     printf("%d", v.integer);
00078   }
00079   if(type == TYPE_REAL) {
00080     printf("%f", v.real);
00081   }
00082   if(type == TYPE_SYMBOL) {
00083     printf("%s ", v.string);
00084   }
00085   if(type == TYPE_BOOLEAN) {
00086     printf("%s ", v.boolean ? "true" : "false");
00087   }
00088   printf("\n");
00089 }
00090 
00091 } // end of namespace lisp
00092 
00093 /* EOF */

Generated on Mon Jun 9 03:38:18 2014 for SuperTux by  doxygen 1.5.1