Mercurial > jwe > qt-gui-with-push-parser
comparison 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 |
comparison
equal
deleted
inserted
replaced
0:dff751fb985c | 1:08df60a01bc1 |
---|---|
7 #include <iostream> | 7 #include <iostream> |
8 | 8 |
9 #include <cctype> | 9 #include <cctype> |
10 #include <cmath> | 10 #include <cmath> |
11 #include <cstdio> | 11 #include <cstdio> |
12 #include <unistd.h> | |
12 | 13 |
13 #include "main.h" | 14 #include "main.h" |
14 #include "gui-main.h" | 15 #include "gui-main.h" |
15 #include "tty-main.h" | 16 #include "tty-main.h" |
16 #include "parse.h" | 17 #include "parse.h" |
19 { | 20 { |
20 FILE *outfile = 0; | 21 FILE *outfile = 0; |
21 size_t bufptr = 0; | 22 size_t bufptr = 0; |
22 size_t chunk_size = 0; | 23 size_t chunk_size = 0; |
23 | 24 |
24 const char *buf; | 25 const char *buf; |
25 bool beg_of_stmt = true; | 26 bool beg_of_stmt = true; |
26 | 27 |
27 static int yylex (void); | 28 static int yylex (void); |
28 static void yyerror (char const *); | 29 |
30 static void debug_trace (const char *); | |
29 } | 31 } |
30 | 32 |
31 static void yyerror (char const *); | 33 static void yyerror (char const *); |
32 | 34 |
33 %} | 35 %} |
44 %% | 46 %% |
45 | 47 |
46 input : // empty | 48 input : // empty |
47 { } | 49 { } |
48 | input line | 50 | input line |
51 | error | |
52 { | |
53 interpreter::debug_trace ("ABORT"); | |
54 YYABORT; | |
55 } | |
49 ; | 56 ; |
50 | 57 |
51 line : ';' | 58 line : ';' |
52 { } | 59 { } |
53 | exp ';' | 60 | exp ';' |
57 } | 64 } |
58 ; | 65 ; |
59 | 66 |
60 exp : NUM | 67 exp : NUM |
61 { | 68 { |
69 interpreter::debug_trace ("NUM"); | |
62 $$ = $1; | 70 $$ = $1; |
63 interpreter::beg_of_stmt = false; | 71 interpreter::beg_of_stmt = false; |
64 } | 72 } |
65 | exp '+' exp | 73 | exp '+' exp |
66 { | 74 { |
75 interpreter::debug_trace ("ADD"); | |
67 $$ = $1 + $3; | 76 $$ = $1 + $3; |
68 interpreter::beg_of_stmt = false; | 77 interpreter::beg_of_stmt = false; |
69 } | 78 } |
70 | exp '-' exp | 79 | exp '-' exp |
71 { | 80 { |
81 interpreter::debug_trace ("SUB"); | |
72 $$ = $1 - $3; | 82 $$ = $1 - $3; |
73 interpreter::beg_of_stmt = false; | 83 interpreter::beg_of_stmt = false; |
74 } | 84 } |
75 | exp '*' exp | 85 | exp '*' exp |
76 { | 86 { |
87 interpreter::debug_trace ("MUL"); | |
77 $$ = $1 * $3; | 88 $$ = $1 * $3; |
78 interpreter::beg_of_stmt = false; | 89 interpreter::beg_of_stmt = false; |
79 } | 90 } |
80 | exp '/' exp | 91 | exp '/' exp |
81 { | 92 { |
93 interpreter::debug_trace ("DIV"); | |
82 $$ = $1 / $3; | 94 $$ = $1 / $3; |
83 interpreter::beg_of_stmt = false; | 95 interpreter::beg_of_stmt = false; |
84 } | 96 } |
85 | '-' exp %prec NEG | 97 | '-' exp %prec NEG |
86 { | 98 { |
99 interpreter::debug_trace ("NEG"); | |
87 $$ = -$2; | 100 $$ = -$2; |
88 interpreter::beg_of_stmt = false; | 101 interpreter::beg_of_stmt = false; |
89 } | 102 } |
90 | exp '^' exp | 103 | exp '^' exp |
91 { | 104 { |
105 interpreter::debug_trace ("EXP"); | |
92 $$ = std::pow ($1, $3); | 106 $$ = std::pow ($1, $3); |
93 interpreter::beg_of_stmt = false; | 107 interpreter::beg_of_stmt = false; |
94 } | 108 } |
95 | '(' exp ')' | 109 | '(' exp ')' |
96 { | 110 { |
111 interpreter::debug_trace ("PAREN"); | |
97 $$ = $2; | 112 $$ = $2; |
98 interpreter::beg_of_stmt = false; | 113 interpreter::beg_of_stmt = false; |
99 } | 114 } |
100 ; | 115 ; |
101 | 116 |
131 | 146 |
132 // Return a single char. | 147 // Return a single char. |
133 return c; | 148 return c; |
134 } | 149 } |
135 | 150 |
136 static void yyerror (char const *msg) | |
137 { | |
138 std::cerr << "parse error: " << msg << std::endl; | |
139 } | |
140 | |
141 static yypstate *ps = 0; | 151 static yypstate *ps = 0; |
142 | 152 |
143 void | 153 void |
144 parser_fini (void) | 154 parser_fini (void) |
145 { | 155 { |
173 | 183 |
174 status = yypush_parse (ps); | 184 status = yypush_parse (ps); |
175 } | 185 } |
176 while (status == YYPUSH_MORE); | 186 while (status == YYPUSH_MORE); |
177 | 187 |
178 return status; | 188 return -2; |
179 } | 189 } |
180 | 190 |
181 void emit_result (double value) | 191 void emit_result (double value) |
182 { | 192 { |
183 if (tty_mode) | 193 // Simulate a delay in calculation. |
194 sleep (1); | |
195 | |
196 if (calc::tty_mode) | |
184 tty::emit_result (value); | 197 tty::emit_result (value); |
185 else | 198 else |
186 gui::emit_result (value); | 199 gui::emit_result (value); |
187 } | 200 } |
201 | |
202 void emit_error (const char *msg) | |
203 { | |
204 if (calc::tty_mode) | |
205 tty::emit_error (msg); | |
206 else | |
207 gui::emit_error (msg); | |
208 } | |
209 | |
210 void debug_trace (const char *msg) | |
211 { | |
212 if (calc::debug_mode) | |
213 std::cerr << msg << std::endl; | |
214 } | |
188 } | 215 } |
189 | 216 |
190 static void yyerror (char const *msg) | 217 static void yyerror (char const *msg) |
191 { | 218 { |
192 interpreter::yyerror (msg); | 219 interpreter::emit_error (msg); |
193 } | 220 } |