/
scheme.c
174 lines (143 loc) · 3.87 KB
/
scheme.c
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
/*
* Bootstrap Scheme - a quick and very dirty Scheme interpreter.
* Copyright (C) 2010 Peter Michaux (http://peter.michaux.ca/)
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public
* License version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License version 3 for more details.
*
* You should have received a copy of the GNU Affero General Public
* License version 3 along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
/**************************** MODEL ******************************/
typedef enum {FIXNUM} object_type;
typedef struct object {
object_type type;
union {
struct {
long value;
} fixnum;
} data;
} object;
/* no GC so truely "unlimited extent" */
object *alloc_object(void) {
object *obj;
obj = malloc(sizeof(object));
if (obj == NULL) {
fprintf(stderr, "out of memory\n");
exit(1);
}
return obj;
}
object *make_fixnum(long value) {
object *obj;
obj = alloc_object();
obj->type = FIXNUM;
obj->data.fixnum.value = value;
return obj;
}
char is_fixnum(object *obj) {
return obj->type == FIXNUM;
}
/***************************** READ ******************************/
char is_delimiter(int c) {
return isspace(c) || c == EOF ||
c == '(' || c == ')' ||
c == '"' || c == ';';
}
int peek(FILE *in) {
int c;
c = getc(in);
ungetc(c, in);
return c;
}
void eat_whitespace(FILE *in) {
int c;
while ((c = getc(in)) != EOF) {
if (isspace(c)) {
continue;
}
else if (c == ';') { /* comments are whitespace also */
while (((c = getc(in)) != EOF) && (c != '\n'));
continue;
}
ungetc(c, in);
break;
}
}
object *read(FILE *in) {
int c;
short sign = 1;
long num = 0;
eat_whitespace(in);
c = getc(in);
if (isdigit(c) || (c == '-' && (isdigit(peek(in))))) {
/* read a fixnum */
if (c == '-') {
sign = -1;
}
else {
ungetc(c, in);
}
while (isdigit(c = getc(in))) {
num = (num * 10) + (c - '0');
}
num *= sign;
if (is_delimiter(c)) {
ungetc(c, in);
return make_fixnum(num);
}
else {
fprintf(stderr, "number not followed by delimiter\n");
exit(1);
}
}
else {
fprintf(stderr, "bad input. Unexpected '%c'\n", c);
exit(1);
}
fprintf(stderr, "read illegal state\n");
exit(1);
}
/*************************** EVALUATE ****************************/
/* until we have lists and symbols just echo */
object *eval(object *exp) {
return exp;
}
/**************************** PRINT ******************************/
void write(object *obj) {
switch (obj->type) {
case FIXNUM:
printf("%ld", obj->data.fixnum.value);
break;
default:
fprintf(stderr, "cannot write unknown type\n");
exit(1);
}
}
/***************************** REPL ******************************/
int main(void) {
printf("Welcome to Bootstrap Scheme. "
"Use ctrl-c to exit.\n");
while (1) {
printf("> ");
write(eval(read(stdin)));
printf("\n");
}
return 0;
}
/**************************** MUSIC *******************************
Slipknot, Neil Young, Pearl Jam, The Dead Weather,
Dave Matthews Band, Alice in Chains, White Zombie, Blind Melon,
Priestess, Puscifer
*/