aboutsummaryrefslogtreecommitdiff
path: root/system/kernel/pdp_symbol.c
blob: 32e9e3342fce010cdce0a1ddc2c8f72420d63025 (plain)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 *   Pure Data Packet system implementation. : code implementing pdp's namespace (symbols)
 *   Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include <string.h>
#include <pthread.h>
#include "pdp_symbol.h"
#include "pdp_list.h"
#include "pdp_debug.h"

// some extra prototypes
void *pdp_alloc(int size);
void pdp_dealloc(void *data);

// the symbol hash mutex
static pthread_mutex_t pdp_hash_mutex;

#define HASHSIZE 1024
static t_pdp_symbol *pdp_symhash[HASHSIZE];


#define LOCK pthread_mutex_lock(&pdp_hash_mutex)
#define UNLOCK pthread_mutex_unlock(&pdp_hash_mutex)


static void _pdp_symbol_init(t_pdp_symbol *s)
{
    memset(s, 0, sizeof(*s));
    s->s_forth.t = a_undef;
}


/* shamelessly copied from pd src and made thread safe */
t_pdp_symbol *_pdp_dogensym(char *s, t_pdp_symbol *oldsym)
{
    t_pdp_symbol **sym1, *sym2;
    unsigned int hash1 = 0,  hash2 = 0;
    int length = 0;
    char *s2 = s;
    while (*s2)
    {
        hash1 += *s2;
        hash2 += hash1;
        length++;
        s2++;
    }
    sym1 = pdp_symhash + (hash2 & (HASHSIZE-1));

    /* lock hash */
    LOCK;

    while (sym2 = *sym1)
    {
        if (!strcmp(sym2->s_name, s)) goto gotit;
        sym1 = &sym2->s_next;
    }
    if (oldsym){
	sym2 = oldsym;
    }
    else
    {
        sym2 = (t_pdp_symbol *)pdp_alloc(sizeof(*sym2));
	_pdp_symbol_init(sym2);
        sym2->s_name = pdp_alloc(length+1);
        sym2->s_next = 0;
        strcpy(sym2->s_name, s);
    }
    *sym1 = sym2;

 gotit:

    /* unlock hash */
    UNLOCK;
    return (sym2);
}

t_pdp_symbol *pdp_gensym(char *s)
{
    return(_pdp_dogensym(s, 0));
}


/* connect a parsed typelist to a symbol type name
   1 = succes, 0 = error (symbol already connected) */
int pdp_symbol_set_typelist(t_pdp_symbol *s, t_pdp_list *typelist)
{
    int status = 0;
    LOCK;
    if (!s->s_type){
	s->s_type = typelist;
	status = 1;
    }
    UNLOCK;
    return status;
}


void pdp_symbol_apply_all(t_pdp_symbol_iterator it)
{
    int i;
    for (i=0; i<HASHSIZE; i++){
	t_pdp_symbol *s;
	for (s = pdp_symhash[i]; s; s=s->s_next){
	    it(s);
	}
	
    }
}

t_pdp_symbol _pdp_sym_wildcard;
t_pdp_symbol _pdp_sym_float;
t_pdp_symbol _pdp_sym_int;
t_pdp_symbol _pdp_sym_symbol;
t_pdp_symbol _pdp_sym_packet;
t_pdp_symbol _pdp_sym_pointer;
t_pdp_symbol _pdp_sym_invalid;
t_pdp_symbol _pdp_sym_list;
t_pdp_symbol _pdp_sym_question_mark;
t_pdp_symbol _pdp_sym_atom;
t_pdp_symbol _pdp_sym_null;
t_pdp_symbol _pdp_sym_quote_start;
t_pdp_symbol _pdp_sym_quote_end;
t_pdp_symbol _pdp_sym_return;
t_pdp_symbol _pdp_sym_nreturn;
t_pdp_symbol _pdp_sym_defstart;
t_pdp_symbol _pdp_sym_defend;
t_pdp_symbol _pdp_sym_if;
t_pdp_symbol _pdp_sym_then;
t_pdp_symbol _pdp_sym_local;
t_pdp_symbol _pdp_sym_forth;
t_pdp_symbol _pdp_sym_call;
t_pdp_symbol _pdp_sym_push;
t_pdp_symbol _pdp_sym_pop;

static void _sym(char *name, t_pdp_symbol *s)
{
    t_pdp_symbol *realsym;
    _pdp_symbol_init(s);
    s->s_name = name;
    realsym = _pdp_dogensym(name, s); 
    PDP_ASSERT(realsym == s); // if this fails, the symbol was already defined
}

void pdp_symbol_setup(void)
{
    // create mutexes
    pthread_mutex_init(&pdp_hash_mutex, NULL);

    // init symbol hash
    memset(pdp_symhash, 0, HASHSIZE * sizeof(t_pdp_symbol *));

    // setup predefined symbols (those that have direct pointer access for speedup)
    _sym("*",        &_pdp_sym_wildcard);
    _sym("float",    &_pdp_sym_float);
    _sym("int",      &_pdp_sym_int);
    _sym("symbol",   &_pdp_sym_symbol);
    _sym("packet",   &_pdp_sym_packet);
    _sym("pointer",  &_pdp_sym_pointer);
    _sym("invalid",  &_pdp_sym_invalid);
    _sym("list",     &_pdp_sym_list);
    _sym("?",        &_pdp_sym_question_mark);
    _sym("atom",     &_pdp_sym_atom);
    _sym("null",     &_pdp_sym_null);
    _sym("[",        &_pdp_sym_quote_start);
    _sym("]",        &_pdp_sym_quote_end);
    _sym("ret",      &_pdp_sym_return);
    _sym("nret",     &_pdp_sym_nreturn);
    _sym(":",        &_pdp_sym_defstart);
    _sym(";",        &_pdp_sym_defend);
    _sym("if",       &_pdp_sym_if);
    _sym("then",     &_pdp_sym_then);
    _sym("local",    &_pdp_sym_local);
    _sym("forth",    &_pdp_sym_forth);
    _sym("call",     &_pdp_sym_call);
    _sym("push",     &_pdp_sym_push);
    _sym("pop",      &_pdp_sym_pop);

}