GNU Octave 11.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-2026 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 "symtab.h"
61#include "url-handle-manager.h"
62
63extern OCTINTERP_API bool quit_allowed;
64
65// TRUE means we are ready to interpret commands, but not everything
66// is ready for interactive use.
67extern OCTINTERP_API bool octave_interpreter_ready;
68
69// TRUE means we've processed all the init code and we are good to go.
70extern OCTINTERP_API std::atomic<bool> octave_initialized;
71
72#include "oct-time.h"
73
75
76class profiler;
77class child_list;
78class push_parser;
79
80// The time we last time we changed directories.
81extern sys::time Vlast_chdir_time;
82
83// The application object contains a pointer to the current
84// interpreter and the interpreter contains a pointer back to the
85// application context so we need a forward declaration for one (or
86// both) of them...
87
88class application;
89
91{
92public:
93
94 temporary_file_list () : m_files () { }
95
96 OCTAVE_DISABLE_COPY_MOVE (temporary_file_list)
97
99
100 void insert (const std::string& file);
101
102 void cleanup ();
103
104private:
105
106 // List of temporary files to delete when we exit.
107 std::set<std::string> m_files;
108
109};
110
111class OCTINTERP_API interpreter
112{
113public:
114
115 // Create an interpreter object and perform basic initialization.
116
117 interpreter (application *app_context = nullptr);
118
119 OCTAVE_DISABLE_COPY_MOVE (interpreter)
120
121 // Clean up the interpreter object.
122
123 ~interpreter ();
124
125 void intern_nargin (octave_idx_type nargs);
126
127 // If creating an embedded interpreter, you may inhibit reading
128 // the command history file by calling initialize_history with
129 // read_history_file = false prior to calling initialize.
130
131 void initialize_history (bool read_history_file = false);
132
133 // If creating an embedded interpreter, you may inhibit setting
134 // the default compiled-in path by calling initialize_load_path
135 // with set_initial_path = false prior calling initialize. After
136 // that, you can add directories to the load path to set up a
137 // custom path.
138
139 void initialize_load_path (bool set_initial_path = true);
140
141 // Load command line history, set the load path.
142
143 void initialize ();
144
145 // Note: GET_LINE_AND_EVAL is only used by new experimental terminal
146 // widget.
147
148 void get_line_and_eval ();
149
150 // Parse a line of input. If input ends at a complete statement
151 // boundary, execute the resulting parse tree. Useful to handle
152 // parsing user input when running in server mode.
153
154 void parse_and_execute (const std::string& input, bool& incomplete_parse);
155
156 // Initialize the interpreter (if not already done by an explicit
157 // call to initialize), execute startup files, --eval option code,
158 // script files, and/or interactive commands.
159
160 int execute ();
161
162 bool server_mode () const { return m_evaluator.server_mode (); }
163
164 bool interactive () const
165 {
166 return m_interactive;
167 }
168
169 void interactive (bool arg)
170 {
171 m_interactive = arg;
172 }
173
174 void read_site_files (bool flag)
175 {
176 m_read_site_files = flag;
177 }
178
179 void read_user_files (bool flag)
180 {
181 m_read_user_files = flag;
182 }
183
184 OCTAVE_DEPRECATED (10, "interpreter::read_init_files is deprecated, use read_user_files")
185 void read_init_files (bool flag)
186 {
187 read_user_files (flag);
188 }
189
190 void init_trace (bool flag)
191 {
192 m_init_trace = flag;
193 }
194
195 OCTAVE_DEPRECATED (10, "interpreter::verbose is deprecated, use init_trace")
196 void verbose (bool flag)
197 {
198 init_trace (flag);
199 }
200
201 void traditional (bool flag)
202 {
203 m_traditional = flag;
204 }
205
206 bool traditional () const
207 {
208 return m_traditional;
209 }
210
211 void inhibit_startup_message (bool flag)
212 {
213 m_inhibit_startup_message = flag;
214 }
215
216 bool in_top_level_repl () const
217 {
218 return m_evaluator.in_top_level_repl ();
219 }
220
221 bool is_initialized () const
222 {
223 return m_initialized;
224 }
225
227 {
228 m_interrupt_all_in_process_group = b;
229 }
230
232 {
233 return m_interrupt_all_in_process_group;
234 }
235
237 {
238 return m_app_context;
239 }
240
242 {
243 return m_display_info;
244 }
245
247 {
248 return m_environment;
249 }
250
252 {
253 return m_evaluator.get_error_system ();
254 }
255
256 tree_evaluator& get_evaluator ();
257
259 {
260 return m_help_system;
261 }
262
264 {
265 return m_input_system;
266 }
267
269 {
270 return m_output_system;
271 }
272
274 {
275 return m_history_system;
276 }
277
279 {
280 return m_dynamic_loader;
281 }
282
284 {
285 return m_load_path;
286 }
287
289 {
290 return m_load_save_system;
291 }
292
294 {
295 return m_type_info;
296 }
297
299 {
300 return m_symbol_table;
301 }
302
303 symbol_scope get_top_scope () const;
304 symbol_scope get_current_scope () const;
305 symbol_scope require_current_scope (const std::string& who) const;
306
307 profiler& get_profiler ();
308
309 stream_list& get_stream_list ();
310
312 {
313 return m_child_list;
314 }
315
316 url_handle_manager& get_url_handle_manager ();
317
319 {
320 return m_cdef_manager;
321 }
322
324 {
325 return m_gtk_manager;
326 }
327
329 {
330 return m_event_manager;
331 }
332
334 {
335 return *m_gh_manager;
336 }
337
338 // Any Octave code that needs to change the current directory should
339 // call this function instead of calling the system chdir function
340 // directly so that the load-path and GUI may be notified of the
341 // change.
342
343 int chdir (const std::string& dir);
344
345 void mlock (bool skip_first = false) const;
346 void munlock (bool skip_first = false) const;
347 bool mislocked (bool skip_first = false) const;
348
349 // NOTE: since we have a version that accepts a bool argument, we
350 // can't rely on automatic conversion from char* to std::string.
351 void munlock (const char *nm);
352 void munlock (const std::string& nm);
353
354 bool mislocked (const char *nm);
355 bool mislocked (const std::string& nm);
356
357 std::string mfilename (const std::string& opt = "") const;
358
359 octave_value_list eval_string (const std::string& eval_str, bool silent,
360 int& parse_status, int nargout);
361
362 octave_value eval_string (const std::string& eval_str, bool silent,
363 int& parse_status);
364
365 octave_value_list eval_string (const octave_value& arg, bool silent,
366 int& parse_status, int nargout);
367
368 octave_value_list eval (const std::string& try_code, int nargout);
369
370 octave_value_list eval (const std::string& try_code,
371 const std::string& catch_code, int nargout);
372
373 octave_value_list evalin (const std::string& context,
374 const std::string& try_code, int nargout);
375
376 octave_value_list evalin (const std::string& context,
377 const std::string& try_code,
378 const std::string& catch_code, int nargout);
379
381 feval (const char *name,
382 const octave_value_list& args = octave_value_list (),
383 int nargout = 0);
384
386 feval (const std::string& name,
387 const octave_value_list& args = octave_value_list (),
388 int nargout = 0);
389
392 const octave_value_list& args = octave_value_list (),
393 int nargout = 0);
394
396 feval (const octave_value& f_arg,
397 const octave_value_list& args = octave_value_list (),
398 int nargout = 0);
399
400 octave_value_list feval (const octave_value_list& args, int nargout = 0);
401
402 std::string inputname (int n, bool ids_only = true) const;
403
404 octave_value make_function_handle (const std::string& name);
405
406 void install_variable (const std::string& name, const octave_value& value,
407 bool global);
408
409 void set_global_value (const std::string& name, const octave_value& value);
410
411 octave_value global_varval (const std::string& name) const;
412
413 void global_assign (const std::string& name,
414 const octave_value& val = octave_value ());
415
416 octave_value top_level_varval (const std::string& name) const;
417
418 void top_level_assign (const std::string& name,
419 const octave_value& val = octave_value ());
420
421 bool is_variable (const std::string& name) const;
422
423 bool is_local_variable (const std::string& name) const;
424
425 octave_value varval (const std::string& name) const;
426
427 void assign (const std::string& name,
428 const octave_value& val = octave_value ());
429
430 void assignin (const std::string& context, const std::string& varname,
431 const octave_value& val = octave_value ());
432
433 void source_file (const std::string& file_name,
434 const std::string& context = "",
435 bool verbose = false, bool require_file = true);
436
437 bool at_top_level () const;
438
439 bool isglobal (const std::string& name) const;
440
441 octave_value find (const std::string& name);
442
443 void clear_all (bool force = false);
444
445 void clear_objects ();
446
447 void clear_variable (const std::string& name);
448
449 void clear_variable_pattern (const std::string& pattern);
450
451 void clear_variable_regexp (const std::string& pattern);
452
453 void clear_variables ();
454
455 void clear_global_variable (const std::string& name);
456
457 void clear_global_variable_pattern (const std::string& pattern);
458
459 void clear_global_variable_regexp (const std::string& pattern);
460
461 void clear_global_variables ();
462
463 void clear_functions (bool force = false);
464
465 void clear_function (const std::string& name);
466
467 void clear_symbol (const std::string& name);
468
469 void clear_function_pattern (const std::string& pat);
470
471 void clear_function_regexp (const std::string& pat);
472
473 void clear_symbol_pattern (const std::string& pat);
474
475 void clear_symbol_regexp (const std::string& pat);
476
477 std::list<std::string> variable_names ();
478
479 std::list<std::string> top_level_variable_names ();
480
481 std::list<std::string> global_variable_names ();
482
483 std::list<std::string> user_function_names ();
484
485 std::list<std::string> autoloaded_functions () const;
486
487 void interrupt ();
488
489 // Pause interpreter execution at the next available statement and
490 // enter the debugger.
491 void pause ();
492
493 // Exit debugger or stop execution and return to the top-level REPL
494 // or server loop.
495 void stop ();
496
497 // Add EXPR to the set of expressions that may be evaluated when the
498 // debugger stops at a breakpoint.
499 void add_debug_watch_expression (const std::string& expr);
500
501 // Remove EXPR from the set of expressions that may be evaluated
502 // when the debugger stops at a breakpoint.
503 void remove_debug_watch_expression (const std::string& expr);
504
505 // Clear the set of expressions that may be evaluated when the
506 // debugger stops at a breakpoint.
507 void clear_debug_watch_expressions ();
508
509 // Return the set of expressions that may be evaluated when the
510 // debugger stops at a breakpoint.
511 std::set<std::string> debug_watch_expressions () const;
512
513 // Resume interpreter execution if paused.
514 void resume ();
515
516 octave_value PS1 (const octave_value_list& args, int nargout);
517 std::string PS1 () const;
518 std::string PS1 (const std::string& s);
519 void set_PS1 (const std::string& s);
520
521 octave_value PS2 (const octave_value_list& args, int nargout);
522 std::string PS2 () const;
523 std::string PS2 (const std::string& s);
524 void set_PS2 (const std::string& s);
525
526 octave_value PS4 (const octave_value_list& args, int nargout);
527 std::string PS4 () const;
528 std::string PS4 (const std::string& s);
529 void set_PS4 (const std::string& s);
530
531 // Provided for convenience. Will be removed once we eliminate the
532 // old terminal widget.
533 bool experimental_terminal_widget () const;
534
535 void handle_exception (const execution_exception& ee);
536
537 void recover_from_exception ();
538
539 void mark_for_deletion (const std::string& file);
540
541 void cleanup_tmp_files ();
542
543 void quit (int exit_status, bool force = false, bool confirm = true);
544
545 void cancel_quit (bool flag) { m_cancel_quit = flag; }
546
548 {
549 return m_executing_finish_script;
550 }
551
552 void add_atexit_fcn (const std::string& fname);
553
554 bool remove_atexit_fcn (const std::string& fname);
555
556 static interpreter * the_interpreter () { return s_instance; }
557
558private:
559
560 void display_startup_message () const;
561
562 int execute_startup_files ();
563
564 int execute_eval_option_code ();
565
566 int execute_command_line_file ();
567
568 void run_startup_tests ();
569
570 int main_loop ();
571
572 int server_loop ();
573
574 void shutdown ();
575
576 void execute_atexit_fcns ();
577
578 void maximum_braindamage ();
579
580 void execute_pkg_add (const std::string& dir);
581
582 int safe_source_file (const std::string& file_name,
583 const std::string& context = "",
584 bool verbose = false, bool require_file = true);
585
586 //--------
587
588 // The interpreter instance; Currently it is only possible to
589 // have one, so OCTAVE_THREAD_LOCAL will normally be defined to be
590 // empty. Eventually we would like to allow multiple interpreters
591 // to be active at once, but they will still be limited to one per
592 // thread. When that is possible, OCTAVE_THREAD_LOCAL can be
593 // replaced by the C++ thread_local keyword. For now, use a macro
594 // to allow experimenting with thread_local storage.
595
596 OCTAVE_THREAD_LOCAL static interpreter *s_instance;
597
598 application *m_app_context;
599
600 temporary_file_list m_tmp_files;
601
602 std::list<std::string> m_atexit_fcns;
603
604 display_info m_display_info;
605
606 environment m_environment;
607
608 tree_evaluator m_evaluator;
609
610 help_system m_help_system;
611
612 input_system m_input_system;
613
614 output_system m_output_system;
615
616 history_system m_history_system;
617
618 dynamic_loader m_dynamic_loader;
619
620 load_path m_load_path;
621
622 load_save_system m_load_save_system;
623
624 type_info m_type_info;
625
626 symbol_table m_symbol_table;
627
628 stream_list m_stream_list;
629
630 child_list m_child_list;
631
632 url_handle_manager m_url_handle_manager;
633
634 cdef_manager m_cdef_manager;
635
636 gtk_manager m_gtk_manager;
637
638 event_manager m_event_manager;
639
640 gh_manager *m_gh_manager;
641
642 // TRUE means this is an interactive interpreter (forced or not).
643 bool m_interactive;
644
645 bool m_read_site_files;
646
647 bool m_read_user_files;
648
649 bool m_init_trace;
650
651 bool m_traditional;
652
653 bool m_inhibit_startup_message;
654
655 bool m_load_path_initialized;
656
657 bool m_history_initialized;
658
659 bool m_interrupt_all_in_process_group;
660
661 bool m_cancel_quit;
662
663 bool m_executing_finish_script;
664
665 bool m_executing_atexit;
666
667 bool m_initialized;
668};
669
670OCTAVE_END_NAMESPACE(octave)
671
672#endif
Provides threadsafe access to octave.
void interrupt_all_in_process_group(bool b)
cdef_manager & get_cdef_manager()
error_system & get_error_system()
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
OCTAVE_DEPRECATED(11, "") typedef bool(*b_d_Mapper)(double)
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)
int chdir(const std::string &path_arg)