Mercurial > jwe > qt-gui-with-push-parser
annotate tty-main.cpp @ 4:0e154787183d
new interpreter and qt_interpreter objects
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 22 May 2019 17:30:46 -0400 |
parents | 08df60a01bc1 |
children | 1b575145197e |
rev | line source |
---|---|
0 | 1 #include <iostream> |
2 #include <string> | |
3 | |
4 #include <cstdio> | |
5 #include <cstdlib> | |
6 | |
7 #include <readline/readline.h> | |
8 #include <readline/history.h> | |
9 | |
10 #include "gui-main.h" | |
11 #include "tty-main.h" | |
12 | |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
1
diff
changeset
|
13 #include "interpreter.h" |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
1
diff
changeset
|
14 #include "parser.h" |
0 | 15 |
16 namespace tty | |
17 { | |
18 int main (int, char **) | |
19 { | |
20 std::cout | |
21 << "Example Calculator.\n" | |
22 << "Available operations: + - * / ^ ()\n" | |
23 << "Semicolon terminates statement.\n" | |
24 << "GNU Readline available for history editing.\n" << std::endl; | |
25 | |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
1
diff
changeset
|
26 interpreter::init (); |
0 | 27 |
28 for (;;) | |
29 { | |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
1
diff
changeset
|
30 char *tmp = readline (parser::beg_of_stmt ? ">> " : ""); |
0 | 31 |
32 if (! tmp) | |
33 break; | |
34 | |
35 if (*tmp) | |
36 { | |
37 add_history (tmp); | |
38 using_history (); | |
39 } | |
40 | |
41 std::string line = tmp; | |
42 | |
43 free (tmp); | |
44 | |
45 int status = interpreter::parse_and_execute (line); | |
46 | |
47 if (status < 0) | |
48 break; | |
49 } | |
50 | |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
1
diff
changeset
|
51 interpreter::fini (); |
0 | 52 |
53 return 0; | |
54 } | |
55 | |
56 void emit_result (double value) | |
57 { | |
58 std::cout << "ans = " << value << std::endl; | |
59 } | |
1
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
60 |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
61 void emit_error (const char *msg) |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
62 { |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
63 std::cerr << "parse error: " << msg << std::endl; |
08df60a01bc1
debug flag, handle input with signal
John W. Eaton <jwe@octave.org>
parents:
0
diff
changeset
|
64 } |
0 | 65 } |