annotate tty-main.cpp @ 9:822a2fe5bb51

move command window to separate file and other refactoring
author John W. Eaton <jwe@octave.org>
date Thu, 23 May 2019 12:36:26 -0400
parents 1b575145197e
children 69f5d5e42d2b
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
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 for (;;)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
28 {
4
0e154787183d new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents: 1
diff changeset
29 char *tmp = readline (parser::beg_of_stmt ? ">> " : "");
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
30
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 if (! tmp)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34 if (*tmp)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
35 {
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36 add_history (tmp);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 using_history ();
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
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 std::string line = tmp;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42 free (tmp);
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43
6
1b575145197e interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents: 4
diff changeset
44 int status = interp.parse_and_execute (line);
0
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 if (status < 0)
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 break;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48 }
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 return 0;
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 }
dff751fb985c initial revision
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 }