comparison src/audacious/tuple_formatter.c @ 3790:9121baaf0190 audacious-1.4.0-beta2

add some debugging support code.
author William Pitcock <nenolod@atheme.org>
date Fri, 19 Oct 2007 02:37:01 -0500
parents a76b55624e51
children df7c8b32c389 6d0db63e05aa 1382d265b71d
comparison
equal deleted inserted replaced
3789:0f5fecede1c6 3790:9121baaf0190
29 * TUPLE_USE_COMPILER: 29 * TUPLE_USE_COMPILER:
30 * Undefine this to disable usage of the Tuplez compiler implementation. 30 * Undefine this to disable usage of the Tuplez compiler implementation.
31 * This may be useful for prototyping new features of the language. 31 * This may be useful for prototyping new features of the language.
32 */ 32 */
33 #define TUPLE_USE_COMPILER 33 #define TUPLE_USE_COMPILER
34
35 /*
36 * TUPLE_COMPILER_DEBUG:
37 * Define this to debug the execution process of the Tuplez compiled
38 * bytecode. This may be useful if bugs creep in.
39 */
40 #undef TUPLE_COMPILER_DEBUG
34 41
35 #ifdef TUPLE_USE_COMPILER 42 #ifdef TUPLE_USE_COMPILER
36 # include "tuple_compiler.h" 43 # include "tuple_compiler.h"
37 #endif 44 #endif
38 45
549 last_ctx = tuple_evalctx_new(); 556 last_ctx = tuple_evalctx_new();
550 last_string = g_strdup(string); 557 last_string = g_strdup(string);
551 last_ev = tuple_formatter_compile(last_ctx, last_string); 558 last_ev = tuple_formatter_compile(last_ctx, last_string);
552 } 559 }
553 560
561 #ifdef TUPLE_COMPILER_DEBUG
562 {
563 gint level = 0;
564
565 tuple_formatter_print(stderr, &level, last_ctx, last_ev);
566 }
567 #endif
568
554 tuple_evalctx_reset(last_ctx); 569 tuple_evalctx_reset(last_ctx);
555 return tuple_formatter_eval(last_ctx, last_ev, tuple); 570 return tuple_formatter_eval(last_ctx, last_ev, tuple);
556 #else 571 #else
557 return tuple_formatter_process_construct(tuple, string); 572 return tuple_formatter_process_construct(tuple, string);
558 #endif 573 #endif
574 rv = g_strdup(tuple_get_string(tuple, FIELD_FILE_NAME, NULL)); 589 rv = g_strdup(tuple_get_string(tuple, FIELD_FILE_NAME, NULL));
575 } 590 }
576 591
577 return rv; 592 return rv;
578 } 593 }
594
595 #ifdef TUPLE_COMPILER_DEBUG
596 static void print_vars(FILE *f, TupleEvalContext *ctx, TupleEvalNode *node, gint start, gint end)
597 {
598 gint i;
599 g_return_if_fail(node != NULL);
600 g_return_if_fail(ctx != NULL);
601 g_return_if_fail(start >= 0);
602 g_return_if_fail(start <= end);
603 g_return_if_fail(end < MAX_VAR);
604
605 for (i = start; i <= end; i++) {
606 TupleEvalVar *v = NULL;
607 gchar *s = NULL;
608 gint n = node->var[i];
609
610 if (n >= 0) {
611 v = ctx->variables[n];
612 if (v) {
613 s = v->name;
614
615 if (v->type == VAR_CONST)
616 fprintf(f, "(const)");
617 else if (v->type == VAR_DEF)
618 fprintf(f, "(def)");
619 }
620 }
621
622 fprintf(f, "var[%d]=(%d),\"%s\" ", i, n, s);
623 }
624 }
625
626 gint tuple_formatter_print(FILE *f, gint *level, TupleEvalContext *ctx, TupleEvalNode *expr)
627 {
628 TupleEvalNode *curr = expr;
629
630 if (!expr) return -1;
631
632 (*level)++;
633
634 /* Evaluate tuple in given TupleEval expression in given
635 * context and return resulting string.
636 */
637 while (curr) {
638 gint i;
639 for (i = 0; i < *level; i++)
640 fprintf(f, " ");
641
642 switch (curr->opcode) {
643 case OP_RAW:
644 fprintf(f, "OP_RAW text=\"%s\"\n", curr->text);
645 break;
646
647 case OP_FIELD:
648 fprintf(f, "OP_FIELD ");
649 print_vars(f, ctx, curr, 0, 0);
650 fprintf(f, "\n");
651 break;
652
653 case OP_EXISTS:
654 fprintf(f, "OP_EXISTS ");
655 print_vars(f, ctx, curr, 0, 0);
656 fprintf(f, "\n");
657 tuple_formatter_print(f, level, ctx, curr->children);
658 break;
659
660 case OP_DEF_STRING:
661 fprintf(f, "OP_DEF_STRING ");
662 fprintf(f, "\n");
663 break;
664
665 case OP_DEF_INT:
666 fprintf(f, "OP_DEF_INT ");
667 fprintf(f, "\n");
668 break;
669
670 case OP_EQUALS:
671 fprintf(f, "OP_EQUALS ");
672 print_vars(f, ctx, curr, 0, 1);
673 fprintf(f, "\n");
674 tuple_formatter_print(f, level, ctx, curr->children);
675 break;
676
677 case OP_NOT_EQUALS:
678 fprintf(f, "OP_NOT_EQUALS ");
679 print_vars(f, ctx, curr, 0, 1);
680 fprintf(f, "\n");
681 tuple_formatter_print(f, level, ctx, curr->children);
682 break;
683
684 case OP_IS_EMPTY:
685 fprintf(f, "OP_IS_EMPTY ");
686 print_vars(f, ctx, curr, 0, 0);
687 fprintf(f, "\n");
688 tuple_formatter_print(f, level, ctx, curr->children);
689 break;
690
691 default:
692 fprintf(f, "Unimplemented opcode %d!\n", curr->opcode);
693 break;
694 }
695
696 curr = curr->next;
697 }
698
699 (*level)--;
700
701 return 0;
702 }
703
704 #endif