This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsymbol.c
101 lines (79 loc) · 2.07 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
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
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
#include "object.h"
#include "state.h"
#define to_cstr(a) (pic_str(pic, obj_value(pic, a), NULL))
#define kh_pic_str_hash(a) (kh_str_hash_func(to_cstr(a)))
#define kh_pic_str_cmp(a, b) (kh_str_cmp_func(to_cstr(a), to_cstr(b)))
KHASH_DEFINE(oblist, struct string *, struct symbol *, kh_pic_str_hash, kh_pic_str_cmp)
pic_value
pic_intern(pic_state *pic, pic_value str)
{
khash_t(oblist) *h = &pic->oblist;
struct symbol *sym;
int it;
int ret;
it = kh_put(oblist, h, str_ptr(pic, str), &ret);
if (ret == 0) { /* if exists */
sym = kh_val(h, it);
pic_protect(pic, obj_value(pic, sym));
return obj_value(pic, sym);
}
kh_val(h, it) = NULL; /* dummy */
sym = (struct symbol *)pic_obj_alloc(pic, PIC_TYPE_SYMBOL);
sym->str = str_ptr(pic, str);
kh_val(h, it) = sym;
return obj_value(pic, sym);
}
pic_value
pic_sym_name(pic_state *pic, pic_value sym)
{
return obj_value(pic, sym_ptr(pic, sym)->str);
}
static pic_value
pic_symbol_symbol_p(pic_state *pic)
{
pic_value v;
pic_get_args(pic, "o", &v);
return pic_bool_value(pic, pic_sym_p(pic, v));
}
static pic_value
pic_symbol_symbol_eq_p(pic_state *pic)
{
int argc, i;
pic_value *argv;
pic_get_args(pic, "*", &argc, &argv);
for (i = 0; i < argc; ++i) {
if (! pic_sym_p(pic, argv[i])) {
return pic_false_value(pic);
}
if (! pic_eq_p(pic, argv[i], argv[0])) {
return pic_false_value(pic);
}
}
return pic_true_value(pic);
}
static pic_value
pic_symbol_symbol_to_string(pic_state *pic)
{
pic_value sym;
pic_get_args(pic, "m", &sym);
return pic_sym_name(pic, sym);
}
static pic_value
pic_symbol_string_to_symbol(pic_state *pic)
{
pic_value str;
pic_get_args(pic, "s", &str);
return pic_intern(pic, str);
}
void
pic_init_symbol(pic_state *pic)
{
pic_defun(pic, "symbol?", pic_symbol_symbol_p);
pic_defun(pic, "symbol=?", pic_symbol_symbol_eq_p);
pic_defun(pic, "symbol->string", pic_symbol_symbol_to_string);
pic_defun(pic, "string->symbol", pic_symbol_string_to_symbol);
}