scide: implement selectionLength for openDocument
[supercollider.git] / lang / LangPrimSource / PyrSymbolPrim.cpp
blob280047d8f5c3d1b66fa33705462925aace591fa1
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;
230 a = g->sp - 1;
231 b = g->sp;
232 if (!IsSym(a) || !IsSym(b)) return errWrongType;
233 // int32 alen = slotRawSymbol(a)->length;
234 // int32 blen = slotRawSymbol(b)->length;
235 // length = sc_min(alen, blen);
236 if (lo_pattern_match(slotRawSymbol(a)->name, slotRawSymbol(b)->name)) {
237 SetTrue(a);
238 } else {
239 SetFalse(a);
241 return errNone;
244 int prSymbol_isMap(struct VMGlobals *g, int numArgsPushed);
245 int prSymbol_isMap(struct VMGlobals *g, int numArgsPushed)
247 PyrSlot *a = g->sp;
249 char *str = slotRawSymbol(a)->name;
250 if(strlen(str)>1 && (str[0]=='a' || str[0]=='c') && str[1]>='0' && str[1]<='9')
251 SetTrue(a);
252 else
253 SetFalse(a);
255 return errNone;
260 void initSymbolPrimitives();
261 void initSymbolPrimitives()
263 int base, index = 0;
265 base = nextPrimitiveIndex();
267 definePrimitive(base, index++, "_SymbolIsPrefix", prSymbolIsPrefix, 2, 0);
268 //definePrimitive(base, index++, "_SymbolString", prSymbolString, 1, 0);
269 definePrimitive(base, index++, "_SymbolClass", prSymbolClass, 1, 0);
270 definePrimitive(base, index++, "_SymbolIsClassName", prSymbolIsClassName, 1, 0);
271 definePrimitive(base, index++, "_SymbolIsMetaClassName", prSymbolIsMetaClassName, 1, 0);
272 definePrimitive(base, index++, "_SymbolIsSetter", prSymbolIsSetter, 1, 0);
273 definePrimitive(base, index++, "_SymbolAsSetter", prSymbolAsSetter, 1, 0);
274 definePrimitive(base, index++, "_SymbolAsGetter", prSymbolAsGetter, 1, 0);
275 definePrimitive(base, index++, "_Symbol_AsInteger", prSymbol_AsInteger, 1, 0);
276 definePrimitive(base, index++, "_Symbol_PrimitiveIndex", prSymbol_PrimitiveIndex, 1, 0);
277 definePrimitive(base, index++, "_Symbol_SpecialIndex", prSymbol_SpecialIndex, 1, 0);
278 definePrimitive(base, index++, "_Symbol_AsFloat", prSymbol_AsFloat, 1, 0);
279 definePrimitive(base, index++, "_Symbol_matchOSCPattern", prSymbol_matchOSCPattern, 2, 0);
280 definePrimitive(base, index++, "_Symbol_IsMap", prSymbol_isMap, 1, 0);
284 #if _SC_PLUGINS_
286 #include "SCPlugin.h"
288 // export the function that SC will call to load the plug in.
289 #pragma export on
290 extern "C" { SCPlugIn* loadPlugIn(void); }
291 #pragma export off
294 // define plug in object
295 class APlugIn : public SCPlugIn
297 public:
298 APlugIn();
299 virtual ~APlugIn();
301 virtual void AboutToCompile();
304 APlugIn::APlugIn()
306 // constructor for plug in
309 APlugIn::~APlugIn()
311 // destructor for plug in
314 void APlugIn::AboutToCompile()
316 // this is called each time the class library is compiled.
317 initSymbolPrimitives();
320 // This function is called when the plug in is loaded into SC.
321 // It returns an instance of APlugIn.
322 SCPlugIn* loadPlugIn()
324 return new APlugIn();
327 #endif