1 /*
2  * lispreader.h
3  *
4  * Copyright (C) 1998-2004 Mark Probst
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 #ifndef __LISPREADER_H__
23 #define __LISPREADER_H__
24 
25 #include <stdio.h>
26 
27 #include <allocator.h>
28 
29 #define LISP_STREAM_MMAP_FILE  1
30 #define LISP_STREAM_STRING     2
31 #define LISP_STREAM_FILE       3
32 #define LISP_STREAM_ANY        4
33 
34 #define LISP_LAST_MMAPPED_STREAM   LISP_STREAM_STRING
35 
36 #define LISP_TYPE_INTERNAL      -3
37 #define LISP_TYPE_PARSE_ERROR   -2
38 #define LISP_TYPE_EOF           -1
39 #define LISP_TYPE_NIL           0
40 #define LISP_TYPE_SYMBOL        1
41 #define LISP_TYPE_INTEGER       2
42 #define LISP_TYPE_STRING        3
43 #define LISP_TYPE_REAL          4
44 #define LISP_TYPE_CONS          5
45 #define LISP_TYPE_PATTERN_CONS  6
46 #define LISP_TYPE_BOOLEAN       7
47 #define LISP_TYPE_PATTERN_VAR   8
48 
49 #define LISP_PATTERN_ANY        1
50 #define LISP_PATTERN_SYMBOL     2
51 #define LISP_PATTERN_STRING     3
52 #define LISP_PATTERN_INTEGER    4
53 #define LISP_PATTERN_REAL       5
54 #define LISP_PATTERN_BOOLEAN    6
55 #define LISP_PATTERN_LIST       7
56 #define LISP_PATTERN_OR         8
57 #define LISP_PATTERN_NUMBER     9
58 
59 typedef struct
60 {
61     int type;
62 
63     union
64     {
65 	FILE *file;
66 	struct
67 	{
68 	    char *buf;
69 	    char *end;
70 	    char *pos;
71 	} mmap;
72         struct
73 	{
74 	    void *data;
75 	    int (*next_char) (void *data);
76 	    void (*unget_char) (char c, void *data);
77 	} any;
78     } v;
79 } lisp_stream_t;
80 
81 typedef struct _lisp_object_t lisp_object_t;
82 struct _lisp_object_t
83 {
84     int type;
85 
86     union
87     {
88 	struct
89 	{
90 	    struct _lisp_object_t *car;
91 	    struct _lisp_object_t *cdr;
92 	} cons;
93 
94 	char *string;
95 	int integer;
96 	float real;
97 
98 	struct
99 	{
100 	    int type;
101 	    int index;
102 	    struct _lisp_object_t *sub;
103 	} pattern;
104     } v;
105 };
106 
107 lisp_stream_t* lisp_stream_init_path (lisp_stream_t *stream, const char *path);
108 lisp_stream_t* lisp_stream_init_file (lisp_stream_t *stream, FILE *file);
109 lisp_stream_t* lisp_stream_init_string (lisp_stream_t *stream, char *buf);
110 lisp_stream_t* lisp_stream_init_any (lisp_stream_t *stream, void *data,
111 				     int (*next_char) (void *data),
112 				     void (*unget_char) (char c, void *data));
113 
114 void lisp_stream_free_path  (lisp_stream_t *stream);
115 
116 lisp_object_t* lisp_read_with_allocator (allocator_t *allocator, lisp_stream_t *in);
117 lisp_object_t* lisp_read (lisp_stream_t *in);
118 
119 void lisp_free_with_allocator (allocator_t *allocator, lisp_object_t *obj);
120 void lisp_free (lisp_object_t *obj);
121 
122 lisp_object_t* lisp_read_from_string_with_allocator (allocator_t *allocator, const char *buf);
123 lisp_object_t* lisp_read_from_string (const char *buf);
124 
125 int lisp_compile_pattern (lisp_object_t **obj, int *num_subs);
126 int lisp_match_pattern (lisp_object_t *pattern, lisp_object_t *obj, lisp_object_t **vars, int num_subs);
127 int lisp_match_string (const char *pattern_string, lisp_object_t *obj, lisp_object_t **vars);
128 
129 int lisp_type (lisp_object_t *obj);
130 int lisp_integer (lisp_object_t *obj);
131 float lisp_real (lisp_object_t *obj);
132 char* lisp_symbol (lisp_object_t *obj);
133 char* lisp_string (lisp_object_t *obj);
134 int lisp_boolean (lisp_object_t *obj);
135 lisp_object_t* lisp_car (lisp_object_t *obj);
136 lisp_object_t* lisp_cdr (lisp_object_t *obj);
137 
138 lisp_object_t* lisp_cxr (lisp_object_t *obj, const char *x);
139 
140 lisp_object_t* lisp_make_integer_with_allocator (allocator_t *allocator, int value);
141 lisp_object_t* lisp_make_real_with_allocator (allocator_t *allocator, float value);
142 lisp_object_t* lisp_make_symbol_with_allocator (allocator_t *allocator, const char *value);
143 lisp_object_t* lisp_make_string_with_allocator (allocator_t *allocator, const char *value);
144 lisp_object_t* lisp_make_cons_with_allocator (allocator_t *allocator, lisp_object_t *car, lisp_object_t *cdr);
145 lisp_object_t* lisp_make_boolean_with_allocator (allocator_t *allocator, int value);
146 
147 lisp_object_t* lisp_make_integer (int value);
148 lisp_object_t* lisp_make_real (float value);
149 lisp_object_t* lisp_make_symbol (const char *value);
150 lisp_object_t* lisp_make_string (const char *value);
151 lisp_object_t* lisp_make_cons (lisp_object_t *car, lisp_object_t *cdr);
152 lisp_object_t* lisp_make_boolean (int value);
153 
154 int lisp_list_length (lisp_object_t *obj);
155 lisp_object_t* lisp_list_nth_cdr (lisp_object_t *obj, int index);
156 lisp_object_t* lisp_list_nth (lisp_object_t *obj, int index);
157 
158 void lisp_dump (lisp_object_t *obj, FILE *out);
159 
160 #define lisp_nil()           ((lisp_object_t*)0)
161 
162 #define lisp_nil_p(obj)      (obj == 0)
163 #define lisp_integer_p(obj)  (lisp_type((obj)) == LISP_TYPE_INTEGER)
164 #define lisp_real_p(obj)     (lisp_type((obj)) == LISP_TYPE_REAL)
165 #define lisp_symbol_p(obj)   (lisp_type((obj)) == LISP_TYPE_SYMBOL)
166 #define lisp_string_p(obj)   (lisp_type((obj)) == LISP_TYPE_STRING)
167 #define lisp_cons_p(obj)     (lisp_type((obj)) == LISP_TYPE_CONS)
168 #define lisp_boolean_p(obj)  (lisp_type((obj)) == LISP_TYPE_BOOLEAN)
169 
170 #endif
171