From 392fb37ed0ff986d2c6440a3e759bb9360e1b902 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Sat, 6 Aug 2016 17:39:06 +0200 Subject: Make debug prints conditional --- parser.c | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/parser.c b/parser.c index 2827211..857b11d 100644 --- a/parser.c +++ b/parser.c @@ -11,6 +11,16 @@ #include "parser.h" +#undef DEBUG + +#ifdef DEBUG +#define DBG(...) __VA_ARGS__ +#else +#define DBG(...) +#endif +#define DBGF(...) DBG(fprintf(stderr,__VA_ARGS__)) + + #define NOT_IMPLEMENTED false @@ -151,6 +161,7 @@ static Token nexttoken(const char **sourcep,bool expectop){ } +#ifdef DEBUG static void printtoken(FILE *stream,Token tok,const char *msg){ const char *type; switch(tok.type){ @@ -173,6 +184,7 @@ static void printtoken(FILE *stream,Token tok,const char *msg){ fprintf(stream,"(%s) Token: %s (null)\n",msg,type); } } +#endif static AST* parseexpr(const char *source,int *reslen,int minprec,int maxprec); @@ -180,7 +192,7 @@ static AST* parseexpr(const char *source,int *reslen,int minprec,int maxprec); static AST* parseterm(const char *source,int *reslen){ const char *origsource=source; const Token tok=nexttoken(&source,false); - printtoken(stderr,tok,"parseterm"); + DBG(printtoken(stderr,tok,"parseterm")); AST *node; switch(tok.type){ case TT_NUM:{ @@ -246,7 +258,7 @@ static AST* parseterm(const char *source,int *reslen){ const char *tempsource=source; Token next=nexttoken(&source,false); if(next.len==1&&next.str[0]=='('){ - printtoken(stderr,next,"function "); + DBG(printtoken(stderr,next,"function ")); AST **args=malloc(sizeof(AST*)); int nargs=0; int len; @@ -262,14 +274,14 @@ static AST* parseterm(const char *source,int *reslen){ source+=len; args[nargs++]=arg; next=nexttoken(&source,false); - printtoken(stderr,next,"func-sep "); + DBG(printtoken(stderr,next,"func-sep ")); if(next.type!=TT_SYM||next.len!=1||(next.str[0]!=','&&next.str[0]!=')')){ FREEARGSRETNULL; } if(next.str[0]==')')break; } } else { - printtoken(stderr,next,"func-end "); + DBG(printtoken(stderr,next,"func-end ")); source=src; } node=malloc(sizeof(AST)); @@ -304,7 +316,7 @@ static AST* parseterm(const char *source,int *reslen){ if(!node)return NULL; source+=len; Token aftertok=nexttoken(&source,false); - printtoken(stderr,aftertok,"braceclose"); + DBG(printtoken(stderr,aftertok,"braceclose")); if(aftertok.type!=TT_SYM||aftertok.len!=1||aftertok.str[0]!=')'){ ast_free(node); return NULL; @@ -357,14 +369,14 @@ static AST* parseexpr_(const char *source,int *reslen,int minprec,int maxprec){ while(true){ const char *beforeop=source; Token tok=nexttoken(&source,true); - printtoken(stderr,tok,"parseEXPR"); + DBG(printtoken(stderr,tok,"parseEXPR")); if(tok.type==TT_ENDSTMT){ - fprintf(stderr," (token undo)\n"); + DBGF(" (token undo)\n"); source=beforeop; break; } if(tok.type==TT_SYM&&tok.len==1&&(tok.str[0]==')'||tok.str[0]==',')){ - fprintf(stderr," (token undo)\n"); + DBGF(" (token undo)\n"); source=beforeop; break; } @@ -374,7 +386,7 @@ static AST* parseexpr_(const char *source,int *reslen,int minprec,int maxprec){ } int prec=precedence_len(tok.str,tok.len); if(prec>> (%d)\x1B[0m\n",depth); + DBGF("\x1B[32mEXPR ENTER >>> (%d)\x1B[0m\n",depth); depth++; AST *r=parseexpr_(source,reslen,minprec,maxprec); depth--; - fprintf(stderr,"\x1B[32mEXPR LEAVE <<< (%d)\x1B[0m\n",depth); + DBGF("\x1B[32mEXPR LEAVE <<< (%d)\x1B[0m\n",depth); return r; } @@ -488,7 +500,7 @@ AST* parse(const char *source,char **errmsg){ cursor+=reslen; const char *src=source+cursor; Token tok=nexttoken(&src,false); - printtoken(stderr,tok,"parse "); + DBG(printtoken(stderr,tok,"parse ")); if(tok.type!=TT_ENDSTMT){ ast_free(bl); *errmsg=reportparseerror(source); -- cgit v1.2.3