2
* Pure Data Packet system implementation. : code implementing pdp's namespace (symbols)
3
* Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23
#include "pdp_symbol.h"
25
#include "pdp_debug.h"
27
// some extra prototypes
28
void *pdp_alloc(int size);
29
void pdp_dealloc(void *data);
31
// the symbol hash mutex
32
static pthread_mutex_t pdp_hash_mutex;
35
static t_pdp_symbol *pdp_symhash[HASHSIZE];
38
#define LOCK pthread_mutex_lock(&pdp_hash_mutex)
39
#define UNLOCK pthread_mutex_unlock(&pdp_hash_mutex)
42
static void _pdp_symbol_init(t_pdp_symbol *s)
44
memset(s, 0, sizeof(*s));
45
s->s_forth.t = a_undef;
49
/* shamelessly copied from pd src and made thread safe */
50
t_pdp_symbol *_pdp_dogensym(char *s, t_pdp_symbol *oldsym)
52
t_pdp_symbol **sym1, *sym2;
53
unsigned int hash1 = 0, hash2 = 0;
63
sym1 = pdp_symhash + (hash2 & (HASHSIZE-1));
70
if (!strcmp(sym2->s_name, s)) goto gotit;
78
sym2 = (t_pdp_symbol *)pdp_alloc(sizeof(*sym2));
79
_pdp_symbol_init(sym2);
80
sym2->s_name = pdp_alloc(length+1);
82
strcpy(sym2->s_name, s);
93
t_pdp_symbol *pdp_gensym(char *s)
95
return(_pdp_dogensym(s, 0));
99
/* connect a parsed typelist to a symbol type name
100
1 = succes, 0 = error (symbol already connected) */
101
int pdp_symbol_set_typelist(t_pdp_symbol *s, t_pdp_list *typelist)
106
s->s_type = typelist;
114
void pdp_symbol_apply_all(t_pdp_symbol_iterator it)
117
for (i=0; i<HASHSIZE; i++){
119
for (s = pdp_symhash[i]; s; s=s->s_next){
126
t_pdp_symbol _pdp_sym_wildcard;
127
t_pdp_symbol _pdp_sym_float;
128
t_pdp_symbol _pdp_sym_int;
129
t_pdp_symbol _pdp_sym_symbol;
130
t_pdp_symbol _pdp_sym_packet;
131
t_pdp_symbol _pdp_sym_pointer;
132
t_pdp_symbol _pdp_sym_invalid;
133
t_pdp_symbol _pdp_sym_list;
134
t_pdp_symbol _pdp_sym_question_mark;
135
t_pdp_symbol _pdp_sym_atom;
136
t_pdp_symbol _pdp_sym_null;
137
t_pdp_symbol _pdp_sym_quote_start;
138
t_pdp_symbol _pdp_sym_quote_end;
139
t_pdp_symbol _pdp_sym_return;
140
t_pdp_symbol _pdp_sym_nreturn;
141
t_pdp_symbol _pdp_sym_defstart;
142
t_pdp_symbol _pdp_sym_defend;
143
t_pdp_symbol _pdp_sym_if;
144
t_pdp_symbol _pdp_sym_then;
145
t_pdp_symbol _pdp_sym_local;
146
t_pdp_symbol _pdp_sym_forth;
147
t_pdp_symbol _pdp_sym_call;
148
t_pdp_symbol _pdp_sym_push;
149
t_pdp_symbol _pdp_sym_pop;
151
static void _sym(char *name, t_pdp_symbol *s)
155
PDP_ASSERT(_pdp_dogensym(name, s) == s);
159
void pdp_symbol_setup(void)
162
pthread_mutex_init(&pdp_hash_mutex, NULL);
165
memset(pdp_symhash, 0, HASHSIZE * sizeof(t_pdp_symbol *));
167
// setup predefined symbols
168
_sym("*", &_pdp_sym_wildcard);
169
_sym("float", &_pdp_sym_float);
170
_sym("int", &_pdp_sym_int);
171
_sym("symbol", &_pdp_sym_symbol);
172
_sym("packet", &_pdp_sym_packet);
173
_sym("pointer", &_pdp_sym_pointer);
174
_sym("invalid", &_pdp_sym_invalid);
175
_sym("list", &_pdp_sym_list);
176
_sym("?", &_pdp_sym_question_mark);
177
_sym("atom", &_pdp_sym_atom);
178
_sym("null", &_pdp_sym_null);
179
_sym("[", &_pdp_sym_quote_start);
180
_sym("]", &_pdp_sym_quote_end);
181
_sym("ret", &_pdp_sym_return);
182
_sym("nret", &_pdp_sym_nreturn);
183
_sym(":", &_pdp_sym_defstart);
184
_sym(";", &_pdp_sym_defend);
185
_sym("if", &_pdp_sym_if);
186
_sym("then", &_pdp_sym_then);
187
_sym("local", &_pdp_sym_local);
188
_sym("forth", &_pdp_sym_forth);
189
_sym("call", &_pdp_sym_call);
190
_sym("push", &_pdp_sym_push);
191
_sym("pop", &_pdp_sym_pop);