-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymbol.c
72 lines (60 loc) · 1.65 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
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
#include <stdlib.h>
#include <string.h>
#include "cons.h"
#include "object.h"
#include "symbol.h"
static pobject symbol_table = NIL;
static pobject symbol_new_by_slice(char *value, int start, int end)
{
int len = end - start;
pobject o = object_new(T_SYMBOL);
symbol_value_set(o, malloc(len + 1));
strncpy(symbol_value(o), value + start, len);
symbol_value(o)[len] = '\0';
o->data.symbol.length = len;
return o;
}
pobject symbol_intern(char *value)
{
return symbol_intern_by_slice(value, 0, strlen(value));
}
pobject symbol_intern_by_slice(char *value, int start, int end)
{
pobject result = NIL, cur = symbol_table;
char *str;
int len = end - start, i;
while (cur) {
result = cons_car(cur);
str = symbol_value(result);
for (i = 0; i < len; ++i) {
if ((str[i] == '\0') || (str[i] != value[start + i])) {
result = NIL;
break;
}
}
if (result && (str[i] == '\0'))
return result;
cur = cons_cdr(cur);
}
result = symbol_new_by_slice(value, start, end);
cons_stack_push(&symbol_table, result, 0);
return result;
}
int symbol_ends_with_three_dots(pobject symbol)
{
int len = symbol_length(symbol);
return (len > 2)
&& (symbol_value(symbol)[len-1] == '.')
&& (symbol_value(symbol)[len-2] == '.')
&& (symbol_value(symbol)[len-3] == '.');
}
void symbol_cleanup()
{
pobject next;
while (is_cons(symbol_table)) {
next = cons_cdr(symbol_table);
object_free(cons_car(symbol_table));
object_free(symbol_table);
symbol_table = next;
}
}