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