bumping version to 3.5-rc1
[supercollider.git] / lang / LangSource / DumpParseNode.cpp
bloba5d3d3b0d25884f660824142d397bd6fddd16e01
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
21 #ifdef SC_WIN32
22 # define snprintf _snprintf
23 # define PATH_MAX _MAX_PATH
24 #endif
25 #include "SCBase.h"
26 #include "PyrParseNode.h"
27 #include "PyrLexer.h"
28 #include "PyrKernel.h"
29 #include "Opcodes.h"
30 #include "PyrPrimitive.h"
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <ctype.h>
35 extern int textpos;
37 void dumpNodeList(PyrParseNode *node)
39 for (; node; node = node->mNext) {
40 DUMPNODE(node, 0);
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)
52 dumpPushLit(level);
53 else if (mClassno == pn_PushNameNode)
54 postfl("%2d PushName '%s'\n", level, slotRawSymbol(&mSlot)->name);
55 else if (mClassno == pn_LiteralNode)
56 dumpLiteral(level);
57 else {
58 postfl("%2d SlotNode\n", level);
59 dumpPyrSlot(&mSlot);
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);
148 else {
149 DUMPNODE((PyrParseNode*)slotRawObject(&mSlot), level);
153 void PyrSlotNode::dumpLiteral(int level)
155 postfl("%2d Literal\n", level);
156 if (!IsPtr(&mSlot)) dumpPyrSlot(&mSlot);
157 else {
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)
241 char str[128];
242 slotString(slot, str);
243 post(" %s\n", str);
246 void slotString(PyrSlot *slot, char *str)
248 switch (GetTag(slot)) {
249 case tagInt :
250 sprintf(str, "Integer %d", slotRawInt(slot));
251 break;
252 case tagChar :
253 sprintf(str, "Character %d '%c'", static_cast<int>(slotRawChar(slot)), static_cast<int>(slotRawChar(slot)));
254 break;
255 case tagSym :
256 if (strlen(slotRawSymbol(slot)->name) > 240) {
257 char str2[256];
258 memcpy(str2, slotRawSymbol(slot)->name, 240);
259 str2[240] = 0;
260 snprintf(str, 256, "Symbol '%s...'", str2);
261 } else {
262 snprintf(str, 256, "Symbol '%s'", slotRawSymbol(slot)->name);
264 break;
265 case tagObj :
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) {
272 char str2[48];
273 int len;
274 if (slotRawObject(slot)->size > 47) {
275 memcpy(str2, (char*)slotRawObject(slot)->slots, 44);
276 str2[44] = '.';
277 str2[45] = '.';
278 str2[46] = '.';
279 str2[47] = 0;
280 } else {
281 len = sc_min(47, slotRawObject(slot)->size);
282 memcpy(str2, (char*)slotRawObject(slot)->slots, len);
283 str2[len] = 0;
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)) {
295 do {
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);
303 } else {
304 sprintf(str, "instance of FunctionDef in closed FunctionDef");
306 } else {
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);
316 } else {
317 sprintf(str, "Frame (%p) of Function", slotRawFrame(slot));
319 } else {
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);
325 } else {
326 sprintf(str, "NULL Object Pointer");
328 break;
329 case tagNil :
330 sprintf(str, "nil");
331 break;
332 case tagFalse :
333 sprintf(str, "false");
334 break;
335 case tagTrue :
336 sprintf(str, "true");
337 break;
338 case tagPtr :
339 sprintf(str, "RawPointer %p", slotRawPtr(slot));
340 break;
341 default :
343 union {
344 int32 i[2];
345 double f;
346 } u;
347 u.f = slotRawFloat(slot);
348 sprintf(str, "Float %f %08X %08X", u.f, u.i[0], u.i[1]);
349 break;
354 void slotOneWord(PyrSlot *slot, char *str)
356 str[0] = 0;
357 switch (GetTag(slot)) {
358 case tagInt :
359 sprintf(str, "%d", slotRawInt(slot));
360 break;
361 case tagChar :
362 sprintf(str, "$%c", static_cast<int>(slotRawChar(slot)));
363 break;
364 case tagSym :
365 if (strlen(slotRawSymbol(slot)->name) > 240) {
366 char str2[256];
367 memcpy(str2, slotRawSymbol(slot)->name, 240);
368 str2[240] = 0;
369 snprintf(str, 256, "'%s...'", str2);
370 } else {
371 snprintf(str, 256, "'%s'", slotRawSymbol(slot)->name);
373 break;
374 case tagObj :
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) {
380 char str2[32];
381 int len;
382 if (slotRawObject(slot)->size > 31) {
383 memcpy(str2, (char*)slotRawObject(slot)->slots, 28);
384 str2[28] = '.';
385 str2[29] = '.';
386 str2[30] = '.';
387 str2[31] = 0;
388 } else {
389 len = sc_min(31, slotRawObject(slot)->size);
390 memcpy(str2, (char*)slotRawObject(slot)->slots, len);
391 str2[len] = 0;
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)) {
403 do {
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);
411 } else {
412 sprintf(str, "< FunctionDef in closed FunctionDef >");
414 } else {
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);
426 } else {
427 sprintf(str, "Frame (%0X) of Function", slotRawInt(slot));
429 } else if (classptr == class_array) {
430 sprintf(str, "[*%d]", slotRawObject(slot)->size);
431 } else {
432 sprintf(str, "<instance of %s>", slotRawSymbol(&classptr->name)->name);
434 } else {
435 sprintf(str, "NULL Object Pointer");
437 break;
438 case tagNil :
439 sprintf(str, "nil");
440 break;
441 case tagFalse :
442 sprintf(str, "false");
443 break;
444 case tagTrue :
445 sprintf(str, "true");
446 break;
447 case tagPtr :
448 sprintf(str, "ptr%p", slotRawPtr(slot));
449 break;
450 default :
451 sprintf(str, "%.14g", slotRawFloat(slot));
452 break;
456 bool postString(PyrSlot *slot, char *str)
458 bool res = true;
459 switch (GetTag(slot)) {
460 case tagInt :
461 sprintf(str, "%d", slotRawInt(slot));
462 break;
463 case tagChar :
464 sprintf(str, "%c", slotRawChar(slot));
465 break;
466 case tagSym :
467 str[0] = 0;
468 res = false;
469 break;
470 case tagObj :
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);
476 } else {
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);
482 } else {
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) {
491 char str2[48];
492 int len;
493 if (slotRawObject(slot)->size > 47) {
494 memcpy(str2, (char*)slotRawObject(slot)->slots, 44);
495 str2[44] = '.';
496 str2[45] = '.';
497 str2[46] = '.';
498 str2[47] = 0;
499 } else {
500 len = sc_min(47, slotRawObject(slot)->size);
501 memcpy(str2, (char*)slotRawObject(slot)->slots, len);
502 str2[len] = 0;
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)) {
515 do {
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);
523 } else {
524 sprintf(str, "a FunctionDef in closed FunctionDef");
526 } else {
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);
538 } else {
539 sprintf(str, "Frame (%0X) of Function", slotRawInt(slot));
541 } else {
542 str[0] = 0;
543 res = false;
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);
549 } else {
550 sprintf(str, "NULL Object Pointer");
552 break;
553 case tagNil :
554 sprintf(str, "nil");
555 break;
556 case tagFalse :
557 sprintf(str, "false");
558 break;
559 case tagTrue :
560 sprintf(str, "true");
561 break;
562 case tagPtr :
563 sprintf(str, "%p", slotRawPtr(slot));
564 break;
565 default :
566 sprintf(str, "%.14g", slotRawFloat(slot));
567 break;
569 return res;
573 int asCompileString(PyrSlot *slot, char *str)
575 switch (GetTag(slot)) {
576 case tagInt :
577 sprintf(str, "%d", slotRawInt(slot));
578 break;
579 case tagChar :
581 int c = slotRawChar(slot);
582 if (isprint(c)) {
583 sprintf(str, "$%c", c);
584 } else {
585 switch (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);
594 break;
596 case tagSym :
597 return errFailed;
598 case tagObj :
599 return errFailed;
600 case tagNil :
601 sprintf(str, "nil");
602 break;
603 case tagFalse :
604 sprintf(str, "false");
605 break;
606 case tagTrue :
607 sprintf(str, "true");
608 break;
609 case tagPtr :
610 strcpy(str, "/*Ptr*/ nil");
611 break;
612 default :
613 sprintf(str, "%f", slotRawFloat(slot));
614 break;
616 return errNone;
620 void stringFromPyrString(PyrString *obj, char *str, int maxlength);
621 void stringFromPyrString(PyrString *obj, char *str, int maxlength)
623 if (obj->classptr == class_string) {
624 int len;
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;
631 } else {
632 len = sc_min(maxlength-1, obj->size);
633 memcpy(str, obj->s, len);
634 str[len] = 0;
636 } else {
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";
646 const char * src;
647 int len;
648 if (obj && obj->classptr == class_string) {
649 len = sc_min(maxlength-1, obj->size);
650 src = obj->s;
651 } else {
652 len = sizeof(not_a_string);
653 src = not_a_string;
655 memcpy(str+1, src, len);
656 str[0] = len;