Mercurial > jwe > qt-gui-with-push-parser
annotate interpreter.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 | 04867eba6428 |
children | 894be158b32d |
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) |
7
04867eba6428
function objects and signals/slots for errors and results
John W. Eaton <jwe@octave.org>
parents:
6
diff
changeset
|
17 : m_error_handler ([] (const char *msg) |
04867eba6428
function objects and signals/slots for errors and results
John W. Eaton <jwe@octave.org>
parents:
6
diff
changeset
|
18 { 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
|
19 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
|
20 { std::cout << "ans = " << value << std::endl; }) |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
21 { |
6
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
22 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
|
23 { |
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
24 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
|
25 exit (1); |
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
26 } |
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 the_interpreter = this; |
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
29 |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
30 parser::init (); |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
31 } |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
32 |
6
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
33 interpreter::~interpreter (void) |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
34 { |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
35 parser::fini (); |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
36 } |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
37 |
6
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
38 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
|
39 { |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
40 return parser::parse_and_execute (line); |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
41 } |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
42 |
6
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
43 void interpreter::emit_result (double value) |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
44 { |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
45 // Simulate a delay in calculation. |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
46 sleep (1); |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
47 |
7
04867eba6428
function objects and signals/slots for errors and results
John W. Eaton <jwe@octave.org>
parents:
6
diff
changeset
|
48 m_result_handler (value); |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
49 } |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
50 |
6
1b575145197e
interpreter is now a class instead of a namespace with functions
John W. Eaton <jwe@octave.org>
parents:
4
diff
changeset
|
51 void interpreter::emit_error (const char *msg) |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
52 { |
7
04867eba6428
function objects and signals/slots for errors and results
John W. Eaton <jwe@octave.org>
parents:
6
diff
changeset
|
53 m_error_handler (msg); |
4
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
54 } |
0e154787183d
new interpreter and qt_interpreter objects
John W. Eaton <jwe@octave.org>
parents:
diff
changeset
|
55 } |