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
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.
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.
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.
43 Parser::parse(const std::string& filename)
45 std::ifstream in(filename.c_str());
47 std::stringstream msg;
48 msg << "Parser problem: Couldn't open file '" << filename << "'.";
49 throw std::runtime_error(msg.str());
55 Parser::parse(std::istream& stream)
58 lexer = new Lexer(stream);
60 token = lexer->getNextToken();
61 Lisp* result = new Lisp(Lisp::TYPE_CONS);
62 result->v.cons.car = read();
63 result->v.cons.cdr = 0;
76 case Lexer::TOKEN_EOF: {
77 std::stringstream msg;
78 msg << "Parse Error at line " << lexer->getLineNumber() << ": "
80 throw std::runtime_error(msg.str());
82 case Lexer::TOKEN_CLOSE_PAREN: {
83 std::stringstream msg;
84 msg << "Parse Error at line " << lexer->getLineNumber() << ": "
86 throw std::runtime_error(msg.str());
88 case Lexer::TOKEN_OPEN_PAREN: {
89 result = new Lisp(Lisp::TYPE_CONS);
91 token = lexer->getNextToken();
92 if(token == Lexer::TOKEN_CLOSE_PAREN) {
93 result->v.cons.car = 0;
94 result->v.cons.cdr = 0;
100 cur->v.cons.car = read();
101 if(token == Lexer::TOKEN_CLOSE_PAREN) {
105 cur->v.cons.cdr = new Lisp(Lisp::TYPE_CONS);
106 cur = cur->v.cons.cdr;
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);
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);
125 case Lexer::TOKEN_INTEGER:
126 result = new Lisp(Lisp::TYPE_INTEGER);
127 sscanf(lexer->getString(), "%d", &result->v.integer);
129 case Lexer::TOKEN_REAL:
130 result = new Lisp(Lisp::TYPE_REAL);
131 sscanf(lexer->getString(), "%f", &result->v.real);
133 case Lexer::TOKEN_TRUE:
134 result = new Lisp(Lisp::TYPE_BOOLEAN);
135 result->v.boolean = true;
137 case Lexer::TOKEN_FALSE:
138 result = new Lisp(Lisp::TYPE_BOOLEAN);
139 result->v.boolean = false;
143 // this should never happen
147 token = lexer->getNextToken();
151 } // end of namespace lisp