Removed the LISP directory, which wasn't used for quite some time now anyway

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@5256 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria 2010-04-24 19:11:12 +00:00
parent c6e5fe1e67
commit c7f14ffbe0
9 changed files with 0 additions and 1258 deletions

View File

@ -173,10 +173,6 @@
9551C8B10FC1B72500DB481B /* kart_properties.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C2B11D0F296545000D3E5D /* kart_properties.cpp */; };
9551C8B20FC1B72500DB481B /* kart_properties_manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C2B11F0F296545000D3E5D /* kart_properties_manager.cpp */; };
9551C8B30FC1B72500DB481B /* moveable.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C2B1210F296545000D3E5D /* moveable.cpp */; };
9551C8B50FC1B72500DB481B /* lexer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C2B12A0F296545000D3E5D /* lexer.cpp */; };
9551C8B60FC1B72500DB481B /* lisp.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C2B12C0F296545000D3E5D /* lisp.cpp */; };
9551C8B70FC1B72500DB481B /* parser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C2B12E0F296545000D3E5D /* parser.cpp */; };
9551C8B80FC1B72500DB481B /* writer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C2B1300F296545000D3E5D /* writer.cpp */; };
9551C8B90FC1B72500DB481B /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C2B1360F296545000D3E5D /* main.cpp */; };
9551C8BA0FC1B72500DB481B /* main_loop.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C2B1380F296545000D3E5D /* main_loop.cpp */; };
9551C8BE0FC1B72500DB481B /* follow_the_leader.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95C2B14A0F296545000D3E5D /* follow_the_leader.cpp */; };
@ -777,14 +773,6 @@
95C2B1200F296545000D3E5D /* kart_properties_manager.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = kart_properties_manager.hpp; path = ../../karts/kart_properties_manager.hpp; sourceTree = SOURCE_ROOT; };
95C2B1210F296545000D3E5D /* moveable.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = moveable.cpp; path = ../../karts/moveable.cpp; sourceTree = SOURCE_ROOT; };
95C2B1220F296545000D3E5D /* moveable.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = moveable.hpp; path = ../../karts/moveable.hpp; sourceTree = SOURCE_ROOT; };
95C2B12A0F296545000D3E5D /* lexer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = lexer.cpp; path = ../../lisp/lexer.cpp; sourceTree = SOURCE_ROOT; };
95C2B12B0F296545000D3E5D /* lexer.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = lexer.hpp; path = ../../lisp/lexer.hpp; sourceTree = SOURCE_ROOT; };
95C2B12C0F296545000D3E5D /* lisp.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = lisp.cpp; path = ../../lisp/lisp.cpp; sourceTree = SOURCE_ROOT; };
95C2B12D0F296545000D3E5D /* lisp.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = lisp.hpp; path = ../../lisp/lisp.hpp; sourceTree = SOURCE_ROOT; };
95C2B12E0F296545000D3E5D /* parser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = parser.cpp; path = ../../lisp/parser.cpp; sourceTree = SOURCE_ROOT; };
95C2B12F0F296545000D3E5D /* parser.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = parser.hpp; path = ../../lisp/parser.hpp; sourceTree = SOURCE_ROOT; };
95C2B1300F296545000D3E5D /* writer.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = writer.cpp; path = ../../lisp/writer.cpp; sourceTree = SOURCE_ROOT; };
95C2B1310F296545000D3E5D /* writer.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = writer.hpp; path = ../../lisp/writer.hpp; sourceTree = SOURCE_ROOT; };
95C2B1360F296545000D3E5D /* main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = main.cpp; path = ../../main.cpp; sourceTree = SOURCE_ROOT; };
95C2B1380F296545000D3E5D /* main_loop.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = main_loop.cpp; path = ../../main_loop.cpp; sourceTree = SOURCE_ROOT; };
95C2B1390F296545000D3E5D /* main_loop.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; name = main_loop.hpp; path = ../../main_loop.hpp; sourceTree = SOURCE_ROOT; };
@ -1354,7 +1342,6 @@
95C2B0F50F296545000D3E5D /* items */,
95A118280F77EA3100B18B3D /* input */,
95C2B1160F296545000D3E5D /* karts */,
95C2B1290F296545000D3E5D /* lisp */,
95C2B1470F296545000D3E5D /* modes */,
95C2B1590F296545000D3E5D /* network */,
95C2B17F0F296545000D3E5D /* physics */,
@ -1832,22 +1819,6 @@
path = ../../karts;
sourceTree = SOURCE_ROOT;
};
95C2B1290F296545000D3E5D /* lisp */ = {
isa = PBXGroup;
children = (
95C2B12A0F296545000D3E5D /* lexer.cpp */,
95C2B12B0F296545000D3E5D /* lexer.hpp */,
95C2B12C0F296545000D3E5D /* lisp.cpp */,
95C2B12D0F296545000D3E5D /* lisp.hpp */,
95C2B12E0F296545000D3E5D /* parser.cpp */,
95C2B12F0F296545000D3E5D /* parser.hpp */,
95C2B1300F296545000D3E5D /* writer.cpp */,
95C2B1310F296545000D3E5D /* writer.hpp */,
);
name = lisp;
path = ../../lisp;
sourceTree = SOURCE_ROOT;
};
95C2B1470F296545000D3E5D /* modes */ = {
isa = PBXGroup;
children = (
@ -2425,10 +2396,6 @@
9551C8B10FC1B72500DB481B /* kart_properties.cpp in Sources */,
9551C8B20FC1B72500DB481B /* kart_properties_manager.cpp in Sources */,
9551C8B30FC1B72500DB481B /* moveable.cpp in Sources */,
9551C8B50FC1B72500DB481B /* lexer.cpp in Sources */,
9551C8B60FC1B72500DB481B /* lisp.cpp in Sources */,
9551C8B70FC1B72500DB481B /* parser.cpp in Sources */,
9551C8B80FC1B72500DB481B /* writer.cpp in Sources */,
9551C8B90FC1B72500DB481B /* main.cpp in Sources */,
9551C8BA0FC1B72500DB481B /* main_loop.cpp in Sources */,
9551C8BE0FC1B72500DB481B /* follow_the_leader.cpp in Sources */,

View File

@ -1,267 +0,0 @@
// $Id$
//
// TuxKart - a fun racing game with go-kart
// Copyright (C) 2004 Matthias Braun <matze@braunis.de>
// code in this file based on lispreader from Mark Probst
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <sstream>
#include <stdexcept>
#include <cstdio>
#include <cstring>
#include "lisp/lexer.hpp"
namespace lisp
{
class EOFException
{}
;
Lexer::Lexer(std::istream& newstream)
: m_stream(newstream), m_is_eof(false)
{
try
{
// trigger a refill of the m_buffer
m_c = 0;
m_buffer_end = m_c + 1;
nextChar();
}
catch(EOFException& e)
{(void)e; // avoid 'unreferenced local variable' warning
}
}
//-----------------------------------------------------------------------------
Lexer::~Lexer()
{}
//-----------------------------------------------------------------------------
void
Lexer::nextChar()
{
++m_c;
if(m_c >= m_buffer_end)
{
if(m_is_eof)
throw EOFException();
m_stream.read(m_buffer, LEXER_BUFFER_SIZE);
std::streamsize n = m_stream.gcount();
m_c = m_buffer;
m_buffer_end = m_buffer + n;
// the following is a hack that appends an additional ' ' at the end of
// the file to avoid problems when parsing symbols/elements and a sudden
// EOF. This is faster than relying on unget and IMO also nicer.
if(n < LEXER_BUFFER_SIZE || n == 0)
{
*m_buffer_end = ' ';
++m_buffer_end;
m_is_eof = true;
}
}
}
//-----------------------------------------------------------------------------
Lexer::TokenType
Lexer::getNextToken()
{
static const char* delims = "\"();";
try
{
while(isspace(*m_c))
{
nextChar();
if(*m_c == '\n')
++m_line_number;
};
m_token_length = 0;
switch(*m_c)
{
case ';': // comment
while(true)
{
nextChar();
if(*m_c == '\n')
{
++m_line_number;
break;
}
}
return getNextToken(); // and again
case '(':
nextChar();
return TOKEN_OPEN_PAREN;
case ')':
nextChar();
return TOKEN_CLOSE_PAREN;
case '"': // string
{
const int STARTLINE = m_line_number;
try
{
while(1)
{
nextChar();
if(*m_c == '"')
break;
if(*m_c == '\\')
{
nextChar();
switch(*m_c)
{
case 'n':
*m_c = '\n';
break;
case 't':
*m_c = '\t';
break;
}
}
if(m_token_length < MAX_TOKEN_LENGTH)
m_token_string[m_token_length++] = *m_c;
}
m_token_string[m_token_length] = 0;
}
catch(EOFException& )
{
std::ostringstream msg;
msg << "Parse error in line " << STARTLINE
<< ": EOF while parsing string.";
throw std::runtime_error(msg.str());
}
nextChar();
return TOKEN_STRING;
}
case '#': // constant
try
{
nextChar();
while(isalnum(*m_c) || *m_c == '_')
{
if(m_token_length < MAX_TOKEN_LENGTH)
m_token_string[m_token_length++] = *m_c;
nextChar();
}
m_token_string[m_token_length] = 0;
}
catch(EOFException& )
{
std::ostringstream msg;
msg << "Parse Error in line " << m_line_number
<< ": EOF while parsing constant.";
throw std::runtime_error(msg.str());
}
if(strcmp(m_token_string, "t") == 0)
return TOKEN_TRUE;
if(strcmp(m_token_string, "f") == 0)
return TOKEN_FALSE;
// this would be the place to add more sophisticated handling of
// constants
{
std::ostringstream msg;
msg << "Parse Error in line " << m_line_number
<< ": Unknown constant '" << m_token_string<<"'.";
throw std::runtime_error(msg.str());
}
case '_': // can be begin translation
try
{
nextChar();
if(*m_c == '(')
{
nextChar();
return TOKEN_TRANSLATION;
}
m_token_string[m_token_length++] = '_';
// Fall through to symbol handling
}
catch(EOFException& )
{
}
default:
if(isdigit(*m_c) || *m_c == '-')
{
bool have_nondigits = false;
bool have_digits = false;
int have_floating_point = 0;
do
{
if(isdigit(*m_c))
have_digits = true;
else if(*m_c == '.')
++have_floating_point;
else if(isalnum(*m_c) || *m_c == '_')
have_nondigits = true;
if(m_token_length < MAX_TOKEN_LENGTH)
m_token_string[m_token_length++] = *m_c;
nextChar();
}
while(!isspace(*m_c) && !strchr(delims, *m_c));
m_token_string[m_token_length] = 0;
// no nextChar
if(have_nondigits || !have_digits || have_floating_point > 1)
return TOKEN_SYMBOL;
else if(have_floating_point == 1)
return TOKEN_REAL;
else
return TOKEN_INTEGER;
}
else
{
do
{
if(m_token_length < MAX_TOKEN_LENGTH)
m_token_string[m_token_length++] = *m_c;
nextChar();
}
while(!isspace(*m_c) && !strchr(delims, *m_c));
m_token_string[m_token_length] = 0;
// no nextChar
return TOKEN_SYMBOL;
}
}
}
catch(EOFException& )
{
return TOKEN_EOF;
}
}
} // end of namespace lisp

View File

@ -1,76 +0,0 @@
// $Id$
//
// TuxKart - a fun racing game with go-kart
// Copyright (C) 2004 Matthias Braun <matze@braunis.de>
// code in this file based on lispreader from Mark Probst
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef __LISPLEXER_H__
#define __LISPLEXER_H__
namespace lisp
{
class Lexer
{
public:
enum TokenType
{
TOKEN_EOF,
TOKEN_OPEN_PAREN,
TOKEN_CLOSE_PAREN,
TOKEN_TRANSLATION,
TOKEN_SYMBOL,
TOKEN_STRING,
TOKEN_INTEGER,
TOKEN_REAL,
TOKEN_TRUE,
TOKEN_FALSE
};
Lexer(std::istream& stream);
~Lexer();
TokenType getNextToken();
const char* getString() const
{ return m_token_string; }
int getLineNumber() const
{ return m_line_number; }
private:
enum
{
MAX_TOKEN_LENGTH = 4096,
LEXER_BUFFER_SIZE = 1024
};
inline void nextChar();
std::istream& m_stream;
bool m_is_eof;
int m_line_number;
char m_buffer[LEXER_BUFFER_SIZE+1];
char* m_buffer_end;
char* m_c;
char m_token_string[MAX_TOKEN_LENGTH + 1];
int m_token_length;
};
} // end of namespace lisp
#endif

View File

@ -1,106 +0,0 @@
// $Id$
//
// TuxKart - a fun racing game with go-kart
// Copyright (C) 2004 Matthias Braun <matze@braunis.de>
// code in this file based on lispreader from Mark Probst
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "lisp/lisp.hpp"
namespace lisp
{
Lisp::Lisp(LispType newtype)
: m_type(newtype)
{}
//-----------------------------------------------------------------------------
Lisp::~Lisp()
{
if(m_type == TYPE_SYMBOL || m_type == TYPE_STRING)
delete[] m_v.m_string;
if(m_type == TYPE_CONS)
{
delete m_v.m_cons.m_cdr;
delete m_v.m_cons.m_car;
}
}
//-----------------------------------------------------------------------------
const Lisp*
Lisp::getLisp(const char* name) const
{
const Lisp* P;
for(P = getCdr(); P != 0; P = P->getCdr())
{
const Lisp* CHILD = P->getCar();
// Also ignore if the child is not a CONS type, i.e.
// a TYPE_INTEGER is found, for which car is not defined!
if(!CHILD || CHILD->m_type!=TYPE_CONS)
continue;
if(!CHILD->getCar())
continue;
std::string CHILDName;
if(!CHILD->getCar()->get(CHILDName))
continue;
if(CHILDName == name)
return CHILD;
}
return 0;
}
//-----------------------------------------------------------------------------
//FIXME: is the boolean handled by this function? should the argument be
//removed?
void
Lisp::print(int ) const
{
if(m_type == TYPE_CONS)
{
printf("(");
if(m_v.m_cons.m_car)
m_v.m_cons.m_car->print();
if(m_v.m_cons.m_cdr)
{
printf(",");
if(m_v.m_cons.m_cdr)
m_v.m_cons.m_cdr->print();
}
printf(")");
}
if(m_type == TYPE_STRING)
{
printf("'%s' ", m_v.m_string);
}
if(m_type == TYPE_INTEGER)
{
printf("%d", m_v.m_integer);
}
if(m_type == TYPE_REAL)
{
printf("%f", m_v.m_real);
}
if(m_type == TYPE_SYMBOL)
{
printf("%s ", m_v.m_string);
}
}
} // end of namespace lisp

View File

@ -1,256 +0,0 @@
// $Id$
//
// TuxKart - a fun racing game with go-kart
// Copyright (C) 2004 Matthias Braun <matze@braunis.de>
// code in this file based on lispreader from Mark Probst
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef __LISPREADER_H__
#define __LISPREADER_H__
#include <string>
#include <vector>
#include "irrlicht.h"
#include "utils/vec3.hpp"
namespace lisp
{
class Lisp
{
public:
~Lisp();
enum LispType
{
TYPE_CONS,
TYPE_SYMBOL,
TYPE_INTEGER,
TYPE_STRING,
TYPE_REAL,
TYPE_BOOLEAN
};
LispType getType() const
{ return m_type; }
const Lisp* getCar() const
{ return m_v.m_cons.m_car; }
const Lisp* getCdr() const
{ return m_v.m_cons.m_cdr; }
bool get(std::string& val) const
{
if(m_type != TYPE_STRING && m_type != TYPE_SYMBOL)
return false;
val = m_v.m_string;
return true;
}
bool get(int& val) const
{
if(m_type != TYPE_INTEGER)
return false;
val = m_v.m_integer;
return true;
}
bool get(float& val) const
{
if(m_type != TYPE_REAL && m_type != TYPE_INTEGER)
return false;
val = m_type==TYPE_REAL ? m_v.m_real : m_v.m_integer;
return true;
}
bool get(bool& val) const
{
if(m_type != TYPE_BOOLEAN)
return false;
val = m_v.m_boolean;
return true;
}
/* conveniance functions which traverse the list until a child with a
* specified name is found. The value part is then interpreted in a specific
* way. The functions return true, if a child was found and could be
* interpreted correctly, otherwise false is returned and the variable value
* is not changed.
* (Please note that searching the lisp structure is O(n) so these functions
* are no good idea for performance critical areas)
*/
template<class T>
bool get(const char* name, T& val) const
{
const Lisp* lisp = getLisp(name);
if(!lisp)
return false;
lisp = lisp->getCdr();
if(!lisp)
return false;
lisp = lisp->getCar();
if(!lisp)
return false;
return lisp->get(val);
}
template<class T>
bool get(const std::string &name, T& val) const
{
return get(name.c_str(), val);
}
bool get(const char* name, float* val, int num) const
{
const Lisp* lisp = getLisp(name);
if(!lisp)
return false;
lisp = lisp->getCdr();
if(!lisp)
return false;
for(int i = 0; i < num && lisp; ++i)
{
const Lisp* m_car = lisp->getCar();
if(!m_car)
return false;
m_car->get(val[i]);
lisp = lisp->getCdr();
}
return true;
}
/*
bool get(const char* name, sgVec3& val) const
{
const Lisp* lisp = getLisp(name);
if(!lisp)
return false;
lisp = lisp->getCdr();
if(!lisp)
return false;
for(int i = 0; i < 3 && lisp; ++i)
{
const Lisp* m_car = lisp->getCar();
if(!m_car)
return false;
m_car->get(val[i]);
lisp = lisp->getCdr();
}
return true;
}
*/
bool get(const char* name, core::vector3df& val) const
{
const Lisp* lisp = getLisp(name);
if(!lisp)
return false;
lisp = lisp->getCdr();
if(!lisp) return false;
const Lisp* m_car = lisp->getCar();
if(!m_car) return false;
m_car->get(val.X);
lisp = lisp->getCdr();
m_car = lisp->getCar();
if(!m_car) return false;
m_car->get(val.Y);
lisp = lisp->getCdr();
m_car = lisp->getCar();
if(!m_car) return false;
m_car->get(val.Z);
lisp = lisp->getCdr();
return true;
}
bool get(const char* name, Vec3& val) const
{
const Lisp* lisp = getLisp(name);
if(!lisp)
return false;
lisp = lisp->getCdr();
if(!lisp)
return false;
float f[3];
for(int i = 0; i < 3 && lisp; ++i)
{
const Lisp* m_car = lisp->getCar();
if(!m_car)
return false;
m_car->get(f[i]);
lisp = lisp->getCdr();
}
// Lists are stored in reverse order, so reverse to original order
val.setValue(f[2],f[1],f[0]);
return true;
}
template<class T>
bool getVector(const char* name, std::vector<T>& vec) const
{
const Lisp* child = getLisp(name);
if(!child)
return false;
for(child = child->getCdr(); child != 0; child = child->getCdr())
{
T val;
if(!child->getCar())
continue;
if(child->getCar()->get(val))
{
vec.insert(vec.begin(),val);
}
}
return true;
}
const Lisp* getLisp(const char* name) const;
const Lisp* getLisp(const std::string& name) const
{ return getLisp(name.c_str()); }
// for debugging
void print(int indent = 0) const;
private:
friend class Parser;
Lisp(LispType newtype);
LispType m_type;
union
{
struct
{
Lisp* m_car;
Lisp* m_cdr;
}
m_cons;
char* m_string;
int m_integer;
bool m_boolean;
float m_real;
}
m_v;
};
} // end of namespace lisp
#endif

View File

@ -1,193 +0,0 @@
// $Id$
//
// TuxKart - a fun racing game with go-kart
// Copyright (C) 2004 Matthias Braun <matze@braunis.de>
// code in this file based on lispreader from Mark Probst
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <sstream>
#include <stdexcept>
#include <fstream>
#include "lisp/parser.hpp"
#include "lisp/lisp.hpp"
#include "utils/translation.hpp"
namespace lisp
{
Parser::Parser()
: m_lexer(0)
{}
//-----------------------------------------------------------------------------
Parser::~Parser()
{
delete m_lexer;
}
//-----------------------------------------------------------------------------
Lisp*
Parser::parse(const std::string& filename)
{
std::ifstream in(filename.c_str());
if(!in.good())
{
std::ostringstream msg;
msg << "Couldn't open file '" << filename << "'.";
throw std::runtime_error(msg.str());
}
return parse(in);
}
//-----------------------------------------------------------------------------
Lisp*
Parser::parse(std::istream& m_stream)
{
delete m_lexer;
m_lexer = new Lexer(m_stream);
m_token = m_lexer->getNextToken();
Lisp* result = new Lisp(Lisp::TYPE_CONS);
result->m_v.m_cons.m_car = 0;
result->m_v.m_cons.m_cdr = readList();
delete m_lexer;
m_lexer = 0;
return result;
}
//-----------------------------------------------------------------------------
Lisp*
Parser::read()
{
Lisp* result=NULL;
switch(m_token)
{
case Lexer::TOKEN_EOF:
{
std::ostringstream msg;
msg << "Parse Error at line " << m_lexer->getLineNumber()
<< ": Unexpected EOF.";
throw std::runtime_error(msg.str());
}
case Lexer::TOKEN_CLOSE_PAREN:
{
std::ostringstream msg;
msg << "Parse Error at line " << m_lexer->getLineNumber()
<< ": Unexpected ')'.";
throw std::runtime_error(msg.str());
}
case Lexer::TOKEN_TRANSLATION:
{
result = new Lisp(Lisp::TYPE_STRING);
m_token = m_lexer->getNextToken();
Lisp* next=read();
if(next->getType()!=Lisp::TYPE_STRING)
{
std::ostringstream msg;
msg << "Parse Error at line " << m_lexer->getLineNumber()
<< ": No string inside translation.";
throw std::runtime_error(msg.str());
}
irr::core::stringc trans=irr::core::stringc(_(next->m_v.m_string));
const size_t LEN = trans.size() + 1;
result->m_v.m_string = new char[LEN];
memcpy(result->m_v.m_string, trans.c_str(), LEN);
delete next;
break;
}
case Lexer::TOKEN_OPEN_PAREN:
result = new Lisp(Lisp::TYPE_CONS);
m_token = m_lexer->getNextToken();
result->m_v.m_cons.m_car = read();
result->m_v.m_cons.m_cdr = readList();
if(m_token != Lexer::TOKEN_CLOSE_PAREN)
{
std::ostringstream msg;
msg << "Parse Error at line " << m_lexer->getLineNumber()
<< ": Expected ')'.";
throw std::runtime_error(msg.str());
}
break;
case Lexer::TOKEN_SYMBOL:
{
result = new Lisp(Lisp::TYPE_SYMBOL);
const size_t LEN = strlen(m_lexer->getString()) + 1;
result->m_v.m_string = new char[LEN];
memcpy(result->m_v.m_string, m_lexer->getString(), LEN);
break;
}
case Lexer::TOKEN_STRING:
{
result = new Lisp(Lisp::TYPE_STRING);
const size_t LEN = strlen(m_lexer->getString()) + 1;
result->m_v.m_string = new char[LEN];
memcpy(result->m_v.m_string, m_lexer->getString(), LEN);
break;
}
case Lexer::TOKEN_INTEGER:
result = new Lisp(Lisp::TYPE_INTEGER);
sscanf(m_lexer->getString(), "%d", &result->m_v.m_integer);
break;
case Lexer::TOKEN_REAL:
result = new Lisp(Lisp::TYPE_REAL);
sscanf(m_lexer->getString(), "%f", &result->m_v.m_real);
break;
case Lexer::TOKEN_TRUE:
result = new Lisp(Lisp::TYPE_BOOLEAN);
result->m_v.m_boolean = true;
break;
case Lexer::TOKEN_FALSE:
result = new Lisp(Lisp::TYPE_BOOLEAN);
result->m_v.m_boolean = false;
break;
default:
// this should never happen
assert(false);
}
m_token = m_lexer->getNextToken();
return result;
}
//-----------------------------------------------------------------------------
Lisp*
Parser::readList()
{
Lisp* result = 0;
while(m_token != Lexer::TOKEN_CLOSE_PAREN && m_token != Lexer::TOKEN_EOF)
{
Lisp* newlisp = new Lisp(Lisp::TYPE_CONS);
newlisp->m_v.m_cons.m_car = read();
newlisp->m_v.m_cons.m_cdr = result;
result = newlisp;
}
return result;
}
} // end of namespace lisp

View File

@ -1,51 +0,0 @@
// $Id$
//
// TuxKart - a fun racing game with go-kart
// Copyright (C) 2004 Matthias Braun <matze@braunis.de>
// code in this file based on lispreader from Mark Probst
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef __LISPPARSER_H__
#define __LISPPARSER_H__
#include <string>
#include "lisp/lexer.hpp"
namespace lisp
{
class Lisp;
class Parser
{
public:
Parser();
~Parser();
Lisp* parse(const std::string& filename);
Lisp* parse(std::istream& stream);
private:
Lisp* read();
Lisp* readList();
Lexer* m_lexer;
Lexer::TokenType m_token;
};
} // end of namespace lisp
#endif

View File

@ -1,210 +0,0 @@
// $Id$
//
// TuxKart - a fun racing game with go-kart
// Copyright (C) 2004 Matthias Braun <matze@braunis.de>
// code in this file based on lispreader from Mark Probst
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <iostream>
#include <stdexcept>
#include <fstream>
#include <sstream>
#include "lisp/writer.hpp"
#include "utils/translation.hpp"
namespace lisp
{
Writer::Writer(const std::string& filename)
: m_indent_depth(0)
{
m_owner = true;
#ifdef WIN32
// With mingw, the files are written dos style (crlf), but
// these files can't be read with the lexer here. So we have
// to force the file to be written as binary for windows.
m_out = new std::ofstream(filename.c_str(),::std::ios_base::binary);
#else
m_out = new std::ofstream(filename.c_str());
#endif
if(!m_out->good())
{
std::ostringstream msg;
msg << "LispWriter Error: Couldn't open file '" << filename
<< "' for writing.";
throw std::runtime_error(msg.str());
}
}
//-----------------------------------------------------------------------------
Writer::Writer(std::ostream& newout)
: m_indent_depth(0)
{
m_owner = false;
m_out = &newout;
}
//-----------------------------------------------------------------------------
Writer::~Writer()
{
if(m_lists.size() > 0)
{
std::cerr << "Warning: Not all sections closed in lispwriter!\n";
}
if(m_owner)
delete m_out;
}
//-----------------------------------------------------------------------------
void
Writer::writeComment(const std::string& comment)
{
indent();
*m_out << "; " << comment << "\n";
}
//-----------------------------------------------------------------------------
void
Writer::beginList(const std::string& listname)
{
indent();
*m_out << '(' << listname << '\n';
m_indent_depth += 2;
m_lists.push_back(listname);
}
//-----------------------------------------------------------------------------
void
Writer::endList(const std::string& listname)
{
if(m_lists.size() == 0)
{
fprintf(stderr, "Trying to close list '%s, which is not open.\n",
listname.c_str());
return;
}
if(m_lists.back() != listname)
{
fprintf(stderr,
"Warning: trying to close list '%s' while list '%s' is open.\n",
listname.c_str(), m_lists.back().c_str());
return;
}
m_lists.pop_back();
m_indent_depth -= 2;
indent();
*m_out << ")\n";
}
//-----------------------------------------------------------------------------
void
Writer::write(const std::string& name, int value)
{
indent();
*m_out << '(' << name << ' ' << value << ")\n";
}
//-----------------------------------------------------------------------------
void
Writer::write(const std::string& name, float value)
{
indent();
*m_out << '(' << name << ' ' << value << ")\n";
}
//-----------------------------------------------------------------------------
void
Writer::write(const std::string& name, const std::string& value)
{
indent();
*m_out << '(' << name << " \"" << value << "\")\n";
}
//-----------------------------------------------------------------------------
void
Writer::write(const std::string& name, const char* value)
{
indent();
*m_out << '(' << name << " \"" << value << "\")\n";
}
//-----------------------------------------------------------------------------
void
Writer::write(const std::string& name, bool value)
{
indent();
*m_out << '(' << name << ' ' << (value ? "#t" : "#f") << ")\n";
}
//-----------------------------------------------------------------------------
void
Writer::write(const std::string& name, const std::vector<int>& value)
{
indent();
*m_out << '(' << name;
for(std::vector<int>::const_iterator i = value.begin(); i != value.end(); ++i)
*m_out << " " << *i;
*m_out << ")\n";
}
//-----------------------------------------------------------------------------
void
Writer::write(const std::string& name, const std::vector<unsigned int>& value)
{
indent();
*m_out << '(' << name;
for(std::vector<unsigned int>::const_iterator i = value.begin(); i != value.end(); ++i)
*m_out << " " << *i;
*m_out << ")\n";
}
//-----------------------------------------------------------------------------
void
Writer::write(const std::string& name, const std::vector<std::string>& value)
{
indent();
*m_out << '(' << name;
for(std::vector<std::string>::const_iterator i = value.begin(); i != value.end(); ++i)
*m_out << " " << "\"" << *i << "\"";
*m_out << ")\n";
}
//-----------------------------------------------------------------------------
void
Writer::indent()
{
for(int i = 0; i<m_indent_depth; ++i)
*m_out << ' ';
}
} // end of namespace lisp

View File

@ -1,66 +0,0 @@
// $Id$
//
// TuxKart - a fun racing game with go-kart
// Copyright (C) 2004 Matthias Braun <matze@braunis.de>
// code in this file based on lispreader from Mark Probst
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef __LISPWRITER_H__
#define __LISPWRITER_H__
#include <iostream>
#include <string>
#include <vector>
namespace lisp
{
class Writer
{
public:
Writer(const std::string& filename);
Writer(std::ostream& out);
~Writer();
void writeComment(const std::string& comment);
void beginList(const std::string& listname);
void write(const std::string& name, int value);
void write(const std::string& name, unsigned int value) {write(name, (int)value);}
void write(const std::string& name, float value);
void write(const std::string& name, const std::string& value);
void write(const std::string& name, const char* value);
void write(const std::string& name, bool value);
void write(const std::string& name, const std::vector<int>& value);
void write(const std::string& name, const std::vector<unsigned int>& value);
void write(const std::string& name, const std::vector<std::string>& value);
// add more write-functions when needed...
void endList(const std::string& listname);
private:
void indent();
bool m_owner;
std::ostream* m_out;
int m_indent_depth;
std::vector<std::string> m_lists;
};
} //namespace lisp
#endif