annotate tty-main.cpp @ 19:9e2211f5f293 default tip

NOTES: more updates
author John W. Eaton <jwe@octave.org>
date Tue, 28 May 2019 23:06:08 -0400
parents 894be158b32d
children
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 "tty-main.h"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11
4
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 1
diff changeset
12 #include "interpreter.h"
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 1
diff changeset
13 #include "parser.h"
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14
9
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents: 6
diff changeset
15 namespace calc
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 {
9
822a2fe5bb51 move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents: 6
diff changeset
17 int tty_main (int, char **)
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 std::cout
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
20 << "Example Calculator.\n"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
21 << "Available operations: + - * / ^ ()\n"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
22 << "Semicolon terminates statement.\n"
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 << "GNU Readline available for history editing.\n" << std::endl;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24
6
1b575145197e interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
25 calc::interpreter interp;
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26
12
894be158b32d define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents: 10
diff changeset
27 calc::parser& parser = interp.get_parser ();
894be158b32d define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents: 10
diff changeset
28
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 for (;;)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30 {
12
894be158b32d define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents: 10
diff changeset
31 char *tmp = readline (parser.beg_of_stmt () ? ">> " : "");
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 if (! tmp)
10
69f5d5e42d2b print newline on EOF in tty mode
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
34 {
69f5d5e42d2b print newline on EOF in tty mode
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
35 std::cout << "\n";
69f5d5e42d2b print newline on EOF in tty mode
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
36 break;
69f5d5e42d2b print newline on EOF in tty mode
John W. Eaton <jwe@octave.org>
parents: 9
diff changeset
37 }
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39 if (*tmp)
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 add_history (tmp);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 using_history ();
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 }
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 std::string line = tmp;
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 free (tmp);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48
6
1b575145197e interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
49 int status = interp.parse_and_execute (line);
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 if (status < 0)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
53 }
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 return 0;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
57 }