Now the growings animation looks pretty cool :)
[supertux.git] / src / lispreader.cpp
index 0ece43d..51ff97c 100644 (file)
@@ -21,8 +21,8 @@
  * Boston, MA 02111-1307, USA.
  */
 
+#include <iostream>
 #include <string>
-#include <assert.h>
 #include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 static char token_string[MAX_TOKEN_LENGTH + 1] = "";
 static int token_length = 0;
 
-static lisp_object_t end_marker = { LISP_TYPE_EOF , {0,0}  };
-static lisp_object_t error_object = { LISP_TYPE_PARSE_ERROR , {0,0}  };
-static lisp_object_t close_paren_marker = { LISP_TYPE_PARSE_ERROR , {0,0}  };
-static lisp_object_t dot_marker = { LISP_TYPE_PARSE_ERROR , {0,0} };
+static lisp_object_t end_marker = { LISP_TYPE_EOF, {{0, 0}} };
+static lisp_object_t error_object = { LISP_TYPE_PARSE_ERROR , {{0,0}}  };
+static lisp_object_t close_paren_marker = { LISP_TYPE_PARSE_ERROR , {{0,0}}  };
+static lisp_object_t dot_marker = { LISP_TYPE_PARSE_ERROR , {{0,0}} };
 
 static void
 _token_clear (void)
@@ -63,7 +63,8 @@ _token_clear (void)
 static void
 _token_append (char c)
 {
-  assert(token_length < MAX_TOKEN_LENGTH);
+  if (token_length >= MAX_TOKEN_LENGTH)
+    throw LispReaderException("_token_append()", __FILE__, __LINE__);
 
   token_string[token_length++] = c;
   token_string[token_length] = '\0';
@@ -92,7 +93,8 @@ _next_char (lisp_stream_t *stream)
     case LISP_STREAM_ANY:
       return stream->v.any.next_char(stream->v.any.data);
     }
-  assert(0);
+
+  throw LispReaderException("_next_char()", __FILE__, __LINE__);
   return EOF;
 }
 
@@ -114,7 +116,7 @@ _unget_char (char c, lisp_stream_t *stream)
       break;
 
     default :
-      assert(0);
+      throw LispReaderException("_unget_char()", __FILE__, __LINE__);
     }
 }
 
@@ -266,7 +268,7 @@ _scan (lisp_stream_t *stream)
         }
     }
 
-  assert(0);
+  throw LispReaderException("_scan()", __FILE__, __LINE__);
   return TOKEN_ERROR;
 }
 
@@ -304,7 +306,8 @@ lisp_stream_init_any (lisp_stream_t *stream, void *data,
                       int (*next_char) (void *data),
                       void (*unget_char) (char c, void *data))
 {
-  assert(next_char != 0 && unget_char != 0);
+  if (next_char == 0 || unget_char == 0)
+    throw LispReaderException("lisp_stream_init_any()", __FILE__, __LINE__);
 
   stream->type = LISP_STREAM_ANY;
   stream->v.any.data = data;
@@ -492,7 +495,7 @@ lisp_read (lisp_stream_t *in)
       return lisp_make_boolean(0);
     }
 
-  assert(0);
+  throw LispReaderException("lisp_read()", __FILE__, __LINE__);
   return &error_object;
 }
 
@@ -642,7 +645,8 @@ static int _match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_obje
 static int
 _match_pattern_var (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars)
 {
-  assert(lisp_type(pattern) == LISP_TYPE_PATTERN_VAR);
+  if (lisp_type(pattern) != LISP_TYPE_PATTERN_VAR)
+    throw LispReaderException("_match_pattern_var", __FILE__, __LINE__);
 
   switch (pattern->v.pattern.type)
     {
@@ -686,7 +690,8 @@ _match_pattern_var (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **
 
         for (sub = pattern->v.pattern.sub; sub != 0; sub = lisp_cdr(sub))
           {
-            assert(lisp_type(sub) == LISP_TYPE_CONS);
+            if (lisp_type(sub) != LISP_TYPE_CONS)
+              throw LispReaderException("_match_pattern_var()", __FILE__, __LINE__);
 
             if (_match_pattern(lisp_car(sub), obj, vars))
               matched = 1;
@@ -698,7 +703,7 @@ _match_pattern_var (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **
       break;
 
     default :
-      assert(0);
+      throw LispReaderException("_match_pattern_var()", __FILE__, __LINE__);
     }
 
   if (vars != 0)
@@ -748,7 +753,7 @@ _match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars
       break;
 
     default :
-      assert(0);
+      throw LispReaderException("_match_pattern()", __FILE__, __LINE__);
     }
 
   return 0;
@@ -803,7 +808,8 @@ lisp_type (lisp_object_t *obj)
 int
 lisp_integer (lisp_object_t *obj)
 {
-  assert(obj->type == LISP_TYPE_INTEGER);
+  if (obj->type != LISP_TYPE_INTEGER)
+    throw LispReaderException("lisp_integer()", __FILE__, __LINE__);
 
   return obj->v.integer;
 }
@@ -811,7 +817,8 @@ lisp_integer (lisp_object_t *obj)
 char*
 lisp_symbol (lisp_object_t *obj)
 {
-  assert(obj->type == LISP_TYPE_SYMBOL);
+  if (obj->type != LISP_TYPE_SYMBOL)
+    throw LispReaderException("lisp_symbol()", __FILE__, __LINE__);
 
   return obj->v.string;
 }
@@ -819,7 +826,8 @@ lisp_symbol (lisp_object_t *obj)
 char*
 lisp_string (lisp_object_t *obj)
 {
-  assert(obj->type == LISP_TYPE_STRING);
+  if (obj->type != LISP_TYPE_STRING)
+    throw LispReaderException("lisp_string()", __FILE__, __LINE__);
 
   return obj->v.string;
 }
@@ -827,7 +835,8 @@ lisp_string (lisp_object_t *obj)
 int
 lisp_boolean (lisp_object_t *obj)
 {
-  assert(obj->type == LISP_TYPE_BOOLEAN);
+  if (obj->type != LISP_TYPE_BOOLEAN)
+    throw LispReaderException("lisp_boolean()", __FILE__, __LINE__);
 
   return obj->v.integer;
 }
@@ -835,7 +844,8 @@ lisp_boolean (lisp_object_t *obj)
 float
 lisp_real (lisp_object_t *obj)
 {
-  assert(obj->type == LISP_TYPE_REAL || obj->type == LISP_TYPE_INTEGER);
+  if (obj->type != LISP_TYPE_REAL && obj->type != LISP_TYPE_INTEGER)
+    throw LispReaderException("lisp_real()", __FILE__, __LINE__);
 
   if (obj->type == LISP_TYPE_INTEGER)
     return obj->v.integer;
@@ -845,7 +855,8 @@ lisp_real (lisp_object_t *obj)
 lisp_object_t*
 lisp_car (lisp_object_t *obj)
 {
-  assert(obj->type == LISP_TYPE_CONS || obj->type == LISP_TYPE_PATTERN_CONS);
+  if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
+    throw LispReaderException("lisp_car()", __FILE__, __LINE__);
 
   return obj->v.cons.car;
 }
@@ -853,7 +864,8 @@ lisp_car (lisp_object_t *obj)
 lisp_object_t*
 lisp_cdr (lisp_object_t *obj)
 {
-  assert(obj->type == LISP_TYPE_CONS || obj->type == LISP_TYPE_PATTERN_CONS);
+  if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
+    throw LispReaderException("lisp_cdr()", __FILE__, __LINE__);
 
   return obj->v.cons.cdr;
 }
@@ -869,7 +881,7 @@ lisp_cxr (lisp_object_t *obj, const char *x)
     else if (x[i] == 'd')
       obj = lisp_cdr(obj);
     else
-      assert(0);
+      throw LispReaderException("lisp_cxr()", __FILE__, __LINE__);
 
   return obj;
 }
@@ -881,7 +893,8 @@ lisp_list_length (lisp_object_t *obj)
 
   while (obj != 0)
     {
-      assert(obj->type == LISP_TYPE_CONS || obj->type == LISP_TYPE_PATTERN_CONS);
+      if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
+        throw LispReaderException("lisp_list_length()", __FILE__, __LINE__);
 
       ++length;
       obj = obj->v.cons.cdr;
@@ -895,8 +908,10 @@ lisp_list_nth_cdr (lisp_object_t *obj, int index)
 {
   while (index > 0)
     {
-      assert(obj != 0);
-      assert(obj->type == LISP_TYPE_CONS || obj->type == LISP_TYPE_PATTERN_CONS);
+      if (obj == 0)
+        throw LispReaderException("lisp_list_nth_cdr()", __FILE__, __LINE__);
+      if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
+        throw LispReaderException("lisp_list_nth_cdr()", __FILE__, __LINE__);
 
       --index;
       obj = obj->v.cons.cdr;
@@ -910,7 +925,8 @@ lisp_list_nth (lisp_object_t *obj, int index)
 {
   obj = lisp_list_nth_cdr(obj, index);
 
-  assert(obj != 0);
+  if (obj == 0)
+    throw LispReaderException("lisp_list_nth()", __FILE__, __LINE__);
 
   return obj->v.cons.car;
 }
@@ -992,18 +1008,43 @@ lisp_dump (lisp_object_t *obj, FILE *out)
       break;
 
     default :
-      assert(0);
+      throw LispReaderException("lisp_dump()", __FILE__, __LINE__);
     }
 }
 
 using namespace std;
 
 LispReader::LispReader (lisp_object_t* l)
-    : lst (l)
+    : owner(0), lst (l)
+{
+}
+
+LispReader::~LispReader()
 {
-  //std::cout << "LispReader: " << std::flush;
-  //lisp_dump(lst, stdout);
-  //std::cout << std::endl;
+  if(owner)
+    lisp_free(owner);
+}
+
+LispReader*
+LispReader::load(const std::string& filename, const std::string& toplevellist)
+{
+  lisp_object_t* obj = lisp_read_from_file(filename);
+
+  if(obj->type == LISP_TYPE_EOF || obj->type == LISP_TYPE_PARSE_ERROR) {
+    lisp_free(obj);
+    throw LispReaderException("LispReader::load", __FILE__, __LINE__);
+  }
+
+  if(toplevellist != lisp_symbol(lisp_car(obj))) {
+    lisp_car(obj);
+    throw LispReaderException("LispReader::load wrong toplevel symbol",
+        __FILE__, __LINE__);
+  }
+  
+  LispReader* reader = new LispReader(lisp_cdr(obj));  
+  reader->owner = obj;
+
+  return reader;
 }
 
 lisp_object_t*
@@ -1036,313 +1077,169 @@ LispReader::search_for(const char* name)
 }
 
 bool
-LispReader::read_int (const char* name, int* i)
+LispReader::read_int (const char* name, int& i)
 {
   lisp_object_t* obj = search_for (name);
-  if (obj)
-    {
-      if (!lisp_integer_p(lisp_car(obj)))
-        st_abort("LispReader expected type integer at token: ", name);
-      *i = lisp_integer(lisp_car(obj));
-      return true;
-    }
-  return false;
+  if(!obj)
+    return false;
+      
+  if (!lisp_integer_p(lisp_car(obj)))
+    return false;
+  
+  i = lisp_integer(lisp_car(obj));
+  return true;
 }
 
 bool
-LispReader::read_lisp(const char* name, lisp_object_t** b)
+LispReader::read_lisp(const char* name, lisp_object_t*& b)
 {
   lisp_object_t* obj = search_for (name);
-  if (obj)
-    {
-      *b = obj;
-      return true;
-    }
-  else
+  if (!obj)
     return false;
+  
+  b = obj;
+  return true;
 }
 
-bool
-LispReader::read_float (const char* name, float* f)
+LispReader*
+LispReader::read_lisp(const char* name)
 {
-  lisp_object_t* obj = search_for (name);
-  if (obj)
-    {
-      if (!lisp_real_p(lisp_car(obj)) && !lisp_integer_p(lisp_car(obj)))
-        st_abort("LispReader expected type real at token: ", name);
-      *f = lisp_real(lisp_car(obj));
-      return true;
-    }
-  return false;
-}
+  lisp_object_t* obj = search_for(name);
+  if(!obj)
+    return 0;
 
-bool
-LispReader::read_string_vector (const char* name, std::vector<std::string>* vec)
-{
-  lisp_object_t* obj = search_for (name);
-  if (obj)
-    {
-      while(!lisp_nil_p(obj))
-        {
-          if (!lisp_string_p(lisp_car(obj)))
-            st_abort("LispReader expected type string at token: ", name);
-          vec->push_back(lisp_string(lisp_car(obj)));
-          obj = lisp_cdr(obj);
-        }
-      return true;
-    }
-  return false;    
+  return new LispReader(obj);
 }
 
 bool
-LispReader::read_int_vector (const char* name, std::vector<int>* vec)
+LispReader::read_float (const char* name, float& f)
 {
   lisp_object_t* obj = search_for (name);
-  if (obj)
-    {
-      while(!lisp_nil_p(obj))
-        {
-          if (!lisp_integer_p(lisp_car(obj)))
-            st_abort("LispReader expected type integer at token: ", name);
-          vec->push_back(lisp_integer(lisp_car(obj)));
-          obj = lisp_cdr(obj);
-        }
-      return true;
-    }
-  return false;    
+  if (!obj)
+    return false;
+  
+  if (!lisp_real_p(lisp_car(obj)) && !lisp_integer_p(lisp_car(obj)))
+    st_abort("LispReader expected type real at token: ", name);
+  
+  f = lisp_real(lisp_car(obj));
+  return true;
 }
 
 bool
-LispReader::read_char_vector (const char* name, std::vector<char>* vec)
+LispReader::read_string_vector (const char* name, std::vector<std::string>& vec)
 {
   lisp_object_t* obj = search_for (name);
-  if (obj)
-    {
-      while(!lisp_nil_p(obj))
-        {
-          vec->push_back(*lisp_string(lisp_car(obj)));
-          obj = lisp_cdr(obj);
-        }
-      return true;
-    }
-  return false;    
-}
+  if (!obj)
+    return false;
 
-bool
-LispReader::read_string (const char* name, std::string* str)
-{
-  lisp_object_t* obj = search_for (name);
-  if (obj)
-    {
-      if (!lisp_string_p(lisp_car(obj)))
-        st_abort("LispReader expected type string at token: ", name);
-     *str = lisp_string(lisp_car(obj));
-      return true;
-    }
-  return false;  
+  vec.clear();
+  while(!lisp_nil_p(obj))
+  {
+    if (!lisp_string_p(lisp_car(obj)))
+      st_abort("LispReader expected type string at token: ", name);
+    vec.push_back(lisp_string(lisp_car(obj)));
+    obj = lisp_cdr(obj);
+  }
+  return true;
 }
 
 bool
-LispReader::read_bool (const char* name, bool* b)
+LispReader::read_int_vector (const char* name, std::vector<int>& vec)
 {
   lisp_object_t* obj = search_for (name);
-  if (obj)
-    {
-      if (!lisp_boolean_p(lisp_car(obj)))
-        st_abort("LispReader expected type bool at token: ", name);
-      *b = lisp_boolean(lisp_car(obj));
-      return true;
-    }
-  return false;
-}
-
-LispWriter::LispWriter (const char* name)
-{
-  lisp_objs.push_back(lisp_make_symbol (name));
-}
-
-void
-LispWriter::append (lisp_object_t* obj)
-{
-  lisp_objs.push_back(obj);
-}
-
-lisp_object_t*
-LispWriter::make_list3 (lisp_object_t* a, lisp_object_t* b, lisp_object_t* c)
-{
-  return lisp_make_cons (a, lisp_make_cons(b, lisp_make_cons(c, lisp_nil())));
-}
+  if (!obj)
+    return false;
 
-lisp_object_t*
-LispWriter::make_list2 (lisp_object_t* a, lisp_object_t* b)
-{
-  return lisp_make_cons (a, lisp_make_cons(b, lisp_nil()));
+  vec.clear();
+  while(!lisp_nil_p(obj))
+  {
+    if (!lisp_integer_p(lisp_car(obj)))
+      st_abort("LispReader expected type integer at token: ", name);
+    vec.push_back(lisp_integer(lisp_car(obj)));
+    obj = lisp_cdr(obj);
+  }
+  return true;
 }
 
-void
-LispWriter::write_float (const char* name, float f)
+bool
+LispReader::read_int_vector (const char* name, std::vector<unsigned int>& vec)
 {
-  append(make_list2 (lisp_make_symbol (name),
-                     lisp_make_real(f)));
-}
+  lisp_object_t* obj = search_for (name);
+  if (!obj)
+    return false;
 
-void
-LispWriter::write_int (const char* name, int i)
-{
-  append(make_list2 (lisp_make_symbol (name),
-                     lisp_make_integer(i)));
+  vec.clear();
+  while(!lisp_nil_p(obj))
+  {
+    if (!lisp_integer_p(lisp_car(obj)))
+      st_abort("LispReader expected type integer at token: ", name);
+    vec.push_back(lisp_integer(lisp_car(obj)));
+    obj = lisp_cdr(obj);
+  }
+  return true;
 }
 
-void
-LispWriter::write_string (const char* name, const char* str)
+bool
+LispReader::read_char_vector (const char* name, std::vector<char>& vec)
 {
-  append(make_list2 (lisp_make_symbol (name),
-                     lisp_make_string(str)));
+  lisp_object_t* obj = search_for (name);
+  if (!obj)
+    return false;
+  vec.clear();
+  while(!lisp_nil_p(obj))
+  {
+    vec.push_back(*lisp_string(lisp_car(obj)));
+    obj = lisp_cdr(obj);
+  }
+  return true;
 }
 
-void
-LispWriter::write_symbol (const char* name, const char* symname)
+bool
+LispReader::read_string (const char* name, std::string& str)
 {
-  append(make_list2 (lisp_make_symbol (name),
-                     lisp_make_symbol(symname)));
-}
+  lisp_object_t* obj = search_for (name);
+  if (!obj)
+    return false;
 
-void
-LispWriter::write_lisp_obj(const char* name, lisp_object_t* lst)
-{
-  append(make_list2 (lisp_make_symbol (name),
-                     lst));
+  if (!lisp_string_p(lisp_car(obj)))
+    st_abort("LispReader expected type string at token: ", name);
+  str = lisp_string(lisp_car(obj));
+  return true;
 }
 
-void
-LispWriter::write_boolean (const char* name, bool b)
+bool
+LispReader::read_bool (const char* name, bool& b)
 {
-  append(make_list2 (lisp_make_symbol (name),
-                     lisp_make_boolean(b)));
+  lisp_object_t* obj = search_for (name);
+  if (!obj)
+    return false;
+  
+  if (!lisp_boolean_p(lisp_car(obj)))
+    st_abort("LispReader expected type bool at token: ", name);
+  b = lisp_boolean(lisp_car(obj));
+  return true;
 }
 
 lisp_object_t*
-LispWriter::create_lisp ()
-{
-  lisp_object_t* lisp_obj = lisp_nil();
-
-  for(std::vector<lisp_object_t*>::reverse_iterator i = lisp_objs.rbegin ();
-      i != lisp_objs.rend (); ++i)
-    {
-      lisp_obj = lisp_make_cons (*i, lisp_obj);
-    }
-  lisp_objs.clear();
-
-  return lisp_obj;
-}
-
-#if 0
-void mygzungetc(char c, void* file)
-{
-  gzungetc(c, file);
-}
-
-lisp_stream_t* lisp_stream_init_gzfile (lisp_stream_t *stream, gzFile file)
-{
-  return lisp_stream_init_any (stream, file, gzgetc, mygzungetc);
-}
-#endif
-
-lisp_object_t* lisp_read_from_gzfile(const char* filename)
-{
-  bool done = false;
-  lisp_object_t* root_obj = 0;
-  int chunk_size = 128 * 1024;
-  int buf_pos = 0;
-  int try_number = 1;
-  char* buf = static_cast<char*>(malloc(chunk_size));
-  assert(buf);
-
-  gzFile in = gzopen(filename, "r");
-
-  while (!done)
-    {
-      int ret = gzread(in, buf + buf_pos, chunk_size);
-      if (ret == -1)
-        {
-          free (buf);
-          assert(!"Error while reading from file");
-        }
-      else if (ret == chunk_size) // buffer got full, eof not yet there so resize
-        {
-          buf_pos = chunk_size * try_number;
-          try_number += 1;
-          buf = static_cast<char*>(realloc(buf, chunk_size * try_number));
-          assert(buf);
-        }
-      else 
-        {
-          // everything fine, encountered EOF 
-          done = true;
-        }
-    }
-      
-  lisp_stream_t stream;
-  lisp_stream_init_string (&stream, buf);
-  root_obj = lisp_read (&stream);
-      
-  free(buf);
-  gzclose(in);
-
-  return root_obj;
-}
-
-bool has_suffix(const char* data, const char* suffix)
+LispReader::get_lisp()
 {
-  int suffix_len = strlen(suffix);
-  int data_len   = strlen(data);
-  
-  const char* data_suffix = (data + data_len - suffix_len);
-
-  if (data_suffix >= data)
-    {
-      return (strcmp(data_suffix, suffix) == 0);
-    }
-  else
-    {
-      return false;
-    }
+  return lst;
 }
 
 lisp_object_t* lisp_read_from_file(const std::string& filename)
 {
-  lisp_stream_t stream;
+  FILE* in = fopen(filename.c_str(), "r");
 
-  if (has_suffix(filename.c_str(), ".gz"))
-    {
-      return lisp_read_from_gzfile(filename.c_str());
-#if 0
-      lisp_object_t* obj = 0;
-      gzFile in = gzopen(filename, "r");
-
-      if (in)
-        {
-          lisp_stream_init_gzfile(&stream, in);
-          obj = lisp_read(&stream);
-          gzclose(in);
-        }
-        return obj;
-#endif
-    }
-  else
-    {
-      lisp_object_t* obj = 0;
-      FILE* in = fopen(filename.c_str(), "r");
+  if(!in)
+    return 0;
 
-      if (in)
-        {
-          lisp_stream_init_file(&stream, in);
-          obj = lisp_read(&stream);
-          fclose(in);
-        }
+  lisp_stream_t stream;
+  lisp_stream_init_file(&stream, in);
+  lisp_object_t* obj = lisp_read(&stream);
+  fclose(in);
 
-      return obj;
-    }
+  return obj;
 }
 
 // EOF //