# HG changeset patch # User John W. Eaton # Date 1558374358 14400 # Node ID 08df60a01bc1e8d838495c30d1c99493fc84fa89 # Parent dff751fb985c23a8459d6cadb05d361f0e289038 debug flag, handle input with signal diff -r dff751fb985c -r 08df60a01bc1 gui-main.cpp --- a/gui-main.cpp Mon May 13 09:48:06 2019 -0500 +++ b/gui-main.cpp Mon May 20 13:45:58 2019 -0400 @@ -107,6 +107,9 @@ connect (this, SIGNAL (result_available (double)), this, SLOT (handle_result (double))); + connect (this, SIGNAL (input_char_available (int)), + this, SLOT (handle_input_char (int))); + insert_at_end ("Qt Example Calculator.\n" "Available operations: + - * / ^ ()\n" @@ -121,6 +124,9 @@ void command_window::accept (const std::string& line) { + if (calc::debug_mode) + std::cerr << "accept: " << line << std::endl; + insert_at_end ("\n"); if (! line.empty ()) @@ -161,6 +167,13 @@ insert_at_cursor (text); } + void command_window::emit_error (const std::string& msg) + { + insert_at_end ("parse error: " + msg); + + rl_abort (0, 0); + } + void command_window::emit_result (double value) { emit result_available (value); @@ -231,15 +244,17 @@ } if (key >= 0) - { - // FIXME: should this emit a signal instead? - available_char = key; - rl_callback_read_char (); - return; - } + emit input_char_available (key); } } + + void command_window::handle_input_char (int key) + { + available_char = key; + rl_callback_read_char (); + } + int command_window::do_arrow_key (int arrow_key) { int retval = 0; @@ -333,6 +348,7 @@ void command_window::insert_result (double value) { std::ostringstream buf; + buf << "ans = " << value << "\n"; insert_at_cursor (buf.str ()); @@ -372,6 +388,11 @@ return status; } + void emit_error (const std::string& msg) + { + calc_interaction_window->emit_error (msg); + } + void emit_result (double value) { calc_interaction_window->emit_result (value); diff -r dff751fb985c -r 08df60a01bc1 gui-main.h --- a/gui-main.h Mon May 13 09:48:06 2019 -0500 +++ b/gui-main.h Mon May 20 13:45:58 2019 -0400 @@ -34,14 +34,20 @@ void insert_at_end (const std::string& text); + void emit_error (const std::string& msg); + void emit_result (double value); signals: + void input_char_available (int key); + void result_available (double value); public slots: + void handle_input_char (int key); + void handle_result (double value); protected: @@ -77,6 +83,8 @@ extern int main (int argc, char **argv); + extern void emit_error (const std::string& msg); + extern void emit_result (double value); } diff -r dff751fb985c -r 08df60a01bc1 main.cpp --- a/main.cpp Mon May 13 09:48:06 2019 -0500 +++ b/main.cpp Mon May 20 13:45:58 2019 -0400 @@ -5,7 +5,11 @@ #include "gui-main.h" #include "tty-main.h" -int tty_mode = false; +namespace calc +{ + bool tty_mode = false; + bool debug_mode = false; +} int main (int argc, char **argv) @@ -19,12 +23,17 @@ for (int i = 1; i < argc; i++) { if (argv[i] == std::string ("--tty")) - tty_mode = true; + calc::tty_mode = true; + else + new_argv[new_argc++] = argv[i]; + + if (argv[i] == std::string ("--debug")) + calc::debug_mode = true; else new_argv[new_argc++] = argv[i]; } - int status = tty_mode + int status = calc::tty_mode ? tty::main (new_argc, new_argv) : gui::main (new_argc, new_argv); diff -r dff751fb985c -r 08df60a01bc1 main.h --- a/main.h Mon May 13 09:48:06 2019 -0500 +++ b/main.h Mon May 20 13:45:58 2019 -0400 @@ -1,1 +1,5 @@ -extern int tty_mode; +namespace calc +{ + extern bool tty_mode; + extern bool debug_mode; +} diff -r dff751fb985c -r 08df60a01bc1 parse.h --- a/parse.h Mon May 13 09:48:06 2019 -0500 +++ b/parse.h Mon May 20 13:45:58 2019 -0400 @@ -7,5 +7,7 @@ extern int parse_and_execute (const std::string& line); + extern void emit_error (const char *msg); + extern void emit_result (double value); } diff -r dff751fb985c -r 08df60a01bc1 parse.yy --- a/parse.yy Mon May 13 09:48:06 2019 -0500 +++ b/parse.yy Mon May 20 13:45:58 2019 -0400 @@ -9,6 +9,7 @@ #include #include #include +#include #include "main.h" #include "gui-main.h" @@ -21,11 +22,12 @@ size_t bufptr = 0; size_t chunk_size = 0; - const char *buf; - bool beg_of_stmt = true; + const char *buf; + bool beg_of_stmt = true; static int yylex (void); - static void yyerror (char const *); + + static void debug_trace (const char *); } static void yyerror (char const *); @@ -46,6 +48,11 @@ input : // empty { } | input line + | error + { + interpreter::debug_trace ("ABORT"); + YYABORT; + } ; line : ';' @@ -59,41 +66,49 @@ exp : NUM { + interpreter::debug_trace ("NUM"); $$ = $1; interpreter::beg_of_stmt = false; } | exp '+' exp { + interpreter::debug_trace ("ADD"); $$ = $1 + $3; interpreter::beg_of_stmt = false; } | exp '-' exp { + interpreter::debug_trace ("SUB"); $$ = $1 - $3; interpreter::beg_of_stmt = false; } | exp '*' exp { + interpreter::debug_trace ("MUL"); $$ = $1 * $3; interpreter::beg_of_stmt = false; } | exp '/' exp { + interpreter::debug_trace ("DIV"); $$ = $1 / $3; interpreter::beg_of_stmt = false; } | '-' exp %prec NEG { + interpreter::debug_trace ("NEG"); $$ = -$2; interpreter::beg_of_stmt = false; } | exp '^' exp { + interpreter::debug_trace ("EXP"); $$ = std::pow ($1, $3); interpreter::beg_of_stmt = false; } | '(' exp ')' { + interpreter::debug_trace ("PAREN"); $$ = $2; interpreter::beg_of_stmt = false; } @@ -133,11 +148,6 @@ return c; } - static void yyerror (char const *msg) - { - std::cerr << "parse error: " << msg << std::endl; - } - static yypstate *ps = 0; void @@ -175,19 +185,36 @@ } while (status == YYPUSH_MORE); - return status; + return -2; } void emit_result (double value) { - if (tty_mode) + // Simulate a delay in calculation. + sleep (1); + + if (calc::tty_mode) tty::emit_result (value); else gui::emit_result (value); } + + void emit_error (const char *msg) + { + if (calc::tty_mode) + tty::emit_error (msg); + else + gui::emit_error (msg); + } + + void debug_trace (const char *msg) + { + if (calc::debug_mode) + std::cerr << msg << std::endl; + } } static void yyerror (char const *msg) { - interpreter::yyerror (msg); + interpreter::emit_error (msg); } diff -r dff751fb985c -r 08df60a01bc1 tty-main.cpp --- a/tty-main.cpp Mon May 13 09:48:06 2019 -0500 +++ b/tty-main.cpp Mon May 20 13:45:58 2019 -0400 @@ -56,4 +56,9 @@ { std::cout << "ans = " << value << std::endl; } + + void emit_error (const char *msg) + { + std::cerr << "parse error: " << msg << std::endl; + } } diff -r dff751fb985c -r 08df60a01bc1 tty-main.h --- a/tty-main.h Mon May 13 09:48:06 2019 -0500 +++ b/tty-main.h Mon May 20 13:45:58 2019 -0400 @@ -3,4 +3,5 @@ extern int main (int argc, char **argv); extern void emit_result (double value); + extern void emit_error (const char *msg); }