supernova: fix for small audio vector sizes
[supercollider.git] / lang / LangPrimSource / PyrSymbolPrim.cpp
blob18e7cc5a39ee8eaeb8efc0f51bff7477acf8426a
1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 Primitives for Symbol.
26 #include <string.h>
27 #include <stdlib.h>
28 #include "PyrPrimitive.h"
29 #include "PyrSymbol.h"
30 #include "VMGlobals.h"
31 #include "PyrKernel.h"
32 #include "SCBase.h"
35 int prSymbolString(struct VMGlobals *g, int numArgsPushed);
36 int prSymbolString(struct VMGlobals *g, int numArgsPushed)
38 PyrSlot *a;
39 PyrString *string;
41 a = g->sp;
42 if (NotSym(a)) return errWrongType;
43 string = newPyrString(g->gc, slotRawSymbol(a)->name, 0, true);
44 SetObject(a, string);
45 return errNone;
49 int prSymbolIsPrefix(struct VMGlobals *g, int numArgsPushed);
50 int prSymbolIsPrefix(struct VMGlobals *g, int numArgsPushed)
52 PyrSlot *a, *b;
53 int length;
55 a = g->sp - 1;
56 b = g->sp;
57 if (!IsSym(a) || !IsSym(b)) return errWrongType;
58 int32 alen = slotRawSymbol(a)->length;
59 int32 blen = slotRawSymbol(b)->length;
60 length = sc_min(alen, blen);
61 if (memcmp(slotRawSymbol(a)->name, slotRawSymbol(b)->name, length) == 0) {
62 SetTrue(a);
63 } else {
64 SetFalse(a);
66 return errNone;
69 int prSymbolClass(struct VMGlobals *g, int numArgsPushed);
70 int prSymbolClass(struct VMGlobals *g, int numArgsPushed)
72 PyrSlot *a;
73 PyrClass *classobj;
74 //char firstChar;
76 a = g->sp;
77 if (slotRawSymbol(a)->flags & sym_Class) {
78 //firstChar = slotRawSymbol(a)->name[0];
79 //if (firstChar >= 'A' && firstChar <= 'Z') {
80 classobj = slotRawSymbol(a)->u.classobj;
81 if (classobj) {
82 SetObject(a, classobj);
83 } else {
84 SetNil(a);
86 } else {
87 SetNil(a);
89 return errNone;
92 int prSymbolIsSetter(struct VMGlobals *g, int numArgsPushed);
93 int prSymbolIsSetter(struct VMGlobals *g, int numArgsPushed)
95 PyrSlot *a;
97 a = g->sp;
98 if (slotRawSymbol(a)->flags & sym_Setter) {
99 SetTrue(a);
100 } else {
101 SetFalse(a);
103 return errNone;
106 int prSymbolAsSetter(struct VMGlobals *g, int numArgsPushed);
107 int prSymbolAsSetter(struct VMGlobals *g, int numArgsPushed)
109 PyrSlot *a;
110 char str[256];
111 int len;
113 a = g->sp;
114 if (!(slotRawSymbol(a)->flags & sym_Setter)) {
115 if ((slotRawSymbol(a)->flags & sym_Class) || (slotRawSymbol(a)->flags & sym_Primitive)) {
116 error("Cannot convert class names or primitive names to setters.\n");
117 return errFailed;
119 if (strlen(slotRawSymbol(a)->name)>255) {
120 error("symbol name too long.\n");
121 return errFailed;
123 strcpy(str, slotRawSymbol(a)->name);
124 len = strlen(str);
125 str[len] = '_';
126 str[len+1] = 0;
128 //postfl("prSymbolAsSetter %s\n", str);
129 SetRaw(a, getsym(str));
131 return errNone;
134 int prSymbolAsGetter(struct VMGlobals *g, int numArgsPushed);
135 int prSymbolAsGetter(struct VMGlobals *g, int numArgsPushed)
137 PyrSlot *a;
138 char str[256];
140 a = g->sp;
141 if ((slotRawSymbol(a)->flags & sym_Setter)) {
142 if ((slotRawSymbol(a)->flags & sym_Class) || (slotRawSymbol(a)->flags & sym_Primitive)) {
143 error("Cannot convert class names or primitive names to getters.\n");
144 return errFailed;
146 strcpy(str, slotRawSymbol(a)->name);
147 str[strlen(str)-1] = 0;
148 //postfl("prSymbolAsGetter %s\n", str);
149 SetRaw(a, getsym(str));
151 return errNone;
154 int prSymbolIsClassName(struct VMGlobals *g, int numArgsPushed);
155 int prSymbolIsClassName(struct VMGlobals *g, int numArgsPushed)
157 PyrSlot *a;
159 a = g->sp;
160 if (slotRawSymbol(a)->flags & sym_Class) {
161 SetTrue(a);
162 } else {
163 SetFalse(a);
165 return errNone;
168 int prSymbolIsMetaClassName(struct VMGlobals *g, int numArgsPushed);
169 int prSymbolIsMetaClassName(struct VMGlobals *g, int numArgsPushed)
171 PyrSlot *a;
173 a = g->sp;
174 if (slotRawSymbol(a)->flags & sym_MetaClass) {
175 SetTrue(a);
176 } else {
177 SetFalse(a);
179 return errNone;
182 int prSymbol_AsInteger(struct VMGlobals *g, int numArgsPushed);
183 int prSymbol_AsInteger(struct VMGlobals *g, int numArgsPushed)
185 PyrSlot *a = g->sp;
187 char *str = slotRawSymbol(a)->name;
188 SetInt(a, atoi(str));
190 return errNone;
193 int prSymbol_PrimitiveIndex(struct VMGlobals *g, int numArgsPushed);
194 int prSymbol_PrimitiveIndex(struct VMGlobals *g, int numArgsPushed)
196 PyrSlot *a = g->sp;
198 SetInt(a, slotRawSymbol(a)->u.index);
200 return errNone;
203 int prSymbol_SpecialIndex(struct VMGlobals *g, int numArgsPushed);
204 int prSymbol_SpecialIndex(struct VMGlobals *g, int numArgsPushed)
206 PyrSlot *a = g->sp;
208 SetInt(a, slotRawSymbol(a)->specialIndex);
210 return errNone;
214 int prSymbol_AsFloat(struct VMGlobals *g, int numArgsPushed);
215 int prSymbol_AsFloat(struct VMGlobals *g, int numArgsPushed)
217 PyrSlot *a = g->sp;
219 char *str = slotRawSymbol(a)->name;
220 SetFloat(a, atof(str));
222 return errNone;
225 int prSymbol_matchOSCPattern(struct VMGlobals *g, int numArgsPushed);
226 int prSymbol_matchOSCPattern(struct VMGlobals *g, int numArgsPushed)
228 PyrSlot *a, *b;
229 int length;
231 a = g->sp - 1;
232 b = g->sp;
233 if (!IsSym(a) || !IsSym(b)) return errWrongType;
234 // int32 alen = slotRawSymbol(a)->length;
235 // int32 blen = slotRawSymbol(b)->length;
236 // length = sc_min(alen, blen);
237 if (lo_pattern_match(slotRawSymbol(a)->name, slotRawSymbol(b)->name)) {
238 SetTrue(a);
239 } else {
240 SetFalse(a);
242 return errNone;
245 int prSymbol_isMap(struct VMGlobals *g, int numArgsPushed);
246 int prSymbol_isMap(struct VMGlobals *g, int numArgsPushed)
248 PyrSlot *a = g->sp;
250 char *str = slotRawSymbol(a)->name;
251 if(strlen(str)>1 && (str[0]=='a' || str[0]=='c') && str[1]>='0' && str[1]<='9')
252 SetTrue(a);
253 else
254 SetFalse(a);
256 return errNone;
261 void initSymbolPrimitives();
262 void initSymbolPrimitives()
264 int base, index = 0;
266 base = nextPrimitiveIndex();
268 definePrimitive(base, index++, "_SymbolIsPrefix", prSymbolIsPrefix, 2, 0);
269 //definePrimitive(base, index++, "_SymbolString", prSymbolString, 1, 0);
270 definePrimitive(base, index++, "_SymbolClass", prSymbolClass, 1, 0);
271 definePrimitive(base, index++, "_SymbolIsClassName", prSymbolIsClassName, 1, 0);
272 definePrimitive(base, index++, "_SymbolIsMetaClassName", prSymbolIsMetaClassName, 1, 0);
273 definePrimitive(base, index++, "_SymbolIsSetter", prSymbolIsSetter, 1, 0);
274 definePrimitive(base, index++, "_SymbolAsSetter", prSymbolAsSetter, 1, 0);
275 definePrimitive(base, index++, "_SymbolAsGetter", prSymbolAsGetter, 1, 0);
276 definePrimitive(base, index++, "_Symbol_AsInteger", prSymbol_AsInteger, 1, 0);
277 definePrimitive(base, index++, "_Symbol_PrimitiveIndex", prSymbol_PrimitiveIndex, 1, 0);
278 definePrimitive(base, index++, "_Symbol_SpecialIndex", prSymbol_SpecialIndex, 1, 0);
279 definePrimitive(base, index++, "_Symbol_AsFloat", prSymbol_AsFloat, 1, 0);
280 definePrimitive(base, index++, "_Symbol_matchOSCPattern", prSymbol_matchOSCPattern, 2, 0);
281 definePrimitive(base, index++, "_Symbol_IsMap", prSymbol_isMap, 1, 0);
285 #if _SC_PLUGINS_
287 #include "SCPlugin.h"
289 // export the function that SC will call to load the plug in.
290 #pragma export on
291 extern "C" { SCPlugIn* loadPlugIn(void); }
292 #pragma export off
295 // define plug in object
296 class APlugIn : public SCPlugIn
298 public:
299 APlugIn();
300 virtual ~APlugIn();
302 virtual void AboutToCompile();
305 APlugIn::APlugIn()
307 // constructor for plug in
310 APlugIn::~APlugIn()
312 // destructor for plug in
315 void APlugIn::AboutToCompile()
317 // this is called each time the class library is compiled.
318 initSymbolPrimitives();
321 // This function is called when the plug in is loaded into SC.
322 // It returns an instance of APlugIn.
323 SCPlugIn* loadPlugIn()
325 return new APlugIn();
328 #endif