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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
1 #include <iostream>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2 #include <string>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 #include <cstdio>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 #include <cstdlib>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 #include <readline/readline.h>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 #include <readline/history.h>
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 #include "gui-main.h"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 #include "tty-main.h"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
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
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 namespace tty
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 int main (int, char **)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20 std::cout
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 << "Example Calculator.\n"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 << "Available operations: + - * / ^ ()\n"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 << "Semicolon terminates statement.\n"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 << "GNU Readline available for history editing.\n" << std::endl;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25
4
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 1
diff changeset
26 interpreter::init ();
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 for (;;)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
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
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 if (! tmp)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 if (*tmp)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 add_history (tmp);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 using_history ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 std::string line = tmp;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 free (tmp);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45 int status = interpreter::parse_and_execute (line);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 if (status < 0)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50
4
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 1
diff changeset
51 interpreter::fini ();
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 return 0;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
54 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
55
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 void emit_result (double value)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
58 std::cout << "ans = " << value << std::endl;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
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
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 }