using namespace std;
LispReader::LispReader (lisp_object_t* l)
- : lst (l)
+ : owner(0), lst (l)
{
- //std::cout << "LispReader: " << std::flush;
- //lisp_dump(lst, stdout);
- //std::cout << std::endl;
+}
+
+LispReader::~LispReader()
+{
+ 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*
}
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); /* Instead of giving up, we return with false now. */
- return false;
- }
- *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)
+LispReader::get_lisp()
{
- 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));
- if (!buf)
- throw LispReaderException("lisp_read_from_gzfile()", __FILE__, __LINE__);
-
- gzFile in = gzopen(filename, "r");
-
- while (!done)
- {
- int ret = gzread(in, buf + buf_pos, chunk_size);
- if (ret == -1)
- {
- free (buf);
- throw LispReaderException("Error while reading from file", __FILE__, __LINE__);
- }
- 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));
-
- if (!buf)
- throw LispReaderException("lisp_read_from_gzfile()", __FILE__, __LINE__);
- }
- 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)
-{
- 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());
- }
- 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 //