diff parse.yy @ 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
line wrap: on
line diff
--- 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);
 }