diff --git a/interpreter.h b/interpreter.h index 08beaa3..a28c49f 100644 --- a/interpreter.h +++ b/interpreter.h @@ -512,6 +512,9 @@ struct Picoc_Struct { struct Table StringTable; struct TableEntry *StringHashTable[STRING_TABLE_SIZE]; char *StrEmpty; + + /* stats */ + int CollectStats; }; /* table.c */ diff --git a/picoc.c b/picoc.c index c5e1201..8df11dd 100644 --- a/picoc.c +++ b/picoc.c @@ -23,17 +23,20 @@ int main(int argc, char **argv) { int ParamCount = 1; int DontRunMain = false; + int CollectStats = false; + int StatsType = 0; int StackSize = getenv("STACKSIZE") ? atoi(getenv("STACKSIZE")) : PICOC_STACK_SIZE; Picoc pc; if (argc < 2 || strcmp(argv[ParamCount], "-h") == 0) { printf(PICOC_VERSION " \n" "Format:\n\n" - "> picoc ... [- ...] : run a program, calls main() as the entry point\n" - "> picoc -s ... [- ...] : run a script, runs the program without calling main()\n" - "> picoc -i : interactive mode, Ctrl+d to exit\n" - "> picoc -c : copyright info\n" - "> picoc -h : this help message\n"); + "> picoc ... [- ...] : run a program, calls main() as the entry point\n" + "> picoc -s ... [- ...] : run a script, runs the program without calling main()\n" + "> picoc -d[type] ... [- ...] : run a program, outputting debugging stats\n" + "> picoc -i : interactive mode, Ctrl+d to exit\n" + "> picoc -c : copyright info\n" + "> picoc -h : this help message\n"); return 0; } @@ -48,6 +51,13 @@ int main(int argc, char **argv) DontRunMain = true; PicocIncludeAllSystemHeaders(&pc); ParamCount++; + } else if (strncmp(argv[ParamCount], "-d", 2) == 0) { + if (strlen(argv[ParamCount]) > 2) { + StatsType = atoi(&argv[ParamCount][2]); + } + CollectStats = true; + pc.CollectStats = true; + ParamCount++; } if (argc > ParamCount && strcmp(argv[ParamCount], "-i") == 0) { @@ -68,7 +78,9 @@ int main(int argc, char **argv) PicocCleanup(&pc); - stats_print_tokens(0); + if (CollectStats) { + stats_print_tokens(StatsType == 1); + } return pc.PicocExitValue; } diff --git a/stats.c b/stats.c index 88da697..2864e4e 100644 --- a/stats.c +++ b/stats.c @@ -120,14 +120,20 @@ struct LexTokenStat LexTokenStats[NO_TOKENS] = { void stats_log_statement(enum LexToken token, struct ParseState *parser) { - fprintf(stderr, "Parsing Statement %s (%d) in %s (%d) at %s:%d:%d\n", LexTokenStats[token].name, token, RunModeNames[parser->Mode], parser->Mode, parser->FileName, parser->Line, parser->CharacterPos); - LexTokenStats[token].count[parser->Mode]++; + if (parser->pc->CollectStats) { + fprintf(stderr, "Parsing Statement %s (%d) in %s (%d) at %s:%d:%d\n", LexTokenStats[token].name, token, + RunModeNames[parser->Mode], parser->Mode, parser->FileName, parser->Line, parser->CharacterPos); + LexTokenStats[token].count[parser->Mode]++; + } } void stats_log_expression(enum LexToken token, struct ParseState *parser) { - fprintf(stderr, "Parsing Expression %s (%d) in %s (%d) at %s:%d:%d\n", LexTokenStats[token].name, token, RunModeNames[parser->Mode], parser->Mode, parser->FileName, parser->Line, parser->CharacterPos); - LexTokenStats[token].count[parser->Mode]++; + if (parser->pc->CollectStats) { + fprintf(stderr, "Parsing Expression %s (%d) in %s (%d) at %s:%d:%d\n", LexTokenStats[token].name, token, + RunModeNames[parser->Mode], parser->Mode, parser->FileName, parser->Line, parser->CharacterPos); + LexTokenStats[token].count[parser->Mode]++; + } }