Debian Codegraph @

src/core/chuck_symbol.cpp

raw
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: chuck_symbol.cpp
28 // desc: symbols in the syntax, adapted from Tiger compiler by Appel Appel
29 //
30 // author: Ge Wang ([email protected] | [email protected])
31 // adapted from: Andrew Appel ([email protected])
32 // date: Autumn 2002
33 //-----------------------------------------------------------------------------
34 #include "chuck_symbol.h"
35 #include "chuck_errmsg.h"
36 #include "chuck_table.h"
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40
41
42
43
44 // S_Symbol
45 struct S_Symbol_ { c_str name; S_Symbol next; };
46
47 static S_Symbol mksymbol( c_constr name, S_Symbol next )
48 {
49 S_Symbol s = (S_Symbol)checked_malloc(sizeof(*s));
50 t_CKINT len = strlen(name) + 1;
51 s->name = (c_str)checked_malloc((int)len);
52 strcpy(s->name, (c_str)name); s->next=next;
53 return s;
54 }
55
56 #define CK_SIM_HASH_SIZE 65347 /* should be prime */
57
58 static S_Symbol hashtable[CK_SIM_HASH_SIZE];
59
60 static unsigned int s_hash(const char *s0)
61 {
62 unsigned int h=0; const char *s;
63 for(s=s0; *s; s++)
64 h = h*65599 + *s;
65 return h;
66 }
67
68 static int streq(c_constr a, c_constr b)
69 {
70 return !strcmp((char*)a,(char*)b);
71 }
72
73 S_Symbol insert_symbol(c_constr name)
74 {
75 S_Symbol syms = NULL, sym;
76 int index= s_hash(name) % CK_SIM_HASH_SIZE;
77
78 if( !name ) return NULL;
79 syms = hashtable[index];
80 for(sym=syms; sym; sym=sym->next)
81 if (streq(sym->name,name)) return sym;
82 sym = mksymbol(name,syms);
83 hashtable[index]=sym;
84
85 return sym;
86 }
87
88 c_str S_name( S_Symbol sym )
89 {
90 return sym->name;
91 }
92
93 S_table S_empty( void )
94 {
95 return TAB_empty();
96 }
97
98 S_table S_empty2( unsigned int size )
99 {
100 return TAB_empty2(size);
101 }
102
103 void S_enter( S_table t, S_Symbol sym, void *value )
104 {
105 TAB_enter(t,sym,value);
106 }
107
108 void S_enter2( S_table t, c_constr str, void * value )
109 {
110 TAB_enter(t,insert_symbol(str),value);
111 }
112
113 void * S_look( S_table t, S_Symbol sym )
114 {
115 return TAB_look(t,sym);
116 }
117
118 void * S_look2( S_table t, c_constr str )
119 {
120 return TAB_look(t,insert_symbol(str));
121 }
122
123 // BUG: this could result in a bad free if the S_Symbol ever gets cleaned up
124 static struct S_Symbol_ marksym = { (char *)"<mark>", 0 };
125
126 void S_beginScope( S_table t )
127 {
128 S_enter( t, &marksym, NULL );
129 }
130
131 void S_endScope( S_table t )
132 {
133 S_Symbol s;
134 do s = (S_Symbol)TAB_pop(t);
135 while (s != &marksym);
136 }
137
138 void S_pop( S_table t )
139 {
140 TAB_pop(t);
141 }
142
143 void S_dump( S_table t, void (*show)(S_Symbol sym, void *binding) )
144 {
145 TAB_dump( t, (void (*)(void *, void *)) show );
146 }
147
148 /* the str->whatever table */
149
150 void * checked_malloc( t_CKINT len )
151 {
152 if( !len ) return NULL;
153
154 void *p = calloc( len, 1 );
155 if( !p )
156 {
157 EM_error2( 0, "out of memory!\n" );
158 exit( 1 );
159 }
160
161 return p;
162 }
163
164 c_str cc_str( char * s )
165 {
166 t_CKINT len = strlen(s) + 1;
167 c_str p = (c_str)checked_malloc( len );
168 strcpy( p, s );
169
170 return p;
171 }
172
173 U_boolList U_BoolList( t_CKBOOL head, U_boolList tail )
174 {
175 U_boolList list = (U_boolList)checked_malloc( sizeof(*list) );
176 list->head = head;
177 list->tail = tail;
178
179 return list;
180 }