5 * Copyright (C) 1998-2000 Mark Probst
6 * Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
31 #include "lispreader.h"
33 #define TOKEN_ERROR -1
35 #define TOKEN_OPEN_PAREN 1
36 #define TOKEN_CLOSE_PAREN 2
37 #define TOKEN_SYMBOL 3
38 #define TOKEN_STRING 4
39 #define TOKEN_INTEGER 5
41 #define TOKEN_PATTERN_OPEN_PAREN 7
44 #define TOKEN_FALSE 10
47 #define MAX_TOKEN_LENGTH 1024
49 static char token_string[MAX_TOKEN_LENGTH + 1] = "";
50 static int token_length = 0;
52 static lisp_object_t end_marker = { LISP_TYPE_EOF, {{0, 0}} };
53 static lisp_object_t error_object = { LISP_TYPE_PARSE_ERROR , {{0,0}} };
54 static lisp_object_t close_paren_marker = { LISP_TYPE_PARSE_ERROR , {{0,0}} };
55 static lisp_object_t dot_marker = { LISP_TYPE_PARSE_ERROR , {{0,0}} };
60 token_string[0] = '\0';
65 _token_append (char c)
67 if (token_length >= MAX_TOKEN_LENGTH)
68 throw LispReaderException("_token_append()", __FILE__, __LINE__);
70 token_string[token_length++] = c;
71 token_string[token_length] = '\0';
75 _next_char (lisp_stream_t *stream)
79 case LISP_STREAM_FILE :
80 return getc(stream->v.file);
82 case LISP_STREAM_STRING :
84 char c = stream->v.string.buf[stream->v.string.pos];
89 ++stream->v.string.pos;
95 return stream->v.any.next_char(stream->v.any.data);
98 throw LispReaderException("_next_char()", __FILE__, __LINE__);
103 _unget_char (char c, lisp_stream_t *stream)
105 switch (stream->type)
107 case LISP_STREAM_FILE :
108 ungetc(c, stream->v.file);
111 case LISP_STREAM_STRING :
112 --stream->v.string.pos;
115 case LISP_STREAM_ANY:
116 stream->v.any.unget_char(c, stream->v.any.data);
120 throw LispReaderException("_unget_char()", __FILE__, __LINE__);
125 _scan (lisp_stream_t *stream)
127 static char *delims = "\"();";
135 c = _next_char(stream);
138 else if (c == ';') /* comment start */
141 c = _next_char(stream);
153 return TOKEN_OPEN_PAREN;
156 return TOKEN_CLOSE_PAREN;
161 c = _next_char(stream);
168 c = _next_char(stream);
190 c = _next_char(stream);
203 c = _next_char(stream);
208 return TOKEN_PATTERN_OPEN_PAREN;
215 if (isdigit(c) || c == '-')
217 int have_nondigits = 0;
219 int have_floating_point = 0;
226 have_floating_point++;
229 c = _next_char(stream);
231 if (c != EOF && !isdigit(c) && !isspace(c) && c != '.' && !strchr(delims, c))
234 while (c != EOF && !isspace(c) && !strchr(delims, c));
237 _unget_char(c, stream);
239 if (have_nondigits || !have_digits || have_floating_point > 1)
241 else if (have_floating_point == 1)
244 return TOKEN_INTEGER;
250 c = _next_char(stream);
251 if (c != EOF && !isspace(c) && !strchr(delims, c))
255 _unget_char(c, stream);
262 c = _next_char(stream);
264 while (c != EOF && !isspace(c) && !strchr(delims, c));
266 _unget_char(c, stream);
272 throw LispReaderException("_scan()", __FILE__, __LINE__);
276 static lisp_object_t*
277 lisp_object_alloc (int type)
279 lisp_object_t *obj = (lisp_object_t*)malloc(sizeof(lisp_object_t));
287 lisp_stream_init_file (lisp_stream_t *stream, FILE *file)
289 stream->type = LISP_STREAM_FILE;
290 stream->v.file = file;
296 lisp_stream_init_string (lisp_stream_t *stream, char *buf)
298 stream->type = LISP_STREAM_STRING;
299 stream->v.string.buf = buf;
300 stream->v.string.pos = 0;
306 lisp_stream_init_any (lisp_stream_t *stream, void *data,
307 int (*next_char) (void *data),
308 void (*unget_char) (char c, void *data))
310 if (next_char == 0 || unget_char == 0)
311 throw LispReaderException("lisp_stream_init_any()", __FILE__, __LINE__);
313 stream->type = LISP_STREAM_ANY;
314 stream->v.any.data = data;
315 stream->v.any.next_char= next_char;
316 stream->v.any.unget_char = unget_char;
322 lisp_make_integer (int value)
324 lisp_object_t *obj = lisp_object_alloc(LISP_TYPE_INTEGER);
326 obj->v.integer = value;
332 lisp_make_real (float value)
334 lisp_object_t *obj = lisp_object_alloc(LISP_TYPE_REAL);
342 lisp_make_symbol (const char *value)
344 lisp_object_t *obj = lisp_object_alloc(LISP_TYPE_SYMBOL);
346 obj->v.string = strdup(value);
352 lisp_make_string (const char *value)
354 lisp_object_t *obj = lisp_object_alloc(LISP_TYPE_STRING);
356 obj->v.string = strdup(value);
362 lisp_make_cons (lisp_object_t *car, lisp_object_t *cdr)
364 lisp_object_t *obj = lisp_object_alloc(LISP_TYPE_CONS);
366 obj->v.cons.car = car;
367 obj->v.cons.cdr = cdr;
373 lisp_make_boolean (int value)
375 lisp_object_t *obj = lisp_object_alloc(LISP_TYPE_BOOLEAN);
377 obj->v.integer = value ? 1 : 0;
382 static lisp_object_t*
383 lisp_make_pattern_cons (lisp_object_t *car, lisp_object_t *cdr)
385 lisp_object_t *obj = lisp_object_alloc(LISP_TYPE_PATTERN_CONS);
387 obj->v.cons.car = car;
388 obj->v.cons.cdr = cdr;
393 static lisp_object_t*
394 lisp_make_pattern_var (int type, int index, lisp_object_t *sub)
396 lisp_object_t *obj = lisp_object_alloc(LISP_TYPE_PATTERN_VAR);
398 obj->v.pattern.type = type;
399 obj->v.pattern.index = index;
400 obj->v.pattern.sub = sub;
406 lisp_read (lisp_stream_t *in)
408 int token = _scan(in);
409 lisp_object_t *obj = lisp_nil();
411 if (token == TOKEN_EOF)
417 return &error_object;
422 case TOKEN_OPEN_PAREN :
423 case TOKEN_PATTERN_OPEN_PAREN :
425 lisp_object_t *last = lisp_nil(), *car;
430 if (car == &error_object || car == &end_marker)
433 return &error_object;
435 else if (car == &dot_marker)
437 if (lisp_nil_p(last))
440 return &error_object;
444 if (car == &error_object || car == &end_marker)
451 last->v.cons.cdr = car;
453 if (_scan(in) != TOKEN_CLOSE_PAREN)
456 return &error_object;
459 car = &close_paren_marker;
462 else if (car != &close_paren_marker)
464 if (lisp_nil_p(last))
465 obj = last = (token == TOKEN_OPEN_PAREN ? lisp_make_cons(car, lisp_nil()) : lisp_make_pattern_cons(car, lisp_nil()));
467 last = last->v.cons.cdr = lisp_make_cons(car, lisp_nil());
470 while (car != &close_paren_marker);
474 case TOKEN_CLOSE_PAREN :
475 return &close_paren_marker;
478 return lisp_make_symbol(token_string);
481 return lisp_make_string(token_string);
484 return lisp_make_integer(atoi(token_string));
487 return lisp_make_real((float)atof(token_string));
493 return lisp_make_boolean(1);
496 return lisp_make_boolean(0);
499 throw LispReaderException("lisp_read()", __FILE__, __LINE__);
500 return &error_object;
504 lisp_free (lisp_object_t *obj)
511 case LISP_TYPE_INTERNAL :
512 case LISP_TYPE_PARSE_ERROR :
516 case LISP_TYPE_SYMBOL :
517 case LISP_TYPE_STRING :
521 case LISP_TYPE_CONS :
522 case LISP_TYPE_PATTERN_CONS :
523 lisp_free(obj->v.cons.car);
524 lisp_free(obj->v.cons.cdr);
527 case LISP_TYPE_PATTERN_VAR :
528 lisp_free(obj->v.pattern.sub);
536 lisp_read_from_string (const char *buf)
538 lisp_stream_t stream;
540 lisp_stream_init_string(&stream, (char*)buf);
541 return lisp_read(&stream);
545 _compile_pattern (lisp_object_t **obj, int *index)
550 switch (lisp_type(*obj))
552 case LISP_TYPE_PATTERN_CONS :
561 { "any", LISP_PATTERN_ANY },
562 { "symbol", LISP_PATTERN_SYMBOL },
563 { "string", LISP_PATTERN_STRING },
564 { "integer", LISP_PATTERN_INTEGER },
565 { "real", LISP_PATTERN_REAL },
566 { "boolean", LISP_PATTERN_BOOLEAN },
567 { "list", LISP_PATTERN_LIST },
568 { "or", LISP_PATTERN_OR },
574 lisp_object_t *pattern;
577 if (lisp_type(lisp_car(*obj)) != LISP_TYPE_SYMBOL)
580 type_name = lisp_symbol(lisp_car(*obj));
581 for (i = 0; types[i].name != 0; ++i)
583 if (strcmp(types[i].name, type_name) == 0)
585 type = types[i].type;
590 if (types[i].name == 0)
593 if (type != LISP_PATTERN_OR && lisp_cdr(*obj) != 0)
596 pattern = lisp_make_pattern_var(type, (*index)++, lisp_nil());
598 if (type == LISP_PATTERN_OR)
600 lisp_object_t *cdr = lisp_cdr(*obj);
602 if (!_compile_pattern(&cdr, index))
608 pattern->v.pattern.sub = cdr;
610 (*obj)->v.cons.cdr = lisp_nil();
619 case LISP_TYPE_CONS :
620 if (!_compile_pattern(&(*obj)->v.cons.car, index))
622 if (!_compile_pattern(&(*obj)->v.cons.cdr, index))
631 lisp_compile_pattern (lisp_object_t **obj, int *num_subs)
636 result = _compile_pattern(obj, &index);
638 if (result && num_subs != 0)
644 static int _match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars);
647 _match_pattern_var (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars)
649 if (lisp_type(pattern) != LISP_TYPE_PATTERN_VAR)
650 throw LispReaderException("_match_pattern_var", __FILE__, __LINE__);
652 switch (pattern->v.pattern.type)
654 case LISP_PATTERN_ANY :
657 case LISP_PATTERN_SYMBOL :
658 if (obj == 0 || lisp_type(obj) != LISP_TYPE_SYMBOL)
662 case LISP_PATTERN_STRING :
663 if (obj == 0 || lisp_type(obj) != LISP_TYPE_STRING)
667 case LISP_PATTERN_INTEGER :
668 if (obj == 0 || lisp_type(obj) != LISP_TYPE_INTEGER)
672 case LISP_PATTERN_REAL :
673 if (obj == 0 || lisp_type(obj) != LISP_TYPE_REAL)
677 case LISP_PATTERN_BOOLEAN :
678 if (obj == 0 || lisp_type(obj) != LISP_TYPE_BOOLEAN)
682 case LISP_PATTERN_LIST :
683 if (obj == 0 || lisp_type(obj) != LISP_TYPE_CONS)
687 case LISP_PATTERN_OR :
692 for (sub = pattern->v.pattern.sub; sub != 0; sub = lisp_cdr(sub))
694 if (lisp_type(sub) != LISP_TYPE_CONS)
695 throw LispReaderException("_match_pattern_var()", __FILE__, __LINE__);
697 if (_match_pattern(lisp_car(sub), obj, vars))
707 throw LispReaderException("_match_pattern_var()", __FILE__, __LINE__);
711 vars[pattern->v.pattern.index] = obj;
717 _match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars)
725 if (lisp_type(pattern) == LISP_TYPE_PATTERN_VAR)
726 return _match_pattern_var(pattern, obj, vars);
728 if (lisp_type(pattern) != lisp_type(obj))
731 switch (lisp_type(pattern))
733 case LISP_TYPE_SYMBOL :
734 return strcmp(lisp_symbol(pattern), lisp_symbol(obj)) == 0;
736 case LISP_TYPE_STRING :
737 return strcmp(lisp_string(pattern), lisp_string(obj)) == 0;
739 case LISP_TYPE_INTEGER :
740 return lisp_integer(pattern) == lisp_integer(obj);
742 case LISP_TYPE_REAL :
743 return lisp_real(pattern) == lisp_real(obj);
745 case LISP_TYPE_CONS :
747 int result1, result2;
749 result1 = _match_pattern(lisp_car(pattern), lisp_car(obj), vars);
750 result2 = _match_pattern(lisp_cdr(pattern), lisp_cdr(obj), vars);
752 return result1 && result2;
757 throw LispReaderException("_match_pattern()", __FILE__, __LINE__);
764 lisp_match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars, int num_subs)
769 for (i = 0; i < num_subs; ++i)
770 vars[i] = &error_object;
772 return _match_pattern(pattern, obj, vars);
776 lisp_match_string (const char *pattern_string, lisp_object_t *obj, lisp_object_t **vars)
778 lisp_object_t *pattern;
782 pattern = lisp_read_from_string(pattern_string);
784 if (pattern != 0 && (lisp_type(pattern) == LISP_TYPE_EOF
785 || lisp_type(pattern) == LISP_TYPE_PARSE_ERROR))
788 if (!lisp_compile_pattern(&pattern, &num_subs))
794 result = lisp_match_pattern(pattern, obj, vars, num_subs);
802 lisp_type (lisp_object_t *obj)
805 return LISP_TYPE_NIL;
810 lisp_integer (lisp_object_t *obj)
812 if (obj->type != LISP_TYPE_INTEGER)
813 throw LispReaderException("lisp_integer()", __FILE__, __LINE__);
815 return obj->v.integer;
819 lisp_symbol (lisp_object_t *obj)
821 if (obj->type != LISP_TYPE_SYMBOL)
822 throw LispReaderException("lisp_symbol()", __FILE__, __LINE__);
824 return obj->v.string;
828 lisp_string (lisp_object_t *obj)
830 if (obj->type != LISP_TYPE_STRING)
831 throw LispReaderException("lisp_string()", __FILE__, __LINE__);
833 return obj->v.string;
837 lisp_boolean (lisp_object_t *obj)
839 if (obj->type != LISP_TYPE_BOOLEAN)
840 throw LispReaderException("lisp_boolean()", __FILE__, __LINE__);
842 return obj->v.integer;
846 lisp_real (lisp_object_t *obj)
848 if (obj->type != LISP_TYPE_REAL && obj->type != LISP_TYPE_INTEGER)
849 throw LispReaderException("lisp_real()", __FILE__, __LINE__);
851 if (obj->type == LISP_TYPE_INTEGER)
852 return obj->v.integer;
857 lisp_car (lisp_object_t *obj)
859 if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
860 throw LispReaderException("lisp_car()", __FILE__, __LINE__);
862 return obj->v.cons.car;
866 lisp_cdr (lisp_object_t *obj)
868 if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
869 throw LispReaderException("lisp_cdr()", __FILE__, __LINE__);
871 return obj->v.cons.cdr;
875 lisp_cxr (lisp_object_t *obj, const char *x)
879 for (i = strlen(x) - 1; i >= 0; --i)
882 else if (x[i] == 'd')
885 throw LispReaderException("lisp_cxr()", __FILE__, __LINE__);
891 lisp_list_length (lisp_object_t *obj)
897 if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
898 throw LispReaderException("lisp_list_length()", __FILE__, __LINE__);
901 obj = obj->v.cons.cdr;
908 lisp_list_nth_cdr (lisp_object_t *obj, int index)
913 throw LispReaderException("lisp_list_nth_cdr()", __FILE__, __LINE__);
914 if (obj->type != LISP_TYPE_CONS && obj->type != LISP_TYPE_PATTERN_CONS)
915 throw LispReaderException("lisp_list_nth_cdr()", __FILE__, __LINE__);
918 obj = obj->v.cons.cdr;
925 lisp_list_nth (lisp_object_t *obj, int index)
927 obj = lisp_list_nth_cdr(obj, index);
930 throw LispReaderException("lisp_list_nth()", __FILE__, __LINE__);
932 return obj->v.cons.car;
936 lisp_dump (lisp_object_t *obj, FILE *out)
944 switch (lisp_type(obj))
947 fputs("#<eof>", out);
950 case LISP_TYPE_PARSE_ERROR :
951 fputs("#<error>", out);
954 case LISP_TYPE_INTEGER :
955 fprintf(out, "%d", lisp_integer(obj));
958 case LISP_TYPE_REAL :
959 fprintf(out, "%f", lisp_real(obj));
962 case LISP_TYPE_SYMBOL :
963 fputs(lisp_symbol(obj), out);
966 case LISP_TYPE_STRING :
971 for (p = lisp_string(obj); *p != 0; ++p)
973 if (*p == '"' || *p == '\\')
981 case LISP_TYPE_CONS :
982 case LISP_TYPE_PATTERN_CONS :
983 fputs(lisp_type(obj) == LISP_TYPE_CONS ? "(" : "#?(", out);
986 lisp_dump(lisp_car(obj), out);
990 if (lisp_type(obj) != LISP_TYPE_CONS
991 && lisp_type(obj) != LISP_TYPE_PATTERN_CONS)
1004 case LISP_TYPE_BOOLEAN :
1005 if (lisp_boolean(obj))
1012 throw LispReaderException("lisp_dump()", __FILE__, __LINE__);
1016 using namespace std;
1018 LispReader::LispReader (lisp_object_t* l)
1023 LispReader::~LispReader()
1030 LispReader::load(const std::string& filename, const std::string& toplevellist)
1032 lisp_object_t* obj = lisp_read_from_file(filename);
1034 if(obj->type == LISP_TYPE_EOF || obj->type == LISP_TYPE_PARSE_ERROR) {
1036 throw LispReaderException("LispReader::load", __FILE__, __LINE__);
1039 if(toplevellist != lisp_symbol(lisp_car(obj))) {
1041 throw LispReaderException("LispReader::load wrong toplevel symbol",
1042 __FILE__, __LINE__);
1045 LispReader* reader = new LispReader(lisp_cdr(obj));
1046 reader->owner = obj;
1052 LispReader::search_for(const char* name)
1054 //std::cout << "LispReader::search_for(" << name << ")" << std::endl;
1055 lisp_object_t* cursor = lst;
1057 while(!lisp_nil_p(cursor))
1059 lisp_object_t* cur = lisp_car(cursor);
1061 if (!lisp_cons_p(cur) || !lisp_symbol_p (lisp_car(cur)))
1063 lisp_dump(cur, stdout);
1064 //throw ConstruoError (std::string("LispReader: Read error in search_for ") + name);
1065 printf("LispReader: Read error in search\n");
1069 if (strcmp(lisp_symbol(lisp_car(cur)), name) == 0)
1071 return lisp_cdr(cur);
1075 cursor = lisp_cdr (cursor);
1081 LispReader::read_int (const char* name, int& i)
1083 lisp_object_t* obj = search_for (name);
1087 if (!lisp_integer_p(lisp_car(obj)))
1090 i = lisp_integer(lisp_car(obj));
1095 LispReader::read_lisp(const char* name, lisp_object_t*& b)
1097 lisp_object_t* obj = search_for (name);
1106 LispReader::read_lisp(const char* name)
1108 lisp_object_t* obj = search_for(name);
1112 return new LispReader(obj);
1116 LispReader::read_float (const char* name, float& f)
1118 lisp_object_t* obj = search_for (name);
1122 if (!lisp_real_p(lisp_car(obj)) && !lisp_integer_p(lisp_car(obj)))
1123 st_abort("LispReader expected type real at token: ", name);
1125 f = lisp_real(lisp_car(obj));
1130 LispReader::read_string_vector (const char* name, std::vector<std::string>& vec)
1132 lisp_object_t* obj = search_for (name);
1137 while(!lisp_nil_p(obj))
1139 if (!lisp_string_p(lisp_car(obj)))
1140 st_abort("LispReader expected type string at token: ", name);
1141 vec.push_back(lisp_string(lisp_car(obj)));
1142 obj = lisp_cdr(obj);
1148 LispReader::read_int_vector (const char* name, std::vector<int>& vec)
1150 lisp_object_t* obj = search_for (name);
1155 while(!lisp_nil_p(obj))
1157 if (!lisp_integer_p(lisp_car(obj)))
1158 st_abort("LispReader expected type integer at token: ", name);
1159 vec.push_back(lisp_integer(lisp_car(obj)));
1160 obj = lisp_cdr(obj);
1166 LispReader::read_int_vector (const char* name, std::vector<unsigned int>& vec)
1168 lisp_object_t* obj = search_for (name);
1173 while(!lisp_nil_p(obj))
1175 if (!lisp_integer_p(lisp_car(obj)))
1176 st_abort("LispReader expected type integer at token: ", name);
1177 vec.push_back(lisp_integer(lisp_car(obj)));
1178 obj = lisp_cdr(obj);
1184 LispReader::read_char_vector (const char* name, std::vector<char>& vec)
1186 lisp_object_t* obj = search_for (name);
1191 while(!lisp_nil_p(obj))
1193 vec.push_back(*lisp_string(lisp_car(obj)));
1194 obj = lisp_cdr(obj);
1200 LispReader::read_string (const char* name, std::string& str)
1202 lisp_object_t* obj = search_for (name);
1206 if (!lisp_string_p(lisp_car(obj)))
1207 st_abort("LispReader expected type string at token: ", name);
1208 str = lisp_string(lisp_car(obj));
1213 LispReader::read_bool (const char* name, bool& b)
1215 lisp_object_t* obj = search_for (name);
1219 if (!lisp_boolean_p(lisp_car(obj)))
1220 st_abort("LispReader expected type bool at token: ", name);
1221 b = lisp_boolean(lisp_car(obj));
1226 LispReader::get_lisp()
1231 lisp_object_t* lisp_read_from_file(const std::string& filename)
1233 FILE* in = fopen(filename.c_str(), "r");
1238 lisp_stream_t stream;
1239 lisp_stream_init_file(&stream, in);
1240 lisp_object_t* obj = lisp_read(&stream);