move over rewritten lispreader from tuxkart (with additional fixes), generalized...
[supertux.git] / lib / lisp / parser.cpp
1 //  $Id$
2 //
3 //  TuxKart - a fun racing game with go-kart
4 //  Copyright (C) 2004 Matthias Braun <matze@braunis.de>
5 //  code in this file based on lispreader from Mark Probst
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License
9 //  as published by the Free Software Foundation; either version 2
10 //  of the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 //  GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public License
18 //  along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 #include <config.h>
21
22 #include <sstream>
23 #include <stdexcept>
24 #include <fstream>
25
26 #include "parser.h"
27 #include "lisp.h"
28
29 namespace lisp
30 {
31
32 Parser::Parser()
33   : lexer(0)
34 {
35 }
36
37 Parser::~Parser()
38 {
39   delete lexer;
40 }
41
42 Lisp*
43 Parser::parse(const std::string& filename)
44 {
45   std::ifstream in(filename.c_str());
46   if(!in.good()) {
47     std::stringstream msg;
48     msg << "Parser problem: Couldn't open file '" << filename << "'.";
49     throw std::runtime_error(msg.str());
50   }
51   return parse(in);
52 }
53
54 Lisp*
55 Parser::parse(std::istream& stream)
56 {
57   delete lexer;
58   lexer = new Lexer(stream);
59
60   token = lexer->getNextToken();
61   Lisp* result = new Lisp(Lisp::TYPE_CONS);
62   result->v.cons.car = read();
63   result->v.cons.cdr = 0;
64   
65   delete lexer;
66   lexer = 0;
67
68   return result;    
69 }
70
71 Lisp*
72 Parser::read()
73 {
74   Lisp* result;
75   switch(token) {
76     case Lexer::TOKEN_EOF: {
77       std::stringstream msg;
78       msg << "Parse Error at line " << lexer->getLineNumber() << ": "
79         << "Unexpected EOF.";
80       throw std::runtime_error(msg.str());
81     }
82     case Lexer::TOKEN_CLOSE_PAREN: {
83       std::stringstream msg;
84       msg << "Parse Error at line " << lexer->getLineNumber() << ": "
85         << "Unexpected ')'.";
86       throw std::runtime_error(msg.str());
87     }
88     case Lexer::TOKEN_OPEN_PAREN: {
89       result = new Lisp(Lisp::TYPE_CONS);
90       
91       token = lexer->getNextToken();
92       if(token == Lexer::TOKEN_CLOSE_PAREN) {
93         result->v.cons.car = 0;
94         result->v.cons.cdr = 0;
95         break;
96       }
97
98       Lisp* cur = result;
99       do {
100         cur->v.cons.car = read();
101         if(token == Lexer::TOKEN_CLOSE_PAREN) {
102           cur->v.cons.cdr = 0;
103           break;
104         }
105         cur->v.cons.cdr = new Lisp(Lisp::TYPE_CONS);
106         cur = cur->v.cons.cdr;
107       } while(1);
108
109       break;
110     }
111     case Lexer::TOKEN_SYMBOL: {
112       result = new Lisp(Lisp::TYPE_SYMBOL);
113       size_t len = strlen(lexer->getString()) + 1;
114       result->v.string = new char[len];
115       memcpy(result->v.string, lexer->getString(), len);
116       break;
117     }
118     case Lexer::TOKEN_STRING: {
119       result = new Lisp(Lisp::TYPE_STRING);
120       size_t len = strlen(lexer->getString()) + 1;
121       result->v.string = new char[len];
122       memcpy(result->v.string, lexer->getString(), len);
123       break;
124     }
125     case Lexer::TOKEN_INTEGER:
126       result = new Lisp(Lisp::TYPE_INTEGER);
127       sscanf(lexer->getString(), "%d", &result->v.integer);
128       break;
129     case Lexer::TOKEN_REAL:
130       result = new Lisp(Lisp::TYPE_REAL);
131       sscanf(lexer->getString(), "%f", &result->v.real);
132       break;
133     case Lexer::TOKEN_TRUE:
134       result = new Lisp(Lisp::TYPE_BOOLEAN);
135       result->v.boolean = true;
136       break;
137     case Lexer::TOKEN_FALSE:
138       result = new Lisp(Lisp::TYPE_BOOLEAN);
139       result->v.boolean = false;
140       break;
141
142     default:
143       // this should never happen
144       assert(false);
145   }
146
147   token = lexer->getNextToken();
148   return result;
149 }
150
151 } // end of namespace lisp