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