f5d88644799f7400a6c028bc119590c337fc13bc
[supertux.git] / src / lisp / lisp.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "lisp.hpp"
23
24 namespace lisp
25 {
26
27 Lisp::Lisp(LispType newtype)
28   : type(newtype)
29 {
30 }
31
32 Lisp::~Lisp()
33 {
34   // resources should be on parser obstack, so no need to delete anything
35 }
36
37 const Lisp*
38 Lisp::get_lisp(const char* name) const
39 {
40   for(const Lisp* p = this; p != 0; p = p->get_cdr()) {
41     const Lisp* child = p->get_car();
42     if(!child || child->get_type() != TYPE_CONS)
43       continue;
44     const Lisp* childname = child->get_car();
45     if(!childname)
46       continue;
47     std::string childName;
48     if(!childname->get(childName))
49       continue;
50     if(childName == name) {
51       return child->get_cdr();
52     }
53   }
54
55   return 0;
56 }
57
58 void
59 Lisp::print(int indent) const
60 {
61   for(int i = 0; i < indent; ++i)
62     printf(" ");
63
64   if(type == TYPE_CONS) {
65     printf("(\n");
66     const Lisp* lisp = this;
67     while(lisp) {
68       if(lisp->v.cons.car)
69         lisp->v.cons.car->print(indent + 1);
70       lisp = lisp->v.cons.cdr;
71     }
72     for(int i = 0; i < indent; ++i)
73       printf(" ");
74     printf(")");
75   }
76   if(type == TYPE_STRING) {
77     printf("'%s' ", v.string);
78   }
79   if(type == TYPE_INTEGER) {
80     printf("%d", v.integer);
81   }
82   if(type == TYPE_REAL) {
83     printf("%f", v.real);
84   }
85   if(type == TYPE_SYMBOL) {
86     printf("%s ", v.string);
87   }
88   if(type == TYPE_BOOLEAN) {
89     printf("%s ", v.boolean ? "true" : "false");
90   }
91   printf("\n");
92 }
93
94 } // end of namespace lisp