Mercurial > audlegacy
annotate Plugins/Visualization/paranormal/pn/pnscript.c @ 1717:837983bac90f trunk
[svn] Fixed a lot of warnings that only showed up on *BSD.
author | js |
---|---|
date | Sat, 16 Sep 2006 16:26:54 -0700 |
parents | 1423ab8e5cbc |
children |
rev | line source |
---|---|
1507 | 1 /* Paranormal - A highly customizable audio visualization library |
2 * Copyright (C) 2001 Jamie Gennis <jgennis@mindspring.com> | |
3 * | |
4 * This library is free software; you can redistribute it and/or | |
5 * modify it under the terms of the GNU Library General Public | |
6 * License as published by the Free Software Foundation; either | |
7 * version 2 of the License, or (at your option) any later version. | |
8 * | |
9 * This library is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 * Library General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU Library General Public | |
15 * License along with this library; if not, write to the Free | |
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
17 */ | |
18 | |
19 #include <config.h> | |
20 | |
21 #include <math.h> | |
22 #include "pnscript.h" | |
23 | |
24 /* Initialization */ | |
25 static void pn_script_class_init (PnScriptClass *class); | |
26 | |
27 /* Internal parser */ | |
28 gboolean pn_script_internal_parse_string (PnScript *script, | |
29 const gchar *string); | |
30 | |
31 static GObjectClass *parent_class = NULL; | |
32 | |
33 GType | |
34 pn_script_get_type (void) | |
35 { | |
36 static GType script_type = 0; | |
37 | |
38 if (! script_type) | |
39 { | |
40 static const GTypeInfo script_info = | |
41 { | |
42 sizeof (PnScriptClass), | |
43 NULL, /* base_init */ | |
44 NULL, /* base_finalize */ | |
45 (GClassInitFunc) pn_script_class_init, | |
46 NULL, /* class_finalize */ | |
47 NULL, /* class_data */ | |
48 sizeof (PnScript), | |
49 0, /* n_preallocs */ | |
50 NULL /* instance_init */ | |
51 }; | |
52 | |
53 /* FIXME: should this be dynamic? */ | |
54 script_type = g_type_register_static (PN_TYPE_OBJECT, | |
55 "PnScript", | |
56 &script_info, | |
57 0); | |
58 } | |
59 return script_type; | |
60 } | |
61 | |
62 static void | |
63 pn_script_class_init (PnScriptClass *class) | |
64 { | |
65 GObjectClass *gobject_class; | |
66 PnObjectClass *object_class; | |
67 | |
68 gobject_class = (GObjectClass *) class; | |
69 object_class = (PnObjectClass *) class; | |
70 | |
71 parent_class = g_type_class_peek_parent (class); | |
72 } | |
73 | |
74 static void | |
75 pn_script_unref_variables (PnScript *script) | |
76 { | |
77 g_return_if_fail (script != NULL); | |
78 g_return_if_fail (PN_IS_SCRIPT (script)); | |
79 | |
80 if (script->code && script->symbol_table) | |
81 { | |
82 guint *op; | |
83 | |
84 for (op = script->code; *op != PN_OP_END; op++) | |
85 switch (*op) | |
86 { | |
87 case PN_OP_PUSHC: | |
88 op++; | |
89 break; | |
90 | |
91 case PN_OP_PUSHV: | |
92 pn_symbol_table_unref_variable (script->symbol_table, PN_VARIABLE (*(++op))); | |
93 break; | |
94 | |
95 case PN_OP_SET: | |
96 pn_symbol_table_unref_variable (script->symbol_table, PN_VARIABLE (*(++op))); | |
97 break; | |
98 } | |
99 } | |
100 } | |
101 | |
102 /** | |
103 * pn_script_new | |
104 * | |
105 * Creates a new #PnScript object. | |
106 * | |
107 * Returns: The newly created #PnScript object | |
108 */ | |
109 PnScript* | |
110 pn_script_new (void) | |
111 { | |
112 return (PnScript *) g_object_new (PN_TYPE_SCRIPT, NULL); | |
113 } | |
114 | |
115 /** | |
116 * pn_script_parse_string | |
117 * @script: A #PnScript | |
118 * @symbol_table: the #PnSymbolTable to associate with the script | |
119 * @string: a string containing the script | |
120 * | |
121 * Parses a script, compiling it to a bytecode that is stored within the script object. | |
122 * All in-script variables within the script are added to the specified symbol table (if | |
123 * they are not already in it). If errors are encountered while parsing the script, | |
124 * they are output using pn_error(). | |
125 * | |
126 * Returns: %TRUE on success; %FALSE otherwise | |
127 */ | |
128 gboolean | |
129 pn_script_parse_string (PnScript *script, PnSymbolTable *symbol_table, const gchar *string) | |
130 { | |
131 g_return_val_if_fail (script != NULL, FALSE); | |
132 g_return_val_if_fail (PN_IS_SCRIPT (script), FALSE); | |
133 g_return_val_if_fail (symbol_table != NULL, FALSE); | |
134 g_return_val_if_fail (PN_IS_SYMBOL_TABLE (symbol_table), FALSE); | |
135 g_return_val_if_fail (string != NULL, FALSE); | |
136 | |
137 /* Make sure if it's the same symbol table, we don't destroy it */ | |
138 pn_object_ref (PN_OBJECT (symbol_table)); | |
139 pn_object_sink (PN_OBJECT (symbol_table)); | |
140 | |
141 /* get rid of the old script */ | |
142 if (script->symbol_table) | |
143 { | |
144 pn_script_unref_variables (script); | |
145 pn_object_unref (PN_OBJECT (script->symbol_table)); | |
146 } | |
147 if (script->stack) | |
148 { | |
149 g_free (script->stack); | |
150 script->stack = NULL; | |
151 } | |
152 if (script->code) | |
153 { | |
154 g_free (script->code); | |
155 script->code = NULL; | |
156 } | |
157 if (script->constant_table) | |
158 { | |
159 g_free (script->constant_table); | |
160 script->constant_table = NULL; | |
161 } | |
162 | |
163 /* Set our new symbol table */ | |
164 script->symbol_table = symbol_table; | |
165 | |
166 return pn_script_internal_parse_string (script, string); | |
167 } | |
168 | |
169 /** | |
170 * pn_script_execute | |
171 * @script: a #PnScript | |
172 * | |
173 * Executes a script, updating all variabes in the associated symbol | |
174 * table as the script dictates. | |
175 */ | |
176 void | |
177 pn_script_execute (PnScript *script) | |
178 { | |
179 guint *op; | |
180 gdouble stack[64]; | |
181 guint stack_top = 0; | |
1604
1423ab8e5cbc
[svn] Fix up the warning fixes; by Torbj«Órn Svensson (a.k.a. Azoff on #audacious).
chainsaw
parents:
1593
diff
changeset
|
182 gdouble temp; |
1507 | 183 |
184 g_return_if_fail (script != NULL); | |
185 g_return_if_fail (PN_IS_SCRIPT (script)); | |
186 | |
187 if (! script->code) | |
188 return; | |
189 | |
190 for (op = script->code; *op != PN_OP_END; op++) | |
191 switch (*op) | |
192 { | |
193 #define PUSH(f) stack[stack_top++] = f; | |
194 #define POP stack_top--; | |
195 #define POPV (stack[--stack_top]) | |
196 #define PEEK (stack[stack_top-1]) | |
197 #define PEEKN(n) (stack[stack_top-n]) | |
198 case PN_OP_PUSHC: | |
199 case PN_OP_PUSHV: | |
200 PUSH (* (gdouble *)(*(++op))); | |
201 break; | |
202 | |
203 case PN_OP_POP: | |
204 POP; | |
205 break; | |
206 | |
207 case PN_OP_SET: | |
208 *(gdouble *)(*(++op)) = PEEK; | |
209 break; | |
210 | |
211 case PN_OP_ADD: | |
1593
6647d5cc717c
[svn] Additional (PPC?) warning squashing by Joseph Jezak from Gentoo.
chainsaw
parents:
1507
diff
changeset
|
212 /* PEEKN (2) += POPV; */ |
6647d5cc717c
[svn] Additional (PPC?) warning squashing by Joseph Jezak from Gentoo.
chainsaw
parents:
1507
diff
changeset
|
213 temp = POPV; |
1604
1423ab8e5cbc
[svn] Fix up the warning fixes; by Torbj«Órn Svensson (a.k.a. Azoff on #audacious).
chainsaw
parents:
1593
diff
changeset
|
214 PEEKN(1) += temp; |
1507 | 215 break; |
216 | |
217 case PN_OP_SUB: | |
1593
6647d5cc717c
[svn] Additional (PPC?) warning squashing by Joseph Jezak from Gentoo.
chainsaw
parents:
1507
diff
changeset
|
218 /* PEEKN (2) -= POPV; */ |
6647d5cc717c
[svn] Additional (PPC?) warning squashing by Joseph Jezak from Gentoo.
chainsaw
parents:
1507
diff
changeset
|
219 temp = POPV; |
1604
1423ab8e5cbc
[svn] Fix up the warning fixes; by Torbj«Órn Svensson (a.k.a. Azoff on #audacious).
chainsaw
parents:
1593
diff
changeset
|
220 PEEKN(1) -= temp; |
1507 | 221 break; |
222 | |
223 case PN_OP_MUL: | |
1593
6647d5cc717c
[svn] Additional (PPC?) warning squashing by Joseph Jezak from Gentoo.
chainsaw
parents:
1507
diff
changeset
|
224 /* PEEKN (2) *= POPV; */ |
6647d5cc717c
[svn] Additional (PPC?) warning squashing by Joseph Jezak from Gentoo.
chainsaw
parents:
1507
diff
changeset
|
225 temp = POPV; |
1604
1423ab8e5cbc
[svn] Fix up the warning fixes; by Torbj«Órn Svensson (a.k.a. Azoff on #audacious).
chainsaw
parents:
1593
diff
changeset
|
226 PEEKN(1) *= temp; |
1507 | 227 break; |
228 | |
229 case PN_OP_DIV: | |
230 if (PEEK != 0) | |
231 PEEKN (2) /= PEEK; | |
232 else | |
233 PEEK = 0; | |
234 POP; | |
235 break; | |
236 | |
237 case PN_OP_NEG: | |
238 PEEK = -PEEK; | |
239 break; | |
240 | |
241 case PN_OP_POW: | |
242 PEEKN (2) = pow (PEEKN (2), PEEK); | |
243 POP; | |
244 break; | |
245 | |
246 case PN_OP_ABS: | |
247 PEEK = ABS (PEEK); | |
248 break; | |
249 | |
250 case PN_OP_MAX: | |
251 PEEKN (2) = MAX (PEEK, PEEKN (2)); | |
252 POP; | |
253 break; | |
254 | |
255 case PN_OP_MIN: | |
256 PEEKN (2) = MIN (PEEK, PEEKN (2)); | |
257 POP; | |
258 break; | |
259 | |
260 case PN_OP_SIN: | |
261 PEEK = sin (PEEK); | |
262 break; | |
263 | |
264 case PN_OP_COS: | |
265 PEEK = cos (PEEK); | |
266 break; | |
267 | |
268 case PN_OP_TAN: | |
269 PEEK = tan (PEEK); | |
270 break; | |
271 | |
272 case PN_OP_ASIN: | |
273 PEEK = asin (PEEK); | |
274 break; | |
275 | |
276 case PN_OP_ACOS: | |
277 PEEK = acos (PEEK); | |
278 break; | |
279 | |
280 case PN_OP_ATAN: | |
281 PEEK = atan (PEEK); | |
282 break; | |
283 } | |
284 } | |
285 |