lang: ast: suppress debug output by default

This commit is contained in:
2024-12-05 19:37:58 +00:00
parent c0a90f40b5
commit 9a4b074381
3 changed files with 48 additions and 22 deletions

View File

@@ -9,16 +9,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef IVY_LANG_DEBUG
static void print_state_stack(struct ivy_parser *parser) static void print_state_stack(struct ivy_parser *parser)
{ {
b_queue_iterator it = {0}; b_queue_iterator it = {0};
b_queue_foreach (&it, &parser->p_state) { b_queue_foreach (&it, &parser->p_state) {
struct parser_state *state struct parser_state *state
= b_unbox(struct parser_state, it.entry, s_entry); = 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)); ivy_ast_node_type_to_string(state->s_node->n_type));
} }
} }
#endif
enum ivy_status ivy_parser_create(struct ivy_parser **parser) 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); struct token_parse_result result = func(parser, tok);
parser->p_status = result.r_status; 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); print_state_stack(parser);
#endif
if (result.r_flags & PARSE_REPEAT_TOKEN) { if (result.r_flags & PARSE_REPEAT_TOKEN) {
continue; continue;
} }
@@ -144,8 +147,10 @@ struct parser_state *parser_push_state(
node_type->n_init_state(parser, state, arg); 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); print_state_stack(parser);
#endif
return state; return state;
} }
@@ -165,8 +170,10 @@ void parser_pop_state(struct ivy_parser *parser, enum pop_state_flags flags)
free(state); free(state);
printf("states (after pop)\n"); #ifdef IVY_LANG_DEBUG
debug_printf("states (after pop)\n");
print_state_stack(parser); print_state_stack(parser);
#endif
} }
void parser_replace_current_node( void parser_replace_current_node(

View File

@@ -1,4 +1,5 @@
#include "../node.h" #include "../node.h"
#include "../debug.h"
#include "expr.h" #include "expr.h"
#include <blue/object/string.h> #include <blue/object/string.h>
@@ -12,40 +13,40 @@ static void print_operand(struct ivy_ast_node *node)
case IVY_AST_IDENT: { case IVY_AST_IDENT: {
struct ivy_ast_ident_node *ident struct ivy_ast_ident_node *ident
= (struct ivy_ast_ident_node *)node; = (struct ivy_ast_ident_node *)node;
printf("%s", ident->n_content->t_str); debug_printf("%s", ident->n_content->t_str);
break; break;
} }
case IVY_AST_INT: { case IVY_AST_INT: {
struct ivy_ast_int_node *v = (struct ivy_ast_int_node *)node; 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; break;
} }
case IVY_AST_DOUBLE: { case IVY_AST_DOUBLE: {
struct ivy_ast_double_node *v = (struct ivy_ast_double_node *)node; 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; break;
} }
case IVY_AST_OP: { case IVY_AST_OP: {
struct ivy_ast_op_node *v = (struct ivy_ast_op_node *)node; 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; break;
} }
case IVY_AST_MSG: { case IVY_AST_MSG: {
struct ivy_ast_msg_node *v = (struct ivy_ast_msg_node *)node; struct ivy_ast_msg_node *v = (struct ivy_ast_msg_node *)node;
if (v->n_sel->n_msg_name) { 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 { } else {
printf("<keyword-msg>"); debug_printf("<keyword-msg>");
} }
break; break;
} }
case IVY_AST_STRING: { case IVY_AST_STRING: {
struct ivy_ast_string_node *v = (struct ivy_ast_string_node *)node; 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; break;
} }
default: default:
printf("<node>"); debug_printf("<node>");
break; 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; return (struct ivy_ast_node *)new_op_node;
} }
#ifdef IVY_LANG_DEBUG
static void print_expr_queues(struct expr_parser_state *state) static void print_expr_queues(struct expr_parser_state *state)
{ {
b_queue_iterator it = {0}; b_queue_iterator it = {0};
printf("operators:"); debug_printf("operators:");
b_queue_foreach (&it, &state->s_operator_stack) { b_queue_foreach (&it, &state->s_operator_stack) {
struct ivy_ast_node *n struct ivy_ast_node *n
= b_unbox(struct ivy_ast_node, it.entry, n_entry); = b_unbox(struct ivy_ast_node, it.entry, n_entry);
fputc(' ', stdout); debug_printf(" ");
print_operand(n); print_operand(n);
} }
printf("\noperands:"); debug_printf("\noperands:");
b_queue_foreach (&it, &state->s_output_queue) { b_queue_foreach (&it, &state->s_output_queue) {
struct ivy_ast_node *n struct ivy_ast_node *n
= b_unbox(struct ivy_ast_node, it.entry, n_entry); = b_unbox(struct ivy_ast_node, it.entry, n_entry);
fputc(' ', stdout); debug_printf(" ");
print_operand(n); print_operand(n);
} }
printf("\n"); debug_printf("\n");
} }
#endif
void arith_push_operator(struct expr_parser_state *state, struct ivy_ast_node *node) 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); b_queue_push_back(&state->s_operator_stack, &node->n_entry);
#ifdef IVY_LANG_DEBUG
print_expr_queues(state); print_expr_queues(state);
#endif
} }
static bool op_node_is_complete(struct ivy_ast_op_node *node) static bool op_node_is_complete(struct ivy_ast_op_node *node)
@@ -293,7 +298,7 @@ enum ivy_status expr_finalise_arith(
} }
#if 0 #if 0
printf("** after linearisation:\n"); debug_printf("** after linearisation:\n");
print_expr_queues(state); print_expr_queues(state);
#endif #endif
@@ -367,7 +372,7 @@ enum ivy_status expr_finalise_arith(
} }
#if 0 #if 0
printf("** after hierarchisation:\n"); debug_printf("** after hierarchisation:\n");
print_expr_queues(state); print_expr_queues(state);
#endif #endif
@@ -387,10 +392,10 @@ enum ivy_status expr_finalise_arith(
} }
#if 0 #if 0
printf("** after de-linearisation:\n"); debug_printf("** after de-linearisation:\n");
print_expr_queues(state); print_expr_queues(state);
ivy_ast_node_print(*expr_tree); ivy_ast_node_print(*expr_tree);
printf("------\n"); debug_printf("------\n");
#endif #endif
/* the final node remaining on the temp operand stack is the root node /* 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); arith_push_operator(state, op);
state->s_prev_component = EXPR_CMP_OPERATOR; state->s_prev_component = EXPR_CMP_OPERATOR;
#ifdef IVY_LANG_DEBUG
print_expr_queues(state); print_expr_queues(state);
#endif
return PARSE_RESULT(IVY_OK, 0); return PARSE_RESULT(IVY_OK, 0);
} }

12
lang/debug.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef _DEBUG_H_
#define _DEBUG_H_
#include <stdio.h>
#if defined(IVY_LANG_DEBUG)
#define debug_printf(...) printf(__VA_ARGS__)
#else
#define debug_printf(...)
#endif
#endif