-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol.c
44 lines (37 loc) · 1.09 KB
/
symbol.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
#include <string.h>
#include "lilscheme.h"
// TODO: what is the type checking policy? Should primitives protect the user,
// or should these routines protect _me_?
// Symbols
// todo: reconsider the policy on case-sensitivity
Handle CreateSymbol(const char *name) {
// Despite the name of this procedure, this will only create a new symbol
// if one is already in the interned symbols list
Handle sym = FindSymbolNamed(name);
if (sym == nil) {
size_t len = strlen(name);
sym = CreateObject(TYPE_SYMBOL, len+1); // null termination
Retain(sym);
strcpy(DATA_AREA(char, sym), name);
internedSymbols = CreateCons(sym, internedSymbols);
Unretain(sym);
}
return sym;
}
Handle FindSymbolNamed(const char *desiredName) {
Handle here = internedSymbols;
while (here != nil) {
Handle sym = Car(here);
if (strcmp(NameOfSymbol(sym), desiredName) == 0) {
return sym;
}
here = Cdr(here);
}
return nil;
}
char *NameOfSymbol(Handle sym) {
if (DEREF(sym)->type != TYPE_SYMBOL) {
panic("can't NameOfSymbol that type");
}
return DATA_AREA(char, sym);
}