GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
interpreter.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2002-2025 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if ! defined (octave_interpreter_h)
27#define octave_interpreter_h 1
28
29#include "octave-config.h"
30
31#include <atomic>
32#include <map>
33#include <set>
34#include <stack>
35#include <string>
36
37#include "child-list.h"
38#include "oct-time.h"
39#include "quit.h"
40#include "str-vec.h"
41
42#include "cdef-manager.h"
43#include "display.h"
44#include "dynamic-ld.h"
45#include "environment.h"
46#include "error.h"
47#include "event-manager.h"
48#include "gh-manager.h"
49#include "graphics.h"
50#include "gtk-manager.h"
51#include "help.h"
52#include "input.h"
53#include "load-path.h"
54#include "load-save.h"
55#include "oct-hist.h"
56#include "oct-stream.h"
57#include "ov-typeinfo.h"
58#include "pager.h"
59#include "pt-eval.h"
60#include "settings.h"
61#include "symtab.h"
62#include "url-handle-manager.h"
63
64extern OCTINTERP_API bool quit_allowed;
65
66// TRUE means we are ready to interpret commands, but not everything
67// is ready for interactive use.
68extern OCTINTERP_API bool octave_interpreter_ready;
69
70// TRUE means we've processed all the init code and we are good to go.
71extern OCTINTERP_API std::atomic<bool> octave_initialized;
72
73#include "oct-time.h"
74
76
77class profiler;
78class child_list;
79class push_parser;
80
81// The time we last time we changed directories.
82extern sys::time Vlast_chdir_time;
83
84// The application object contains a pointer to the current
85// interpreter and the interpreter contains a pointer back to the
86// application context so we need a forward declaration for one (or
87// both) of them...
88
89class application;
90
92{
93public:
94
95 temporary_file_list () : m_files () { }
96
97 OCTAVE_DISABLE_COPY_MOVE (temporary_file_list)
98
100
101 void insert (const std::string& file);
102
103 void cleanup ();
104
105private:
106
107 // List of temporary files to delete when we exit.
108 std::set<std::string> m_files;
109
110};
111
112class OCTINTERP_API interpreter
113{
114public:
115
116 // Create an interpreter object and perform basic initialization.
117
118 interpreter (application *app_context = nullptr);
119
120 OCTAVE_DISABLE_COPY_MOVE (interpreter)
121
122 // Clean up the interpreter object.
123
124 ~interpreter ();
125
126 void intern_nargin (octave_idx_type nargs);
127
128 // If creating an embedded interpreter, you may inhibit reading
129 // the command history file by calling initialize_history with
130 // read_history_file = false prior to calling initialize.
131
132 void initialize_history (bool read_history_file = false);
133
134 // If creating an embedded interpreter, you may inhibit setting
135 // the default compiled-in path by calling initialize_load_path
136 // with set_initial_path = false prior calling initialize. After
137 // that, you can add directories to the load path to set up a
138 // custom path.
139
140 void initialize_load_path (bool set_initial_path = true);
141
142 // Load command line history, set the load path.
143
144 void initialize ();
145
146 // Note: GET_LINE_AND_EVAL is only used by new experimental terminal
147 // widget.
148
149 void get_line_and_eval ();
150
151 // Parse a line of input. If input ends at a complete statement
152 // boundary, execute the resulting parse tree. Useful to handle
153 // parsing user input when running in server mode.
154
155 void parse_and_execute (const std::string& input, bool& incomplete_parse);
156
157 // Initialize the interpreter (if not already done by an explicit
158 // call to initialize), execute startup files, --eval option code,
159 // script files, and/or interactive commands.
160
161 int execute ();
162
163 bool server_mode () const { return m_evaluator.server_mode (); }
164
165 bool interactive () const
166 {
167 return m_interactive;
168 }
169
170 void interactive (bool arg)
171 {
172 m_interactive = arg;
173 }
174
175 void read_site_files (bool flag)
176 {
177 m_read_site_files = flag;
178 }
179
180 void read_user_files (bool flag)
181 {
182 m_read_user_files = flag;
183 }
184
185 OCTAVE_DEPRECATED (10, "interpreter::read_init_files is deprecated, use read_user_files")
186 void read_init_files (bool flag)
187 {
188 read_user_files (flag);
189 }
190
191 void init_trace (bool flag)
192 {
193 m_init_trace = flag;
194 }
195
196 OCTAVE_DEPRECATED (10, "interpreter::verbose is deprecated, use init_trace")
197 void verbose (bool flag)
198 {
199 init_trace (flag);
200 }
201
202 void traditional (bool flag)
203 {
204 m_traditional = flag;
205 }
206
207 bool traditional () const
208 {
209 return m_traditional;
210 }
211
212 void inhibit_startup_message (bool flag)
213 {
214 m_inhibit_startup_message = flag;
215 }
216
217 bool in_top_level_repl () const
218 {
219 return m_evaluator.in_top_level_repl ();
220 }
221
222 bool is_initialized () const
223 {
224 return m_initialized;
225 }
226
227 OCTAVE_DEPRECATED (9, "use octave::is_initialized instead")
228 bool initialized () const
229 {
230 return is_initialized ();
231 }
232
234 {
235 m_interrupt_all_in_process_group = b;
236 }
237
239 {
240 return m_interrupt_all_in_process_group;
241 }
242
244 {
245 return m_app_context;
246 }
247
249 {
250 return m_display_info;
251 }
252
254 {
255 return m_environment;
256 }
257
259 {
260 return m_settings;
261 }
262
264 {
265 return m_error_system;
266 }
267
268 tree_evaluator& get_evaluator ();
269
271 {
272 return m_help_system;
273 }
274
276 {
277 return m_input_system;
278 }
279
281 {
282 return m_output_system;
283 }
284
286 {
287 return m_history_system;
288 }
289
291 {
292 return m_dynamic_loader;
293 }
294
296 {
297 return m_load_path;
298 }
299
301 {
302 return m_load_save_system;
303 }
304
306 {
307 return m_type_info;
308 }
309
311 {
312 return m_symbol_table;
313 }
314
315 symbol_scope get_top_scope () const;
316 symbol_scope get_current_scope () const;
317 symbol_scope require_current_scope (const std::string& who) const;
318
319 profiler& get_profiler ();
320
321 stream_list& get_stream_list ();
322
324 {
325 return m_child_list;
326 }
327
328 url_handle_manager& get_url_handle_manager ();
329
331 {
332 return m_cdef_manager;
333 }
334
336 {
337 return m_gtk_manager;
338 }
339
341 {
342 return m_event_manager;
343 }
344
346 {
347 return *m_gh_manager;
348 }
349
350 // Any Octave code that needs to change the current directory should
351 // call this function instead of calling the system chdir function
352 // directly so that the load-path and GUI may be notified of the
353 // change.
354
355 int chdir (const std::string& dir);
356
357 void mlock (bool skip_first = false) const;
358 void munlock (bool skip_first = false) const;
359 bool mislocked (bool skip_first = false) const;
360
361 // NOTE: since we have a version that accepts a bool argument, we
362 // can't rely on automatic conversion from char* to std::string.
363 void munlock (const char *nm);
364 void munlock (const std::string& nm);
365
366 bool mislocked (const char *nm);
367 bool mislocked (const std::string& nm);
368
369 std::string mfilename (const std::string& opt = "") const;
370
371 octave_value_list eval_string (const std::string& eval_str, bool silent,
372 int& parse_status, int nargout);
373
374 octave_value eval_string (const std::string& eval_str, bool silent,
375 int& parse_status);
376
377 octave_value_list eval_string (const octave_value& arg, bool silent,
378 int& parse_status, int nargout);
379
380 octave_value_list eval (const std::string& try_code, int nargout);
381
382 octave_value_list eval (const std::string& try_code,
383 const std::string& catch_code, int nargout);
384
385 octave_value_list evalin (const std::string& context,
386 const std::string& try_code, int nargout);
387
388 octave_value_list evalin (const std::string& context,
389 const std::string& try_code,
390 const std::string& catch_code, int nargout);
391
393 feval (const char *name,
394 const octave_value_list& args = octave_value_list (),
395 int nargout = 0);
396
398 feval (const std::string& name,
399 const octave_value_list& args = octave_value_list (),
400 int nargout = 0);
401
404 const octave_value_list& args = octave_value_list (),
405 int nargout = 0);
406
408 feval (const octave_value& f_arg,
409 const octave_value_list& args = octave_value_list (),
410 int nargout = 0);
411
412 octave_value_list feval (const octave_value_list& args, int nargout = 0);
413
414 std::string inputname (int n, bool ids_only = true) const;
415
416 octave_value make_function_handle (const std::string& name);
417
418 void install_variable (const std::string& name, const octave_value& value,
419 bool global);
420
421 void set_global_value (const std::string& name, const octave_value& value);
422
423 octave_value global_varval (const std::string& name) const;
424
425 void global_assign (const std::string& name,
426 const octave_value& val = octave_value ());
427
428 octave_value top_level_varval (const std::string& name) const;
429
430 void top_level_assign (const std::string& name,
431 const octave_value& val = octave_value ());
432
433 bool is_variable (const std::string& name) const;
434
435 bool is_local_variable (const std::string& name) const;
436
437 octave_value varval (const std::string& name) const;
438
439 void assign (const std::string& name,
440 const octave_value& val = octave_value ());
441
442 void assignin (const std::string& context, const std::string& varname,
443 const octave_value& val = octave_value ());
444
445 void source_file (const std::string& file_name,
446 const std::string& context = "",
447 bool verbose = false, bool require_file = true);
448
449 bool at_top_level () const;
450
451 bool isglobal (const std::string& name) const;
452
453 octave_value find (const std::string& name);
454
455 void clear_all (bool force = false);
456
457 void clear_objects ();
458
459 void clear_variable (const std::string& name);
460
461 void clear_variable_pattern (const std::string& pattern);
462
463 void clear_variable_regexp (const std::string& pattern);
464
465 void clear_variables ();
466
467 void clear_global_variable (const std::string& name);
468
469 void clear_global_variable_pattern (const std::string& pattern);
470
471 void clear_global_variable_regexp (const std::string& pattern);
472
473 void clear_global_variables ();
474
475 void clear_functions (bool force = false);
476
477 void clear_function (const std::string& name);
478
479 void clear_symbol (const std::string& name);
480
481 void clear_function_pattern (const std::string& pat);
482
483 void clear_function_regexp (const std::string& pat);
484
485 void clear_symbol_pattern (const std::string& pat);
486
487 void clear_symbol_regexp (const std::string& pat);
488
489 std::list<std::string> variable_names ();
490
491 std::list<std::string> top_level_variable_names ();
492
493 std::list<std::string> global_variable_names ();
494
495 std::list<std::string> user_function_names ();
496
497 std::list<std::string> autoloaded_functions () const;
498
499 void interrupt ();
500
501 // Pause interpreter execution at the next available statement and
502 // enter the debugger.
503 void pause ();
504
505 // Exit debugger or stop execution and return to the top-level REPL
506 // or server loop.
507 void stop ();
508
509 // Add EXPR to the set of expressions that may be evaluated when the
510 // debugger stops at a breakpoint.
511 void add_debug_watch_expression (const std::string& expr);
512
513 // Remove EXPR from the set of expressions that may be evaluated
514 // when the debugger stops at a breakpoint.
515 void remove_debug_watch_expression (const std::string& expr);
516
517 // Clear the set of expressions that may be evaluated when the
518 // debugger stops at a breakpoint.
519 void clear_debug_watch_expressions ();
520
521 // Return the set of expressions that may be evaluated when the
522 // debugger stops at a breakpoint.
523 std::set<std::string> debug_watch_expressions () const;
524
525 // Resume interpreter execution if paused.
526 void resume ();
527
528 octave_value PS1 (const octave_value_list& args, int nargout);
529 std::string PS1 () const;
530 std::string PS1 (const std::string& s);
531 void set_PS1 (const std::string& s);
532
533 octave_value PS2 (const octave_value_list& args, int nargout);
534 std::string PS2 () const;
535 std::string PS2 (const std::string& s);
536 void set_PS2 (const std::string& s);
537
538 octave_value PS4 (const octave_value_list& args, int nargout);
539 std::string PS4 () const;
540 std::string PS4 (const std::string& s);
541 void set_PS4 (const std::string& s);
542
543 // Provided for convenience. Will be removed once we eliminate the
544 // old terminal widget.
545 bool experimental_terminal_widget () const;
546
547 void handle_exception (const execution_exception& ee);
548
549 void recover_from_exception ();
550
551 void mark_for_deletion (const std::string& file);
552
553 void cleanup_tmp_files ();
554
555 void quit (int exit_status, bool force = false, bool confirm = true);
556
557 void cancel_quit (bool flag) { m_cancel_quit = flag; }
558
560 {
561 return m_executing_finish_script;
562 }
563
564 void add_atexit_fcn (const std::string& fname);
565
566 bool remove_atexit_fcn (const std::string& fname);
567
568 static interpreter * the_interpreter () { return s_instance; }
569
570private:
571
572 void display_startup_message () const;
573
574 int execute_startup_files ();
575
576 int execute_eval_option_code ();
577
578 int execute_command_line_file ();
579
580 int main_loop ();
581
582 int server_loop ();
583
584 void shutdown ();
585
586 void execute_atexit_fcns ();
587
588 void maximum_braindamage ();
589
590 void execute_pkg_add (const std::string& dir);
591
592 int safe_source_file (const std::string& file_name,
593 const std::string& context = "",
594 bool verbose = false, bool require_file = true);
595
596 //--------
597
598 // The interpreter instance; Currently it is only possible to
599 // have one, so OCTAVE_THREAD_LOCAL will normally be defined to be
600 // empty. Eventually we would like to allow multiple interpreters
601 // to be active at once, but they will still be limited to one per
602 // thread. When that is possible, OCTAVE_THREAD_LOCAL can be
603 // replaced by the C++ thread_local keyword. For now, use a macro
604 // to allow experimenting with thread_local storage.
605
606 OCTAVE_THREAD_LOCAL static interpreter *s_instance;
607
608 application *m_app_context;
609
610 temporary_file_list m_tmp_files;
611
612 std::list<std::string> m_atexit_fcns;
613
614 display_info m_display_info;
615
616 environment m_environment;
617
618 settings m_settings;
619
620 tree_evaluator m_evaluator;
621
622 error_system m_error_system;
623
624 help_system m_help_system;
625
626 input_system m_input_system;
627
628 output_system m_output_system;
629
630 history_system m_history_system;
631
632 dynamic_loader m_dynamic_loader;
633
634 load_path m_load_path;
635
636 load_save_system m_load_save_system;
637
638 type_info m_type_info;
639
640 symbol_table m_symbol_table;
641
642 stream_list m_stream_list;
643
644 child_list m_child_list;
645
646 url_handle_manager m_url_handle_manager;
647
648 cdef_manager m_cdef_manager;
649
650 gtk_manager m_gtk_manager;
651
652 event_manager m_event_manager;
653
654 gh_manager *m_gh_manager;
655
656 // TRUE means this is an interactive interpreter (forced or not).
657 bool m_interactive;
658
659 bool m_read_site_files;
660
661 bool m_read_user_files;
662
663 bool m_init_trace;
664
665 bool m_traditional;
666
667 bool m_inhibit_startup_message;
668
669 bool m_load_path_initialized;
670
671 bool m_history_initialized;
672
673 bool m_interrupt_all_in_process_group;
674
675 bool m_cancel_quit;
676
677 bool m_executing_finish_script;
678
679 bool m_executing_atexit;
680
681 bool m_initialized;
682};
683
684OCTAVE_END_NAMESPACE(octave)
685
686#endif
Provides threadsafe access to octave.
void interrupt_all_in_process_group(bool b)
cdef_manager & get_cdef_manager()
error_system & get_error_system()
settings & get_settings()
void read_site_files(bool flag)
bool in_top_level_repl() const
bool traditional() const
display_info & get_display_info()
output_system & get_output_system()
load_path & get_load_path()
application * get_app_context()
bool executing_finish_script() const
gtk_manager & get_gtk_manager()
environment & get_environment()
void cancel_quit(bool flag)
help_system & get_help_system()
history_system & get_history_system()
dynamic_loader & get_dynamic_loader()
void traditional(bool flag)
bool interactive() const
type_info & get_type_info()
void read_user_files(bool flag)
child_list & get_child_list()
void interactive(bool arg)
void inhibit_startup_message(bool flag)
load_save_system & get_load_save_system()
static interpreter * the_interpreter()
bool is_initialized() const
void set_global_value(const std::string &name, const octave_value &value)
gh_manager & get_gh_manager()
bool server_mode() const
bool interrupt_all_in_process_group() const
input_system & get_input_system()
void init_trace(bool flag)
event_manager & get_event_manager()
symbol_table & get_symbol_table()
void insert(const std::string &file)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
bool octave_interpreter_ready
sys::time Vlast_chdir_time
std::atomic< bool > octave_initialized
bool quit_allowed
int chdir(const std::string &path_arg)
Definition lo-sysdep.cc:107
octave_value_list feval(const char *name, const octave_value_list &args, int nargout)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
void source_file(const std::string &file_name, const std::string &context, bool verbose, bool require_file)