1 /*----------------------------------------------------------------------------
2 ChucK Concurrent, On-the-fly Audio Programming Language
3 Compiler and Virtual Machine
5 Copyright (c) 2004 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.cs.princeton.edu/
7 http://soundlab.cs.princeton.edu/
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 -----------------------------------------------------------------------------*/
25 //-----------------------------------------------------------------------------
26 // file: chuck_symbol.c
29 // author: Andrew Appel (appel@cs.princeton.edu)
30 // modified: Ge Wang (gewang@cs.princeton.edu)
31 // Perry R. Cook (prc@cs.princeton.edu)
33 //-----------------------------------------------------------------------------
36 #include "chuck_utils.h"
37 #include "chuck_symbol.h"
38 #include "chuck_table.h"
42 struct S_Symbol_
{ c_str name
; S_Symbol next
; };
44 static S_Symbol
mksymbol( c_constr name
, S_Symbol next
)
46 S_Symbol s
= (S_Symbol
)checked_malloc(sizeof(*s
));
47 s
->name
= (c_str
)checked_malloc(strlen(name
)+1);
48 strcpy(s
->name
, (c_str
)name
); s
->next
=next
;
52 #define SIZE 65347 /* should be prime */
54 static S_Symbol hashtable
[SIZE
];
56 static unsigned int hash(const char *s0
)
58 unsigned int h
=0; const char *s
;
64 static int streq(c_constr a
, c_constr b
)
66 return !strcmp((char*)a
,(char*)b
);
69 S_Symbol
insert_symbol(c_constr name
)
71 S_Symbol syms
= NULL
, sym
;
72 int index
= hash(name
) % SIZE
;
74 if( !name
) return NULL
;
75 syms
= hashtable
[index
];
76 for(sym
=syms
; sym
; sym
=sym
->next
)
77 if (streq(sym
->name
,name
)) return sym
;
78 sym
= mksymbol(name
,syms
);
84 c_str
S_name( S_Symbol sym
)
89 S_table
S_empty( void )
94 S_table
S_empty2( unsigned int size
)
96 return TAB_empty2(size
);
99 void S_enter( S_table t
, S_Symbol sym
, void *value
)
101 TAB_enter(t
,sym
,value
);
104 void S_enter2( S_table t
, c_constr str
, void * value
)
106 TAB_enter(t
,insert_symbol(str
),value
);
109 void * S_look( S_table t
, S_Symbol sym
)
111 return TAB_look(t
,sym
);
114 void * S_look2( S_table t
, c_constr str
)
116 return TAB_look(t
,insert_symbol(str
));
119 static struct S_Symbol_ marksym
= { "<mark>", 0 };
121 void S_beginScope( S_table t
)
123 S_enter( t
, &marksym
, NULL
);
126 void S_endScope( S_table t
)
129 do s
= (S_Symbol
)TAB_pop(t
);
130 while (s
!= &marksym
);
133 void S_pop( S_table t
)
138 void S_dump( S_table t
, void (*show
)(S_Symbol sym
, void *binding
) )
140 TAB_dump( t
, (void (*)(void *, void *)) show
);
143 /* the str->whatever table */