~brenns10/funlisp

ref: 7347d0eca6d0a50799d3048c21f376277e827224 funlisp/src/parse.c -rw-r--r-- 4.4 KiB
7347d0ecStephen Brennan Updates for readthedocs 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * parse.c: recursive descent parser for funlisp
 *
 * Stephen Brennan <stephen@brennan.io>
 */

#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>

#include "funlisp_internal.h"
#include "charbuf.h"

typedef struct {
	lisp_value *result;
	int index;
} result;

result lisp_parse_value(lisp_runtime *rt, char *input, int index);

result lisp_parse_integer(lisp_runtime *rt, char *input, int index)
{
	int n;
	lisp_integer *v = (lisp_integer*)lisp_new(rt, type_integer);
	sscanf(input + index, "%d%n", &v->x, &n);
	return (result){(lisp_value*)v, index + n};
}

char lisp_escape(char escape)
{
	switch (escape) {
	case 'a':
		return '\a';
	case 'b':
		return '\b';
	case 'f':
		return '\f';
	case 'n':
		return '\n';
	case 'r':
		return '\b';
	case 't':
		return '\t';
	case 'v':
		return '\v';
	default:
		return escape;
	}
}

result lisp_parse_string(lisp_runtime *rt, char *input, int index)
{
	int i = index + 1;
	struct charbuf cb;
	cb_init(&cb, 16);
	while (input[i] && input[i] != '"') {
		if (input[i] == '\\') {
			cb_append(&cb, lisp_escape(input[++i]));
		} else {
			cb_append(&cb, input[i]);
		}
		i++;
	}
	cb_trim(&cb);
	lisp_string *str = (lisp_string*)lisp_new(rt, type_string);
	str->s = cb.buf;
	return (result){(lisp_value*)str, ++i};
}

result lisp_parse_list_or_sexp(lisp_runtime *rt, char *input, int index)
{
	while (isspace(input[index])) {index++;}
	if (input[index] == ')') {
		return (result){(lisp_value*)lisp_nil_new(rt), index + 1};
	}

	result r = lisp_parse_value(rt, input, index);
	index = r.index;
	lisp_list *rv = (lisp_list*)lisp_new(rt, type_list);
	rv->left = r.result;
	lisp_list *l = rv;

	while (true) {
		while (isspace(input[index])) {
			index++;
		}

		if (input[index] == '.') {
			index++;
			result r = lisp_parse_value(rt, input, index);
			index = r.index;
			l->right = r.result;
			return (result){(lisp_value*)rv, index};
		} else if (input[index] == ')') {
			index++;
			l->right = lisp_nil_new(rt);
			return (result){(lisp_value*)rv, index};
		} else {
			result r = lisp_parse_value(rt, input, index);
			l->right = lisp_new(rt, type_list);
			l = (lisp_list*)l->right;
			l->left = r.result;
			index = r.index;
		}
	}
}

result lisp_parse_symbol(lisp_runtime *rt, char *input, int index)
{
	int n = 0;
	while (input[index + n] && !isspace(input[index + n]) &&
	       input[index + n] != ')' && input[index + n] != '.' &&
	       input[index + n] != '\'') {
		n++;
	}
	lisp_symbol *s = (lisp_symbol*)lisp_new(rt, type_symbol);
	s->sym = malloc(n + 1);
	strncpy(s->sym, input + index, n);
	s->sym[n] = '\0';
	return (result){(lisp_value*)s, index + n};
}

result lisp_parse_quote(lisp_runtime *rt, char *input, int index)
{
	result r = lisp_parse_value(rt, input, index + 1);
	r.result = lisp_quote(rt, r.result);
	return r;
}

result lisp_parse_value(lisp_runtime *rt, char *input, int index)
{
	while (isspace(input[index])) {
		index++;
	}

	if (input[index] == '"') {
		return lisp_parse_string(rt, input, index);
	}
	if (input[index] == '\0') {
		return (result){NULL, index};
	}
	if (input[index] == ')') {
		return (result){lisp_nil_new(rt), index + 1};
	}
	if (input[index] == '(') {
		return lisp_parse_list_or_sexp(rt, input, index + 1);
	}
	if (input[index] == '\'') {
		return lisp_parse_quote(rt, input, index);
	}
	if (isdigit(input[index])) {
		return lisp_parse_integer(rt, input, index);
	}
	return lisp_parse_symbol(rt, input, index);
}

lisp_value *lisp_parse(lisp_runtime *rt, char *input)
{
	return lisp_parse_value(rt, input, 0).result;
}

static char *read_file(FILE *input)
{
	size_t bufsize = 1024;
	size_t length = 0;
	char *buf = malloc(1024);

	while (!feof(input) && !ferror(input)) {
		length += fread(buf, sizeof(char), bufsize - length, input);
		if (length >= bufsize) {
			bufsize *= 2;
			buf = realloc(buf, bufsize);
		}
	}

	if (feof(input)) {
		buf[length] = '\0';
		return buf;
	} else {
		free(buf);
		return NULL;
	}
}

lisp_value *lisp_load_file(lisp_runtime *rt, lisp_scope *scope, FILE *input)
{
	char *contents = read_file(input);
	lisp_value *last_val = NULL;
	result r;
	r.index = 0;
	r.result = NULL;

	if (!contents)
		return NULL;

	for (;;) {
		r = lisp_parse_value(rt, contents, r.index);
		if (!r.result) {
			lisp_mark(rt, (lisp_value *) scope);
			if (last_val)
				lisp_mark(rt, last_val);
			lisp_sweep(rt);
			free(contents);
			return last_val;
		}
		last_val = lisp_eval(rt, scope, r.result);
	}
}