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 # define snprintf _snprintf
23 # define PATH_MAX _MAX_PATH
26 #include "PyrParseNode.h"
28 #include "PyrKernel.h"
30 #include "PyrPrimitive.h"
37 void dumpNodeList(PyrParseNode
*node
)
39 for (; node
; node
= node
->mNext
) {
44 void PyrCurryArgNode::dump(int level
)
46 postfl("%2d CurryArg %d\n", level
, mArgNum
);
49 void PyrSlotNode::dump(int level
)
51 if (mClassno
== pn_PushLitNode
)
53 else if (mClassno
== pn_PushNameNode
)
54 postfl("%2d PushName '%s'\n", level
, slotRawSymbol(&mSlot
)->name
);
55 else if (mClassno
== pn_LiteralNode
)
58 postfl("%2d SlotNode\n", level
);
61 DUMPNODE(mNext
, level
);
64 void PyrPushKeyArgNode::dump(int level
)
66 postfl("%2d PushKeyArgNode\n", level
);
67 DUMPNODE(mSelector
, level
+1);
68 DUMPNODE(mExpr
, level
+1);
69 DUMPNODE(mNext
, level
);
72 void PyrClassExtNode::dump(int level
)
74 postfl("%2d ClassExt '%s'\n", level
, slotRawSymbol(&mClassName
->mSlot
)->name
);
75 DUMPNODE(mMethods
, level
+1);
76 DUMPNODE(mNext
, level
);
79 void PyrClassNode::dump(int level
)
81 postfl("%2d Class '%s'\n", level
, slotRawSymbol(&mClassName
->mSlot
)->name
);
82 DUMPNODE(mSuperClassName
, level
+1);
83 DUMPNODE(mVarlists
, level
+1);
84 DUMPNODE(mMethods
, level
+1);
85 DUMPNODE(mNext
, level
);
88 void PyrMethodNode::dump(int level
)
90 postfl("%2d MethodNode '%s' %s\n", level
, slotRawSymbol(&mMethodName
->mSlot
)->name
,
91 mPrimitiveName
? slotRawSymbol(&mPrimitiveName
->mSlot
)->name
:"");
92 DUMPNODE(mArglist
, level
+1);
93 DUMPNODE(mBody
, level
+1);
94 DUMPNODE(mNext
, level
);
97 void PyrArgListNode::dump(int level
)
99 postfl("%2d ArgList\n", level
);
100 DUMPNODE(mVarDefs
, level
+1);
101 DUMPNODE(mRest
, level
+1);
102 DUMPNODE(mNext
, level
);
105 void PyrVarListNode::dump(int level
)
107 postfl("%2d VarList\n", level
);
108 DUMPNODE(mVarDefs
, level
+1);
109 DUMPNODE(mNext
, level
);
112 void PyrVarDefNode::dump(int level
)
114 postfl("%2d VarDef '%s'\n", level
, slotRawSymbol(&mVarName
->mSlot
)->name
);
115 DUMPNODE(mDefVal
, level
);
116 DUMPNODE(mNext
, level
);
119 void PyrCallNode::dump(int level
)
121 postfl("%2d Call '%s'\n", level
, slotRawSymbol(&mSelector
->mSlot
)->name
);
122 DUMPNODE(mArglist
, level
+1);
123 DUMPNODE(mKeyarglist
, level
+1);
124 DUMPNODE(mNext
, level
);
127 void PyrBinopCallNode::dump(int level
)
129 postfl("%2d BinopCall '%s'\n", level
, slotRawSymbol(&mSelector
->mSlot
)->name
);
130 DUMPNODE(mArglist
, level
+1);
131 DUMPNODE(mNext
, level
);
134 void PyrDropNode::dump(int level
)
136 postfl("%2d Drop (\n", level
);
137 DUMPNODE(mExpr1
, level
+1);
138 postfl(" -> %2d Drop\n", level
);
139 DUMPNODE(mExpr2
, level
+1);
140 postfl(") %2d Drop\n", level
);
141 DUMPNODE(mNext
, level
);
144 void PyrSlotNode::dumpPushLit(int level
)
146 postfl("%2d PushLit\n", level
);
147 if (!IsPtr(&mSlot
)) dumpPyrSlot(&mSlot
);
149 DUMPNODE((PyrParseNode
*)slotRawObject(&mSlot
), level
);
153 void PyrSlotNode::dumpLiteral(int level
)
155 postfl("%2d Literal\n", level
);
156 if (!IsPtr(&mSlot
)) dumpPyrSlot(&mSlot
);
158 DUMPNODE((PyrParseNode
*)slotRawObject(&mSlot
), level
);
162 void PyrReturnNode::dump(int level
)
164 postfl("%2d Return (\n", level
);
165 DUMPNODE(mExpr
, level
+1);
166 postfl(") %2d Return \n", level
);
167 DUMPNODE(mNext
, level
);
170 void PyrBlockReturnNode::dump(int level
)
172 postfl("%2d FuncReturn\n", level
);
173 DUMPNODE(mNext
, level
);
176 void PyrAssignNode::dump(int level
)
178 postfl("%2d Assign '%s'\n", level
, slotRawSymbol(&mVarName
->mSlot
)->name
);
179 DUMPNODE(mVarName
, level
+1);
180 DUMPNODE(mExpr
, level
+1);
181 DUMPNODE(mNext
, level
);
184 void PyrSetterNode::dump(int level
)
186 //postfl("%2d Assign '%s'\n", level, slotRawSymbol(&mVarName->mSlot)->name);
187 DUMPNODE(mSelector
, level
+1);
188 DUMPNODE(mExpr1
, level
+1);
189 DUMPNODE(mExpr2
, level
+1);
192 void PyrMultiAssignNode::dump(int level
)
194 postfl("%2d MultiAssign\n", level
);
195 DUMPNODE(mVarList
, level
+1);
196 DUMPNODE(mExpr
, level
+1);
197 DUMPNODE(mNext
, level
);
200 void PyrMultiAssignVarListNode::dump(int level
)
202 postfl("%2d MultiAssignVarList\n", level
);
203 DUMPNODE(mVarNames
, level
+1);
204 DUMPNODE(mRest
, level
+1);
205 DUMPNODE(mNext
, level
);
208 void PyrDynDictNode::dump(int level
)
210 postfl("%2d DynDict\n", level
);
211 DUMPNODE(mElems
, level
+1);
212 DUMPNODE(mNext
, level
);
215 void PyrDynListNode::dump(int level
)
217 postfl("%2d DynList\n", level
);
218 DUMPNODE(mElems
, level
+1);
219 DUMPNODE(mNext
, level
);
222 void PyrLitListNode::dump(int level
)
224 postfl("%2d LitList\n", level
);
225 postfl(" %2d mElems\n", level
);
226 DUMPNODE(mElems
, level
+1);
227 postfl(" %2d mNext\n", level
);
228 DUMPNODE(mNext
, level
);
231 void PyrBlockNode::dump(int level
)
233 postfl("%2d Func\n", level
);
234 DUMPNODE(mArglist
, level
+1);
235 DUMPNODE(mBody
, level
+1);
236 DUMPNODE(mNext
, level
);
239 void dumpPyrSlot(PyrSlot
* slot
)
242 slotString(slot
, str
);
246 void slotString(PyrSlot
*slot
, char *str
)
248 switch (GetTag(slot
)) {
250 sprintf(str
, "Integer %d", slotRawInt(slot
));
253 sprintf(str
, "Character %d '%c'", static_cast<int>(slotRawChar(slot
)), static_cast<int>(slotRawChar(slot
)));
256 if (strlen(slotRawSymbol(slot
)->name
) > 240) {
258 memcpy(str2
, slotRawSymbol(slot
)->name
, 240);
260 snprintf(str
, 256, "Symbol '%s...'", str2
);
262 snprintf(str
, 256, "Symbol '%s'", slotRawSymbol(slot
)->name
);
266 if (slotRawObject(slot
)) {
267 PyrClass
* classptr
= slotRawObject(slot
)->classptr
;
268 if (classptr
== class_class
) {
269 sprintf(str
, "class %s (%p)",
270 slotRawSymbol(&((PyrClass
*)slotRawObject(slot
))->name
)->name
, slotRawObject(slot
));
271 } else if (classptr
== class_string
) {
274 if (slotRawObject(slot
)->size
> 47) {
275 memcpy(str2
, (char*)slotRawObject(slot
)->slots
, 44);
281 len
= sc_min(47, slotRawObject(slot
)->size
);
282 memcpy(str2
, (char*)slotRawObject(slot
)->slots
, len
);
285 sprintf(str
, "\"%s\"", str2
);
286 } else if (classptr
== class_method
) {
287 sprintf(str
, "instance of Method %s:%s (%p)",
288 slotRawSymbol(&slotRawClass(&slotRawMethod(slot
)->ownerclass
)->name
)->name
,
289 slotRawSymbol(&slotRawMethod(slot
)->name
)->name
, slotRawMethod(slot
));
290 } else if (classptr
== class_fundef
) {
291 PyrSlot
*context
, *nextcontext
;
292 // find function's method
293 nextcontext
= &slotRawBlock(slot
)->contextDef
;
294 if (NotNil(nextcontext
)) {
296 context
= nextcontext
;
297 nextcontext
= &slotRawBlock(context
)->contextDef
;
298 } while (NotNil(nextcontext
));
299 if (isKindOf(slotRawObject(context
), class_method
)) {
300 sprintf(str
, "instance of FunctionDef in Method %s:%s",
301 slotRawSymbol(&slotRawClass(&slotRawMethod(context
)->ownerclass
)->name
)->name
,
302 slotRawSymbol(&slotRawMethod(context
)->name
)->name
);
304 sprintf(str
, "instance of FunctionDef in closed FunctionDef");
307 sprintf(str
, "instance of FunctionDef - closed");
309 } else if (classptr
== class_frame
) {
310 if (!slotRawFrame(slot
)) {
311 sprintf(str
, "Frame (%0X)", slotRawInt(slot
));
312 } else if (slotRawBlock(&slotRawFrame(slot
)->method
)->classptr
== class_method
) {
313 sprintf(str
, "Frame (%p) of %s:%s", slotRawObject(slot
),
314 slotRawSymbol(&slotRawClass(&slotRawMethod(&slotRawFrame(slot
)->method
)->ownerclass
)->name
)->name
,
315 slotRawSymbol(&slotRawMethod(&slotRawFrame(slot
)->method
)->name
)->name
);
317 sprintf(str
, "Frame (%p) of Function", slotRawFrame(slot
));
320 sprintf(str
, "instance of %s (%p, size=%d, set=%d)",
321 slotRawSymbol(&classptr
->name
)->name
,
322 slotRawObject(slot
), slotRawObject(slot
)->size
,
323 slotRawObject(slot
)->obj_sizeclass
);
326 sprintf(str
, "NULL Object Pointer");
333 sprintf(str
, "false");
336 sprintf(str
, "true");
339 sprintf(str
, "RawPointer %p", slotRawPtr(slot
));
347 u
.f
= slotRawFloat(slot
);
348 sprintf(str
, "Float %f %08X %08X", u
.f
, u
.i
[0], u
.i
[1]);
354 void slotOneWord(PyrSlot
*slot
, char *str
)
357 switch (GetTag(slot
)) {
359 sprintf(str
, "%d", slotRawInt(slot
));
362 sprintf(str
, "$%c", static_cast<int>(slotRawChar(slot
)));
365 if (strlen(slotRawSymbol(slot
)->name
) > 240) {
367 memcpy(str2
, slotRawSymbol(slot
)->name
, 240);
369 snprintf(str
, 256, "'%s...'", str2
);
371 snprintf(str
, 256, "'%s'", slotRawSymbol(slot
)->name
);
375 if (slotRawObject(slot
)) {
376 PyrClass
* classptr
= slotRawObject(slot
)->classptr
;
377 if (classptr
== class_class
) {
378 sprintf(str
, "class %s", slotRawSymbol(&((PyrClass
*)slotRawObject(slot
))->name
)->name
);
379 } else if (classptr
== class_string
) {
382 if (slotRawObject(slot
)->size
> 31) {
383 memcpy(str2
, (char*)slotRawObject(slot
)->slots
, 28);
389 len
= sc_min(31, slotRawObject(slot
)->size
);
390 memcpy(str2
, (char*)slotRawObject(slot
)->slots
, len
);
393 sprintf(str
, "\"%s\"", str2
);
394 } else if (classptr
== class_method
) {
395 sprintf(str
, "%s:%s",
396 slotRawSymbol(&slotRawClass(&slotRawMethod(slot
)->ownerclass
)->name
)->name
,
397 slotRawSymbol(&slotRawMethod(slot
)->name
)->name
);
398 } else if (classptr
== class_fundef
) {
399 PyrSlot
*context
, *nextcontext
;
400 // find function's method
401 nextcontext
= &slotRawBlock(slot
)->contextDef
;
402 if (NotNil(nextcontext
)) {
404 context
= nextcontext
;
405 nextcontext
= &slotRawBlock(context
)->contextDef
;
406 } while (NotNil(nextcontext
));
407 if (isKindOf(slotRawObject(context
), class_method
)) {
408 sprintf(str
, "< FunctionDef in Method %s:%s >",
409 slotRawSymbol(&slotRawClass(&slotRawMethod(context
)->ownerclass
)->name
)->name
,
410 slotRawSymbol(&slotRawMethod(context
)->name
)->name
);
412 sprintf(str
, "< FunctionDef in closed FunctionDef >");
415 sprintf(str
, "< closed FunctionDef >");
417 } else if (classptr
== class_frame
) {
418 if (!slotRawFrame(slot
)) {
419 sprintf(str
, "Frame (null)");
420 } else if (!slotRawBlock(&slotRawFrame(slot
)->method
)) {
421 sprintf(str
, "Frame (null method)");
422 } else if (slotRawBlock(&slotRawFrame(slot
)->method
)->classptr
== class_method
) {
423 sprintf(str
, "Frame (%0X) of %s:%s", slotRawInt(slot
),
424 slotRawSymbol(&slotRawClass(&slotRawMethod(&slotRawFrame(slot
)->method
)->ownerclass
)->name
)->name
,
425 slotRawSymbol(&slotRawMethod(&slotRawFrame(slot
)->method
)->name
)->name
);
427 sprintf(str
, "Frame (%0X) of Function", slotRawInt(slot
));
429 } else if (classptr
== class_array
) {
430 sprintf(str
, "[*%d]", slotRawObject(slot
)->size
);
432 sprintf(str
, "<instance of %s>", slotRawSymbol(&classptr
->name
)->name
);
435 sprintf(str
, "NULL Object Pointer");
442 sprintf(str
, "false");
445 sprintf(str
, "true");
448 sprintf(str
, "ptr%p", slotRawPtr(slot
));
451 sprintf(str
, "%.14g", slotRawFloat(slot
));
456 bool postString(PyrSlot
*slot
, char *str
)
459 switch (GetTag(slot
)) {
461 sprintf(str
, "%d", slotRawInt(slot
));
464 sprintf(str
, "%c", slotRawChar(slot
));
471 /*if (slotRawObject(slot)) {
472 if (slotRawObject(slot)->classptr == class_method) {
473 sprintf(str, "instance of Method %s:%s",
474 slotRawSymbol(&slotRawClass(&slotRawMethod(slot)->ownerclass)->name)->name,
475 slotRawSymbol(&slotRawMethod(slot)->name)->name);
477 sprintf(str, "instance of %s (%p, size=%d, set=%02X)",
478 slotRawSymbol(&slotRawObject(slot)->classptr->name)->name,
479 slotRawObject(slot), slotRawObject(slot)->size,
480 slotRawObject(slot)->obj_sizeclass);
483 sprintf(str, "NULL Object Pointer");
486 if (slotRawObject(slot
)) {
487 PyrClass
* classptr
= slotRawObject(slot
)->classptr
;
488 if (classptr
== class_class
) {
489 sprintf(str
, "class %s", slotRawSymbol(&((PyrClass
*)slotRawObject(slot
))->name
)->name
);
490 /* } else if (classptr == class_string) {
493 if (slotRawObject(slot)->size > 47) {
494 memcpy(str2, (char*)slotRawObject(slot)->slots, 44);
500 len = sc_min(47, slotRawObject(slot)->size);
501 memcpy(str2, (char*)slotRawObject(slot)->slots, len);
504 sprintf(str, "\"%s\"", str2);
506 } else if (classptr
== class_method
) {
507 sprintf(str
, "Method %s:%s",
508 slotRawSymbol(&slotRawClass(&slotRawMethod(slot
)->ownerclass
)->name
)->name
,
509 slotRawSymbol(&slotRawMethod(slot
)->name
)->name
);
510 } else if (classptr
== class_fundef
) {
511 PyrSlot
*context
, *nextcontext
;
512 // find function's method
513 nextcontext
= &slotRawBlock(slot
)->contextDef
;
514 if (NotNil(nextcontext
)) {
516 context
= nextcontext
;
517 nextcontext
= &slotRawBlock(context
)->contextDef
;
518 } while (NotNil(nextcontext
));
519 if (isKindOf(slotRawObject(context
), class_method
)) {
520 sprintf(str
, "a FunctionDef in Method %s:%s",
521 slotRawSymbol(&slotRawClass(&slotRawMethod(context
)->ownerclass
)->name
)->name
,
522 slotRawSymbol(&slotRawMethod(context
)->name
)->name
);
524 sprintf(str
, "a FunctionDef in closed FunctionDef");
527 sprintf(str
, "a FunctionDef - closed");
529 } else if (classptr
== class_frame
) {
530 if (!slotRawFrame(slot
)) {
531 sprintf(str
, "Frame (null)");
532 } else if (!slotRawBlock(&slotRawFrame(slot
)->method
)) {
533 sprintf(str
, "Frame (null method)");
534 } else if (slotRawBlock(&slotRawFrame(slot
)->method
)->classptr
== class_method
) {
535 sprintf(str
, "Frame (%0X) of %s:%s", slotRawInt(slot
),
536 slotRawSymbol(&slotRawClass(&slotRawMethod(&slotRawFrame(slot
)->method
)->ownerclass
)->name
)->name
,
537 slotRawSymbol(&slotRawMethod(&slotRawFrame(slot
)->method
)->name
)->name
);
539 sprintf(str
, "Frame (%0X) of Function", slotRawInt(slot
));
544 // sprintf(str, "instance of %s (%p, size=%d, gcset=%02X)",
545 // slotRawSymbol(&classptr->name)->name,
546 // slotRawObject(slot), slotRawObject(slot)->size,
547 // slotRawObject(slot)->obj_sizeclass);
550 sprintf(str
, "NULL Object Pointer");
557 sprintf(str
, "false");
560 sprintf(str
, "true");
563 sprintf(str
, "%p", slotRawPtr(slot
));
566 sprintf(str
, "%.14g", slotRawFloat(slot
));
573 int asCompileString(PyrSlot
*slot
, char *str
)
575 switch (GetTag(slot
)) {
577 sprintf(str
, "%d", slotRawInt(slot
));
581 int c
= slotRawChar(slot
);
583 sprintf(str
, "$%c", c
);
586 case '\n' : strcpy(str
, "$\\n"); break;
587 case '\r' : strcpy(str
, "$\\r"); break;
588 case '\t' : strcpy(str
, "$\\t"); break;
589 case '\f' : strcpy(str
, "$\\f"); break;
590 case '\v' : strcpy(str
, "$\\v"); break;
591 default: sprintf(str
, "%d.asAscii", c
);
604 sprintf(str
, "false");
607 sprintf(str
, "true");
610 strcpy(str
, "/*Ptr*/ nil");
613 sprintf(str
, "%f", slotRawFloat(slot
));
620 void stringFromPyrString(PyrString
*obj
, char *str
, int maxlength
);
621 void stringFromPyrString(PyrString
*obj
, char *str
, int maxlength
)
623 if (obj
->classptr
== class_string
) {
625 if (obj
->size
> maxlength
-4) {
626 memcpy(str
, obj
->s
, maxlength
-4);
627 str
[maxlength
-4] = '.';
628 str
[maxlength
-3] = '.';
629 str
[maxlength
-2] = '.';
630 str
[maxlength
-1] = 0;
632 len
= sc_min(maxlength
-1, obj
->size
);
633 memcpy(str
, obj
->s
, len
);
637 sprintf(str
, "not a string");
641 void pstrncpy(unsigned char *s1
, unsigned char *s2
, int n
);
643 void pstringFromPyrString(PyrString
*obj
, unsigned char *str
, int maxlength
)
645 static const char not_a_string
[] = "not a string";
648 if (obj
&& obj
->classptr
== class_string
) {
649 len
= sc_min(maxlength
-1, obj
->size
);
652 len
= sizeof(not_a_string
);
655 memcpy(str
+1, src
, len
);