-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.c
52 lines (42 loc) · 1.13 KB
/
value.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
#include "scheme.h"
#include <string.h>
#include <stdarg.h>
#define MAX_SYMBOL (1 << 10)
size_t nsyms ;
char *syms[MAX_SYMBOL] ;
void die(const char *fmt, ...) { /* i don't know where else to put it */
va_list vl ;
va_start(vl, fmt) ;
vfprintf(stderr, fmt, vl) ;
va_end(vl) ;
exit(-2) ;
}
scm_val intern(const char *s) {
size_t i ;
for (i = 0; i < nsyms; i ++)
if (strcmp(syms[i], s) == 0) return MKTAG(i, SYMBOL) ;
ENSURE(syms[nsyms] = strdup(s), "strdup()") ;
ASSERT(nsyms < MAX_SYMBOL) ;
return MKTAG(nsyms ++, SYMBOL) ;
}
const char *sym_to_string(scm_val v) {
int i = UNTAG(v) ;
ASSERT(TAG(v) == SYMBOL) ;
ASSERT(i >= 0 && i < nsyms) ;
return syms[i] ;
}
scm_val assq(scm_val alist, scm_val key) {
scm_val v ;
FOREACH(v, alist) {
ENSURE(LIST_P(v), "assq: not a list\n") ;
ENSURE(PAIR_P(CAR(v)), "assq: not a pair\n") ;
if (EQ_P(CAAR(v), key)) return CAR(v) ;
}
return FALSE ;
}
scm_val cons(scm_val car, scm_val cdr) {
scm_val v = scm_alloc_cell(CONS) ;
CAR(v) = car ;
CDR(v) = cdr ;
return v ;
}