Mercurial > jwe > qt-gui-with-push-parser
changeset 1:08df60a01bc1
debug flag, handle input with signal
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 20 May 2019 13:45:58 -0400 |
parents | dff751fb985c |
children | b97ffa8e4019 |
files | gui-main.cpp gui-main.h main.cpp main.h parse.h parse.yy tty-main.cpp tty-main.h |
diffstat | 8 files changed, 98 insertions(+), 21 deletions(-) [+] |
line wrap: on
line diff
--- 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);
--- 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); }
--- 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);
--- 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; +}
--- 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); }
--- 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 <cctype> #include <cmath> #include <cstdio> +#include <unistd.h> #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); }