diff --git a/lang/ast/ctx.c b/lang/ast/ctx.c index 11d0100..d01056d 100644 --- a/lang/ast/ctx.c +++ b/lang/ast/ctx.c @@ -9,16 +9,18 @@ #include #include +#ifdef IVY_LANG_DEBUG static void print_state_stack(struct ivy_parser *parser) { b_queue_iterator it = {0}; b_queue_foreach (&it, &parser->p_state) { struct parser_state *state = b_unbox(struct parser_state, it.entry, s_entry); - printf(" %s\n", + debug_printf(" %s\n", ivy_ast_node_type_to_string(state->s_node->n_type)); } } +#endif enum ivy_status ivy_parser_create(struct ivy_parser **parser) { @@ -60,9 +62,10 @@ enum ivy_status ivy_parser_push_token( struct token_parse_result result = func(parser, tok); parser->p_status = result.r_status; - printf("states (after token)\n"); +#ifdef IVY_LANG_DEBUG + debug_printf("states (after token)\n"); print_state_stack(parser); - +#endif if (result.r_flags & PARSE_REPEAT_TOKEN) { continue; } @@ -144,8 +147,10 @@ struct parser_state *parser_push_state( node_type->n_init_state(parser, state, arg); } - printf("states (after push)\n"); +#ifdef IVY_LANG_DEBUG + debug_printf("states (after push)\n"); print_state_stack(parser); +#endif return state; } @@ -165,8 +170,10 @@ void parser_pop_state(struct ivy_parser *parser, enum pop_state_flags flags) free(state); - printf("states (after pop)\n"); +#ifdef IVY_LANG_DEBUG + debug_printf("states (after pop)\n"); print_state_stack(parser); +#endif } void parser_replace_current_node( diff --git a/lang/ast/expr/arith.c b/lang/ast/expr/arith.c index 1704c32..d3bc6f0 100644 --- a/lang/ast/expr/arith.c +++ b/lang/ast/expr/arith.c @@ -1,4 +1,5 @@ #include "../node.h" +#include "../debug.h" #include "expr.h" #include @@ -12,40 +13,40 @@ static void print_operand(struct ivy_ast_node *node) case IVY_AST_IDENT: { struct ivy_ast_ident_node *ident = (struct ivy_ast_ident_node *)node; - printf("%s", ident->n_content->t_str); + debug_printf("%s", ident->n_content->t_str); break; } case IVY_AST_INT: { struct ivy_ast_int_node *v = (struct ivy_ast_int_node *)node; - printf("%llu", v->n_value->t_int); + debug_printf("%llu", v->n_value->t_int); break; } case IVY_AST_DOUBLE: { struct ivy_ast_double_node *v = (struct ivy_ast_double_node *)node; - printf("%.2lf", v->n_value->t_double); + debug_printf("%.2lf", v->n_value->t_double); break; } case IVY_AST_OP: { struct ivy_ast_op_node *v = (struct ivy_ast_op_node *)node; - printf("%s", ivy_operator_id_to_string(v->n_op->op_id)); + debug_printf("%s", ivy_operator_id_to_string(v->n_op->op_id)); break; } case IVY_AST_MSG: { struct ivy_ast_msg_node *v = (struct ivy_ast_msg_node *)node; if (v->n_sel->n_msg_name) { - printf("%s()", v->n_sel->n_msg_name->t_str); + debug_printf("%s()", v->n_sel->n_msg_name->t_str); } else { - printf(""); + debug_printf(""); } break; } case IVY_AST_STRING: { struct ivy_ast_string_node *v = (struct ivy_ast_string_node *)node; - printf("\"%s\"", v->n_value->t_str); + debug_printf("\"%s\"", v->n_value->t_str); break; } default: - printf(""); + debug_printf(""); break; } } @@ -154,28 +155,30 @@ static struct ivy_ast_node *create_operator_node_from_token(struct ivy_token *to return (struct ivy_ast_node *)new_op_node; } +#ifdef IVY_LANG_DEBUG static void print_expr_queues(struct expr_parser_state *state) { b_queue_iterator it = {0}; - printf("operators:"); + debug_printf("operators:"); b_queue_foreach (&it, &state->s_operator_stack) { struct ivy_ast_node *n = b_unbox(struct ivy_ast_node, it.entry, n_entry); - fputc(' ', stdout); + debug_printf(" "); print_operand(n); } - printf("\noperands:"); + debug_printf("\noperands:"); b_queue_foreach (&it, &state->s_output_queue) { struct ivy_ast_node *n = b_unbox(struct ivy_ast_node, it.entry, n_entry); - fputc(' ', stdout); + debug_printf(" "); print_operand(n); } - printf("\n"); + debug_printf("\n"); } +#endif void arith_push_operator(struct expr_parser_state *state, struct ivy_ast_node *node) { @@ -222,7 +225,9 @@ void arith_push_operator(struct expr_parser_state *state, struct ivy_ast_node *n } b_queue_push_back(&state->s_operator_stack, &node->n_entry); +#ifdef IVY_LANG_DEBUG print_expr_queues(state); +#endif } static bool op_node_is_complete(struct ivy_ast_op_node *node) @@ -293,7 +298,7 @@ enum ivy_status expr_finalise_arith( } #if 0 - printf("** after linearisation:\n"); + debug_printf("** after linearisation:\n"); print_expr_queues(state); #endif @@ -367,7 +372,7 @@ enum ivy_status expr_finalise_arith( } #if 0 - printf("** after hierarchisation:\n"); + debug_printf("** after hierarchisation:\n"); print_expr_queues(state); #endif @@ -387,10 +392,10 @@ enum ivy_status expr_finalise_arith( } #if 0 - printf("** after de-linearisation:\n"); + debug_printf("** after de-linearisation:\n"); print_expr_queues(state); ivy_ast_node_print(*expr_tree); - printf("------\n"); + debug_printf("------\n"); #endif /* the final node remaining on the temp operand stack is the root node @@ -455,7 +460,9 @@ struct token_parse_result arith_parse_operator( arith_push_operator(state, op); state->s_prev_component = EXPR_CMP_OPERATOR; +#ifdef IVY_LANG_DEBUG print_expr_queues(state); +#endif return PARSE_RESULT(IVY_OK, 0); } diff --git a/lang/debug.h b/lang/debug.h new file mode 100644 index 0000000..af949ff --- /dev/null +++ b/lang/debug.h @@ -0,0 +1,12 @@ +#ifndef _DEBUG_H_ +#define _DEBUG_H_ + +#include + +#if defined(IVY_LANG_DEBUG) +#define debug_printf(...) printf(__VA_ARGS__) +#else +#define debug_printf(...) +#endif + +#endif \ No newline at end of file