7f275d6a9f09a4f8121682b2d21c7cc5a9a954aa
[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 #include <stdio.h>
25
26 namespace lisp
27 {
28
29 Lisp::Lisp(LispType newtype)
30   : type(newtype)
31 {
32 }
33
34 Lisp::~Lisp()
35 {
36   // resources should be on parser obstack, so no need to delete anything
37 }
38
39 const Lisp*
40 Lisp::get_lisp(const char* name) const
41 {
42   for(const Lisp* p = this; p != 0; p = p->get_cdr()) {
43     const Lisp* child = p->get_car();
44     if(!child || child->get_type() != TYPE_CONS)
45       continue;
46     const Lisp* childname = child->get_car();
47     if(!childname)
48       continue;
49     std::string childName;
50     if(!childname->get(childName))
51       continue;
52     if(childName == name) {
53       return child->get_cdr();
54     }
55   }
56
57   return 0;
58 }
59
60 void
61 Lisp::print(int indent) const
62 {
63   for(int i = 0; i < indent; ++i)
64     printf(" ");
65
66   if(type == TYPE_CONS) {
67     printf("(\n");
68     const Lisp* lisp = this;
69     while(lisp) {
70       if(lisp->v.cons.car)
71         lisp->v.cons.car->print(indent + 1);
72       lisp = lisp->v.cons.cdr;
73     }
74     for(int i = 0; i < indent; ++i)
75       printf(" ");
76     printf(")");
77   }
78   if(type == TYPE_STRING) {
79     printf("'%s' ", v.string);
80   }
81   if(type == TYPE_INTEGER) {
82     printf("%d", v.integer);
83   }
84   if(type == TYPE_REAL) {
85     printf("%f", v.real);
86   }
87   if(type == TYPE_SYMBOL) {
88     printf("%s ", v.string);
89   }
90   if(type == TYPE_BOOLEAN) {
91     printf("%s ", v.boolean ? "true" : "false");
92   }
93   printf("\n");
94 }
95
96 } // end of namespace lisp