Mercurial > jwe > qt-gui-with-push-parser
annotate interpreter.cpp @ 12:894be158b32d
define parser as a class and eliminate some global variables
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 23 May 2019 17:57:20 -0400 |
parents | 822a2fe5bb51 |
children | 1e5a1e15fa56 |
rev | line source |
---|---|
6
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
1 #include <iostream> |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
2 #include <string> |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
3 |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
4 #include <unistd.h> |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
5 |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
6 #include "interpreter.h" |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
7 #include "parser.h" |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
8 |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
9 #include "gui-main.h" |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
10 #include "tty-main.h" |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
11 |
6
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
12 namespace calc |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
13 { |
6
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
14 interpreter *interpreter::the_interpreter = nullptr; |
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
15 |
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
16 interpreter::interpreter (void) |
12
894be158b32d
define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
17 : m_parser (*this), |
894be158b32d
define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
18 m_error_handler ([] (const char *msg) |
7
04867eba6428
function objects and signals/slots for errors and results
John W. Eaton <jwe@octave.org>
parents:
6
diff
changeset
|
19 { std::cerr << "error: " << msg << std::endl; }), |
04867eba6428
function objects and signals/slots for errors and results
John W. Eaton <jwe@octave.org>
parents:
6
diff
changeset
|
20 m_result_handler ([] (double value) |
9
822a2fe5bb51
move command window to separate file and other refactoring
John W. Eaton <jwe@octave.org>
parents:
7
diff
changeset
|
21 { std::cout << "ans = " << value << std::endl; }) |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
22 { |
6
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
23 if (the_interpreter) |
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
24 { |
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
25 std::cerr << "multiple interpreters are not possible!" << std::endl; |
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
26 exit (1); |
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
27 } |
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
28 |
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
29 the_interpreter = this; |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
30 } |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
31 |
6
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
32 int interpreter::parse_and_execute (const std::string& line) |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
33 { |
12
894be158b32d
define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
34 return m_parser.parse_and_execute (line); |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
35 } |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
36 |
12
894be158b32d
define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
37 void interpreter::emit_result (double value) const |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
38 { |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
39 // Simulate a delay in calculation. |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
40 sleep (1); |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
41 |
7
04867eba6428
function objects and signals/slots for errors and results
John W. Eaton <jwe@octave.org>
parents:
6
diff
changeset
|
42 m_result_handler (value); |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
43 } |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
44 |
12
894be158b32d
define parser as a class and eliminate some global variables
John W. Eaton <jwe@octave.org>
parents:
9
diff
changeset
|
45 void interpreter::emit_error (const char *msg) const |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
46 { |
7
04867eba6428
function objects and signals/slots for errors and results
John W. Eaton <jwe@octave.org>
parents:
6
diff
changeset
|
47 m_error_handler (msg); |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
48 } |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
49 } |