comparison libinterp/parse-tree/pt-eval.cc @ 31607:aac27ad79be6 stable

maint: Re-indent code after switch to using namespace macros. * build-env.h, build-env.in.cc, Cell.h, __betainc__.cc, __eigs__.cc, __ftp__.cc, __ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __magick_read__.cc, __pchip_deriv__.cc, amd.cc, base-text-renderer.cc, base-text-renderer.h, besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.h, call-stack.cc, call-stack.h, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc, dasrt.cc, data.cc, debug.cc, defaults.cc, defaults.h, det.cc, display.cc, display.h, dlmread.cc, dynamic-ld.cc, dynamic-ld.h, ellipj.cc, environment.cc, environment.h, error.cc, error.h, errwarn.h, event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc, fcn-info.h, fft.cc, fft2.cc, file-io.cc, filter.cc, find.cc, ft-text-renderer.cc, ft-text-renderer.h, gcd.cc, gl-render.cc, gl-render.h, gl2ps-print.cc, gl2ps-print.h, graphics-toolkit.cc, graphics-toolkit.h, graphics.cc, gsvd.cc, gtk-manager.cc, gtk-manager.h, help.cc, help.h, hook-fcn.cc, hook-fcn.h, input.cc, input.h, interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h, inv.cc, jsondecode.cc, jsonencode.cc, latex-text-renderer.cc, latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h, lookup.cc, ls-hdf5.cc, ls-mat4.cc, ls-mat5.cc, lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex.cc, mexproto.h, mxarray.h, mxtypes.in.h, oct-errno.in.cc, oct-hdf5-types.cc, oct-hist.cc, oct-hist.h, oct-map.cc, oct-map.h, oct-opengl.h, oct-prcstrm.h, oct-process.cc, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h, oct-strstrm.h, octave-default-image.h, ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc, pow2.cc, pr-output.cc, psi.cc, qr.cc, quadcc.cc, rand.cc, regexp.cc, settings.cc, settings.h, sighandlers.cc, sighandlers.h, sparse-xpow.cc, sqrtm.cc, stack-frame.cc, stack-frame.h, stream-euler.cc, strfns.cc, svd.cc, syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h, symscope.cc, symscope.h, symtab.cc, symtab.h, sysdep.cc, sysdep.h, text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, time.cc, toplev.cc, typecast.cc, url-handle-manager.cc, url-handle-manager.h, urlwrite.cc, utils.cc, utils.h, variables.cc, variables.h, xdiv.cc, __delaunayn__.cc, __init_fltk__.cc, __init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audioread.cc, convhulln.cc, gzip.cc, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc, cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h, cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h, cdef-utils.cc, cdef-utils.h, ov-base-diag.cc, ov-base-int.cc, ov-base-mat.cc, ov-base-mat.h, ov-base-scalar.cc, ov-base.cc, ov-base.h, ov-bool-mat.cc, ov-bool-mat.h, ov-bool-sparse.cc, ov-bool.cc, ov-builtin.h, ov-cell.cc, ov-ch-mat.cc, ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h, ov-complex.cc, ov-cx-diag.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-dld-fcn.cc, ov-dld-fcn.h, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h, ov-float.cc, ov-flt-complex.cc, ov-flt-cx-diag.cc, ov-flt-cx-mat.cc, ov-flt-re-diag.cc, ov-flt-re-mat.cc, ov-flt-re-mat.h, ov-intx.h, ov-java.cc, ov-lazy-idx.cc, ov-legacy-range.cc, ov-magic-int.cc, ov-mex-fcn.cc, ov-mex-fcn.h, ov-null-mat.cc, ov-perm.cc, ov-range.cc, ov-re-diag.cc, ov-re-mat.cc, ov-re-mat.h, ov-re-sparse.cc, ov-scalar.cc, ov-str-mat.cc, ov-struct.cc, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, ovl.h, octave.cc, octave.h, op-b-sbm.cc, op-bm-sbm.cc, op-cs-scm.cc, op-fm-fcm.cc, op-fs-fcm.cc, op-s-scm.cc, op-scm-cs.cc, op-scm-s.cc, op-sm-cs.cc, ops.h, anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h, comment-list.cc, comment-list.h, filepos.h, lex.h, oct-lvalue.cc, oct-lvalue.h, parse.h, profiler.cc, profiler.h, pt-anon-scopes.cc, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h, pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.cc, pt-bp.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-check.cc, pt-check.h, pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc, pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc, pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc, pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc, pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-walk.cc, pt-walk.h, pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h, idx-vector.cc, idx-vector.h, range-fwd.h, CollocWt.cc, CollocWt.h, aepbalance.cc, aepbalance.h, chol.cc, chol.h, gepbalance.cc, gepbalance.h, gsvd.cc, gsvd.h, hess.cc, hess.h, lo-mappers.cc, lo-mappers.h, lo-specfun.cc, lo-specfun.h, lu.cc, lu.h, oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-norm.h, oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc, qrp.h, randgamma.cc, randgamma.h, randmtzig.cc, randmtzig.h, randpoisson.cc, randpoisson.h, schur.cc, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc, sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc, child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc, file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h, mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h, oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc, oct-time.h, oct-uname.cc, oct-uname.h, action-container.cc, action-container.h, base-list.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, f77-fcn.h, file-info.cc, file-info.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc, lo-hash.h, lo-ieee.h, lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h, oct-base64.cc, oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h, oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h, oct-sparse.cc, oct-sparse.h, oct-string.h, octave-preserve-stream-state.h, pathsearch.cc, pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h, url-transfer.cc, url-transfer.h: Re-indent code after switch to using namespace macros.
author Rik <rik@octave.org>
date Thu, 01 Dec 2022 18:02:15 -0800
parents e88a07dec498
children 23664317f0d3 597f3ee61a48
comparison
equal deleted inserted replaced
31605:e88a07dec498 31607:aac27ad79be6
73 #include "utils.h" 73 #include "utils.h"
74 #include "variables.h" 74 #include "variables.h"
75 75
76 OCTAVE_BEGIN_NAMESPACE(octave) 76 OCTAVE_BEGIN_NAMESPACE(octave)
77 77
78 // Normal evaluator. 78 // Normal evaluator.
79 79
80 class quit_debug_exception 80 class quit_debug_exception
81 {
82 public:
83
84 quit_debug_exception (bool all = false) : m_all (all) { }
85
86 quit_debug_exception (const quit_debug_exception&) = default;
87
88 quit_debug_exception& operator = (const quit_debug_exception&) = default;
89
90 ~quit_debug_exception (void) = default;
91
92 bool all (void) const { return m_all; }
93
94 private:
95
96 bool m_all;
97 };
98
99 class debugger
100 {
101 public:
102
103 enum execution_mode
81 { 104 {
82 public: 105 EX_NORMAL = 0,
83 106 EX_CONTINUE = 1,
84 quit_debug_exception (bool all = false) : m_all (all) { } 107 EX_QUIT = 2,
85 108 EX_QUIT_ALL = 3
86 quit_debug_exception (const quit_debug_exception&) = default;
87
88 quit_debug_exception& operator = (const quit_debug_exception&) = default;
89
90 ~quit_debug_exception (void) = default;
91
92 bool all (void) const { return m_all; }
93
94 private:
95
96 bool m_all;
97 }; 109 };
98 110
99 class debugger 111 debugger (interpreter& interp, std::size_t level)
112 : m_interpreter (interp), m_level (level),
113 m_execution_mode (EX_NORMAL), m_in_debug_repl (false)
114 { }
115
116 int server_loop (void);
117
118 void repl (const std::string& prompt = "debug> ");
119
120 bool in_debug_repl (void) const { return m_in_debug_repl; }
121
122 void dbcont (void) { m_execution_mode = EX_CONTINUE; }
123
124 void dbquit (bool all = false)
100 { 125 {
101 public: 126 if (all)
102 127 m_execution_mode = EX_QUIT_ALL;
103 enum execution_mode 128 else
129 m_execution_mode = EX_QUIT;
130 }
131
132 bool quitting_debugger (void) const;
133
134 private:
135
136 interpreter& m_interpreter;
137
138 std::size_t m_level;
139 execution_mode m_execution_mode;
140 bool m_in_debug_repl;
141 };
142
143 // FIXME: Could the debugger server_loop and repl functions be merged
144 // with the corresponding tree_evaluator functions or do they need to
145 // remain separate? They perform nearly the same functions.
146
147 int debugger::server_loop (void)
148 {
149 // Process events from the event queue.
150
151 tree_evaluator& tw = m_interpreter.get_evaluator ();
152
153 void (tree_evaluator::*server_mode_fptr) (bool)
154 = &tree_evaluator::server_mode;
155 unwind_action act (server_mode_fptr, &tw, true);
156
157 int exit_status = 0;
158
159 do
160 {
161 if (m_execution_mode == EX_CONTINUE || tw.dbstep_flag ())
162 break;
163
164 if (quitting_debugger ())
165 break;
166
167 try
168 {
169 // FIXME: Should we call octave_quit in the octave::sleep
170 // and/or command_editor::run_event_hooks functions?
171
172 octave_quit ();
173
174 // FIXME: Running the event queue should be decoupled from
175 // the command_editor.
176
177 // FIXME: Is it OK to use command_editor::run_event_hooks
178 // here? It may run more than one queued function per call,
179 // and it seems that the checks at the top of the loop
180 // probably need to be done after each individual event
181 // function is executed. For now, maybe the simplest thing
182 // would be to pass a predicate function (lambda expression)
183 // to the command_editor::run_event_hooks and have it check
184 // that and break out of the eval loop(s) if the condition
185 // is met?
186
187 // FIXME: We should also use a condition variable to manage
188 // the execution of entries in the queue and eliminate the
189 // need for the busy-wait loop.
190
191 command_editor::run_event_hooks ();
192
193 release_unreferenced_dynamic_libraries ();
194
195 sleep (0.1);
196 }
197 catch (const interrupt_exception&)
198 {
199 octave_interrupt_state = 1;
200 m_interpreter.recover_from_exception ();
201
202 // Required newline when the user does Ctrl+C at the prompt.
203 if (m_interpreter.interactive ())
204 octave_stdout << "\n";
205 }
206 catch (const index_exception& e)
207 {
208 m_interpreter.recover_from_exception ();
209
210 std::cerr << "error: unhandled index exception: "
211 << e.message () << " -- trying to return to prompt"
212 << std::endl;
213 }
214 catch (const execution_exception& ee)
215 {
216 error_system& es = m_interpreter.get_error_system ();
217
218 es.save_exception (ee);
219 es.display_exception (ee);
220
221 if (m_interpreter.interactive ())
222 {
223 m_interpreter.recover_from_exception ();
224 }
225 else
226 {
227 // We should exit with a nonzero status.
228 exit_status = 1;
229 break;
230 }
231 }
232 catch (const quit_debug_exception& qde)
233 {
234 if (qde.all ())
235 throw;
236
237 // Continue in this debug level.
238 }
239 catch (const std::bad_alloc&)
240 {
241 m_interpreter.recover_from_exception ();
242
243 std::cerr << "error: out of memory -- trying to return to prompt"
244 << std::endl;
245 }
246 }
247 while (exit_status == 0);
248
249 if (exit_status == EOF)
250 {
251 if (m_interpreter.interactive ())
252 octave_stdout << "\n";
253
254 exit_status = 0;
255 }
256
257 return exit_status;
258 }
259
260 void debugger::repl (const std::string& prompt_arg)
261 {
262 unwind_protect frame;
263
264 frame.protect_var (m_in_debug_repl);
265 frame.protect_var (m_execution_mode);
266
267 m_in_debug_repl = true;
268
269 tree_evaluator& tw = m_interpreter.get_evaluator ();
270
271 bool silent = tw.quiet_breakpoint_flag (false);
272
273 frame.add (&tree_evaluator::restore_frame, &tw,
274 tw.current_call_stack_frame_number ());
275
276 tw.goto_frame (tw.debug_frame ());
277
278 octave_user_code *caller = tw.current_user_code ();
279 std::string fcn_file_nm, fcn_nm;
280
281 if (caller)
282 {
283 fcn_file_nm = caller->fcn_file_name ();
284 fcn_nm = fcn_file_nm.empty () ? caller->name () : fcn_file_nm;
285 }
286
287 int curr_debug_line = tw.current_line ();
288
289 std::ostringstream buf;
290
291 input_system& input_sys = m_interpreter.get_input_system ();
292
293 event_manager& evmgr = m_interpreter.get_event_manager ();
294
295 if (! fcn_nm.empty ())
296 {
297 if (input_sys.gud_mode ())
298 {
299 static char ctrl_z = 'Z' & 0x1f;
300
301 buf << ctrl_z << ctrl_z << fcn_nm << ':' << curr_debug_line;
302 }
303 else
304 {
305 // FIXME: we should come up with a clean way to detect
306 // that we are stopped on the no-op command that marks the
307 // end of a function or script.
308
309 if (! silent)
310 {
311 std::shared_ptr<stack_frame> frm = tw.current_user_frame ();
312
313 frm->display_stopped_in_message (buf);
314 }
315
316 evmgr.enter_debugger_event (fcn_nm, fcn_file_nm, curr_debug_line);
317
318 evmgr.set_workspace ();
319
320 frame.add (&event_manager::execute_in_debugger_event, &evmgr,
321 fcn_nm, curr_debug_line);
322
323 if (! silent)
324 {
325 std::string line_buf;
326
327 if (caller)
328 line_buf = caller->get_code_line (curr_debug_line);
329
330 if (! line_buf.empty ())
331 buf << curr_debug_line << ": " << line_buf;
332 }
333 }
334 }
335
336 if (silent)
337 command_editor::erase_empty_line (true);
338
339 std::string stopped_in_msg = buf.str ();
340
341 if (m_interpreter.server_mode ())
342 {
343 if (! stopped_in_msg.empty ())
344 octave_stdout << stopped_in_msg << std::endl;
345
346 evmgr.push_event_queue ();
347
348 frame.add (&event_manager::pop_event_queue, &evmgr);
349
350 frame.add (&tree_evaluator::set_parser, &tw, tw.get_parser ());
351
352 std::shared_ptr<push_parser>
353 debug_parser (new push_parser (m_interpreter));
354
355 tw.set_parser (debug_parser);
356
357 server_loop ();
358 }
359 else
360 {
361 if (! stopped_in_msg.empty ())
362 std::cerr << stopped_in_msg << std::endl;
363
364 std::string tmp_prompt = prompt_arg;
365 if (m_level > 0)
366 tmp_prompt = "[" + std::to_string (m_level) + "]" + prompt_arg;
367
368 frame.add (&input_system::set_PS1, &input_sys, input_sys.PS1 ());
369 input_sys.PS1 (tmp_prompt);
370
371 if (! m_interpreter.interactive ())
372 {
373 void (interpreter::*interactive_fptr) (bool)
374 = &interpreter::interactive;
375 frame.add (interactive_fptr, &m_interpreter,
376 m_interpreter.interactive ());
377
378 m_interpreter.interactive (true);
379
380 // FIXME: should debugging be possible in an embedded
381 // interpreter?
382
383 application *app = application::app ();
384
385 if (app)
386 {
387 void (application::*forced_interactive_fptr) (bool)
388 = &application::forced_interactive;
389 frame.add (forced_interactive_fptr, app,
390 app->forced_interactive ());
391
392 app->forced_interactive (true);
393 }
394 }
395
396 #if defined (OCTAVE_ENABLE_COMMAND_LINE_PUSH_PARSER)
397
398 input_reader reader (m_interpreter);
399
400 push_parser debug_parser (m_interpreter);
401
402 #else
403
404 parser debug_parser (m_interpreter);
405
406 #endif
407
408 error_system& es = m_interpreter.get_error_system ();
409
410 while (m_in_debug_repl)
411 {
412 if (m_execution_mode == EX_CONTINUE || tw.dbstep_flag ())
413 break;
414
415 if (quitting_debugger ())
416 break;
417
418 try
419 {
420 debug_parser.reset ();
421
422 #if defined (OCTAVE_ENABLE_COMMAND_LINE_PUSH_PARSER)
423
424 int retval = 0;
425
426 std::string prompt
427 = command_editor::decode_prompt_string (tmp_prompt);
428
429 do
430 {
431 bool eof = false;
432 std::string input_line = reader.get_input (prompt, eof);
433
434 if (eof)
435 {
436 retval = EOF;
437 break;
438 }
439
440 retval = debug_parser.run (input_line, false);
441
442 prompt = command_editor::decode_prompt_string (input_sys.PS2 ());
443 }
444 while (retval < 0);
445
446 #else
447
448 int retval = debug_parser.run ();
449
450 #endif
451 if (command_editor::interrupt (false))
452 {
453 // Break regardless of m_execution_mode value.
454
455 quitting_debugger ();
456
457 break;
458 }
459 else
460 {
461 if (retval == 0)
462 {
463 std::shared_ptr<tree_statement_list> stmt_list
464 = debug_parser.statement_list ();
465
466 if (stmt_list)
467 stmt_list->accept (tw);
468
469 if (octave_completion_matches_called)
470 octave_completion_matches_called = false;
471
472 // FIXME: the following statement is here because
473 // the last command may have been a dbup, dbdown, or
474 // dbstep command that changed the current debug
475 // frame. If so, we need to reset the current frame
476 // for the call stack. But is this right way to do
477 // this job? What if the statement list was
478 // something like "dbup; dbstack"? Will the call to
479 // dbstack use the right frame? If not, how can we
480 // fix this problem?
481 tw.goto_frame (tw.debug_frame ());
482 }
483
484 octave_quit ();
485 }
486 }
487 catch (const execution_exception& ee)
488 {
489 es.save_exception (ee);
490 es.display_exception (ee);
491
492 // Ignore errors when in debugging mode;
493 m_interpreter.recover_from_exception ();
494 }
495 catch (const quit_debug_exception& qde)
496 {
497 if (qde.all ())
498 throw;
499
500 // Continue in this debug level.
501 }
502 }
503 }
504 }
505
506 bool debugger::quitting_debugger (void) const
507 {
508 if (m_execution_mode == EX_QUIT)
509 {
510 // If there is no enclosing debug level or the top-level
511 // repl is not active, handle dbquit the same as dbcont.
512
513 if (m_level > 0 || m_interpreter.server_mode ()
514 || m_interpreter.in_top_level_repl ())
515 throw quit_debug_exception ();
516 else
517 return true;
518 }
519
520 if (m_execution_mode == EX_QUIT_ALL)
521 {
522 // If the top-level repl is not active, handle "dbquit all"
523 // the same as dbcont.
524
525 if (m_interpreter.server_mode () || m_interpreter.in_top_level_repl ())
526 throw quit_debug_exception (true);
527 else
528 return true;
529 }
530
531 return false;
532 }
533
534 bool tree_evaluator::at_top_level (void) const
535 {
536 return m_call_stack.at_top_level ();
537 }
538
539 std::string
540 tree_evaluator::mfilename (const std::string& opt) const
541 {
542 std::string fname;
543
544 octave_user_code *fcn = m_call_stack.current_user_code ();
545
546 if (fcn)
547 {
548 fname = fcn->fcn_file_name ();
549
550 if (fname.empty ())
551 fname = fcn->name ();
552 }
553
554 if (opt == "fullpathext")
555 return fname;
556
557 std::size_t dpos = fname.rfind (sys::file_ops::dir_sep_char ());
558 std::size_t epos = fname.rfind ('.');
559
560 if (epos <= dpos+1)
561 epos = std::string::npos;
562
563 if (epos != std::string::npos)
564 fname = fname.substr (0, epos);
565
566 if (opt == "fullpath")
567 return fname;
568
569 if (dpos != std::string::npos)
570 fname = fname.substr (dpos+1);
571
572 return fname;
573 }
574
575 void tree_evaluator::parse_and_execute (const std::string& input,
576 bool& incomplete_parse)
577 {
578 incomplete_parse = false;
579
580 unwind_protect_var<bool> upv (m_in_top_level_repl, true);
581
582 if (at_top_level ())
583 {
584 dbstep_flag (0);
585 reset_debug_state ();
586 }
587
588 // FIXME: OK to do this job here, or should it be in the functions
589 // that do the actual prompting?
590
591 // Update the time stamp for the "prompt" so that automatically
592 // finding modified files based on file modification times will
593 // work. In the future, we may do something completely different to
594 // check for changes to files but for now, we rely on the prompt
595 // time stamp to limit the checks for file modification times.
596
597 Vlast_prompt_time.stamp ();
598
599 bool eof = false;
600
601 event_manager& evmgr = m_interpreter.get_event_manager ();
602
603 if (command_history::add (input))
604 evmgr.append_history (input);
605
606 m_exit_status = m_parser->run (input, eof);
607
608 if (m_exit_status == 0)
609 {
610 std::shared_ptr<tree_statement_list>
611 stmt_list = m_parser->statement_list ();
612
613 if (stmt_list)
614 {
615 command_editor::increment_current_command_number ();
616
617 eval (stmt_list, m_interpreter.interactive ());
618
619 evmgr.set_workspace ();
620 }
621 else if (m_parser->at_end_of_input ())
622 m_exit_status = EOF;
623 }
624 else
625 incomplete_parse = true;
626
627 // FIXME: Should we be checking m_exit_status or incomplete_parse or
628 // both here? Could EOF have a value other than -1, and is there
629 // possible confusion between that state and the parser returning -1?
630
631 if (m_exit_status == -1)
632 m_exit_status = 0;
633 else
634 m_parser->reset ();
635
636 evmgr.pre_input_event ();
637 }
638
639 void tree_evaluator::get_line_and_eval (void)
640 {
641 std::mutex mtx;
642 std::unique_lock<std::mutex> lock (mtx);
643 std::condition_variable cv;
644 bool incomplete_parse = false;
645 bool evaluation_pending = false;
646 bool exiting = false;
647
648 input_system& input_sys = m_interpreter.get_input_system ();
649 event_manager& evmgr = m_interpreter.get_event_manager ();
650
651 while (true)
652 {
653 // FIXME: Detect EOF? Use readline? If
654 // so, then we need to disable idle event loop hook function
655 // execution.
656
657 std::string ps = incomplete_parse ? input_sys.PS2 () : input_sys.PS1 ();
658
659 std::cout << command_editor::decode_prompt_string (ps);
660
661 std::string input;
662 std::getline (std::cin, input);
663
664 if (input.empty ())
665 continue;
666
667 incomplete_parse = false;
668 evaluation_pending = true;
669 exiting = false;
670
671 evmgr.post_event
672 ([&] (interpreter& interp)
104 { 673 {
105 EX_NORMAL = 0, 674 // INTERPRETER THREAD
106 EX_CONTINUE = 1, 675
107 EX_QUIT = 2, 676 std::lock_guard<std::mutex> local_lock (mtx);
108 EX_QUIT_ALL = 3
109 };
110
111 debugger (interpreter& interp, std::size_t level)
112 : m_interpreter (interp), m_level (level),
113 m_execution_mode (EX_NORMAL), m_in_debug_repl (false)
114 { }
115
116 int server_loop (void);
117
118 void repl (const std::string& prompt = "debug> ");
119
120 bool in_debug_repl (void) const { return m_in_debug_repl; }
121
122 void dbcont (void) { m_execution_mode = EX_CONTINUE; }
123
124 void dbquit (bool all = false)
125 {
126 if (all)
127 m_execution_mode = EX_QUIT_ALL;
128 else
129 m_execution_mode = EX_QUIT;
130 }
131
132 bool quitting_debugger (void) const;
133
134 private:
135
136 interpreter& m_interpreter;
137
138 std::size_t m_level;
139 execution_mode m_execution_mode;
140 bool m_in_debug_repl;
141 };
142
143 // FIXME: Could the debugger server_loop and repl functions be merged
144 // with the corresponding tree_evaluator functions or do they need to
145 // remain separate? They perform nearly the same functions.
146
147 int debugger::server_loop (void)
148 {
149 // Process events from the event queue.
150
151 tree_evaluator& tw = m_interpreter.get_evaluator ();
152
153 void (tree_evaluator::*server_mode_fptr) (bool)
154 = &tree_evaluator::server_mode;
155 unwind_action act (server_mode_fptr, &tw, true);
156
157 int exit_status = 0;
158
159 do
160 {
161 if (m_execution_mode == EX_CONTINUE || tw.dbstep_flag ())
162 break;
163
164 if (quitting_debugger ())
165 break;
166 677
167 try 678 try
168 { 679 {
169 // FIXME: Should we call octave_quit in the octave::sleep 680 interp.parse_and_execute (input, incomplete_parse);
170 // and/or command_editor::run_event_hooks functions?
171
172 octave_quit ();
173
174 // FIXME: Running the event queue should be decoupled from
175 // the command_editor.
176
177 // FIXME: Is it OK to use command_editor::run_event_hooks
178 // here? It may run more than one queued function per call,
179 // and it seems that the checks at the top of the loop
180 // probably need to be done after each individual event
181 // function is executed. For now, maybe the simplest thing
182 // would be to pass a predicate function (lambda expression)
183 // to the command_editor::run_event_hooks and have it check
184 // that and break out of the eval loop(s) if the condition
185 // is met?
186
187 // FIXME: We should also use a condition variable to manage
188 // the execution of entries in the queue and eliminate the
189 // need for the busy-wait loop.
190
191 command_editor::run_event_hooks ();
192
193 release_unreferenced_dynamic_libraries ();
194
195 sleep (0.1);
196 } 681 }
197 catch (const interrupt_exception&) 682 catch (const exit_exception&)
198 { 683 {
199 octave_interrupt_state = 1; 684 evaluation_pending = false;
200 m_interpreter.recover_from_exception (); 685 exiting = true;
201 686 cv.notify_all ();
202 // Required newline when the user does Ctrl+C at the prompt. 687 throw;
203 if (m_interpreter.interactive ())
204 octave_stdout << "\n";
205 }
206 catch (const index_exception& e)
207 {
208 m_interpreter.recover_from_exception ();
209
210 std::cerr << "error: unhandled index exception: "
211 << e.message () << " -- trying to return to prompt"
212 << std::endl;
213 } 688 }
214 catch (const execution_exception& ee) 689 catch (const execution_exception& ee)
215 { 690 {
216 error_system& es = m_interpreter.get_error_system (); 691 error_system& es = m_interpreter.get_error_system ();
217 692
219 es.display_exception (ee); 694 es.display_exception (ee);
220 695
221 if (m_interpreter.interactive ()) 696 if (m_interpreter.interactive ())
222 { 697 {
223 m_interpreter.recover_from_exception (); 698 m_interpreter.recover_from_exception ();
699 m_parser->reset ();
700 evaluation_pending = false;
701 cv.notify_all ();
224 } 702 }
225 else 703 else
226 { 704 {
227 // We should exit with a nonzero status. 705 evaluation_pending = false;
228 exit_status = 1; 706 cv.notify_all ();
229 break; 707 throw exit_exception (1);
230 } 708 }
231 } 709 }
232 catch (const quit_debug_exception& qde) 710 catch (...)
233 { 711 {
234 if (qde.all ()) 712 evaluation_pending = false;
235 throw; 713 cv.notify_all ();
236 714 throw;
237 // Continue in this debug level.
238 } 715 }
239 catch (const std::bad_alloc&) 716
240 { 717 evaluation_pending = false;
718 cv.notify_all ();
719 });
720
721 // Wait until evaluation is finished before prompting for input
722 // again.
723
724 cv.wait (lock, [&] { return ! evaluation_pending; });
725
726 if (exiting)
727 break;
728 }
729 }
730
731 int tree_evaluator::repl (void)
732 {
733 // The big loop. Read, Eval, Print, Loop. Normally user
734 // interaction at the command line in a terminal session, but we may
735 // also end up here when reading from a pipe or when stdin is
736 // connected to a file by the magic of input redirection.
737
738 int exit_status = 0;
739
740 // FIXME: should this choice be a command-line option? Note that we
741 // intend that the push parser interface only be used for
742 // interactive sessions.
743
744 #if defined (OCTAVE_ENABLE_COMMAND_LINE_PUSH_PARSER)
745 static bool use_command_line_push_parser = true;
746 #else
747 static bool use_command_line_push_parser = false;
748 #endif
749
750 // The following logic is written as it is to allow easy transition
751 // to setting USE_COMMAND_LINE_PUSH_PARSER at run time and to
752 // simplify the logic of the main loop below by using the same
753 // base_parser::run interface for both push and pull parsers.
754
755 std::shared_ptr<base_parser> repl_parser;
756
757 if (m_interpreter.interactive ())
758 {
759 if (use_command_line_push_parser)
760 {
761 push_parser *pp
762 = new push_parser (m_interpreter,
763 new input_reader (m_interpreter));
764
765 repl_parser = std::shared_ptr<base_parser> (pp);
766 }
767 else
768 {
769 parser *pp = new parser (new lexer (m_interpreter));
770 repl_parser = std::shared_ptr<base_parser> (pp);
771 }
772 }
773 else
774 {
775 parser *pp = new parser (new lexer (stdin, m_interpreter));
776 repl_parser = std::shared_ptr<base_parser> (pp);
777 }
778
779 do
780 {
781 try
782 {
783 unwind_protect_var<bool> upv (m_in_top_level_repl, true);
784
785 repl_parser->reset ();
786
787 if (at_top_level ())
788 {
789 dbstep_flag (0);
790 reset_debug_state ();
791 }
792
793 exit_status = repl_parser->run ();
794
795 if (exit_status == 0)
796 {
797 std::shared_ptr<tree_statement_list>
798 stmt_list = repl_parser->statement_list ();
799
800 if (stmt_list)
801 {
802 command_editor::increment_current_command_number ();
803
804 eval (stmt_list, m_interpreter.interactive ());
805 }
806 else if (repl_parser->at_end_of_input ())
807 {
808 exit_status = EOF;
809 break;
810 }
811 }
812 }
813 catch (const interrupt_exception&)
814 {
815 m_interpreter.recover_from_exception ();
816
817 // Required newline when the user does Ctrl+C at the prompt.
818 if (m_interpreter.interactive ())
819 octave_stdout << "\n";
820 }
821 catch (const index_exception& ie)
822 {
823 m_interpreter.recover_from_exception ();
824
825 std::cerr << "error: unhandled index exception: "
826 << ie.message () << " -- trying to return to prompt"
827 << std::endl;
828 }
829 catch (const execution_exception& ee)
830 {
831 error_system& es = m_interpreter.get_error_system ();
832
833 es.save_exception (ee);
834 es.display_exception (ee);
835
836 if (m_interpreter.interactive ())
241 m_interpreter.recover_from_exception (); 837 m_interpreter.recover_from_exception ();
242 838 else
243 std::cerr << "error: out of memory -- trying to return to prompt" 839 {
244 << std::endl; 840 // We should exit with a nonzero status.
245 } 841 exit_status = 1;
246 } 842 break;
247 while (exit_status == 0); 843 }
248 844 }
249 if (exit_status == EOF) 845 catch (const quit_debug_exception&)
250 { 846 {
251 if (m_interpreter.interactive ()) 847 m_interpreter.recover_from_exception ();
252 octave_stdout << "\n"; 848
253 849 // FIXME: Does anything else need to happen here?
254 exit_status = 0; 850 }
255 } 851 catch (const std::bad_alloc&)
256 852 {
257 return exit_status; 853 m_interpreter.recover_from_exception ();
258 } 854
259 855 std::cerr << "error: out of memory -- trying to return to prompt"
260 void debugger::repl (const std::string& prompt_arg) 856 << std::endl;
857 }
858 }
859 while (exit_status == 0);
860
861 if (exit_status == EOF)
862 {
863 if (m_interpreter.interactive ())
864 octave_stdout << "\n";
865
866 exit_status = 0;
867 }
868
869 return exit_status;
870 }
871
872 int tree_evaluator::server_loop (void)
873 {
874 // Process events from the event queue.
875
876 unwind_protect_var<bool> upv1 (m_server_mode, true);
877
878 m_exit_status = 0;
879
880 std::shared_ptr<push_parser> parser (new push_parser (m_interpreter));
881 unwind_protect_var<std::shared_ptr<push_parser>> upv2 (m_parser, parser);
882
883 // FIXME: We are currently resetting the parser after every call to
884 // recover_from_exception. This action should probably be handled
885 // in a more consistent way, but resetting the parser in every call
886 // to interpreter::recover_from_exception appears to cause
887 // segfaults in the test suite.
888
889 do
890 {
891 try
892 {
893 // FIXME: Should we call octave_quit in the octave::sleep
894 // and/or command_editor::run_event_hooks functions?
895
896 octave_quit ();
897
898 // FIXME: Running the event queue should be decoupled from
899 // the command_editor. We should also use a condition
900 // variable to manage the execution of entries in the queue
901 // and eliminate the need for the busy-wait loop.
902
903 command_editor::run_event_hooks ();
904
905 release_unreferenced_dynamic_libraries ();
906
907 sleep (0.1);
908 }
909 catch (const interrupt_exception&)
910 {
911 octave_interrupt_state = 1;
912 m_interpreter.recover_from_exception ();
913 m_parser->reset ();
914
915 // Required newline when the user does Ctrl+C at the prompt.
916 if (m_interpreter.interactive ())
917 octave_stdout << "\n";
918 }
919 catch (const index_exception& e)
920 {
921 m_interpreter.recover_from_exception ();
922 m_parser->reset ();
923
924 std::cerr << "error: unhandled index exception: "
925 << e.message () << " -- trying to return to prompt"
926 << std::endl;
927 }
928 catch (const execution_exception& ee)
929 {
930 error_system& es = m_interpreter.get_error_system ();
931
932 es.save_exception (ee);
933 es.display_exception (ee);
934
935 if (m_interpreter.interactive ())
936 {
937 m_interpreter.recover_from_exception ();
938 m_parser->reset ();
939 }
940 else
941 {
942 // We should exit with a nonzero status.
943 m_exit_status = 1;
944 break;
945 }
946 }
947 catch (const quit_debug_exception&)
948 {
949 octave_interrupt_state = 1;
950 m_interpreter.recover_from_exception ();
951 m_parser->reset ();
952 }
953 catch (const exit_exception& xe)
954 {
955 m_exit_status = xe.exit_status ();
956 break;
957 }
958 catch (const std::bad_alloc&)
959 {
960 m_interpreter.recover_from_exception ();
961 m_parser->reset ();
962
963 std::cerr << "error: out of memory -- trying to return to prompt"
964 << std::endl;
965 }
966 }
967 while (m_exit_status == 0);
968
969 if (m_exit_status == EOF)
970 {
971 if (m_interpreter.interactive ())
972 octave_stdout << "\n";
973
974 m_exit_status = 0;
975 }
976
977 return m_exit_status;
978 }
979
980 void tree_evaluator::eval (std::shared_ptr<tree_statement_list>& stmt_list,
981 bool interactive)
982 {
983 try
984 {
985 stmt_list->accept (*this);
986
987 octave_quit ();
988
989 if (! interactive)
990 {
991 bool quit = (m_returning || m_breaking);
992
993 if (m_returning)
994 m_returning = 0;
995
996 if (m_breaking)
997 m_breaking--;
998
999 if (quit)
1000 return;
1001 }
1002
1003 if (octave_completion_matches_called)
1004 octave_completion_matches_called = false;
1005 }
1006 catch (const quit_debug_exception&)
1007 {
1008 m_interpreter.recover_from_exception ();
1009 }
1010 }
1011
1012 octave_value_list
1013 tree_evaluator::eval_string (const std::string& eval_str, bool silent,
1014 int& parse_status, int nargout)
1015 {
1016 octave_value_list retval;
1017
1018 parser eval_parser (eval_str, m_interpreter);
1019
1020 do
1021 {
1022 eval_parser.reset ();
1023
1024 // If we are looking at
1025 //
1026 // val = eval ("code");
1027 //
1028 // then don't allow code to be parsed as a command.
1029
1030 if (nargout > 0)
1031 eval_parser.disallow_command_syntax ();
1032
1033 parse_status = eval_parser.run ();
1034
1035 if (parse_status == 0)
1036 {
1037 std::shared_ptr<tree_statement_list> stmt_list
1038 = eval_parser.statement_list ();
1039
1040 if (stmt_list)
1041 {
1042 tree_statement *stmt = nullptr;
1043
1044 if (stmt_list->length () == 1
1045 && (stmt = stmt_list->front ())
1046 && stmt->is_expression ())
1047 {
1048 tree_expression *expr = stmt->expression ();
1049
1050 if (silent)
1051 expr->set_print_flag (false);
1052
1053 retval = expr->evaluate_n (*this, nargout);
1054
1055 bool do_bind_ans = false;
1056
1057 if (expr->is_identifier ())
1058 do_bind_ans = ! is_variable (expr);
1059 else
1060 do_bind_ans = ! expr->is_assignment_expression ();
1061
1062 if (do_bind_ans && ! retval.empty ())
1063 bind_ans (retval(0), expr->print_result ());
1064
1065 if (nargout == 0)
1066 retval = octave_value_list ();
1067 }
1068 else if (nargout == 0)
1069 stmt_list->accept (*this);
1070 else
1071 error ("eval: invalid use of statement list");
1072
1073 if (returning () || breaking () || continuing ())
1074 break;
1075 }
1076 else if (eval_parser.at_end_of_input ())
1077 break;
1078 }
1079 }
1080 while (parse_status == 0);
1081
1082 return retval;
1083 }
1084
1085 octave_value tree_evaluator::eval_string (const std::string& eval_str,
1086 bool silent, int& parse_status)
1087 {
1088 octave_value retval;
1089
1090 octave_value_list tmp = eval_string (eval_str, silent, parse_status, 1);
1091
1092 if (! tmp.empty ())
1093 retval = tmp(0);
1094
1095 return retval;
1096 }
1097
1098 octave_value_list tree_evaluator::eval_string (const octave_value& arg,
1099 bool silent, int& parse_status,
1100 int nargout)
1101 {
1102 std::string s = arg.xstring_value ("eval: expecting string argument");
1103
1104 return eval_string (s, silent, parse_status, nargout);
1105 }
1106
1107 octave_value_list tree_evaluator::eval (const std::string& try_code,
1108 int nargout)
1109 {
1110 int parse_status = 0;
1111
1112 return eval_string (try_code, nargout > 0, parse_status, nargout);
1113 }
1114
1115 octave_value_list tree_evaluator::eval (const std::string& try_code,
1116 const std::string& catch_code,
1117 int nargout)
1118 {
1119 octave_value_list retval;
1120
1121 error_system& es = m_interpreter.get_error_system ();
1122
1123 int parse_status = 0;
1124
1125 bool execution_error = false;
1126
1127 octave_value_list tmp;
1128
1129 try
1130 {
1131 tmp = eval_string (try_code, nargout > 0, parse_status, nargout);
1132 }
1133 catch (const execution_exception& ee)
1134 {
1135 es.save_exception (ee);
1136 m_interpreter.recover_from_exception ();
1137
1138 execution_error = true;
1139 }
1140
1141 if (parse_status != 0 || execution_error)
1142 {
1143 tmp = eval_string (catch_code, nargout > 0, parse_status, nargout);
1144
1145 retval = (nargout > 0) ? tmp : octave_value_list ();
1146 }
1147 else
1148 {
1149 if (nargout > 0)
1150 retval = tmp;
1151
1152 // FIXME: we should really be rethrowing whatever
1153 // exception occurred, not just throwing an
1154 // execution exception.
1155 if (execution_error)
1156 throw execution_exception ();
1157 }
1158
1159 return retval;
1160 }
1161
1162 octave_value_list tree_evaluator::evalin (const std::string& context,
1163 const std::string& try_code,
1164 int nargout)
1165 {
1166 unwind_action act ([=] (std::size_t frm)
261 { 1167 {
262 unwind_protect frame; 1168 m_call_stack.restore_frame (frm);
263 1169 }, m_call_stack.current_frame ());
264 frame.protect_var (m_in_debug_repl); 1170
265 frame.protect_var (m_execution_mode); 1171 if (context == "caller")
266 1172 m_call_stack.goto_caller_frame ();
267 m_in_debug_repl = true; 1173 else if (context == "base")
268 1174 m_call_stack.goto_base_frame ();
269 tree_evaluator& tw = m_interpreter.get_evaluator (); 1175 else
270 1176 error (R"(evalin: CONTEXT must be "caller" or "base")");
271 bool silent = tw.quiet_breakpoint_flag (false); 1177
272 1178 int parse_status = 0;
273 frame.add (&tree_evaluator::restore_frame, &tw, 1179
274 tw.current_call_stack_frame_number ()); 1180 return eval_string (try_code, nargout > 0, parse_status, nargout);
275 1181 }
276 tw.goto_frame (tw.debug_frame ()); 1182
277 1183 octave_value_list tree_evaluator::evalin (const std::string& context,
278 octave_user_code *caller = tw.current_user_code (); 1184 const std::string& try_code,
279 std::string fcn_file_nm, fcn_nm; 1185 const std::string& catch_code,
280 1186 int nargout)
281 if (caller) 1187 {
282 { 1188 octave_value_list retval;
283 fcn_file_nm = caller->fcn_file_name (); 1189
284 fcn_nm = fcn_file_nm.empty () ? caller->name () : fcn_file_nm; 1190 unwind_action act1 ([=] (std::size_t frm)
285 } 1191 {
286 1192 m_call_stack.restore_frame (frm);
287 int curr_debug_line = tw.current_line (); 1193 }, m_call_stack.current_frame ());
288 1194
289 std::ostringstream buf; 1195 if (context == "caller")
290 1196 m_call_stack.goto_caller_frame ();
291 input_system& input_sys = m_interpreter.get_input_system (); 1197 else if (context == "base")
292 1198 m_call_stack.goto_base_frame ();
293 event_manager& evmgr = m_interpreter.get_event_manager (); 1199 else
294 1200 error (R"(evalin: CONTEXT must be "caller" or "base")");
295 if (! fcn_nm.empty ()) 1201
296 { 1202 error_system& es = m_interpreter.get_error_system ();
297 if (input_sys.gud_mode ()) 1203
298 { 1204 int parse_status = 0;
299 static char ctrl_z = 'Z' & 0x1f; 1205
300 1206 bool execution_error = false;
301 buf << ctrl_z << ctrl_z << fcn_nm << ':' << curr_debug_line; 1207
302 } 1208 octave_value_list tmp;
303 else 1209
304 { 1210 try
305 // FIXME: we should come up with a clean way to detect 1211 {
306 // that we are stopped on the no-op command that marks the 1212 tmp = eval_string (try_code, nargout > 0, parse_status, nargout);
307 // end of a function or script. 1213 }
308 1214 catch (const execution_exception& ee)
309 if (! silent) 1215 {
310 { 1216 es.save_exception (ee);
311 std::shared_ptr<stack_frame> frm = tw.current_user_frame (); 1217 m_interpreter.recover_from_exception ();
312 1218
313 frm->display_stopped_in_message (buf); 1219 execution_error = true;
314 } 1220 }
315 1221
316 evmgr.enter_debugger_event (fcn_nm, fcn_file_nm, curr_debug_line); 1222 if (parse_status != 0 || execution_error)
317 1223 {
318 evmgr.set_workspace (); 1224 tmp = eval_string (catch_code, nargout > 0, parse_status, nargout);
319 1225
320 frame.add (&event_manager::execute_in_debugger_event, &evmgr, 1226 retval = (nargout > 0) ? tmp : octave_value_list ();
321 fcn_nm, curr_debug_line); 1227 }
322 1228 else
323 if (! silent) 1229 {
324 { 1230 if (nargout > 0)
325 std::string line_buf; 1231 retval = tmp;
326 1232
327 if (caller) 1233 // FIXME: we should really be rethrowing whatever
328 line_buf = caller->get_code_line (curr_debug_line); 1234 // exception occurred, not just throwing an
329 1235 // execution exception.
330 if (! line_buf.empty ()) 1236 if (execution_error)
331 buf << curr_debug_line << ": " << line_buf; 1237 throw execution_exception ();
332 } 1238 }
333 } 1239
334 } 1240 return retval;
335 1241 }
336 if (silent) 1242
337 command_editor::erase_empty_line (true); 1243 void
338 1244 tree_evaluator::visit_anon_fcn_handle (tree_anon_fcn_handle&)
339 std::string stopped_in_msg = buf.str (); 1245 {
340 1246 panic_impossible ();
341 if (m_interpreter.server_mode ()) 1247 }
342 { 1248
343 if (! stopped_in_msg.empty ()) 1249 void
344 octave_stdout << stopped_in_msg << std::endl; 1250 tree_evaluator::visit_argument_list (tree_argument_list&)
345 1251 {
346 evmgr.push_event_queue (); 1252 panic_impossible ();
347 1253 }
348 frame.add (&event_manager::pop_event_queue, &evmgr); 1254
349 1255 void
350 frame.add (&tree_evaluator::set_parser, &tw, tw.get_parser ()); 1256 tree_evaluator::visit_arguments_block (tree_arguments_block&)
351 1257 {
352 std::shared_ptr<push_parser> 1258 warning ("function arguments validation blocks are not supported; INCORRECT RESULTS ARE POSSIBLE");
353 debug_parser (new push_parser (m_interpreter)); 1259 }
354 1260
355 tw.set_parser (debug_parser); 1261 void
356 1262 tree_evaluator::visit_args_block_attribute_list (tree_args_block_attribute_list&)
357 server_loop (); 1263 {
358 } 1264 panic_impossible ();
359 else 1265 }
360 { 1266
361 if (! stopped_in_msg.empty ()) 1267 void
362 std::cerr << stopped_in_msg << std::endl; 1268 tree_evaluator::visit_args_block_validation_list (tree_args_block_validation_list&)
363 1269 {
364 std::string tmp_prompt = prompt_arg; 1270 panic_impossible ();
365 if (m_level > 0) 1271 }
366 tmp_prompt = "[" + std::to_string (m_level) + "]" + prompt_arg; 1272
367 1273 void
368 frame.add (&input_system::set_PS1, &input_sys, input_sys.PS1 ()); 1274 tree_evaluator::visit_arg_validation (tree_arg_validation&)
369 input_sys.PS1 (tmp_prompt); 1275 {
370 1276 panic_impossible ();
371 if (! m_interpreter.interactive ()) 1277 }
372 { 1278
373 void (interpreter::*interactive_fptr) (bool) 1279 void
374 = &interpreter::interactive; 1280 tree_evaluator::visit_arg_size_spec (tree_arg_size_spec&)
375 frame.add (interactive_fptr, &m_interpreter, 1281 {
376 m_interpreter.interactive ()); 1282 panic_impossible ();
377 1283 }
378 m_interpreter.interactive (true); 1284
379 1285 void
380 // FIXME: should debugging be possible in an embedded 1286 tree_evaluator::visit_arg_validation_fcns (tree_arg_validation_fcns&)
381 // interpreter? 1287 {
382 1288 panic_impossible ();
383 application *app = application::app (); 1289 }
384 1290
385 if (app) 1291 void
386 { 1292 tree_evaluator::visit_binary_expression (tree_binary_expression&)
387 void (application::*forced_interactive_fptr) (bool) 1293 {
388 = &application::forced_interactive; 1294 panic_impossible ();
389 frame.add (forced_interactive_fptr, app, 1295 }
390 app->forced_interactive ()); 1296
391 1297 void
392 app->forced_interactive (true); 1298 tree_evaluator::visit_boolean_expression (tree_boolean_expression&)
393 } 1299 {
394 } 1300 panic_impossible ();
395 1301 }
396 #if defined (OCTAVE_ENABLE_COMMAND_LINE_PUSH_PARSER) 1302
397 1303 void
398 input_reader reader (m_interpreter); 1304 tree_evaluator::visit_compound_binary_expression (tree_compound_binary_expression&)
399 1305 {
400 push_parser debug_parser (m_interpreter); 1306 panic_impossible ();
401 1307 }
402 #else 1308
403 1309 void
404 parser debug_parser (m_interpreter); 1310 tree_evaluator::visit_break_command (tree_break_command& cmd)
405 1311 {
406 #endif 1312 if (m_echo_state)
407 1313 {
408 error_system& es = m_interpreter.get_error_system (); 1314 int line = cmd.line ();
409 1315 if (line < 0)
410 while (m_in_debug_repl) 1316 line = 1;
411 { 1317 echo_code (line);
412 if (m_execution_mode == EX_CONTINUE || tw.dbstep_flag ()) 1318 m_echo_file_pos = line + 1;
1319 }
1320
1321 if (m_debug_mode)
1322 do_breakpoint (cmd.is_active_breakpoint (*this));
1323
1324 if (m_in_loop_command)
1325 m_breaking = 1;
1326 else
1327 error ("break must appear in a loop in the same file as loop command");
1328 }
1329
1330 void
1331 tree_evaluator::visit_colon_expression (tree_colon_expression&)
1332 {
1333 panic_impossible ();
1334 }
1335
1336 void
1337 tree_evaluator::visit_continue_command (tree_continue_command& cmd)
1338 {
1339 if (m_echo_state)
1340 {
1341 int line = cmd.line ();
1342 if (line < 0)
1343 line = 1;
1344 echo_code (line);
1345 m_echo_file_pos = line + 1;
1346 }
1347
1348 if (m_debug_mode)
1349 do_breakpoint (cmd.is_active_breakpoint (*this));
1350
1351 if (m_in_loop_command)
1352 m_continuing = 1;
1353 }
1354
1355 bool
1356 tree_evaluator::statement_printing_enabled (void)
1357 {
1358 return ! (m_silent_functions && (m_statement_context == SC_FUNCTION
1359 || m_statement_context == SC_SCRIPT));
1360 }
1361
1362 void
1363 tree_evaluator::reset_debug_state (void)
1364 {
1365 m_debug_mode = (m_bp_table.have_breakpoints ()
1366 || m_dbstep_flag != 0
1367 || m_break_on_next_stmt
1368 || in_debug_repl ());
1369 }
1370
1371 void
1372 tree_evaluator::reset_debug_state (bool mode)
1373 {
1374 m_debug_mode = mode;
1375 }
1376
1377 void
1378 tree_evaluator::enter_debugger (const std::string& prompt)
1379 {
1380 unwind_protect frame;
1381
1382 frame.add (command_history::ignore_entries,
1383 command_history::ignoring_entries ());
1384
1385 command_history::ignore_entries (false);
1386
1387 frame.add (&call_stack::restore_frame, &m_call_stack,
1388 m_call_stack.current_frame ());
1389
1390 // Don't allow errors or warnings at the debug prompt to push us
1391 // into deeper levels of debugging.
1392
1393 error_system& es = m_interpreter.get_error_system ();
1394
1395 frame.add (&error_system::set_debug_on_error, &es, es.debug_on_error ());
1396
1397 frame.add (&error_system::set_debug_on_warning, &es,
1398 es.debug_on_warning ());
1399
1400 es.debug_on_error (false);
1401 es.debug_on_warning (false);
1402
1403 // Go up to the nearest user code frame.
1404
1405 m_debug_frame = m_call_stack.dbupdown (0);
1406
1407 // FIXME: probably we just want to print one line, not the
1408 // entire statement, which might span many lines...
1409 //
1410 // tree_print_code tpc (octave_stdout);
1411 // stmt.accept (tpc);
1412
1413 debugger *dbgr = new debugger (m_interpreter, m_debugger_stack.size ());
1414
1415 m_debugger_stack.push (dbgr);
1416
1417 frame.add ([=] (void)
1418 {
1419 delete m_debugger_stack.top ();
1420 m_debugger_stack.pop ();
1421 reset_debug_state ();
1422 });
1423
1424 dbgr->repl (prompt);
1425 }
1426
1427 void
1428 tree_evaluator::keyboard (const std::string& prompt)
1429 {
1430 enter_debugger (prompt);
1431 }
1432
1433 void
1434 tree_evaluator::dbupdown (int n, bool verbose)
1435 {
1436 m_debug_frame = m_call_stack.dbupdown (n, verbose);
1437 }
1438
1439 Matrix
1440 tree_evaluator::ignored_fcn_outputs (void) const
1441 {
1442 Matrix retval;
1443
1444 const std::list<octave_lvalue> *lvalues = m_lvalue_list;
1445
1446 if (! lvalues)
1447 return retval;
1448
1449 octave_idx_type nbh = 0;
1450
1451 for (const auto& lval : *lvalues)
1452 nbh += lval.is_black_hole ();
1453
1454 if (nbh > 0)
1455 {
1456 retval.resize (1, nbh);
1457
1458 octave_idx_type k = 0;
1459 octave_idx_type l = 0;
1460
1461 for (const auto& lval : *lvalues)
1462 {
1463 if (lval.is_black_hole ())
1464 retval(l++) = k+1;
1465
1466 k += lval.numel ();
1467 }
1468 }
1469
1470 return retval;
1471 }
1472
1473 // If NAME is an operator (like "+", "-", ...), convert it to the
1474 // corresponding function name ("plus", "minus", ...).
1475
1476 static std::string
1477 get_operator_function_name (const std::string& name)
1478 {
1479 // Bow to the god of compatibility.
1480
1481 // FIXME: it seems ugly to put this here, but there is no single
1482 // function in the parser that converts from the operator name to
1483 // the corresponding function name. At least try to do it without N
1484 // string compares.
1485
1486 // FIXME: .+, .-, **, and .** are deprecated but still need to be
1487 // handled here until they are removed.
1488
1489 std::size_t len = name.length ();
1490
1491 if (len == 3 && name == ".**")
1492 // deprecated
1493 return "power";
1494 else if (len == 2)
1495 {
1496 if (name[0] == '.')
1497 {
1498 switch (name[1])
1499 {
1500 case '\'':
1501 return "transpose";
1502
1503 case '+':
1504 // deprecated
1505 return "plus";
1506
1507 case '-':
1508 // deprecated
1509 return "minus";
1510
1511 case '*':
1512 return "times";
1513
1514 case '/':
1515 return "rdivide";
1516
1517 case '^':
1518 return "power";
1519
1520 case '\\':
1521 return "ldivide";
1522
1523 default:
413 break; 1524 break;
414 1525 }
415 if (quitting_debugger ()) 1526 }
1527 else if (name[1] == '=')
1528 {
1529 switch (name[0])
1530 {
1531 case '<':
1532 return "le";
1533
1534 case '=':
1535 return "eq";
1536
1537 case '>':
1538 return "ge";
1539
1540 case '~':
1541 case '!':
1542 return "ne";
1543
1544 default:
416 break; 1545 break;
417 1546 }
418 try 1547 }
419 { 1548 else if (name == "**")
420 debug_parser.reset (); 1549 // deprecated
421 1550 return "mpower";
422 #if defined (OCTAVE_ENABLE_COMMAND_LINE_PUSH_PARSER) 1551 }
423 1552 else if (len == 1)
424 int retval = 0; 1553 {
425 1554 switch (name[0])
426 std::string prompt 1555 {
427 = command_editor::decode_prompt_string (tmp_prompt); 1556 case '~':
428 1557 case '!':
429 do 1558 return "not";
430 { 1559
431 bool eof = false; 1560 case '\'':
432 std::string input_line = reader.get_input (prompt, eof); 1561 return "ctranspose";
433 1562
434 if (eof) 1563 case '+':
435 { 1564 return "plus";
436 retval = EOF; 1565
437 break; 1566 case '-':
438 } 1567 return "minus";
439 1568
440 retval = debug_parser.run (input_line, false); 1569 case '*':
441 1570 return "mtimes";
442 prompt = command_editor::decode_prompt_string (input_sys.PS2 ()); 1571
443 } 1572 case '/':
444 while (retval < 0); 1573 return "mrdivide";
445 1574
446 #else 1575 case '^':
447 1576 return "mpower";
448 int retval = debug_parser.run (); 1577
449 1578 case '\\':
450 #endif 1579 return "mldivide";
451 if (command_editor::interrupt (false)) 1580
452 { 1581 case '<':
453 // Break regardless of m_execution_mode value. 1582 return "lt";
454 1583
455 quitting_debugger (); 1584 case '>':
456 1585 return "gt";
457 break; 1586
458 } 1587 case '&':
459 else 1588 return "and";
460 { 1589
461 if (retval == 0) 1590 case '|':
462 { 1591 return "or";
463 std::shared_ptr<tree_statement_list> stmt_list 1592
464 = debug_parser.statement_list (); 1593 default:
465
466 if (stmt_list)
467 stmt_list->accept (tw);
468
469 if (octave_completion_matches_called)
470 octave_completion_matches_called = false;
471
472 // FIXME: the following statement is here because
473 // the last command may have been a dbup, dbdown, or
474 // dbstep command that changed the current debug
475 // frame. If so, we need to reset the current frame
476 // for the call stack. But is this right way to do
477 // this job? What if the statement list was
478 // something like "dbup; dbstack"? Will the call to
479 // dbstack use the right frame? If not, how can we
480 // fix this problem?
481 tw.goto_frame (tw.debug_frame ());
482 }
483
484 octave_quit ();
485 }
486 }
487 catch (const execution_exception& ee)
488 {
489 es.save_exception (ee);
490 es.display_exception (ee);
491
492 // Ignore errors when in debugging mode;
493 m_interpreter.recover_from_exception ();
494 }
495 catch (const quit_debug_exception& qde)
496 {
497 if (qde.all ())
498 throw;
499
500 // Continue in this debug level.
501 }
502 }
503 }
504 }
505
506 bool debugger::quitting_debugger (void) const
507 {
508 if (m_execution_mode == EX_QUIT)
509 {
510 // If there is no enclosing debug level or the top-level
511 // repl is not active, handle dbquit the same as dbcont.
512
513 if (m_level > 0 || m_interpreter.server_mode ()
514 || m_interpreter.in_top_level_repl ())
515 throw quit_debug_exception ();
516 else
517 return true;
518 }
519
520 if (m_execution_mode == EX_QUIT_ALL)
521 {
522 // If the top-level repl is not active, handle "dbquit all"
523 // the same as dbcont.
524
525 if (m_interpreter.server_mode () || m_interpreter.in_top_level_repl ())
526 throw quit_debug_exception (true);
527 else
528 return true;
529 }
530
531 return false;
532 }
533
534 bool tree_evaluator::at_top_level (void) const
535 {
536 return m_call_stack.at_top_level ();
537 }
538
539 std::string
540 tree_evaluator::mfilename (const std::string& opt) const
541 {
542 std::string fname;
543
544 octave_user_code *fcn = m_call_stack.current_user_code ();
545
546 if (fcn)
547 {
548 fname = fcn->fcn_file_name ();
549
550 if (fname.empty ())
551 fname = fcn->name ();
552 }
553
554 if (opt == "fullpathext")
555 return fname;
556
557 std::size_t dpos = fname.rfind (sys::file_ops::dir_sep_char ());
558 std::size_t epos = fname.rfind ('.');
559
560 if (epos <= dpos+1)
561 epos = std::string::npos;
562
563 if (epos != std::string::npos)
564 fname = fname.substr (0, epos);
565
566 if (opt == "fullpath")
567 return fname;
568
569 if (dpos != std::string::npos)
570 fname = fname.substr (dpos+1);
571
572 return fname;
573 }
574
575 void tree_evaluator::parse_and_execute (const std::string& input,
576 bool& incomplete_parse)
577 {
578 incomplete_parse = false;
579
580 unwind_protect_var<bool> upv (m_in_top_level_repl, true);
581
582 if (at_top_level ())
583 {
584 dbstep_flag (0);
585 reset_debug_state ();
586 }
587
588 // FIXME: OK to do this job here, or should it be in the functions
589 // that do the actual prompting?
590
591 // Update the time stamp for the "prompt" so that automatically
592 // finding modified files based on file modification times will
593 // work. In the future, we may do something completely different to
594 // check for changes to files but for now, we rely on the prompt
595 // time stamp to limit the checks for file modification times.
596
597 Vlast_prompt_time.stamp ();
598
599 bool eof = false;
600
601 event_manager& evmgr = m_interpreter.get_event_manager ();
602
603 if (command_history::add (input))
604 evmgr.append_history (input);
605
606 m_exit_status = m_parser->run (input, eof);
607
608 if (m_exit_status == 0)
609 {
610 std::shared_ptr<tree_statement_list>
611 stmt_list = m_parser->statement_list ();
612
613 if (stmt_list)
614 {
615 command_editor::increment_current_command_number ();
616
617 eval (stmt_list, m_interpreter.interactive ());
618
619 evmgr.set_workspace ();
620 }
621 else if (m_parser->at_end_of_input ())
622 m_exit_status = EOF;
623 }
624 else
625 incomplete_parse = true;
626
627 // FIXME: Should we be checking m_exit_status or incomplete_parse or
628 // both here? Could EOF have a value other than -1, and is there
629 // possible confusion between that state and the parser returning -1?
630
631 if (m_exit_status == -1)
632 m_exit_status = 0;
633 else
634 m_parser->reset ();
635
636 evmgr.pre_input_event ();
637 }
638
639 void tree_evaluator::get_line_and_eval (void)
640 {
641 std::mutex mtx;
642 std::unique_lock<std::mutex> lock (mtx);
643 std::condition_variable cv;
644 bool incomplete_parse = false;
645 bool evaluation_pending = false;
646 bool exiting = false;
647
648 input_system& input_sys = m_interpreter.get_input_system ();
649 event_manager& evmgr = m_interpreter.get_event_manager ();
650
651 while (true)
652 {
653 // FIXME: Detect EOF? Use readline? If
654 // so, then we need to disable idle event loop hook function
655 // execution.
656
657 std::string ps = incomplete_parse ? input_sys.PS2 () : input_sys.PS1 ();
658
659 std::cout << command_editor::decode_prompt_string (ps);
660
661 std::string input;
662 std::getline (std::cin, input);
663
664 if (input.empty ())
665 continue;
666
667 incomplete_parse = false;
668 evaluation_pending = true;
669 exiting = false;
670
671 evmgr.post_event
672 ([&] (interpreter& interp)
673 {
674 // INTERPRETER THREAD
675
676 std::lock_guard<std::mutex> local_lock (mtx);
677
678 try
679 {
680 interp.parse_and_execute (input, incomplete_parse);
681 }
682 catch (const exit_exception&)
683 {
684 evaluation_pending = false;
685 exiting = true;
686 cv.notify_all ();
687 throw;
688 }
689 catch (const execution_exception& ee)
690 {
691 error_system& es = m_interpreter.get_error_system ();
692
693 es.save_exception (ee);
694 es.display_exception (ee);
695
696 if (m_interpreter.interactive ())
697 {
698 m_interpreter.recover_from_exception ();
699 m_parser->reset ();
700 evaluation_pending = false;
701 cv.notify_all ();
702 }
703 else
704 {
705 evaluation_pending = false;
706 cv.notify_all ();
707 throw exit_exception (1);
708 }
709 }
710 catch (...)
711 {
712 evaluation_pending = false;
713 cv.notify_all ();
714 throw;
715 }
716
717 evaluation_pending = false;
718 cv.notify_all ();
719 });
720
721 // Wait until evaluation is finished before prompting for input
722 // again.
723
724 cv.wait (lock, [&] { return ! evaluation_pending; });
725
726 if (exiting)
727 break; 1594 break;
728 } 1595 }
729 } 1596 }
730 1597
731 int tree_evaluator::repl (void) 1598 return name;
732 { 1599 }
733 // The big loop. Read, Eval, Print, Loop. Normally user 1600
734 // interaction at the command line in a terminal session, but we may 1601 // Creates a function handle that takes into account the context,
735 // also end up here when reading from a pipe or when stdin is 1602 // finding local, nested, private, or sub functions.
736 // connected to a file by the magic of input redirection. 1603
737 1604 octave_value
738 int exit_status = 0; 1605 tree_evaluator::make_fcn_handle (const std::string& name)
739 1606 {
740 // FIXME: should this choice be a command-line option? Note that we 1607 octave_value retval;
741 // intend that the push parser interface only be used for 1608
742 // interactive sessions. 1609 // The str2func function can create a function handle with the name
743 1610 // of an operator (for example, "+"). If so, it is converted to the
744 #if defined (OCTAVE_ENABLE_COMMAND_LINE_PUSH_PARSER) 1611 // name of the corresponding function ("+" -> "plus") and we create
745 static bool use_command_line_push_parser = true; 1612 // a simple function handle using that name.
746 #else 1613
747 static bool use_command_line_push_parser = false; 1614 std::string fcn_name = get_operator_function_name (name);
748 #endif 1615
749 1616 // If FCN_NAME is different from NAME, then NAME is an operator. As
750 // The following logic is written as it is to allow easy transition 1617 // of version 2020a, Matlab apparently uses the function name
751 // to setting USE_COMMAND_LINE_PUSH_PARSER at run time and to 1618 // corresponding to the operator to search for private and local
752 // simplify the logic of the main loop below by using the same 1619 // functions in the current scope but not(!) nested functions.
753 // base_parser::run interface for both push and pull parsers. 1620
754 1621 bool name_is_operator = fcn_name != name;
755 std::shared_ptr<base_parser> repl_parser; 1622
756 1623 std::size_t pos = fcn_name.find ('.');
757 if (m_interpreter.interactive ()) 1624
758 { 1625 if (pos != std::string::npos)
759 if (use_command_line_push_parser) 1626 {
760 { 1627 // Recognize (some of? which ones?) the following cases
761 push_parser *pp 1628 // and create something other than a simple function handle?
762 = new push_parser (m_interpreter, 1629 // Should we just be checking for the last two when the first
763 new input_reader (m_interpreter)); 1630 // element of the dot-separated list is an object? If so, then
764 1631 // should this syntax be limited to a dot-separated list with
765 repl_parser = std::shared_ptr<base_parser> (pp); 1632 // exactly two elements?
766 } 1633 //
767 else 1634 // object . method
768 { 1635 // object . static-method
769 parser *pp = new parser (new lexer (m_interpreter)); 1636 //
770 repl_parser = std::shared_ptr<base_parser> (pp); 1637 // Code to do that duplicates some of simple_fcn_handle::call.
771 } 1638
772 } 1639 // Only accept expressions that contain one '.' separator.
773 else 1640
774 { 1641 // FIXME: The logic here is a bit complicated. Is there a good
775 parser *pp = new parser (new lexer (stdin, m_interpreter)); 1642 // way to simplify it?
776 repl_parser = std::shared_ptr<base_parser> (pp); 1643
777 } 1644 std::string meth_nm = fcn_name.substr (pos+1);
778 1645
779 do 1646 if (meth_nm.find ('.') == std::string::npos)
780 { 1647 {
781 try 1648 std::string obj_nm = fcn_name.substr (0, pos);
782 { 1649
783 unwind_protect_var<bool> upv (m_in_top_level_repl, true); 1650 // If obj_nm is an object in the current scope with a
784 1651 // method named meth_nm, create a classsimple handle.
785 repl_parser->reset (); 1652
786 1653 octave_value object = varval (obj_nm);
787 if (at_top_level ()) 1654
788 { 1655 if (object.is_defined () && object.is_classdef_object ())
789 dbstep_flag (0); 1656 {
790 reset_debug_state (); 1657 octave_classdef *cdef = object.classdef_object_value ();
791 } 1658
792 1659 if (cdef)
793 exit_status = repl_parser->run (); 1660 {
794 1661 std::string class_nm = cdef->class_name ();
795 if (exit_status == 0) 1662
796 { 1663 cdef_object cdef_obj = cdef->get_object ();
797 std::shared_ptr<tree_statement_list> 1664
798 stmt_list = repl_parser->statement_list (); 1665 cdef_class cls = cdef_obj.get_class ();
799 1666
800 if (stmt_list) 1667 cdef_method meth = cls.find_method (meth_nm);
801 { 1668
802 command_editor::increment_current_command_number (); 1669 if (meth.ok ())
803 1670 {
804 eval (stmt_list, m_interpreter.interactive ()); 1671 // If the method we found is static, create a
805 } 1672 // new function name from the class name and
806 else if (repl_parser->at_end_of_input ()) 1673 // method name and create a simple function
807 { 1674 // handle below. Otherwise, create a class
808 exit_status = EOF; 1675 // simple function handle.
809 break; 1676
810 } 1677 if (meth.is_static ())
811 } 1678 fcn_name = class_nm + '.' + meth_nm;
812 } 1679 else
813 catch (const interrupt_exception&)
814 {
815 m_interpreter.recover_from_exception ();
816
817 // Required newline when the user does Ctrl+C at the prompt.
818 if (m_interpreter.interactive ())
819 octave_stdout << "\n";
820 }
821 catch (const index_exception& ie)
822 {
823 m_interpreter.recover_from_exception ();
824
825 std::cerr << "error: unhandled index exception: "
826 << ie.message () << " -- trying to return to prompt"
827 << std::endl;
828 }
829 catch (const execution_exception& ee)
830 {
831 error_system& es = m_interpreter.get_error_system ();
832
833 es.save_exception (ee);
834 es.display_exception (ee);
835
836 if (m_interpreter.interactive ())
837 m_interpreter.recover_from_exception ();
838 else
839 {
840 // We should exit with a nonzero status.
841 exit_status = 1;
842 break;
843 }
844 }
845 catch (const quit_debug_exception&)
846 {
847 m_interpreter.recover_from_exception ();
848
849 // FIXME: Does anything else need to happen here?
850 }
851 catch (const std::bad_alloc&)
852 {
853 m_interpreter.recover_from_exception ();
854
855 std::cerr << "error: out of memory -- trying to return to prompt"
856 << std::endl;
857 }
858 }
859 while (exit_status == 0);
860
861 if (exit_status == EOF)
862 {
863 if (m_interpreter.interactive ())
864 octave_stdout << "\n";
865
866 exit_status = 0;
867 }
868
869 return exit_status;
870 }
871
872 int tree_evaluator::server_loop (void)
873 {
874 // Process events from the event queue.
875
876 unwind_protect_var<bool> upv1 (m_server_mode, true);
877
878 m_exit_status = 0;
879
880 std::shared_ptr<push_parser> parser (new push_parser (m_interpreter));
881 unwind_protect_var<std::shared_ptr<push_parser>> upv2 (m_parser, parser);
882
883 // FIXME: We are currently resetting the parser after every call to
884 // recover_from_exception. This action should probably be handled
885 // in a more consistent way, but resetting the parser in every call
886 // to interpreter::recover_from_exception appears to cause
887 // segfaults in the test suite.
888
889 do
890 {
891 try
892 {
893 // FIXME: Should we call octave_quit in the octave::sleep
894 // and/or command_editor::run_event_hooks functions?
895
896 octave_quit ();
897
898 // FIXME: Running the event queue should be decoupled from
899 // the command_editor. We should also use a condition
900 // variable to manage the execution of entries in the queue
901 // and eliminate the need for the busy-wait loop.
902
903 command_editor::run_event_hooks ();
904
905 release_unreferenced_dynamic_libraries ();
906
907 sleep (0.1);
908 }
909 catch (const interrupt_exception&)
910 {
911 octave_interrupt_state = 1;
912 m_interpreter.recover_from_exception ();
913 m_parser->reset ();
914
915 // Required newline when the user does Ctrl+C at the prompt.
916 if (m_interpreter.interactive ())
917 octave_stdout << "\n";
918 }
919 catch (const index_exception& e)
920 {
921 m_interpreter.recover_from_exception ();
922 m_parser->reset ();
923
924 std::cerr << "error: unhandled index exception: "
925 << e.message () << " -- trying to return to prompt"
926 << std::endl;
927 }
928 catch (const execution_exception& ee)
929 {
930 error_system& es = m_interpreter.get_error_system ();
931
932 es.save_exception (ee);
933 es.display_exception (ee);
934
935 if (m_interpreter.interactive ())
936 {
937 m_interpreter.recover_from_exception ();
938 m_parser->reset ();
939 }
940 else
941 {
942 // We should exit with a nonzero status.
943 m_exit_status = 1;
944 break;
945 }
946 }
947 catch (const quit_debug_exception&)
948 {
949 octave_interrupt_state = 1;
950 m_interpreter.recover_from_exception ();
951 m_parser->reset ();
952 }
953 catch (const exit_exception& xe)
954 {
955 m_exit_status = xe.exit_status ();
956 break;
957 }
958 catch (const std::bad_alloc&)
959 {
960 m_interpreter.recover_from_exception ();
961 m_parser->reset ();
962
963 std::cerr << "error: out of memory -- trying to return to prompt"
964 << std::endl;
965 }
966 }
967 while (m_exit_status == 0);
968
969 if (m_exit_status == EOF)
970 {
971 if (m_interpreter.interactive ())
972 octave_stdout << "\n";
973
974 m_exit_status = 0;
975 }
976
977 return m_exit_status;
978 }
979
980 void tree_evaluator::eval (std::shared_ptr<tree_statement_list>& stmt_list,
981 bool interactive)
982 {
983 try
984 {
985 stmt_list->accept (*this);
986
987 octave_quit ();
988
989 if (! interactive)
990 {
991 bool quit = (m_returning || m_breaking);
992
993 if (m_returning)
994 m_returning = 0;
995
996 if (m_breaking)
997 m_breaking--;
998
999 if (quit)
1000 return;
1001 }
1002
1003 if (octave_completion_matches_called)
1004 octave_completion_matches_called = false;
1005 }
1006 catch (const quit_debug_exception&)
1007 {
1008 m_interpreter.recover_from_exception ();
1009 }
1010 }
1011
1012 octave_value_list
1013 tree_evaluator::eval_string (const std::string& eval_str, bool silent,
1014 int& parse_status, int nargout)
1015 {
1016 octave_value_list retval;
1017
1018 parser eval_parser (eval_str, m_interpreter);
1019
1020 do
1021 {
1022 eval_parser.reset ();
1023
1024 // If we are looking at
1025 //
1026 // val = eval ("code");
1027 //
1028 // then don't allow code to be parsed as a command.
1029
1030 if (nargout > 0)
1031 eval_parser.disallow_command_syntax ();
1032
1033 parse_status = eval_parser.run ();
1034
1035 if (parse_status == 0)
1036 {
1037 std::shared_ptr<tree_statement_list> stmt_list
1038 = eval_parser.statement_list ();
1039
1040 if (stmt_list)
1041 {
1042 tree_statement *stmt = nullptr;
1043
1044 if (stmt_list->length () == 1
1045 && (stmt = stmt_list->front ())
1046 && stmt->is_expression ())
1047 {
1048 tree_expression *expr = stmt->expression ();
1049
1050 if (silent)
1051 expr->set_print_flag (false);
1052
1053 retval = expr->evaluate_n (*this, nargout);
1054
1055 bool do_bind_ans = false;
1056
1057 if (expr->is_identifier ())
1058 do_bind_ans = ! is_variable (expr);
1059 else
1060 do_bind_ans = ! expr->is_assignment_expression ();
1061
1062 if (do_bind_ans && ! retval.empty ())
1063 bind_ans (retval(0), expr->print_result ());
1064
1065 if (nargout == 0)
1066 retval = octave_value_list ();
1067 }
1068 else if (nargout == 0)
1069 stmt_list->accept (*this);
1070 else
1071 error ("eval: invalid use of statement list");
1072
1073 if (returning () || breaking () || continuing ())
1074 break;
1075 }
1076 else if (eval_parser.at_end_of_input ())
1077 break;
1078 }
1079 }
1080 while (parse_status == 0);
1081
1082 return retval;
1083 }
1084
1085 octave_value tree_evaluator::eval_string (const std::string& eval_str,
1086 bool silent, int& parse_status)
1087 {
1088 octave_value retval;
1089
1090 octave_value_list tmp = eval_string (eval_str, silent, parse_status, 1);
1091
1092 if (! tmp.empty ())
1093 retval = tmp(0);
1094
1095 return retval;
1096 }
1097
1098 octave_value_list tree_evaluator::eval_string (const octave_value& arg,
1099 bool silent, int& parse_status,
1100 int nargout)
1101 {
1102 std::string s = arg.xstring_value ("eval: expecting string argument");
1103
1104 return eval_string (s, silent, parse_status, nargout);
1105 }
1106
1107 octave_value_list tree_evaluator::eval (const std::string& try_code,
1108 int nargout)
1109 {
1110 int parse_status = 0;
1111
1112 return eval_string (try_code, nargout > 0, parse_status, nargout);
1113 }
1114
1115 octave_value_list tree_evaluator::eval (const std::string& try_code,
1116 const std::string& catch_code,
1117 int nargout)
1118 {
1119 octave_value_list retval;
1120
1121 error_system& es = m_interpreter.get_error_system ();
1122
1123 int parse_status = 0;
1124
1125 bool execution_error = false;
1126
1127 octave_value_list tmp;
1128
1129 try
1130 {
1131 tmp = eval_string (try_code, nargout > 0, parse_status, nargout);
1132 }
1133 catch (const execution_exception& ee)
1134 {
1135 es.save_exception (ee);
1136 m_interpreter.recover_from_exception ();
1137
1138 execution_error = true;
1139 }
1140
1141 if (parse_status != 0 || execution_error)
1142 {
1143 tmp = eval_string (catch_code, nargout > 0, parse_status, nargout);
1144
1145 retval = (nargout > 0) ? tmp : octave_value_list ();
1146 }
1147 else
1148 {
1149 if (nargout > 0)
1150 retval = tmp;
1151
1152 // FIXME: we should really be rethrowing whatever
1153 // exception occurred, not just throwing an
1154 // execution exception.
1155 if (execution_error)
1156 throw execution_exception ();
1157 }
1158
1159 return retval;
1160 }
1161
1162 octave_value_list tree_evaluator::evalin (const std::string& context,
1163 const std::string& try_code,
1164 int nargout)
1165 {
1166 unwind_action act ([=] (std::size_t frm)
1167 {
1168 m_call_stack.restore_frame (frm);
1169 }, m_call_stack.current_frame ());
1170
1171 if (context == "caller")
1172 m_call_stack.goto_caller_frame ();
1173 else if (context == "base")
1174 m_call_stack.goto_base_frame ();
1175 else
1176 error (R"(evalin: CONTEXT must be "caller" or "base")");
1177
1178 int parse_status = 0;
1179
1180 return eval_string (try_code, nargout > 0, parse_status, nargout);
1181 }
1182
1183 octave_value_list tree_evaluator::evalin (const std::string& context,
1184 const std::string& try_code,
1185 const std::string& catch_code,
1186 int nargout)
1187 {
1188 octave_value_list retval;
1189
1190 unwind_action act1 ([=] (std::size_t frm)
1191 { 1680 {
1192 m_call_stack.restore_frame (frm); 1681 octave_value meth_fcn = meth.get_function ();
1193 }, m_call_stack.current_frame ()); 1682
1194 1683 octave_fcn_handle *fh
1195 if (context == "caller") 1684 = new octave_fcn_handle (object, meth_fcn,
1196 m_call_stack.goto_caller_frame (); 1685 class_nm, meth_nm);
1197 else if (context == "base") 1686
1198 m_call_stack.goto_base_frame (); 1687 return octave_value (fh);
1199 else 1688 }
1200 error (R"(evalin: CONTEXT must be "caller" or "base")"); 1689 }
1201 1690 }
1202 error_system& es = m_interpreter.get_error_system (); 1691 }
1203 1692 }
1204 int parse_status = 0; 1693
1205 1694 // We didn't match anything above, so create handle to SIMPLE
1206 bool execution_error = false; 1695 // package function or static class method. Function resolution
1207 1696 // is performed when the handle is used.
1208 octave_value_list tmp; 1697
1209 1698 return octave_value (new octave_fcn_handle (fcn_name));
1210 try 1699 }
1211 { 1700
1212 tmp = eval_string (try_code, nargout > 0, parse_status, nargout); 1701 // If the function name refers to a sub/local/private function or a
1213 } 1702 // class method/constructor, create scoped function handle that is
1214 catch (const execution_exception& ee) 1703 // bound to that function. Use the same precedence list as
1215 { 1704 // fcn_info::find but limit search to the following types of
1216 es.save_exception (ee); 1705 // functions:
1217 m_interpreter.recover_from_exception (); 1706 //
1218 1707 // nested functions (and subfunctions)
1219 execution_error = true; 1708 // local functions in the current file
1220 } 1709 // private function
1221 1710 // class method
1222 if (parse_status != 0 || execution_error) 1711 //
1223 { 1712 // For anything else we create a simple function handle that will be
1224 tmp = eval_string (catch_code, nargout > 0, parse_status, nargout); 1713 // resolved dynamically in the scope where it is evaluated.
1225 1714
1226 retval = (nargout > 0) ? tmp : octave_value_list (); 1715 symbol_scope curr_scope = get_current_scope ();
1227 } 1716
1228 else 1717 symbol_table& symtab = m_interpreter.get_symbol_table ();
1229 { 1718
1230 if (nargout > 0) 1719 if (curr_scope)
1231 retval = tmp; 1720 {
1232 1721 octave_value ov_fcn
1233 // FIXME: we should really be rethrowing whatever 1722 = symtab.find_scoped_function (fcn_name, curr_scope);
1234 // exception occurred, not just throwing an 1723
1235 // execution exception. 1724 // If name is operator, we are in Fstr2func, so skip the stack
1236 if (execution_error) 1725 // frame for that function.
1237 throw execution_exception (); 1726
1238 } 1727 bool skip_first = name_is_operator;
1239 1728 octave_function *curr_fcn = current_function (skip_first);
1240 return retval; 1729
1241 } 1730 if (ov_fcn.is_defined ())
1242 1731 {
1243 void 1732 octave_function *fcn = ov_fcn.function_value ();
1244 tree_evaluator::visit_anon_fcn_handle (tree_anon_fcn_handle&) 1733
1245 { 1734 if (fcn->is_nested_function ())
1246 panic_impossible (); 1735 {
1247 } 1736 if (! name_is_operator)
1248 1737 {
1249 void 1738 // Get current stack frame and return handle to nested
1250 tree_evaluator::visit_argument_list (tree_argument_list&) 1739 // function.
1251 { 1740
1252 panic_impossible (); 1741 std::shared_ptr<stack_frame> frame
1253 } 1742 = m_call_stack.get_current_stack_frame ();
1254 1743
1255 void 1744 // If we are creating a handle to the current
1256 tree_evaluator::visit_arguments_block (tree_arguments_block&) 1745 // function or a handle to a sibling function (i.e.,
1257 { 1746 // not a child of the current function), then use
1258 warning ("function arguments validation blocks are not supported; INCORRECT RESULTS ARE POSSIBLE"); 1747 // the calling stack frame as the context instead of
1259 } 1748 // the current stack frame.
1260 1749
1261 void 1750 // FIXME: Do we need both checks here or is it
1262 tree_evaluator::visit_args_block_attribute_list (tree_args_block_attribute_list&) 1751 // sufficient to check that the parent of curr_fcn
1263 { 1752 // is the same as the parent of fcn? Is there any
1264 panic_impossible (); 1753 // case where curr_fcn could be nullptr, or does
1265 } 1754 // that indicate an internal error of some kind?
1266 1755
1267 void 1756 if (curr_fcn
1268 tree_evaluator::visit_args_block_validation_list (tree_args_block_validation_list&) 1757 && (fcn_name == curr_fcn->name ()
1269 { 1758 || fcn->parent_fcn_name () == curr_fcn->parent_fcn_name ()))
1270 panic_impossible (); 1759 frame = frame->access_link ();
1271 } 1760
1272 1761 octave_fcn_handle *fh
1273 void 1762 = new octave_fcn_handle (ov_fcn, fcn_name, frame);
1274 tree_evaluator::visit_arg_validation (tree_arg_validation&) 1763
1275 { 1764 return octave_value (fh);
1276 panic_impossible (); 1765 }
1277 } 1766 }
1278 1767 else if (fcn->is_subfunction ()
1279 void 1768 /* || fcn->is_localfunction () */
1280 tree_evaluator::visit_arg_size_spec (tree_arg_size_spec&) 1769 || fcn->is_private_function ())
1281 { 1770 {
1282 panic_impossible (); 1771 // Create handle to SCOPED function (sub/local function
1283 } 1772 // or private function).
1284 1773
1285 void 1774 std::list<std::string> parentage = fcn->parent_fcn_names ();
1286 tree_evaluator::visit_arg_validation_fcns (tree_arg_validation_fcns&) 1775
1287 { 1776 octave_fcn_handle *fh
1288 panic_impossible (); 1777 = new octave_fcn_handle (ov_fcn, fcn_name, parentage);
1289 } 1778
1290 1779 return octave_value (fh);
1291 void 1780 }
1292 tree_evaluator::visit_binary_expression (tree_binary_expression&) 1781 }
1293 { 1782
1294 panic_impossible (); 1783 if (curr_fcn && (curr_fcn->is_class_method ()
1295 } 1784 || curr_fcn->is_class_constructor ()))
1296 1785 {
1297 void 1786 std::string dispatch_class = curr_fcn->dispatch_class ();
1298 tree_evaluator::visit_boolean_expression (tree_boolean_expression&) 1787
1299 { 1788 octave_value ov_meth
1300 panic_impossible (); 1789 = symtab.find_method (fcn_name, dispatch_class);
1301 } 1790
1302 1791 if (ov_meth.is_defined ())
1303 void 1792 {
1304 tree_evaluator::visit_compound_binary_expression (tree_compound_binary_expression&) 1793 octave_function *fcn = ov_meth.function_value ();
1305 { 1794
1306 panic_impossible (); 1795 // FIXME: do we need to check that it is a method of
1307 } 1796 // dispatch_class, or is it sufficient to just check
1308 1797 // that it is a method?
1309 void 1798
1310 tree_evaluator::visit_break_command (tree_break_command& cmd) 1799 if (fcn->is_class_method ())
1311 { 1800 {
1312 if (m_echo_state) 1801 // Create CLASSSIMPLE handle to method but don't
1313 { 1802 // bind to the method. Lookup will be done later.
1314 int line = cmd.line (); 1803
1315 if (line < 0) 1804 octave_fcn_handle *fh
1316 line = 1; 1805 = new octave_fcn_handle (dispatch_class, fcn_name);
1317 echo_code (line); 1806
1318 m_echo_file_pos = line + 1; 1807 return octave_value (fh);
1319 } 1808 }
1320 1809 }
1321 if (m_debug_mode) 1810 }
1322 do_breakpoint (cmd.is_active_breakpoint (*this)); 1811 }
1323 1812
1324 if (m_in_loop_command) 1813 octave_value ov_fcn = symtab.find_user_function (fcn_name);
1325 m_breaking = 1; 1814
1326 else 1815 // Create handle to SIMPLE function. If the function is not found
1327 error ("break must appear in a loop in the same file as loop command"); 1816 // now, then we will look for it again when the handle is used.
1328 } 1817
1329 1818 return octave_value (new octave_fcn_handle (ov_fcn, fcn_name));
1330 void 1819 }
1331 tree_evaluator::visit_colon_expression (tree_colon_expression&)
1332 {
1333 panic_impossible ();
1334 }
1335
1336 void
1337 tree_evaluator::visit_continue_command (tree_continue_command& cmd)
1338 {
1339 if (m_echo_state)
1340 {
1341 int line = cmd.line ();
1342 if (line < 0)
1343 line = 1;
1344 echo_code (line);
1345 m_echo_file_pos = line + 1;
1346 }
1347
1348 if (m_debug_mode)
1349 do_breakpoint (cmd.is_active_breakpoint (*this));
1350
1351 if (m_in_loop_command)
1352 m_continuing = 1;
1353 }
1354
1355 bool
1356 tree_evaluator::statement_printing_enabled (void)
1357 {
1358 return ! (m_silent_functions && (m_statement_context == SC_FUNCTION
1359 || m_statement_context == SC_SCRIPT));
1360 }
1361
1362 void
1363 tree_evaluator::reset_debug_state (void)
1364 {
1365 m_debug_mode = (m_bp_table.have_breakpoints ()
1366 || m_dbstep_flag != 0
1367 || m_break_on_next_stmt
1368 || in_debug_repl ());
1369 }
1370
1371 void
1372 tree_evaluator::reset_debug_state (bool mode)
1373 {
1374 m_debug_mode = mode;
1375 }
1376
1377 void
1378 tree_evaluator::enter_debugger (const std::string& prompt)
1379 {
1380 unwind_protect frame;
1381
1382 frame.add (command_history::ignore_entries,
1383 command_history::ignoring_entries ());
1384
1385 command_history::ignore_entries (false);
1386
1387 frame.add (&call_stack::restore_frame, &m_call_stack,
1388 m_call_stack.current_frame ());
1389
1390 // Don't allow errors or warnings at the debug prompt to push us
1391 // into deeper levels of debugging.
1392
1393 error_system& es = m_interpreter.get_error_system ();
1394
1395 frame.add (&error_system::set_debug_on_error, &es, es.debug_on_error ());
1396
1397 frame.add (&error_system::set_debug_on_warning, &es,
1398 es.debug_on_warning ());
1399
1400 es.debug_on_error (false);
1401 es.debug_on_warning (false);
1402
1403 // Go up to the nearest user code frame.
1404
1405 m_debug_frame = m_call_stack.dbupdown (0);
1406
1407 // FIXME: probably we just want to print one line, not the
1408 // entire statement, which might span many lines...
1409 //
1410 // tree_print_code tpc (octave_stdout);
1411 // stmt.accept (tpc);
1412
1413 debugger *dbgr = new debugger (m_interpreter, m_debugger_stack.size ());
1414
1415 m_debugger_stack.push (dbgr);
1416
1417 frame.add ([=] (void)
1418 {
1419 delete m_debugger_stack.top ();
1420 m_debugger_stack.pop ();
1421 reset_debug_state ();
1422 });
1423
1424 dbgr->repl (prompt);
1425 }
1426
1427 void
1428 tree_evaluator::keyboard (const std::string& prompt)
1429 {
1430 enter_debugger (prompt);
1431 }
1432
1433 void
1434 tree_evaluator::dbupdown (int n, bool verbose)
1435 {
1436 m_debug_frame = m_call_stack.dbupdown (n, verbose);
1437 }
1438
1439 Matrix
1440 tree_evaluator::ignored_fcn_outputs (void) const
1441 {
1442 Matrix retval;
1443
1444 const std::list<octave_lvalue> *lvalues = m_lvalue_list;
1445
1446 if (! lvalues)
1447 return retval;
1448
1449 octave_idx_type nbh = 0;
1450
1451 for (const auto& lval : *lvalues)
1452 nbh += lval.is_black_hole ();
1453
1454 if (nbh > 0)
1455 {
1456 retval.resize (1, nbh);
1457
1458 octave_idx_type k = 0;
1459 octave_idx_type l = 0;
1460
1461 for (const auto& lval : *lvalues)
1462 {
1463 if (lval.is_black_hole ())
1464 retval(l++) = k+1;
1465
1466 k += lval.numel ();
1467 }
1468 }
1469
1470 return retval;
1471 }
1472
1473 // If NAME is an operator (like "+", "-", ...), convert it to the
1474 // corresponding function name ("plus", "minus", ...).
1475
1476 static std::string
1477 get_operator_function_name (const std::string& name)
1478 {
1479 // Bow to the god of compatibility.
1480
1481 // FIXME: it seems ugly to put this here, but there is no single
1482 // function in the parser that converts from the operator name to
1483 // the corresponding function name. At least try to do it without N
1484 // string compares.
1485
1486 // FIXME: .+, .-, **, and .** are deprecated but still need to be
1487 // handled here until they are removed.
1488
1489 std::size_t len = name.length ();
1490
1491 if (len == 3 && name == ".**")
1492 // deprecated
1493 return "power";
1494 else if (len == 2)
1495 {
1496 if (name[0] == '.')
1497 {
1498 switch (name[1])
1499 {
1500 case '\'':
1501 return "transpose";
1502
1503 case '+':
1504 // deprecated
1505 return "plus";
1506
1507 case '-':
1508 // deprecated
1509 return "minus";
1510
1511 case '*':
1512 return "times";
1513
1514 case '/':
1515 return "rdivide";
1516
1517 case '^':
1518 return "power";
1519
1520 case '\\':
1521 return "ldivide";
1522
1523 default:
1524 break;
1525 }
1526 }
1527 else if (name[1] == '=')
1528 {
1529 switch (name[0])
1530 {
1531 case '<':
1532 return "le";
1533
1534 case '=':
1535 return "eq";
1536
1537 case '>':
1538 return "ge";
1539
1540 case '~':
1541 case '!':
1542 return "ne";
1543
1544 default:
1545 break;
1546 }
1547 }
1548 else if (name == "**")
1549 // deprecated
1550 return "mpower";
1551 }
1552 else if (len == 1)
1553 {
1554 switch (name[0])
1555 {
1556 case '~':
1557 case '!':
1558 return "not";
1559
1560 case '\'':
1561 return "ctranspose";
1562
1563 case '+':
1564 return "plus";
1565
1566 case '-':
1567 return "minus";
1568
1569 case '*':
1570 return "mtimes";
1571
1572 case '/':
1573 return "mrdivide";
1574
1575 case '^':
1576 return "mpower";
1577
1578 case '\\':
1579 return "mldivide";
1580
1581 case '<':
1582 return "lt";
1583
1584 case '>':
1585 return "gt";
1586
1587 case '&':
1588 return "and";
1589
1590 case '|':
1591 return "or";
1592
1593 default:
1594 break;
1595 }
1596 }
1597
1598 return name;
1599 }
1600
1601 // Creates a function handle that takes into account the context,
1602 // finding local, nested, private, or sub functions.
1603
1604 octave_value
1605 tree_evaluator::make_fcn_handle (const std::string& name)
1606 {
1607 octave_value retval;
1608
1609 // The str2func function can create a function handle with the name
1610 // of an operator (for example, "+"). If so, it is converted to the
1611 // name of the corresponding function ("+" -> "plus") and we create
1612 // a simple function handle using that name.
1613
1614 std::string fcn_name = get_operator_function_name (name);
1615
1616 // If FCN_NAME is different from NAME, then NAME is an operator. As
1617 // of version 2020a, Matlab apparently uses the function name
1618 // corresponding to the operator to search for private and local
1619 // functions in the current scope but not(!) nested functions.
1620
1621 bool name_is_operator = fcn_name != name;
1622
1623 std::size_t pos = fcn_name.find ('.');
1624
1625 if (pos != std::string::npos)
1626 {
1627 // Recognize (some of? which ones?) the following cases
1628 // and create something other than a simple function handle?
1629 // Should we just be checking for the last two when the first
1630 // element of the dot-separated list is an object? If so, then
1631 // should this syntax be limited to a dot-separated list with
1632 // exactly two elements?
1633 //
1634 // object . method
1635 // object . static-method
1636 //
1637 // Code to do that duplicates some of simple_fcn_handle::call.
1638
1639 // Only accept expressions that contain one '.' separator.
1640
1641 // FIXME: The logic here is a bit complicated. Is there a good
1642 // way to simplify it?
1643
1644 std::string meth_nm = fcn_name.substr (pos+1);
1645
1646 if (meth_nm.find ('.') == std::string::npos)
1647 {
1648 std::string obj_nm = fcn_name.substr (0, pos);
1649
1650 // If obj_nm is an object in the current scope with a
1651 // method named meth_nm, create a classsimple handle.
1652
1653 octave_value object = varval (obj_nm);
1654
1655 if (object.is_defined () && object.is_classdef_object ())
1656 {
1657 octave_classdef *cdef = object.classdef_object_value ();
1658
1659 if (cdef)
1660 {
1661 std::string class_nm = cdef->class_name ();
1662
1663 cdef_object cdef_obj = cdef->get_object ();
1664
1665 cdef_class cls = cdef_obj.get_class ();
1666
1667 cdef_method meth = cls.find_method (meth_nm);
1668
1669 if (meth.ok ())
1670 {
1671 // If the method we found is static, create a
1672 // new function name from the class name and
1673 // method name and create a simple function
1674 // handle below. Otherwise, create a class
1675 // simple function handle.
1676
1677 if (meth.is_static ())
1678 fcn_name = class_nm + '.' + meth_nm;
1679 else
1680 {
1681 octave_value meth_fcn = meth.get_function ();
1682
1683 octave_fcn_handle *fh
1684 = new octave_fcn_handle (object, meth_fcn,
1685 class_nm, meth_nm);
1686
1687 return octave_value (fh);
1688 }
1689 }
1690 }
1691 }
1692 }
1693
1694 // We didn't match anything above, so create handle to SIMPLE
1695 // package function or static class method. Function resolution
1696 // is performed when the handle is used.
1697
1698 return octave_value (new octave_fcn_handle (fcn_name));
1699 }
1700
1701 // If the function name refers to a sub/local/private function or a
1702 // class method/constructor, create scoped function handle that is
1703 // bound to that function. Use the same precedence list as
1704 // fcn_info::find but limit search to the following types of
1705 // functions:
1706 //
1707 // nested functions (and subfunctions)
1708 // local functions in the current file
1709 // private function
1710 // class method
1711 //
1712 // For anything else we create a simple function handle that will be
1713 // resolved dynamically in the scope where it is evaluated.
1714
1715 symbol_scope curr_scope = get_current_scope ();
1716
1717 symbol_table& symtab = m_interpreter.get_symbol_table ();
1718
1719 if (curr_scope)
1720 {
1721 octave_value ov_fcn
1722 = symtab.find_scoped_function (fcn_name, curr_scope);
1723
1724 // If name is operator, we are in Fstr2func, so skip the stack
1725 // frame for that function.
1726
1727 bool skip_first = name_is_operator;
1728 octave_function *curr_fcn = current_function (skip_first);
1729
1730 if (ov_fcn.is_defined ())
1731 {
1732 octave_function *fcn = ov_fcn.function_value ();
1733
1734 if (fcn->is_nested_function ())
1735 {
1736 if (! name_is_operator)
1737 {
1738 // Get current stack frame and return handle to nested
1739 // function.
1740
1741 std::shared_ptr<stack_frame> frame
1742 = m_call_stack.get_current_stack_frame ();
1743
1744 // If we are creating a handle to the current
1745 // function or a handle to a sibling function (i.e.,
1746 // not a child of the current function), then use
1747 // the calling stack frame as the context instead of
1748 // the current stack frame.
1749
1750 // FIXME: Do we need both checks here or is it
1751 // sufficient to check that the parent of curr_fcn
1752 // is the same as the parent of fcn? Is there any
1753 // case where curr_fcn could be nullptr, or does
1754 // that indicate an internal error of some kind?
1755
1756 if (curr_fcn
1757 && (fcn_name == curr_fcn->name ()
1758 || fcn->parent_fcn_name () == curr_fcn->parent_fcn_name ()))
1759 frame = frame->access_link ();
1760
1761 octave_fcn_handle *fh
1762 = new octave_fcn_handle (ov_fcn, fcn_name, frame);
1763
1764 return octave_value (fh);
1765 }
1766 }
1767 else if (fcn->is_subfunction ()
1768 /* || fcn->is_localfunction () */
1769 || fcn->is_private_function ())
1770 {
1771 // Create handle to SCOPED function (sub/local function
1772 // or private function).
1773
1774 std::list<std::string> parentage = fcn->parent_fcn_names ();
1775
1776 octave_fcn_handle *fh
1777 = new octave_fcn_handle (ov_fcn, fcn_name, parentage);
1778
1779 return octave_value (fh);
1780 }
1781 }
1782
1783 if (curr_fcn && (curr_fcn->is_class_method ()
1784 || curr_fcn->is_class_constructor ()))
1785 {
1786 std::string dispatch_class = curr_fcn->dispatch_class ();
1787
1788 octave_value ov_meth
1789 = symtab.find_method (fcn_name, dispatch_class);
1790
1791 if (ov_meth.is_defined ())
1792 {
1793 octave_function *fcn = ov_meth.function_value ();
1794
1795 // FIXME: do we need to check that it is a method of
1796 // dispatch_class, or is it sufficient to just check
1797 // that it is a method?
1798
1799 if (fcn->is_class_method ())
1800 {
1801 // Create CLASSSIMPLE handle to method but don't
1802 // bind to the method. Lookup will be done later.
1803
1804 octave_fcn_handle *fh
1805 = new octave_fcn_handle (dispatch_class, fcn_name);
1806
1807 return octave_value (fh);
1808 }
1809 }
1810 }
1811 }
1812
1813 octave_value ov_fcn = symtab.find_user_function (fcn_name);
1814
1815 // Create handle to SIMPLE function. If the function is not found
1816 // now, then we will look for it again when the handle is used.
1817
1818 return octave_value (new octave_fcn_handle (ov_fcn, fcn_name));
1819 }
1820 1820
1821 /* 1821 /*
1822 %!test 1822 %!test
1823 %! x = {".**", "power"; 1823 %! x = {".**", "power";
1824 %! ".'", "transpose"; 1824 %! ".'", "transpose";
1850 %! for i = 1:rows (x) 1850 %! for i = 1:rows (x)
1851 %! assert (functions (str2func (x{i,1})).function, x{i,2}); 1851 %! assert (functions (str2func (x{i,1})).function, x{i,2});
1852 %! endfor 1852 %! endfor
1853 */ 1853 */
1854 1854
1855 octave_value 1855 octave_value
1856 tree_evaluator::evaluate (tree_decl_elt *elt) 1856 tree_evaluator::evaluate (tree_decl_elt *elt)
1857 {
1858 // Do not allow functions to return null values.
1859
1860 tree_identifier *id = elt->ident ();
1861
1862 return id ? id->evaluate (*this).storable_value () : octave_value ();
1863 }
1864
1865 bool
1866 tree_evaluator::is_variable (const std::string& name) const
1867 {
1868 std::shared_ptr<stack_frame> frame
1869 = m_call_stack.get_current_stack_frame ();
1870
1871 return frame->is_variable (name);
1872 }
1873
1874 bool
1875 tree_evaluator::is_local_variable (const std::string& name) const
1876 {
1877 std::shared_ptr<stack_frame> frame
1878 = m_call_stack.get_current_stack_frame ();
1879
1880 return frame->is_local_variable (name);
1881 }
1882
1883 bool
1884 tree_evaluator::is_variable (const tree_expression *expr) const
1885 {
1886 if (expr->is_identifier ())
1887 {
1888 const tree_identifier *id
1889 = dynamic_cast<const tree_identifier *> (expr);
1890
1891 if (id->is_black_hole ())
1892 return false;
1893
1894 return is_variable (id->symbol ());
1895 }
1896
1897 return false;
1898 }
1899
1900 bool
1901 tree_evaluator::is_defined (const tree_expression *expr) const
1902 {
1903 if (expr->is_identifier ())
1904 {
1905 const tree_identifier *id
1906 = dynamic_cast<const tree_identifier *> (expr);
1907
1908 return is_defined (id->symbol ());
1909 }
1910
1911 return false;
1912 }
1913
1914 bool
1915 tree_evaluator::is_variable (const symbol_record& sym) const
1916 {
1917 std::shared_ptr<stack_frame> frame
1918 = m_call_stack.get_current_stack_frame ();
1919
1920 return frame->is_variable (sym);
1921 }
1922
1923 bool
1924 tree_evaluator::is_defined (const symbol_record& sym) const
1925 {
1926 std::shared_ptr<stack_frame> frame
1927 = m_call_stack.get_current_stack_frame ();
1928
1929 return frame->is_defined (sym);
1930 }
1931
1932 bool tree_evaluator::is_global (const std::string& name) const
1933 {
1934 std::shared_ptr<stack_frame> frame
1935 = m_call_stack.get_current_stack_frame ();
1936
1937 return frame->is_global (name);
1938 }
1939
1940 octave_value
1941 tree_evaluator::varval (const symbol_record& sym) const
1942 {
1943 std::shared_ptr<stack_frame> frame
1944 = m_call_stack.get_current_stack_frame ();
1945
1946 return frame->varval (sym);
1947 }
1948
1949 octave_value
1950 tree_evaluator::varval (const std::string& name) const
1951 {
1952 std::shared_ptr<stack_frame> frame
1953 = m_call_stack.get_current_stack_frame ();
1954
1955 return frame->varval (name);
1956 }
1957
1958 void tree_evaluator::install_variable (const std::string& name,
1959 const octave_value& value,
1960 bool global)
1961 {
1962 std::shared_ptr<stack_frame> frame
1963 = m_call_stack.get_current_stack_frame ();
1964
1965 return frame->install_variable (name, value, global);
1966 }
1967
1968 octave_value
1969 tree_evaluator::global_varval (const std::string& name) const
1970 {
1971 return m_call_stack.global_varval (name);
1972 }
1973
1974 octave_value&
1975 tree_evaluator::global_varref (const std::string& name)
1976 {
1977 return m_call_stack.global_varref (name);
1978 }
1979
1980 void
1981 tree_evaluator::global_assign (const std::string& name,
1982 const octave_value& val)
1983 {
1984 m_call_stack.global_varref (name) = val;
1985 }
1986
1987 octave_value
1988 tree_evaluator::top_level_varval (const std::string& name) const
1989 {
1990 return m_call_stack.get_top_level_value (name);
1991 }
1992
1993 void
1994 tree_evaluator::top_level_assign (const std::string& name,
1995 const octave_value& val)
1996 {
1997 m_call_stack.set_top_level_value (name, val);
1998 }
1999
2000 void
2001 tree_evaluator::assign (const std::string& name, const octave_value& val)
2002 {
2003 std::shared_ptr<stack_frame> frame
2004 = m_call_stack.get_current_stack_frame ();
2005
2006 frame->assign (name, val);
2007 }
2008
2009 void
2010 tree_evaluator::assignin (const std::string& context,
2011 const std::string& name, const octave_value& val)
2012 {
2013 // FIXME: Can this be done without an unwind-protect frame, simply
2014 // by getting a reference to the caller or base stack frame and
2015 // calling assign on that?
2016
2017 unwind_action act ([=] (std::size_t frm)
1857 { 2018 {
1858 // Do not allow functions to return null values. 2019 m_call_stack.restore_frame (frm);
1859 2020 }, m_call_stack.current_frame ());
1860 tree_identifier *id = elt->ident (); 2021
1861 2022 if (context == "caller")
1862 return id ? id->evaluate (*this).storable_value () : octave_value (); 2023 m_call_stack.goto_caller_frame ();
1863 } 2024 else if (context == "base")
1864 2025 m_call_stack.goto_base_frame ();
1865 bool 2026 else
1866 tree_evaluator::is_variable (const std::string& name) const 2027 error (R"(assignin: CONTEXT must be "caller" or "base")");
1867 { 2028
1868 std::shared_ptr<stack_frame> frame 2029 if (valid_identifier (name))
1869 = m_call_stack.get_current_stack_frame (); 2030 {
1870 2031 // Put the check here so that we don't slow down assignments
1871 return frame->is_variable (name); 2032 // generally. Any that go through Octave's parser should have
1872 } 2033 // already been checked.
1873 2034
1874 bool 2035 if (iskeyword (name))
1875 tree_evaluator::is_local_variable (const std::string& name) const 2036 error ("assignin: invalid assignment to keyword '%s'",
1876 { 2037 name.c_str ());
1877 std::shared_ptr<stack_frame> frame 2038
1878 = m_call_stack.get_current_stack_frame (); 2039 assign (name, val);
1879 2040 }
1880 return frame->is_local_variable (name); 2041 else
1881 } 2042 error ("assignin: invalid variable name '%s'", name.c_str ());
1882 2043 }
1883 bool 2044
1884 tree_evaluator::is_variable (const tree_expression *expr) const 2045 void
1885 { 2046 tree_evaluator::source_file (const std::string& file_name,
1886 if (expr->is_identifier ()) 2047 const std::string& context,
1887 { 2048 bool verbose, bool require_file)
1888 const tree_identifier *id 2049 {
1889 = dynamic_cast<const tree_identifier *> (expr); 2050 // Map from absolute name of script file to recursion level. We
1890 2051 // use a map instead of simply placing a limit on recursion in the
1891 if (id->is_black_hole ()) 2052 // source_file function so that two mutually recursive scripts
1892 return false; 2053 // written as
1893 2054 //
1894 return is_variable (id->symbol ()); 2055 // foo1.m:
1895 } 2056 // ------
1896 2057 // foo2
1897 return false; 2058 //
1898 } 2059 // foo2.m:
1899 2060 // ------
1900 bool 2061 // foo1
1901 tree_evaluator::is_defined (const tree_expression *expr) const 2062 //
1902 { 2063 // and called with
1903 if (expr->is_identifier ()) 2064 //
1904 { 2065 // foo1
1905 const tree_identifier *id 2066 //
1906 = dynamic_cast<const tree_identifier *> (expr); 2067 // (for example) will behave the same if they are written as
1907 2068 //
1908 return is_defined (id->symbol ()); 2069 // foo1.m:
1909 } 2070 // ------
1910 2071 // source ("foo2.m")
1911 return false; 2072 //
1912 } 2073 // foo2.m:
1913 2074 // ------
1914 bool 2075 // source ("foo1.m")
1915 tree_evaluator::is_variable (const symbol_record& sym) const 2076 //
1916 { 2077 // and called with
1917 std::shared_ptr<stack_frame> frame 2078 //
1918 = m_call_stack.get_current_stack_frame (); 2079 // source ("foo1.m")
1919 2080 //
1920 return frame->is_variable (sym); 2081 // (for example).
1921 } 2082
1922 2083 static std::map<std::string, int> source_call_depth;
1923 bool 2084
1924 tree_evaluator::is_defined (const symbol_record& sym) const 2085 std::string file_full_name
1925 { 2086 = sys::file_ops::tilde_expand (file_name);
1926 std::shared_ptr<stack_frame> frame 2087
1927 = m_call_stack.get_current_stack_frame (); 2088 std::size_t pos
1928 2089 = file_full_name.find_last_of (sys::file_ops::dir_sep_str ());
1929 return frame->is_defined (sym); 2090
1930 } 2091 std::string dir_name = file_full_name.substr (0, pos);
1931 2092
1932 bool tree_evaluator::is_global (const std::string& name) const 2093 file_full_name = sys::env::make_absolute (file_full_name);
1933 { 2094
1934 std::shared_ptr<stack_frame> frame 2095 unwind_protect frame;
1935 = m_call_stack.get_current_stack_frame (); 2096
1936 2097 if (source_call_depth.find (file_full_name) == source_call_depth.end ())
1937 return frame->is_global (name); 2098 source_call_depth[file_full_name] = -1;
1938 } 2099
1939 2100 frame.protect_var (source_call_depth[file_full_name]);
1940 octave_value 2101
1941 tree_evaluator::varval (const symbol_record& sym) const 2102 source_call_depth[file_full_name]++;
1942 { 2103
1943 std::shared_ptr<stack_frame> frame 2104 if (source_call_depth[file_full_name] >= max_recursion_depth ())
1944 = m_call_stack.get_current_stack_frame (); 2105 error ("max_recursion_depth exceeded");
1945 2106
1946 return frame->varval (sym); 2107 if (! context.empty ())
1947 } 2108 {
1948 2109 frame.add (&call_stack::restore_frame, &m_call_stack,
1949 octave_value 2110 m_call_stack.current_frame ());
1950 tree_evaluator::varval (const std::string& name) const 2111
1951 { 2112 if (context == "caller")
1952 std::shared_ptr<stack_frame> frame 2113 m_call_stack.goto_caller_frame ();
1953 = m_call_stack.get_current_stack_frame (); 2114 else if (context == "base")
1954 2115 m_call_stack.goto_base_frame ();
1955 return frame->varval (name); 2116 else
1956 } 2117 error (R"(source: CONTEXT must be "caller" or "base")");
1957 2118 }
1958 void tree_evaluator::install_variable (const std::string& name, 2119
1959 const octave_value& value, 2120 // Find symbol name that would be in symbol_table, if it were loaded.
1960 bool global) 2121 std::size_t dir_end
1961 { 2122 = file_name.find_last_of (sys::file_ops::dir_sep_chars ());
1962 std::shared_ptr<stack_frame> frame 2123 dir_end = (dir_end == std::string::npos) ? 0 : dir_end + 1;
1963 = m_call_stack.get_current_stack_frame (); 2124
1964 2125 std::size_t extension = file_name.find_last_of ('.');
1965 return frame->install_variable (name, value, global); 2126 if (extension == std::string::npos)
1966 } 2127 extension = file_name.length ();
1967 2128
1968 octave_value 2129 std::string symbol = file_name.substr (dir_end, extension - dir_end);
1969 tree_evaluator::global_varval (const std::string& name) const 2130 std::string full_name = sys::canonicalize_file_name (file_name);
1970 { 2131
1971 return m_call_stack.global_varval (name); 2132 // Check if this file is already loaded (or in the path)
1972 } 2133 symbol_table& symtab = m_interpreter.get_symbol_table ();
1973 2134 octave_value ov_code = symtab.fcn_table_find (symbol);
1974 octave_value& 2135
1975 tree_evaluator::global_varref (const std::string& name) 2136 // For compatibility with Matlab, accept both scripts and
1976 { 2137 // functions.
1977 return m_call_stack.global_varref (name); 2138
1978 } 2139 if (ov_code.is_user_code ())
1979 2140 {
1980 void 2141 octave_user_code *code = ov_code.user_code_value ();
1981 tree_evaluator::global_assign (const std::string& name, 2142
1982 const octave_value& val) 2143 if (! code
1983 { 2144 || (sys::canonicalize_file_name (code->fcn_file_name ())
1984 m_call_stack.global_varref (name) = val; 2145 != full_name))
1985 } 2146 {
1986 2147 // Wrong file, so load it below.
1987 octave_value 2148 ov_code = octave_value ();
1988 tree_evaluator::top_level_varval (const std::string& name) const 2149 }
1989 { 2150 }
1990 return m_call_stack.get_top_level_value (name); 2151 else
1991 } 2152 {
1992 2153 // Not a script, so load it below.
1993 void 2154 ov_code = octave_value ();
1994 tree_evaluator::top_level_assign (const std::string& name, 2155 }
1995 const octave_value& val) 2156
1996 { 2157 // If no symbol of this name, or the symbol is for a different
1997 m_call_stack.set_top_level_value (name, val); 2158 // file, load.
1998 } 2159
1999 2160 if (ov_code.is_undefined ())
2000 void 2161 {
2001 tree_evaluator::assign (const std::string& name, const octave_value& val) 2162 try
2002 { 2163 {
2003 std::shared_ptr<stack_frame> frame 2164 ov_code = parse_fcn_file (m_interpreter, file_full_name,
2004 = m_call_stack.get_current_stack_frame (); 2165 file_name, dir_name, "", "",
2005 2166 require_file, true, false, false);
2006 frame->assign (name, val); 2167 }
2007 } 2168 catch (execution_exception& ee)
2008 2169 {
2009 void 2170 error (ee, "source: error sourcing file '%s'",
2010 tree_evaluator::assignin (const std::string& context, 2171 file_full_name.c_str ());
2011 const std::string& name, const octave_value& val) 2172 }
2012 { 2173 }
2013 // FIXME: Can this be done without an unwind-protect frame, simply 2174
2014 // by getting a reference to the caller or base stack frame and 2175 // Return or error if we don't have a valid script or function.
2015 // calling assign on that? 2176
2016 2177 if (ov_code.is_undefined ())
2017 unwind_action act ([=] (std::size_t frm) 2178 return;
2018 { 2179
2019 m_call_stack.restore_frame (frm); 2180 if (! ov_code.is_user_code ())
2020 }, m_call_stack.current_frame ()); 2181 error ("source: %s is not a script", full_name.c_str ());
2021 2182
2022 if (context == "caller") 2183 if (verbose)
2023 m_call_stack.goto_caller_frame (); 2184 {
2024 else if (context == "base") 2185 octave_stdout << "executing commands from " << full_name << " ... ";
2025 m_call_stack.goto_base_frame (); 2186 octave_stdout.flush ();
2026 else 2187 }
2027 error (R"(assignin: CONTEXT must be "caller" or "base")"); 2188
2028 2189 octave_user_code *code = ov_code.user_code_value ();
2029 if (valid_identifier (name)) 2190
2030 { 2191 code->call (*this, 0, octave_value_list ());
2031 // Put the check here so that we don't slow down assignments 2192
2032 // generally. Any that go through Octave's parser should have 2193 if (verbose)
2033 // already been checked. 2194 octave_stdout << "done." << std::endl;
2034 2195 }
2035 if (iskeyword (name)) 2196
2036 error ("assignin: invalid assignment to keyword '%s'", 2197 void
2037 name.c_str ()); 2198 tree_evaluator::set_auto_fcn_var (stack_frame::auto_var_type avt,
2038 2199 const octave_value& val)
2039 assign (name, val); 2200 {
2040 } 2201 m_call_stack.set_auto_fcn_var (avt, val);
2041 else 2202 }
2042 error ("assignin: invalid variable name '%s'", name.c_str ()); 2203
2043 } 2204 octave_value
2044 2205 tree_evaluator::get_auto_fcn_var (stack_frame::auto_var_type avt) const
2045 void 2206 {
2046 tree_evaluator::source_file (const std::string& file_name, 2207 return m_call_stack.get_auto_fcn_var (avt);
2047 const std::string& context, 2208 }
2048 bool verbose, bool require_file) 2209
2049 { 2210 void
2050 // Map from absolute name of script file to recursion level. We 2211 tree_evaluator::define_parameter_list_from_arg_vector
2051 // use a map instead of simply placing a limit on recursion in the 2212 (tree_parameter_list *param_list, const octave_value_list& args)
2052 // source_file function so that two mutually recursive scripts 2213 {
2053 // written as 2214 if (! param_list || param_list->varargs_only ())
2054 // 2215 return;
2055 // foo1.m: 2216
2056 // ------ 2217 int i = -1;
2057 // foo2 2218
2058 // 2219 for (tree_decl_elt *elt : *param_list)
2059 // foo2.m: 2220 {
2060 // ------ 2221 i++;
2061 // foo1 2222
2062 // 2223 octave_lvalue ref = elt->lvalue (*this);
2063 // and called with 2224
2064 // 2225 if (i < args.length ())
2065 // foo1 2226 {
2066 // 2227 if (args(i).is_defined () && args(i).is_magic_colon ())
2067 // (for example) will behave the same if they are written as 2228 {
2068 // 2229 if (! eval_decl_elt (elt))
2069 // foo1.m: 2230 error ("no default value for argument %d", i+1);
2070 // ------ 2231 }
2071 // source ("foo2.m") 2232 else
2072 // 2233 ref.define (args(i));
2073 // foo2.m: 2234 }
2074 // ------ 2235 else
2075 // source ("foo1.m") 2236 eval_decl_elt (elt);
2076 // 2237 }
2077 // and called with 2238 }
2078 // 2239
2079 // source ("foo1.m") 2240 void
2080 // 2241 tree_evaluator::undefine_parameter_list (tree_parameter_list *param_list)
2081 // (for example). 2242 {
2082 2243 for (tree_decl_elt *elt : *param_list)
2083 static std::map<std::string, int> source_call_depth; 2244 {
2084 2245 octave_lvalue ref = elt->lvalue (*this);
2085 std::string file_full_name 2246
2086 = sys::file_ops::tilde_expand (file_name); 2247 ref.assign (octave_value::op_asn_eq, octave_value ());
2087 2248 }
2088 std::size_t pos 2249 }
2089 = file_full_name.find_last_of (sys::file_ops::dir_sep_str ());
2090
2091 std::string dir_name = file_full_name.substr (0, pos);
2092
2093 file_full_name = sys::env::make_absolute (file_full_name);
2094
2095 unwind_protect frame;
2096
2097 if (source_call_depth.find (file_full_name) == source_call_depth.end ())
2098 source_call_depth[file_full_name] = -1;
2099
2100 frame.protect_var (source_call_depth[file_full_name]);
2101
2102 source_call_depth[file_full_name]++;
2103
2104 if (source_call_depth[file_full_name] >= max_recursion_depth ())
2105 error ("max_recursion_depth exceeded");
2106
2107 if (! context.empty ())
2108 {
2109 frame.add (&call_stack::restore_frame, &m_call_stack,
2110 m_call_stack.current_frame ());
2111
2112 if (context == "caller")
2113 m_call_stack.goto_caller_frame ();
2114 else if (context == "base")
2115 m_call_stack.goto_base_frame ();
2116 else
2117 error (R"(source: CONTEXT must be "caller" or "base")");
2118 }
2119
2120 // Find symbol name that would be in symbol_table, if it were loaded.
2121 std::size_t dir_end
2122 = file_name.find_last_of (sys::file_ops::dir_sep_chars ());
2123 dir_end = (dir_end == std::string::npos) ? 0 : dir_end + 1;
2124
2125 std::size_t extension = file_name.find_last_of ('.');
2126 if (extension == std::string::npos)
2127 extension = file_name.length ();
2128
2129 std::string symbol = file_name.substr (dir_end, extension - dir_end);
2130 std::string full_name = sys::canonicalize_file_name (file_name);
2131
2132 // Check if this file is already loaded (or in the path)
2133 symbol_table& symtab = m_interpreter.get_symbol_table ();
2134 octave_value ov_code = symtab.fcn_table_find (symbol);
2135
2136 // For compatibility with Matlab, accept both scripts and
2137 // functions.
2138
2139 if (ov_code.is_user_code ())
2140 {
2141 octave_user_code *code = ov_code.user_code_value ();
2142
2143 if (! code
2144 || (sys::canonicalize_file_name (code->fcn_file_name ())
2145 != full_name))
2146 {
2147 // Wrong file, so load it below.
2148 ov_code = octave_value ();
2149 }
2150 }
2151 else
2152 {
2153 // Not a script, so load it below.
2154 ov_code = octave_value ();
2155 }
2156
2157 // If no symbol of this name, or the symbol is for a different
2158 // file, load.
2159
2160 if (ov_code.is_undefined ())
2161 {
2162 try
2163 {
2164 ov_code = parse_fcn_file (m_interpreter, file_full_name,
2165 file_name, dir_name, "", "",
2166 require_file, true, false, false);
2167 }
2168 catch (execution_exception& ee)
2169 {
2170 error (ee, "source: error sourcing file '%s'",
2171 file_full_name.c_str ());
2172 }
2173 }
2174
2175 // Return or error if we don't have a valid script or function.
2176
2177 if (ov_code.is_undefined ())
2178 return;
2179
2180 if (! ov_code.is_user_code ())
2181 error ("source: %s is not a script", full_name.c_str ());
2182
2183 if (verbose)
2184 {
2185 octave_stdout << "executing commands from " << full_name << " ... ";
2186 octave_stdout.flush ();
2187 }
2188
2189 octave_user_code *code = ov_code.user_code_value ();
2190
2191 code->call (*this, 0, octave_value_list ());
2192
2193 if (verbose)
2194 octave_stdout << "done." << std::endl;
2195 }
2196
2197 void
2198 tree_evaluator::set_auto_fcn_var (stack_frame::auto_var_type avt,
2199 const octave_value& val)
2200 {
2201 m_call_stack.set_auto_fcn_var (avt, val);
2202 }
2203
2204 octave_value
2205 tree_evaluator::get_auto_fcn_var (stack_frame::auto_var_type avt) const
2206 {
2207 return m_call_stack.get_auto_fcn_var (avt);
2208 }
2209
2210 void
2211 tree_evaluator::define_parameter_list_from_arg_vector
2212 (tree_parameter_list *param_list, const octave_value_list& args)
2213 {
2214 if (! param_list || param_list->varargs_only ())
2215 return;
2216
2217 int i = -1;
2218
2219 for (tree_decl_elt *elt : *param_list)
2220 {
2221 i++;
2222
2223 octave_lvalue ref = elt->lvalue (*this);
2224
2225 if (i < args.length ())
2226 {
2227 if (args(i).is_defined () && args(i).is_magic_colon ())
2228 {
2229 if (! eval_decl_elt (elt))
2230 error ("no default value for argument %d", i+1);
2231 }
2232 else
2233 ref.define (args(i));
2234 }
2235 else
2236 eval_decl_elt (elt);
2237 }
2238 }
2239
2240 void
2241 tree_evaluator::undefine_parameter_list (tree_parameter_list *param_list)
2242 {
2243 for (tree_decl_elt *elt : *param_list)
2244 {
2245 octave_lvalue ref = elt->lvalue (*this);
2246
2247 ref.assign (octave_value::op_asn_eq, octave_value ());
2248 }
2249 }
2250 2250
2251 // END is documented in op-kw-docs. 2251 // END is documented in op-kw-docs.
2252 DEFMETHOD (end, interp, args, , 2252 DEFMETHOD (end, interp, args, ,
2253 doc: /* -*- texinfo -*- 2253 doc: /* -*- texinfo -*-
2254 @deftypefn {} {} end 2254 @deftypefn {} {} end
2290 %! assert (x(end), 10); 2290 %! assert (x(end), 10);
2291 %! assert (x(minus (end, 1)), 9); 2291 %! assert (x(minus (end, 1)), 9);
2292 %! assert (x(minus (minus (end, 1), 1)), 8); 2292 %! assert (x(minus (minus (end, 1), 1)), 8);
2293 */ 2293 */
2294 2294
2295 octave_value_list 2295 octave_value_list
2296 tree_evaluator::convert_to_const_vector (tree_argument_list *args) 2296 tree_evaluator::convert_to_const_vector (tree_argument_list *args)
2297 {
2298 std::list<octave_value> arg_vals;
2299
2300 for (auto elt : *args)
2301 {
2302 // FIXME: is it possible for elt to be invalid?
2303
2304 if (! elt)
2305 break;
2306
2307 octave_value tmp = elt->evaluate (*this);
2308
2309 if (tmp.is_cs_list ())
2310 {
2311 octave_value_list tmp_ovl = tmp.list_value ();
2312
2313 for (octave_idx_type i = 0; i < tmp_ovl.length (); i++)
2314 arg_vals.push_back (tmp_ovl(i));
2315 }
2316 else if (tmp.is_defined ())
2317 arg_vals.push_back (tmp);
2318 }
2319
2320 return octave_value_list (arg_vals);
2321 }
2322
2323 octave_value_list
2324 tree_evaluator::convert_return_list_to_const_vector
2325 (tree_parameter_list *ret_list, int nargout, const Matrix& ignored_outputs,
2326 const Cell& varargout)
2327 {
2328 octave_idx_type vlen = varargout.numel ();
2329 int len = ret_list->length ();
2330
2331 // Special case. Will do a shallow copy.
2332 if (len == 0)
2333 return varargout;
2334 else
2335 {
2336 int i = 0;
2337 int k = 0;
2338 int num_ignored = ignored_outputs.numel ();
2339 int ignored = num_ignored > 0 ? ignored_outputs(k) - 1 : -1;
2340
2341 if (nargout <= len)
2342 {
2343 int nout = nargout > 0 ? nargout : 1;
2344 octave_value_list retval (nout);
2345
2346 for (tree_decl_elt *elt : *ret_list)
2347 {
2348 if (nargout == 0 && ! is_defined (elt->ident ()))
2349 break;
2350
2351 if (ignored >= 0 && i == ignored)
2352 {
2353 i++;
2354 k++;
2355 ignored = k < num_ignored ? ignored_outputs(k) - 1 : -1;
2356 }
2357 else
2358 retval(i++) = evaluate (elt);
2359
2360 if (i == nout)
2361 break;
2362 }
2363
2364 return retval;
2365 }
2366 else
2367 {
2368 octave_value_list retval (len + vlen);
2369
2370 for (tree_decl_elt *elt : *ret_list)
2371 {
2372 if (ignored >= 0 && i == ignored)
2373 {
2374 i++;
2375 k++;
2376 ignored = k < num_ignored ? ignored_outputs(k) - 1 : -1;
2377 }
2378 else
2379 retval(i++) = evaluate (elt);
2380 }
2381
2382 for (octave_idx_type j = 0; j < vlen; j++)
2383 retval(i++) = varargout(j);
2384
2385 return retval;
2386 }
2387 }
2388 }
2389
2390 bool
2391 tree_evaluator::eval_decl_elt (tree_decl_elt *elt)
2392 {
2393 bool retval = false;
2394
2395 tree_identifier *id = elt->ident ();
2396 tree_expression *expr = elt->expression ();
2397
2398 if (id && expr)
2399 {
2400 octave_lvalue ult = id->lvalue (*this);
2401
2402 octave_value init_val = expr->evaluate (*this);
2403
2404 ult.assign (octave_value::op_asn_eq, init_val);
2405
2406 retval = true;
2407 }
2408
2409 return retval;
2410 }
2411
2412 bool
2413 tree_evaluator::switch_case_label_matches (tree_switch_case *expr,
2414 const octave_value& val)
2415 {
2416 tree_expression *label = expr->case_label ();
2417
2418 octave_value label_value = label->evaluate (*this);
2419
2420 if (label_value.is_defined ())
2421 {
2422 if (label_value.iscell ())
2423 {
2424 Cell cell (label_value.cell_value ());
2425
2426 for (octave_idx_type i = 0; i < cell.rows (); i++)
2427 {
2428 for (octave_idx_type j = 0; j < cell.columns (); j++)
2429 {
2430 bool match = val.is_equal (cell(i, j));
2431
2432 if (match)
2433 return true;
2434 }
2435 }
2436 }
2437 else
2438 return val.is_equal (label_value);
2439 }
2440
2441 return false;
2442 }
2443
2444 void tree_evaluator::push_stack_frame (const symbol_scope& scope)
2445 {
2446 m_call_stack.push (scope);
2447 }
2448
2449 void tree_evaluator::push_stack_frame (octave_user_function *fcn,
2450 const std::shared_ptr<stack_frame>& closure_frames)
2451 {
2452 m_call_stack.push (fcn, closure_frames);
2453 }
2454
2455 void tree_evaluator::push_stack_frame (octave_user_function *fcn,
2456 const stack_frame::local_vars_map& local_vars,
2457 const std::shared_ptr<stack_frame>& closure_frames)
2458 {
2459 m_call_stack.push (fcn, local_vars, closure_frames);
2460 }
2461
2462 void tree_evaluator::push_stack_frame (octave_user_script *script)
2463 {
2464 m_call_stack.push (script);
2465 }
2466
2467 void tree_evaluator::push_stack_frame (octave_function *fcn)
2468 {
2469 m_call_stack.push (fcn);
2470 }
2471
2472 void tree_evaluator::pop_stack_frame (void)
2473 {
2474 m_call_stack.pop ();
2475 }
2476
2477 int tree_evaluator::current_line (void) const
2478 {
2479 return m_call_stack.current_line ();
2480 }
2481
2482 int tree_evaluator::current_column (void) const
2483 {
2484 return m_call_stack.current_column ();
2485 }
2486
2487 int tree_evaluator::debug_user_code_line (void) const
2488 {
2489 return m_call_stack.debug_user_code_line ();
2490 }
2491
2492 int tree_evaluator::debug_user_code_column (void) const
2493 {
2494 return m_call_stack.debug_user_code_column ();
2495 }
2496
2497 void tree_evaluator::debug_where (std::ostream& os) const
2498 {
2499 std::shared_ptr<stack_frame> frm = m_call_stack.current_user_frame ();
2500
2501 frm->display_stopped_in_message (os);
2502 }
2503
2504 octave_user_code *tree_evaluator::current_user_code (void) const
2505 {
2506 return m_call_stack.current_user_code ();
2507 }
2508
2509 unwind_protect *tree_evaluator::curr_fcn_unwind_protect_frame (void)
2510 {
2511 return m_call_stack.curr_fcn_unwind_protect_frame ();
2512 }
2513
2514 octave_user_code *tree_evaluator::debug_user_code (void) const
2515 {
2516 return m_call_stack.debug_user_code ();
2517 }
2518
2519 octave_function *tree_evaluator::current_function (bool skip_first) const
2520 {
2521 return m_call_stack.current_function (skip_first);
2522 }
2523
2524 octave_function *tree_evaluator::caller_function (void) const
2525 {
2526 return m_call_stack.current_function (true);
2527 }
2528
2529 bool tree_evaluator::goto_frame (std::size_t n, bool verbose)
2530 {
2531 return m_call_stack.goto_frame (n, verbose);
2532 }
2533
2534 void tree_evaluator::goto_caller_frame (void)
2535 {
2536 m_call_stack.goto_caller_frame ();
2537 }
2538
2539 void tree_evaluator::goto_base_frame (void)
2540 {
2541 m_call_stack.goto_base_frame ();
2542 }
2543
2544 void tree_evaluator::restore_frame (std::size_t n)
2545 {
2546 return m_call_stack.restore_frame (n);
2547 }
2548
2549 std::string tree_evaluator::get_dispatch_class (void) const
2550 {
2551 return m_call_stack.get_dispatch_class ();
2552 }
2553
2554 void tree_evaluator::set_dispatch_class (const std::string& class_name)
2555 {
2556 m_call_stack.set_dispatch_class (class_name);
2557 }
2558
2559 bool
2560 tree_evaluator::is_class_method_executing (std::string& dclass) const
2561 {
2562 return m_call_stack.is_class_method_executing (dclass);
2563 }
2564
2565 bool
2566 tree_evaluator::is_class_constructor_executing (std::string& dclass) const
2567 {
2568 return m_call_stack.is_class_constructor_executing (dclass);
2569 }
2570
2571 std::list<std::shared_ptr<stack_frame>>
2572 tree_evaluator::backtrace_frames (octave_idx_type& curr_user_frame) const
2573 {
2574 return m_call_stack.backtrace_frames (curr_user_frame);
2575 }
2576
2577 std::list<std::shared_ptr<stack_frame>>
2578 tree_evaluator::backtrace_frames (void) const
2579 {
2580 return m_call_stack.backtrace_frames ();
2581 }
2582
2583 std::list<frame_info>
2584 tree_evaluator::backtrace_info (octave_idx_type& curr_user_frame,
2585 bool print_subfn) const
2586 {
2587 return m_call_stack.backtrace_info (curr_user_frame, print_subfn);
2588 }
2589
2590 std::list<frame_info> tree_evaluator::backtrace_info (void) const
2591 {
2592 return m_call_stack.backtrace_info ();
2593 }
2594
2595 octave_map
2596 tree_evaluator::backtrace (octave_idx_type& curr_user_frame,
2597 bool print_subfn) const
2598 {
2599 return m_call_stack.backtrace (curr_user_frame, print_subfn);
2600 }
2601
2602 octave_map tree_evaluator::backtrace (void) const
2603 {
2604 return m_call_stack.backtrace ();
2605 }
2606
2607 octave_map tree_evaluator::empty_backtrace (void) const
2608 {
2609 return m_call_stack.empty_backtrace ();
2610 }
2611
2612 std::string tree_evaluator::backtrace_message (void) const
2613 {
2614 std::list<frame_info> frames = backtrace_info ();
2615
2616 std::ostringstream buf;
2617
2618 for (const auto& frm : frames)
2619 {
2620 buf << " " << frm.fcn_name ();
2621
2622 int line = frm.line ();
2623
2624 if (line > 0)
2625 {
2626 buf << " at line " << line;
2627
2628 int column = frm.column ();
2629
2630 if (column > 0)
2631 buf << " column " << column;
2632
2633 buf << "\n";
2634 }
2635 }
2636
2637 return buf.str ();
2638 }
2639
2640 void tree_evaluator::push_dummy_scope (const std::string& name)
2641 {
2642 symbol_scope dummy_scope (name + "$dummy");
2643
2644 m_call_stack.push (dummy_scope);
2645 }
2646
2647 void tree_evaluator::pop_scope (void)
2648 {
2649 m_call_stack.pop ();
2650 }
2651
2652 symbol_scope tree_evaluator::get_top_scope (void) const
2653 {
2654 return m_call_stack.top_scope ();
2655 }
2656
2657 symbol_scope tree_evaluator::get_current_scope (void) const
2658 {
2659 return m_call_stack.current_scope ();
2660 }
2661
2662 void tree_evaluator::mlock (bool skip_first) const
2663 {
2664 octave_function *fcn = m_call_stack.current_function (skip_first);
2665
2666 if (! fcn)
2667 error ("mlock: invalid use outside a function");
2668
2669 if (fcn->is_builtin_function ())
2670 {
2671 warning ("mlock: locking built-in function has no effect");
2672 return;
2673 }
2674
2675 fcn->lock ();
2676 }
2677
2678 void tree_evaluator::munlock (bool skip_first) const
2679 {
2680 octave_function *fcn = m_call_stack.current_function (skip_first);
2681
2682 if (! fcn)
2683 error ("munlock: invalid use outside a function");
2684
2685 if (fcn->is_builtin_function ())
2686 {
2687 warning ("munlock: unlocking built-in function has no effect");
2688 return;
2689 }
2690
2691 fcn->unlock ();
2692 }
2693
2694 bool tree_evaluator::mislocked (bool skip_first) const
2695 {
2696 octave_function *fcn = m_call_stack.current_function (skip_first);
2697
2698 if (! fcn)
2699 error ("mislocked: invalid use outside a function");
2700
2701 return fcn->islocked ();
2702 }
2703
2704 octave_value
2705 tree_evaluator::max_stack_depth (const octave_value_list& args, int nargout)
2706 {
2707 return m_call_stack.max_stack_depth (args, nargout);
2708 }
2709
2710 void tree_evaluator::display_call_stack (void) const
2711 {
2712 m_call_stack.display ();
2713 }
2714
2715 octave_value tree_evaluator::find (const std::string& name)
2716 {
2717 std::shared_ptr<stack_frame> frame
2718 = m_call_stack.get_current_stack_frame ();
2719
2720 octave_value val = frame->varval (name);
2721
2722 if (val.is_defined ())
2723 return val;
2724
2725 // Subfunction. I think it only makes sense to check for
2726 // subfunctions if we are currently executing a function defined
2727 // from a .m file.
2728
2729 octave_value fcn = frame->find_subfunction (name);
2730
2731 if (fcn.is_defined ())
2732 return fcn;
2733
2734 symbol_table& symtab = m_interpreter.get_symbol_table ();
2735
2736 return symtab.fcn_table_find (name, ovl ());
2737 }
2738
2739 void tree_evaluator::clear_objects (void)
2740 {
2741 std::shared_ptr<stack_frame> frame
2742 = m_call_stack.get_current_stack_frame ();
2743
2744 frame->clear_objects ();
2745 }
2746
2747 void tree_evaluator::clear_variable (const std::string& name)
2748 {
2749 std::shared_ptr<stack_frame> frame
2750 = m_call_stack.get_current_stack_frame ();
2751
2752 frame->clear_variable (name);
2753 }
2754
2755 void tree_evaluator::clear_variable_pattern (const std::string& pattern)
2756 {
2757 std::shared_ptr<stack_frame> frame
2758 = m_call_stack.get_current_stack_frame ();
2759
2760 frame->clear_variable_pattern (pattern);
2761 }
2762
2763 void tree_evaluator::clear_variable_regexp (const std::string& pattern)
2764 {
2765 std::shared_ptr<stack_frame> frame
2766 = m_call_stack.get_current_stack_frame ();
2767
2768 frame->clear_variable_regexp (pattern);
2769 }
2770
2771 void tree_evaluator::clear_variables (void)
2772 {
2773 std::shared_ptr<stack_frame> frame
2774 = m_call_stack.get_current_stack_frame ();
2775
2776 frame->clear_variables ();
2777 }
2778
2779 void tree_evaluator::clear_global_variable (const std::string& name)
2780 {
2781 m_call_stack.clear_global_variable (name);
2782 }
2783
2784 void
2785 tree_evaluator::clear_global_variable_pattern (const std::string& pattern)
2786 {
2787 m_call_stack.clear_global_variable_pattern (pattern);
2788 }
2789
2790 void tree_evaluator::clear_global_variable_regexp(const std::string& pattern)
2791 {
2792 m_call_stack.clear_global_variable_regexp (pattern);
2793 }
2794
2795 void tree_evaluator::clear_global_variables (void)
2796 {
2797 m_call_stack.clear_global_variables ();
2798 }
2799
2800 void tree_evaluator::clear_all (bool force)
2801 {
2802 // FIXME: should this also clear objects?
2803
2804 clear_variables ();
2805 clear_global_variables ();
2806
2807 symbol_table& symtab = m_interpreter.get_symbol_table ();
2808
2809 symtab.clear_functions (force);
2810 }
2811
2812 void tree_evaluator::clear_symbol (const std::string& name)
2813 {
2814 // FIXME: are we supposed to do both here?
2815
2816 clear_variable (name);
2817
2818 symbol_table& symtab = m_interpreter.get_symbol_table ();
2819
2820 symtab.clear_function (name);
2821 }
2822
2823 void tree_evaluator::clear_symbol_pattern (const std::string& pattern)
2824 {
2825 // FIXME: are we supposed to do both here?
2826
2827 clear_variable_pattern (pattern);
2828
2829 symbol_table& symtab = m_interpreter.get_symbol_table ();
2830
2831 symtab.clear_function_pattern (pattern);
2832 }
2833
2834 void tree_evaluator::clear_symbol_regexp (const std::string& pattern)
2835 {
2836 // FIXME: are we supposed to do both here?
2837
2838 clear_variable_regexp (pattern);
2839
2840 symbol_table& symtab = m_interpreter.get_symbol_table ();
2841
2842 symtab.clear_function_regexp (pattern);
2843 }
2844
2845 std::list<std::string> tree_evaluator::global_variable_names (void) const
2846 {
2847 return m_call_stack.global_variable_names ();
2848 }
2849
2850 std::list<std::string> tree_evaluator::top_level_variable_names (void) const
2851 {
2852 return m_call_stack.top_level_variable_names ();
2853 }
2854
2855 std::list<std::string> tree_evaluator::variable_names (void) const
2856 {
2857 return m_call_stack.variable_names ();
2858 }
2859
2860 // Return a pointer to the user-defined function FNAME. If FNAME is empty,
2861 // search backward for the first user-defined function in the
2862 // current call stack.
2863
2864 octave_user_code *
2865 tree_evaluator::get_user_code (const std::string& fname,
2866 const std::string& class_name)
2867 {
2868 octave_user_code *user_code = nullptr;
2869
2870 if (fname.empty ())
2871 user_code = m_call_stack.debug_user_code ();
2872 else
2873 {
2874 std::string name = fname;
2875
2876 if (sys::file_ops::dir_sep_char () != '/' && name[0] == '@')
2877 {
2878 auto beg = name.begin () + 2; // never have @/method
2879 auto end = name.end () - 1; // never have trailing '/'
2880 std::replace (beg, end, '/', sys::file_ops::dir_sep_char ());
2881 }
2882
2883 std::size_t name_len = name.length ();
2884
2885 if (name_len > 2 && name.substr (name_len-2) == ".m")
2886 name = name.substr (0, name_len-2);
2887
2888 if (name.empty ())
2889 return nullptr;
2890
2891 symbol_table& symtab = m_interpreter.get_symbol_table ();
2892
2893 octave_value fcn;
2894 std::size_t p2 = std::string::npos;
2895
2896 if (name[0] == '@')
2897 {
2898 std::size_t p1 = name.find (sys::file_ops::dir_sep_char (), 1);
2899
2900 if (p1 == std::string::npos)
2901 return nullptr;
2902
2903 std::string dispatch_type = name.substr (1, p1-1);
2904
2905 p2 = name.find ('>', p1);
2906
2907 std::string method = name.substr (p1+1, p2-1);
2908
2909 fcn = symtab.find_method (method, dispatch_type);
2910 }
2911 else if (! class_name.empty ())
2912 {
2913 cdef_manager& cdm = m_interpreter.get_cdef_manager ();
2914
2915 fcn = cdm.find_method (class_name, name);
2916
2917 // If there is no classdef method, then try legacy classes.
2918 if (fcn.is_undefined ())
2919 fcn = symtab.find_method (name, class_name);
2920 }
2921 else
2922 {
2923 p2 = name.find ('>');
2924
2925 std::string main_fcn = name.substr (0, p2);
2926
2927 fcn = symtab.find_function (main_fcn);
2928 }
2929
2930 // List of function names sub1>sub2>...
2931 std::string subfuns;
2932
2933 if (p2 != std::string::npos)
2934 subfuns = name.substr (p2+1);
2935
2936 if (fcn.is_defined () && fcn.is_user_code ())
2937 user_code = fcn.user_code_value ();
2938
2939 if (! user_code || subfuns.empty ())
2940 return user_code;
2941
2942 fcn = user_code->find_subfunction (subfuns);
2943
2944 if (fcn.is_undefined ())
2945 return nullptr;
2946
2947 user_code = fcn.user_code_value ();
2948 }
2949
2950 return user_code;
2951 }
2952
2953 std::string
2954 tree_evaluator::current_function_name (bool skip_first) const
2955 {
2956 octave_function *curfcn = m_call_stack.current_function (skip_first);
2957
2958 if (curfcn)
2959 return curfcn->name ();
2960
2961 return "";
2962 }
2963
2964 bool
2965 tree_evaluator::in_user_code (void) const
2966 {
2967 return m_call_stack.current_user_code () != nullptr;
2968 }
2969
2970 void
2971 tree_evaluator::visit_decl_command (tree_decl_command& cmd)
2972 {
2973 if (m_echo_state)
2974 {
2975 int line = cmd.line ();
2976 if (line < 0)
2977 line = 1;
2978 echo_code (line);
2979 m_echo_file_pos = line + 1;
2980 }
2981
2982 if (m_debug_mode)
2983 do_breakpoint (cmd.is_active_breakpoint (*this));
2984
2985 // FIXME: tree_decl_init_list is not derived from tree, so should it
2986 // really have an accept method?
2987
2988 tree_decl_init_list *init_list = cmd.initializer_list ();
2989
2990 if (init_list)
2991 init_list->accept (*this);
2992 }
2993
2994 void
2995 tree_evaluator::visit_decl_elt (tree_decl_elt& elt)
2996 {
2997 tree_identifier *id = elt.ident ();
2998
2999 if (id)
3000 {
3001 if (elt.is_global ())
3002 m_call_stack.make_global (id->symbol ());
3003 else if (elt.is_persistent ())
3004 m_call_stack.make_persistent (id->symbol ());
3005 else
3006 error ("declaration list element not global or persistent");
3007
3008 octave_lvalue ult = id->lvalue (*this);
3009
3010 if (ult.is_undefined ())
3011 {
3012 tree_expression *expr = elt.expression ();
3013
3014 octave_value init_val;
3015
3016 if (expr)
3017 init_val = expr->evaluate (*this);
3018 else
3019 init_val = Matrix ();
3020
3021 ult.assign (octave_value::op_asn_eq, init_val);
3022 }
3023 }
3024 }
3025
3026 template <typename T>
3027 void
3028 tree_evaluator::execute_range_loop (const range<T>& rng, int line,
3029 octave_lvalue& ult,
3030 tree_statement_list *loop_body)
3031 {
3032 octave_idx_type steps = rng.numel ();
3033
3034 if (math::isinf (rng.limit ()))
3035 warning_with_id ("Octave:infinite-loop",
3036 "FOR loop limit is infinite, will stop after %"
3037 OCTAVE_IDX_TYPE_FORMAT " steps", steps);
3038
3039 for (octave_idx_type i = 0; i < steps; i++)
3040 {
3041 if (m_echo_state)
3042 m_echo_file_pos = line;
3043
3044 octave_value val (rng.elem (i));
3045
3046 ult.assign (octave_value::op_asn_eq, val);
3047
3048 if (loop_body)
3049 loop_body->accept (*this);
3050
3051 if (quit_loop_now ())
3052 break;
3053 }
3054 }
3055
3056 void
3057 tree_evaluator::visit_simple_for_command (tree_simple_for_command& cmd)
3058 {
3059 int line = cmd.line ();
3060 if (line < 0)
3061 line = 1;
3062
3063 if (m_echo_state)
3064 {
3065 echo_code (line);
3066 line++;
3067 }
3068
3069 if (m_debug_mode)
3070 do_breakpoint (cmd.is_active_breakpoint (*this));
3071
3072 // FIXME: need to handle PARFOR loops here using cmd.in_parallel ()
3073 // and cmd.maxproc_expr ();
3074
3075 unwind_protect_var<bool> upv (m_in_loop_command, true);
3076
3077 tree_expression *expr = cmd.control_expr ();
3078
3079 octave_value rhs = expr->evaluate (*this);
3080
3081 if (rhs.is_undefined ())
3082 return;
3083
3084 tree_expression *lhs = cmd.left_hand_side ();
3085
3086 octave_lvalue ult = lhs->lvalue (*this);
3087
3088 tree_statement_list *loop_body = cmd.body ();
3089
3090 if (rhs.is_range ())
3091 {
3092 // FIXME: is there a better way to dispatch here?
3093
3094 if (rhs.is_double_type ())
3095 {
3096 execute_range_loop (rhs.range_value (), line, ult, loop_body);
3097 return;
3098 }
3099
3100 // For now, disable all but range<double>.
3101
3102 #if 0
3103 if (rhs.is_int64_type ())
3104 {
3105 execute_range_loop (rhs.int64_range_value (), line, ult, loop_body);
3106 return;
3107 }
3108
3109 if (rhs.is_uint64_type ())
3110 {
3111 execute_range_loop (rhs.uint64_range_value (), line, ult, loop_body);
3112 return;
3113 }
3114
3115 if (rhs.is_int32_type ())
3116 {
3117 execute_range_loop (rhs.int32_range_value (), line, ult, loop_body);
3118 return;
3119 }
3120
3121 if (rhs.is_uint32_type ())
3122 {
3123 execute_range_loop (rhs.uint32_range_value (), line, ult, loop_body);
3124 return;
3125 }
3126
3127 if (rhs.is_int16_type ())
3128 {
3129 execute_range_loop (rhs.int16_range_value (), line, ult, loop_body);
3130 return;
3131 }
3132
3133 if (rhs.is_uint16_type ())
3134 {
3135 execute_range_loop (rhs.uint16_range_value (), line, ult, loop_body);
3136 return;
3137 }
3138
3139 if (rhs.is_int8_type ())
3140 {
3141 execute_range_loop (rhs.int8_range_value (), line, ult, loop_body);
3142 return;
3143 }
3144
3145 if (rhs.is_uint8_type ())
3146 {
3147 execute_range_loop (rhs.uint8_range_value (), line, ult, loop_body);
3148 return;
3149 }
3150
3151 if (rhs.is_single_type ())
3152 {
3153 execute_range_loop (rhs.float_range_value (), line, ult, loop_body);
3154 return;
3155 }
3156 #endif
3157 }
3158
3159 if (rhs.is_scalar_type ())
3160 {
3161 if (m_echo_state)
3162 m_echo_file_pos = line;
3163
3164 ult.assign (octave_value::op_asn_eq, rhs);
3165
3166 if (loop_body)
3167 loop_body->accept (*this);
3168
3169 // Maybe decrement break and continue states.
3170 quit_loop_now ();
3171
3172 return;
3173 }
3174
3175 // Also handle any range types not explicitly handled above, though
3176 // not as efficiently as the specialized code above.
3177
3178 if (rhs.is_range () || rhs.is_matrix_type () || rhs.iscell ()
3179 || rhs.is_string () || rhs.isstruct ())
3180 {
3181 // A matrix or cell is reshaped to 2 dimensions and iterated by
3182 // columns.
3183
3184 dim_vector dv = rhs.dims ().redim (2);
3185
3186 octave_idx_type nrows = dv(0);
3187 octave_idx_type steps = dv(1);
3188
3189 octave_value arg = rhs;
3190 if (rhs.ndims () > 2)
3191 arg = arg.reshape (dv);
3192
3193 if (nrows > 0 && steps > 0)
3194 {
3195 octave_value_list idx;
3196 octave_idx_type iidx;
3197
3198 // for row vectors, use single index to speed things up.
3199 if (nrows == 1)
3200 {
3201 idx.resize (1);
3202 iidx = 0;
3203 }
3204 else
3205 {
3206 idx.resize (2);
3207 idx(0) = octave_value::magic_colon_t;
3208 iidx = 1;
3209 }
3210
3211 for (octave_idx_type i = 1; i <= steps; i++)
3212 {
3213 if (m_echo_state)
3214 m_echo_file_pos = line;
3215
3216 // index_op expects one-based indices.
3217 idx(iidx) = i;
3218 octave_value val = arg.index_op (idx);
3219
3220 ult.assign (octave_value::op_asn_eq, val);
3221
3222 if (loop_body)
3223 loop_body->accept (*this);
3224
3225 if (quit_loop_now ())
3226 break;
3227 }
3228 }
3229 else
3230 {
3231 // Handle empty cases, while still assigning to loop var.
3232 ult.assign (octave_value::op_asn_eq, arg);
3233 }
3234
3235 return;
3236 }
3237
3238 error ("invalid type in for loop expression near line %d, column %d",
3239 cmd.line (), cmd.column ());
3240 }
3241
3242 void
3243 tree_evaluator::visit_complex_for_command (tree_complex_for_command& cmd)
3244 {
3245 int line = cmd.line ();
3246 if (line < 0)
3247 line = 1;
3248
3249 if (m_echo_state)
3250 {
3251 echo_code (line);
3252 line++;
3253 }
3254
3255 if (m_debug_mode)
3256 do_breakpoint (cmd.is_active_breakpoint (*this));
3257
3258 unwind_protect_var<bool> upv (m_in_loop_command, true);
3259
3260 tree_expression *expr = cmd.control_expr ();
3261
3262 octave_value rhs = expr->evaluate (*this);
3263
3264 if (rhs.is_undefined ())
3265 return;
3266
3267 if (! rhs.isstruct ())
3268 error ("in statement 'for [X, Y] = VAL', VAL must be a structure");
3269
3270 // Cycle through structure elements. First element of id_list
3271 // is set to value and the second is set to the name of the
3272 // structure element.
3273
3274 tree_argument_list *lhs = cmd.left_hand_side ();
3275
3276 auto p = lhs->begin ();
3277
3278 tree_expression *elt = *p++;
3279
3280 octave_lvalue val_ref = elt->lvalue (*this);
3281
3282 elt = *p;
3283
3284 octave_lvalue key_ref = elt->lvalue (*this);
3285
3286 const octave_map tmp_val = rhs.map_value ();
3287
3288 tree_statement_list *loop_body = cmd.body ();
3289
3290 string_vector keys = tmp_val.keys ();
3291
3292 octave_idx_type nel = keys.numel ();
3293
3294 for (octave_idx_type i = 0; i < nel; i++)
3295 {
3296 if (m_echo_state)
3297 m_echo_file_pos = line;
3298
3299 std::string key = keys[i];
3300
3301 const Cell val_lst = tmp_val.contents (key);
3302
3303 octave_idx_type n = val_lst.numel ();
3304
3305 octave_value val = (n == 1) ? val_lst(0) : octave_value (val_lst);
3306
3307 val_ref.assign (octave_value::op_asn_eq, val);
3308 key_ref.assign (octave_value::op_asn_eq, key);
3309
3310 if (loop_body)
3311 loop_body->accept (*this);
3312
3313 if (quit_loop_now ())
3314 break;
3315 }
3316 }
3317
3318 void tree_evaluator::visit_spmd_command (tree_spmd_command& cmd)
3319 {
3320 // For now, we just execute the commands serially.
3321
3322 tree_statement_list *body = cmd.body ();
3323
3324 if (body)
3325 body->accept (*this);
3326 }
3327
3328 octave_value
3329 tree_evaluator::evaluate_anon_fcn_handle (tree_anon_fcn_handle& afh)
3330 {
3331 // FIXME: should CMD_LIST be limited to a single expression?
3332 // I think that is what Matlab does.
3333
3334 symbol_scope new_scope;
3335 symbol_scope scope = afh.scope ();
3336 if (scope)
3337 new_scope = scope.dup ();
3338
3339 tree_parameter_list *param_list = afh.parameter_list ();
3340 tree_parameter_list *param_list_dup
3341 = param_list ? param_list->dup (new_scope) : nullptr;
3342
3343 tree_parameter_list *ret_list = nullptr;
3344
3345 tree_statement_list *stmt_list = nullptr;
3346
3347 symbol_scope parent_scope = get_current_scope ();
3348
3349 new_scope.set_parent (parent_scope);
3350 new_scope.set_primary_parent (parent_scope);
3351
3352 tree_expression *expr = afh.expression ();
3353 if (expr)
3354 {
3355 tree_expression *expr_dup = expr->dup (new_scope);
3356 tree_statement *stmt = new tree_statement (expr_dup, nullptr);
3357 stmt_list = new tree_statement_list (stmt);
3358 }
3359
3360 tree_anon_scopes anon_fcn_ctx (afh);
3361
3362 std::set<std::string> free_vars = anon_fcn_ctx.free_variables ();
3363
3364 stack_frame::local_vars_map local_vars;
3365
3366 std::shared_ptr<stack_frame> frame
3367 = m_call_stack.get_current_stack_frame ();
3368
3369 for (auto& name : free_vars)
3370 {
3371 octave_value val = frame->varval (name);
3372
3373 if (val.is_defined ())
3374 local_vars[name] = val;
3375 }
3376
3377 octave_user_function *af
3378 = new octave_user_function (new_scope, param_list_dup, ret_list,
3379 stmt_list);
3380
3381 octave_function *curr_fcn = m_call_stack.current_function ();
3382
3383 bool is_nested = false;
3384
3385 if (curr_fcn)
3386 {
3387 // FIXME: maybe it would be better to just stash curr_fcn
3388 // instead of individual bits of info about it?
3389
3390 // An anonymous function defined inside another nested function
3391 // or parent of a nested function also behaves like a nested
3392 // function.
3393
3394 if (curr_fcn->is_parent_function () || curr_fcn->is_nested_function ())
3395 {
3396 is_nested = true;
3397 af->mark_as_nested_function ();
3398 new_scope.set_nesting_depth (parent_scope.nesting_depth () + 1);
3399 }
3400
3401 af->stash_dir_name (curr_fcn->dir_name ());
3402
3403 new_scope.cache_fcn_file_name (curr_fcn->fcn_file_name ());
3404 new_scope.cache_dir_name (curr_fcn->dir_name ());
3405
3406 // The following is needed so that class method dispatch works
3407 // properly for anonymous functions that wrap class methods.
3408
3409 if (curr_fcn->is_class_method () || curr_fcn->is_class_constructor ())
3410 af->stash_dispatch_class (curr_fcn->dispatch_class ());
3411
3412 af->stash_fcn_file_name (curr_fcn->fcn_file_name ());
3413 }
3414
3415 af->mark_as_anonymous_function ();
3416
3417 octave_value ov_fcn (af);
3418
3419 return (is_nested
3420 ? octave_value (new octave_fcn_handle (ov_fcn, local_vars, frame))
3421 : octave_value (new octave_fcn_handle (ov_fcn, local_vars)));
3422 }
3423
3424 octave_value_list
3425 tree_evaluator::execute_builtin_function (octave_builtin& builtin_function,
3426 int nargout,
3427 const octave_value_list& args)
3428 {
3429 octave_value_list retval;
3430
3431 if (args.has_magic_colon ())
3432 error ("invalid use of colon in function argument list");
3433
3434 profiler::enter<octave_builtin> block (m_profiler, builtin_function);
3435
3436 octave_builtin::fcn fcn = builtin_function.function ();
3437
3438 if (fcn)
3439 retval = (*fcn) (args, nargout);
3440 else
3441 {
3442 octave_builtin::meth meth = builtin_function.method ();
3443
3444 retval = (*meth) (m_interpreter, args, nargout);
3445 }
3446
3447 // Do not allow null values to be returned from functions.
3448 // FIXME: perhaps true builtins should be allowed?
3449
3450 retval.make_storable_values ();
3451
3452 // Fix the case of a single undefined value.
3453 // This happens when a compiled function uses
3454 //
3455 // octave_value retval;
3456 //
3457 // instead of
3458 //
3459 // octave_value_list retval;
3460 //
3461 // the idiom is very common, so we solve that here.
3462
3463 if (retval.length () == 1 && retval.xelem (0).is_undefined ())
3464 retval.clear ();
3465
3466 return retval;
3467 }
3468
3469 octave_value_list
3470 tree_evaluator::execute_mex_function (octave_mex_function& mex_function,
3471 int nargout,
3472 const octave_value_list& args)
3473 {
3474 octave_value_list retval;
3475
3476 if (args.has_magic_colon ())
3477 error ("invalid use of colon in function argument list");
3478
3479 profiler::enter<octave_mex_function> block (m_profiler, mex_function);
3480
3481 retval = call_mex (mex_function, args, nargout);
3482
3483 return retval;
3484 }
3485
3486 octave_value_list
3487 tree_evaluator::execute_user_script (octave_user_script& user_script,
3488 int nargout,
3489 const octave_value_list& args)
3490 {
3491 octave_value_list retval;
3492
3493 std::string file_name = user_script.fcn_file_name ();
3494
3495 if (args.length () != 0 || nargout != 0)
3496 error ("invalid call to script %s", file_name.c_str ());
3497
3498 tree_statement_list *cmd_list = user_script.body ();
3499
3500 if (! cmd_list)
3501 return retval;
3502
3503 // FIXME: Maybe this check belongs in the places where we push a new
3504 // stack frame? Or in the call_stack push method itself?
3505
3506 if (m_call_stack.size () >= static_cast<std::size_t> (m_max_recursion_depth))
3507 error ("max_recursion_depth exceeded");
3508
3509 unwind_protect_var<stmt_list_type> upv (m_statement_context, SC_SCRIPT);
3510
3511 profiler::enter<octave_user_script> block (m_profiler, user_script);
3512
3513 if (echo ())
3514 push_echo_state (tree_evaluator::ECHO_SCRIPTS, file_name);
3515
3516 // FIXME: Should we be using tree_evaluator::eval here?
3517
3518 cmd_list->accept (*this);
3519
3520 if (m_returning)
3521 m_returning = 0;
3522
3523 if (m_breaking)
3524 m_breaking--;
3525
3526 return retval;
3527 }
3528
3529 void
3530 tree_evaluator::visit_octave_user_script (octave_user_script&)
3531 {
3532 // ??
3533 panic_impossible ();
3534 }
3535
3536 octave_value_list
3537 tree_evaluator::execute_user_function (octave_user_function& user_function,
3538 int nargout,
3539 const octave_value_list& xargs)
3540 {
3541 octave_value_list retval;
3542
3543 // If this function is a classdef constructor, extract the first input
3544 // argument, which must be the partially constructed object instance.
3545
3546 octave_value_list args (xargs);
3547 octave_value_list ret_args;
3548
3549 int nargin = args.length ();
3550
3551 if (user_function.is_classdef_constructor ())
3552 {
3553 if (nargin > 0)
3554 {
3555 ret_args = args.slice (0, 1, true);
3556 --nargin;
3557 args = args.slice (1, nargin, true);
3558 }
3559 else
3560 panic_impossible ();
3561 }
3562
3563 // FIXME: this probably shouldn't be a double-precision matrix.
3564 Matrix ignored_outputs = ignored_fcn_outputs ();
3565
3566 tree_parameter_list *param_list = user_function.parameter_list ();
3567
3568 bool takes_varargs = false;
3569 int max_inputs = 0;
3570
3571 if (param_list)
3572 {
3573 takes_varargs = param_list->takes_varargs ();
3574 max_inputs = param_list->length ();
3575 }
3576
3577 if (! takes_varargs && nargin > max_inputs)
3578 {
3579 std::string name = user_function.name ();
3580
3581 if (name.empty ())
3582 name = "@<anonymous>";
3583
3584 error_with_id ("Octave:invalid-fun-call",
3585 "%s: function called with too many inputs",
3586 name.c_str ());
3587 }
3588
3589 define_parameter_list_from_arg_vector (param_list, args);
3590
3591 tree_parameter_list *ret_list = user_function.return_list ();
3592
3593 if (ret_list && ! ret_list->takes_varargs ())
3594 {
3595 int max_outputs = ret_list->length ();
3596
3597 if (nargout > max_outputs)
3598 {
3599 std::string name = user_function.name ();
3600
3601 error_with_id ("Octave:invalid-fun-call",
3602 "%s: function called with too many outputs",
3603 name.c_str ());
3604 }
3605 }
3606
3607 bind_auto_fcn_vars (xargs.name_tags (), ignored_outputs, nargin,
3608 nargout, user_function.takes_varargs (),
3609 user_function.all_va_args (args));
3610
3611 // For classdef constructor, pre-populate the output arguments
3612 // with the pre-initialized object instance, extracted above.
3613
3614 if (user_function.is_classdef_constructor ())
3615 {
3616 if (! ret_list)
3617 error ("%s: invalid classdef constructor, no output argument defined",
3618 user_function.dispatch_class ().c_str ());
3619
3620 define_parameter_list_from_arg_vector (ret_list, ret_args);
3621 }
3622
3623 // FIXME: Maybe this check belongs in the places where we push a
3624 // new stack frame? Or in the call_stack push method itself?
3625
3626 if (m_call_stack.size () >= static_cast<std::size_t> (m_max_recursion_depth))
3627 error ("max_recursion_depth exceeded");
3628
3629 unwind_action act2 ([&user_function] ()
2297 { 3630 {
2298 std::list<octave_value> arg_vals; 3631 user_function.restore_warning_states ();
2299 3632 });
2300 for (auto elt : *args) 3633
3634 // Evaluate the commands that make up the function.
3635
3636 unwind_protect_var<stmt_list_type> upv (m_statement_context, SC_FUNCTION);
3637
3638 tree_statement_list *cmd_list = user_function.body ();
3639
3640 if (cmd_list)
3641 {
3642 profiler::enter<octave_user_function>
3643 block (m_profiler, user_function);
3644
3645 if (echo ())
3646 push_echo_state (tree_evaluator::ECHO_FUNCTIONS,
3647 user_function.fcn_file_name ());
3648
3649 if (user_function.is_special_expr ())
3650 {
3651 panic_if (cmd_list->length () != 1);
3652
3653 tree_statement *stmt = cmd_list->front ();
3654
3655 tree_expression *expr = stmt->expression ();
3656
3657 if (expr)
3658 {
3659 m_call_stack.set_location (stmt->line (), stmt->column ());
3660
3661 retval = expr->evaluate_n (*this, nargout);
3662 }
3663 }
3664 else
3665 cmd_list->accept (*this);
3666
3667 if (m_returning)
3668 m_returning = 0;
3669
3670 if (m_breaking)
3671 m_breaking--;
3672 }
3673
3674 // Copy return values out.
3675
3676 if (ret_list && ! user_function.is_special_expr ())
3677 {
3678 Cell varargout;
3679
3680 if (ret_list->takes_varargs ())
3681 {
3682 octave_value varargout_varval = varval ("varargout");
3683
3684 if (varargout_varval.is_defined ())
3685 varargout = varargout_varval.xcell_value ("varargout must be a cell array object");
3686 }
3687
3688 retval = convert_return_list_to_const_vector (ret_list, nargout,
3689 ignored_outputs,
3690 varargout);
3691 }
3692
3693 return retval;
3694 }
3695
3696 void
3697 tree_evaluator::visit_octave_user_function (octave_user_function&)
3698 {
3699 // ??
3700 panic_impossible ();
3701 }
3702
3703 void
3704 tree_evaluator::visit_octave_user_function_header (octave_user_function&)
3705 {
3706 panic_impossible ();
3707 }
3708
3709 void
3710 tree_evaluator::visit_octave_user_function_trailer (octave_user_function&)
3711 {
3712 panic_impossible ();
3713 }
3714
3715 void
3716 tree_evaluator::visit_function_def (tree_function_def& cmd)
3717 {
3718 octave_value fcn = cmd.function ();
3719
3720 octave_function *f = fcn.function_value ();
3721
3722 if (f)
3723 {
3724 std::string nm = f->name ();
3725
3726 symbol_table& symtab = m_interpreter.get_symbol_table ();
3727
3728 symtab.install_cmdline_function (nm, fcn);
3729
3730 // Make sure that any variable with the same name as the new
3731 // function is cleared.
3732
3733 assign (nm);
3734 }
3735 }
3736
3737 void
3738 tree_evaluator::visit_identifier (tree_identifier&)
3739 {
3740 panic_impossible ();
3741 }
3742
3743 void
3744 tree_evaluator::visit_if_clause (tree_if_clause&)
3745 {
3746 panic_impossible ();
3747 }
3748
3749 void
3750 tree_evaluator::visit_if_command (tree_if_command& cmd)
3751 {
3752 if (m_echo_state)
3753 {
3754 int line = cmd.line ();
3755 if (line < 0)
3756 line = 1;
3757 echo_code (line);
3758 m_echo_file_pos = line + 1;
3759 }
3760
3761 // FIXME: tree_if_command_list is not derived from tree, so should it
3762 // really have an accept method?
3763
3764 tree_if_command_list *lst = cmd.cmd_list ();
3765
3766 if (lst)
3767 lst->accept (*this);
3768 }
3769
3770 void
3771 tree_evaluator::visit_if_command_list (tree_if_command_list& lst)
3772 {
3773 for (tree_if_clause *tic : lst)
3774 {
3775 tree_expression *expr = tic->condition ();
3776
3777 if (! (in_debug_repl ()
3778 && m_call_stack.current_frame () == m_debug_frame))
3779 m_call_stack.set_location (tic->line (), tic->column ());
3780
3781 if (m_debug_mode && ! tic->is_else_clause ())
3782 do_breakpoint (tic->is_active_breakpoint (*this));
3783
3784 if (tic->is_else_clause () || is_logically_true (expr, "if"))
3785 {
3786 tree_statement_list *stmt_lst = tic->commands ();
3787
3788 if (stmt_lst)
3789 stmt_lst->accept (*this);
3790
3791 break;
3792 }
3793 }
3794 }
3795
3796 void
3797 tree_evaluator::visit_index_expression (tree_index_expression&)
3798 {
3799 panic_impossible ();
3800 }
3801
3802 void
3803 tree_evaluator::visit_matrix (tree_matrix&)
3804 {
3805 panic_impossible ();
3806 }
3807
3808 void
3809 tree_evaluator::visit_cell (tree_cell&)
3810 {
3811 panic_impossible ();
3812 }
3813
3814 void
3815 tree_evaluator::visit_multi_assignment (tree_multi_assignment&)
3816 {
3817 panic_impossible ();
3818 }
3819
3820 void
3821 tree_evaluator::visit_no_op_command (tree_no_op_command& cmd)
3822 {
3823 if (m_echo_state)
3824 {
3825 int line = cmd.line ();
3826 if (line < 0)
3827 line = 1;
3828 echo_code (line);
3829 m_echo_file_pos = line + 1;
3830 }
3831
3832 if (m_debug_mode && cmd.is_end_of_fcn_or_script ())
3833 do_breakpoint (cmd.is_active_breakpoint (*this), true);
3834 }
3835
3836 void
3837 tree_evaluator::visit_constant (tree_constant&)
3838 {
3839 panic_impossible ();
3840 }
3841
3842 void
3843 tree_evaluator::visit_fcn_handle (tree_fcn_handle&)
3844 {
3845 panic_impossible ();
3846 }
3847
3848 void
3849 tree_evaluator::visit_parameter_list (tree_parameter_list&)
3850 {
3851 panic_impossible ();
3852 }
3853
3854 void
3855 tree_evaluator::visit_postfix_expression (tree_postfix_expression&)
3856 {
3857 panic_impossible ();
3858 }
3859
3860 void
3861 tree_evaluator::visit_prefix_expression (tree_prefix_expression&)
3862 {
3863 panic_impossible ();
3864 }
3865
3866 void
3867 tree_evaluator::visit_return_command (tree_return_command& cmd)
3868 {
3869 if (m_echo_state)
3870 {
3871 int line = cmd.line ();
3872 if (line < 0)
3873 line = 1;
3874 echo_code (line);
3875 m_echo_file_pos = line + 1;
3876 }
3877
3878 if (m_debug_mode)
3879 do_breakpoint (cmd.is_active_breakpoint (*this));
3880
3881 // Act like dbcont.
3882
3883 if (in_debug_repl () && m_call_stack.current_frame () == m_debug_frame)
3884 dbcont ();
3885 else if (m_statement_context == SC_FUNCTION
3886 || m_statement_context == SC_SCRIPT
3887 || m_in_loop_command)
3888 m_returning = 1;
3889 }
3890
3891 void
3892 tree_evaluator::visit_simple_assignment (tree_simple_assignment&)
3893 {
3894 panic_impossible ();
3895 }
3896
3897 void
3898 tree_evaluator::visit_statement (tree_statement& stmt)
3899 {
3900 tree_command *cmd = stmt.command ();
3901 tree_expression *expr = stmt.expression ();
3902
3903 if (cmd || expr)
3904 {
3905 if (! (in_debug_repl ()
3906 && m_call_stack.current_frame () == m_debug_frame))
3907 m_call_stack.set_location (stmt.line (), stmt.column ());
3908
3909 try
3910 {
3911 if (cmd)
3912 {
3913 unwind_protect_var<const std::list<octave_lvalue> *>
3914 upv (m_lvalue_list, nullptr);
3915
3916 cmd->accept (*this);
3917 }
3918 else
3919 {
3920 if (m_echo_state)
3921 {
3922 int line = stmt.line ();
3923 if (line < 0)
3924 line = 1;
3925 echo_code (line);
3926 m_echo_file_pos = line + 1;
3927 }
3928
3929 if (m_debug_mode)
3930 do_breakpoint (expr->is_active_breakpoint (*this));
3931
3932 // FIXME: maybe all of this should be packaged in
3933 // one virtual function that returns a flag saying whether
3934 // or not the expression will take care of binding ans and
3935 // printing the result.
3936
3937 // FIXME: it seems that we should just have to
3938 // evaluate the expression and that should take care of
3939 // everything, binding ans as necessary?
3940
3941 octave_value tmp_result = expr->evaluate (*this, 0);
3942
3943 if (tmp_result.is_defined ())
3944 {
3945 bool do_bind_ans = false;
3946
3947 if (expr->is_identifier ())
3948 do_bind_ans = ! is_variable (expr);
3949 else
3950 do_bind_ans = ! expr->is_assignment_expression ();
3951
3952 if (do_bind_ans)
3953 bind_ans (tmp_result, expr->print_result ()
3954 && statement_printing_enabled ());
3955 }
3956 }
3957 }
3958 catch (const std::bad_alloc&)
3959 {
3960 // FIXME: We want to use error_with_id here so that give users
3961 // control over this error message but error_with_id will
3962 // require some memory allocations. Is there anything we can
3963 // do to make those more likely to succeed?
3964
3965 error_with_id ("Octave:bad-alloc",
3966 "out of memory or dimension too large for Octave's index type");
3967 }
3968 catch (const interrupt_exception&)
3969 {
3970 // If we are debugging, then continue with next statement.
3971 // Otherwise, jump out of here.
3972
3973 if (m_debug_mode)
3974 m_interpreter.recover_from_exception ();
3975 else
3976 throw;
3977 }
3978 catch (const execution_exception& ee)
3979 {
3980 error_system& es = m_interpreter.get_error_system ();
3981
3982 if ((m_interpreter.interactive ()
3983 || application::forced_interactive ())
3984 && ((es.debug_on_error ()
3985 && m_bp_table.debug_on_err (es.last_error_id ()))
3986 || (es.debug_on_caught ()
3987 && m_bp_table.debug_on_caught (es.last_error_id ())))
3988 && in_user_code ())
3989 {
3990 es.save_exception (ee);
3991 es.display_exception (ee);
3992
3993 enter_debugger ();
3994
3995 // It doesn't make sense to continue execution after an
3996 // error occurs so force the debugger to quit all debug
3997 // levels and return the the top prompt.
3998
3999 throw quit_debug_exception (true);
4000 }
4001 else
4002 throw;
4003 }
4004 }
4005 }
4006
4007 void
4008 tree_evaluator::visit_statement_list (tree_statement_list& lst)
4009 {
4010 // FIXME: commented out along with else clause below.
4011 // static octave_value_list empty_list;
4012
4013 auto p = lst.begin ();
4014
4015 if (p != lst.end ())
4016 {
4017 while (true)
4018 {
4019 tree_statement *elt = *p++;
4020
4021 if (! elt)
4022 error ("invalid statement found in statement list!");
4023
4024 octave_quit ();
4025
4026 elt->accept (*this);
4027
4028 if (m_breaking || m_continuing)
4029 break;
4030
4031 if (m_returning)
4032 break;
4033
4034 if (p == lst.end ())
4035 break;
4036 else
4037 {
4038 // Clear previous values before next statement is
4039 // evaluated so that we aren't holding an extra
4040 // reference to a value that may be used next. For
4041 // example, in code like this:
4042 //
4043 // X = rand (N); # refcount for X should be 1
4044 // # after this statement
4045 //
4046 // X(idx) = val; # no extra copy of X should be
4047 // # needed, but we will be faked
4048 // # out if retval is not cleared
4049 // # between statements here
4050
4051 // result_values = empty_list;
4052 }
4053 }
4054 }
4055 }
4056
4057 void
4058 tree_evaluator::visit_switch_case (tree_switch_case&)
4059 {
4060 panic_impossible ();
4061 }
4062
4063 void
4064 tree_evaluator::visit_switch_case_list (tree_switch_case_list&)
4065 {
4066 panic_impossible ();
4067 }
4068
4069 void
4070 tree_evaluator::visit_switch_command (tree_switch_command& cmd)
4071 {
4072 if (m_echo_state)
4073 {
4074 int line = cmd.line ();
4075 if (line < 0)
4076 line = 1;
4077 echo_code (line);
4078 m_echo_file_pos = line + 1;
4079 }
4080
4081 if (m_debug_mode)
4082 do_breakpoint (cmd.is_active_breakpoint (*this));
4083
4084 tree_expression *expr = cmd.switch_value ();
4085
4086 if (! expr)
4087 error ("missing value in switch command near line %d, column %d",
4088 cmd.line (), cmd.column ());
4089
4090 octave_value val = expr->evaluate (*this);
4091
4092 tree_switch_case_list *lst = cmd.case_list ();
4093
4094 if (lst)
4095 {
4096 for (tree_switch_case *t : *lst)
4097 {
4098 if (t->is_default_case () || switch_case_label_matches (t, val))
4099 {
4100 tree_statement_list *stmt_lst = t->commands ();
4101
4102 if (stmt_lst)
4103 stmt_lst->accept (*this);
4104
4105 break;
4106 }
4107 }
4108 }
4109 }
4110
4111 void
4112 tree_evaluator::visit_try_catch_command (tree_try_catch_command& cmd)
4113 {
4114 if (m_echo_state)
4115 {
4116 int line = cmd.line ();
4117 if (line < 0)
4118 line = 1;
4119 echo_code (line);
4120 m_echo_file_pos = line + 1;
4121 }
4122
4123 bool execution_error = false;
4124 octave_scalar_map err_map;
4125
4126 tree_statement_list *try_code = cmd.body ();
4127
4128 if (try_code)
4129 {
4130 // unwind frame before catch block
4131
4132 unwind_protect frame;
4133
4134 interpreter_try (frame);
4135
4136 // The catch code is *not* added to unwind_protect stack; it
4137 // doesn't need to be run on interrupts.
4138
4139 try
4140 {
4141 try_code->accept (*this);
4142 }
4143 catch (const execution_exception& ee)
4144 {
4145 execution_error = true;
4146
4147 error_system& es = m_interpreter.get_error_system ();
4148
4149 es.save_exception (ee);
4150
4151 err_map.assign ("message", es.last_error_message ());
4152 err_map.assign ("identifier", es.last_error_id ());
4153 err_map.assign ("stack", es.last_error_stack ());
4154
4155 m_interpreter.recover_from_exception ();
4156 }
4157
4158 // Actions attached to unwind_protect frame will run here, prior
4159 // to executing the catch block.
4160 }
4161
4162 if (execution_error)
4163 {
4164 tree_statement_list *catch_code = cmd.cleanup ();
4165
4166 if (catch_code)
4167 {
4168 tree_identifier *expr_id = cmd.identifier ();
4169
4170 if (expr_id)
4171 {
4172 octave_lvalue ult = expr_id->lvalue (*this);
4173
4174 ult.assign (octave_value::op_asn_eq, err_map);
4175 }
4176
4177 // perform actual "catch" block
4178 catch_code->accept (*this);
4179 }
4180 }
4181 }
4182
4183 void
4184 tree_evaluator::do_unwind_protect_cleanup_code (tree_statement_list *list)
4185 {
4186 unwind_protect frame;
4187
4188 frame.protect_var (octave_interrupt_state);
4189 octave_interrupt_state = 0;
4190
4191 // We want to preserve the last location info for possible
4192 // backtracking.
4193
4194 frame.add (&call_stack::set_line, &m_call_stack,
4195 m_call_stack.current_line ());
4196
4197 frame.add (&call_stack::set_column, &m_call_stack,
4198 m_call_stack.current_column ());
4199
4200 // Similarly, if we have seen a return or break statement, allow all
4201 // the cleanup code to run before returning or handling the break.
4202 // We don't have to worry about continue statements because they can
4203 // only occur in loops.
4204
4205 frame.protect_var (m_returning);
4206 m_returning = 0;
4207
4208 frame.protect_var (m_breaking);
4209 m_breaking = 0;
4210
4211 try
4212 {
4213 if (list)
4214 list->accept (*this);
4215 }
4216 catch (const execution_exception& ee)
4217 {
4218 error_system& es = m_interpreter.get_error_system ();
4219
4220 es.save_exception (ee);
4221 m_interpreter.recover_from_exception ();
4222
4223 if (m_breaking || m_returning)
4224 frame.discard (2);
4225 else
4226 frame.run (2);
4227
4228 frame.discard (2);
4229
4230 throw;
4231 }
4232
4233 // The unwind_protects are popped off the stack in the reverse of
4234 // the order they are pushed on.
4235
4236 // FIXME: these statements say that if we see a break or
4237 // return statement in the cleanup block, that we want to use the
4238 // new value of the breaking or returning flag instead of restoring
4239 // the previous value. Is that the right thing to do? I think so.
4240 // Consider the case of
4241 //
4242 // function foo ()
4243 // unwind_protect
4244 // fprintf (stderr, "1: this should always be executed\n");
4245 // break;
4246 // fprintf (stderr, "1: this should never be executed\n");
4247 // unwind_protect_cleanup
4248 // fprintf (stderr, "2: this should always be executed\n");
4249 // return;
4250 // fprintf (stderr, "2: this should never be executed\n");
4251 // end_unwind_protect
4252 // endfunction
4253 //
4254 // If we reset the value of the breaking flag, both the returning
4255 // flag and the breaking flag will be set, and we shouldn't have
4256 // both. So, use the most recent one. If there is no return or
4257 // break in the cleanup block, the values should be reset to
4258 // whatever they were when the cleanup block was entered.
4259
4260 if (m_breaking || m_returning)
4261 frame.discard (2);
4262 else
4263 frame.run (2);
4264 }
4265
4266 void
4267 tree_evaluator::visit_unwind_protect_command (tree_unwind_protect_command& cmd)
4268 {
4269 if (m_echo_state)
4270 {
4271 int line = cmd.line ();
4272 if (line < 0)
4273 line = 1;
4274 echo_code (line);
4275 m_echo_file_pos = line + 1;
4276 }
4277
4278 tree_statement_list *cleanup_code = cmd.cleanup ();
4279
4280 tree_statement_list *unwind_protect_code = cmd.body ();
4281
4282 if (unwind_protect_code)
4283 {
4284 try
4285 {
4286 unwind_protect_code->accept (*this);
4287 }
4288 catch (const execution_exception& ee)
4289 {
4290 error_system& es = m_interpreter.get_error_system ();
4291
4292 // FIXME: Maybe we should be able to temporarily set the
4293 // interpreter's exception handling state to something "safe"
4294 // while the cleanup block runs instead of just resetting it
4295 // here?
4296 es.save_exception (ee);
4297 m_interpreter.recover_from_exception ();
4298
4299 // Run the cleanup code on exceptions, so that it is run even
4300 // in case of interrupt or out-of-memory.
4301 do_unwind_protect_cleanup_code (cleanup_code);
4302
4303 // If an error occurs inside the cleanup code, a new
4304 // exception will be thrown instead of the original.
4305 throw;
4306 }
4307 catch (const interrupt_exception&)
4308 {
4309 // The comments above apply here as well.
4310 m_interpreter.recover_from_exception ();
4311 do_unwind_protect_cleanup_code (cleanup_code);
4312 throw;
4313 }
4314
4315 // Also execute the unwind_protect_cleanump code if the
4316 // unwind_protect block runs without error.
4317 do_unwind_protect_cleanup_code (cleanup_code);
4318 }
4319 }
4320
4321 void
4322 tree_evaluator::visit_while_command (tree_while_command& cmd)
4323 {
4324 int line = cmd.line ();
4325 if (line < 0)
4326 line = 1;
4327
4328 if (m_echo_state)
4329 {
4330 echo_code (line);
4331 line++;
4332 }
4333
4334 unwind_protect_var<bool> upv (m_in_loop_command, true);
4335
4336 tree_expression *expr = cmd.condition ();
4337
4338 if (! expr)
4339 panic_impossible ();
4340
4341 for (;;)
4342 {
4343 if (m_echo_state)
4344 m_echo_file_pos = line;
4345
4346 if (m_debug_mode)
4347 do_breakpoint (cmd.is_active_breakpoint (*this));
4348
4349 if (is_logically_true (expr, "while"))
4350 {
4351 tree_statement_list *loop_body = cmd.body ();
4352
4353 if (loop_body)
4354 loop_body->accept (*this);
4355
4356 if (quit_loop_now ())
4357 break;
4358 }
4359 else
4360 break;
4361 }
4362 }
4363
4364 void
4365 tree_evaluator::visit_do_until_command (tree_do_until_command& cmd)
4366 {
4367 int line = cmd.line ();
4368 if (line < 0)
4369 line = 1;
4370
4371 if (m_echo_state)
4372 {
4373 echo_code (line);
4374 line++;
4375 }
4376
4377 unwind_protect_var<bool> upv (m_in_loop_command, true);
4378
4379 tree_expression *expr = cmd.condition ();
4380
4381 if (! expr)
4382 panic_impossible ();
4383
4384 for (;;)
4385 {
4386 if (m_echo_state)
4387 m_echo_file_pos = line;
4388
4389 tree_statement_list *loop_body = cmd.body ();
4390
4391 if (loop_body)
4392 loop_body->accept (*this);
4393
4394 if (quit_loop_now ())
4395 break;
4396
4397 if (m_debug_mode)
4398 do_breakpoint (cmd.is_active_breakpoint (*this));
4399
4400 if (is_logically_true (expr, "do-until"))
4401 break;
4402 }
4403 }
4404
4405 void
4406 tree_evaluator::visit_superclass_ref (tree_superclass_ref&)
4407 {
4408 panic_impossible ();
4409 }
4410
4411 void
4412 tree_evaluator::visit_metaclass_query (tree_metaclass_query&)
4413 {
4414 panic_impossible ();
4415 }
4416
4417 void tree_evaluator::bind_ans (const octave_value& val, bool print)
4418 {
4419 static std::string ans = "ans";
4420
4421 if (val.is_defined ())
4422 {
4423 if (val.is_cs_list ())
4424 {
4425 octave_value_list lst = val.list_value ();
4426
4427 for (octave_idx_type i = 0; i < lst.length (); i++)
4428 bind_ans (lst(i), print);
4429 }
4430 else
4431 {
4432 // FIXME: Maybe assign could also return the assigned value,
4433 // just for convenience?
4434
4435 assign (ans, val);
4436
4437 if (print)
4438 {
4439 // Use varval instead of displaying VAL directly so that
4440 // we get the right type and value for things like
4441 // magic_int values that may mutate when stored.
4442
4443 octave_value_list args = ovl (varval (ans));
4444 args.stash_name_tags (string_vector (ans));
4445 feval ("display", args);
4446 }
4447 }
4448 }
4449 }
4450
4451 void
4452 tree_evaluator::do_breakpoint (tree_statement& stmt)
4453 {
4454 do_breakpoint (stmt.is_active_breakpoint (*this),
4455 stmt.is_end_of_fcn_or_script ());
4456 }
4457
4458 void
4459 tree_evaluator::do_breakpoint (bool is_breakpoint,
4460 bool is_end_of_fcn_or_script)
4461 {
4462 bool break_on_this_statement = false;
4463
4464 if (is_breakpoint)
4465 break_on_this_statement = true;
4466 else if (m_dbstep_flag > 0)
4467 {
4468 if (m_call_stack.current_frame () == m_debug_frame)
4469 {
4470 if (m_dbstep_flag == 1 || is_end_of_fcn_or_script)
4471 {
4472 // We get here if we are doing a "dbstep" or a "dbstep N" and
4473 // the count has reached 1 so that we must stop and return to
4474 // debug prompt. Alternatively, "dbstep N" has been used but
4475 // the end of the frame has been reached so we stop at the last
4476 // line and return to prompt.
4477
4478 break_on_this_statement = true;
4479 }
4480 else
4481 {
4482 // Executing "dbstep N". Decrease N by one and continue.
4483
4484 m_dbstep_flag--;
4485 }
4486
4487 }
4488 else if (m_dbstep_flag == 1
4489 && m_call_stack.current_frame () < m_debug_frame)
4490 {
4491 // We stepped out from the end of a function.
4492
4493 m_debug_frame = m_call_stack.current_frame ();
4494
4495 break_on_this_statement = true;
4496 }
4497 }
4498 else if (m_dbstep_flag == -1)
4499 {
4500 // We get here if we are doing a "dbstep in".
4501
4502 break_on_this_statement = true;
4503
4504 m_debug_frame = m_call_stack.current_frame ();
4505 }
4506 else if (m_dbstep_flag == -2)
4507 {
4508 // We get here if we are doing a "dbstep out". Check for end of
4509 // function and whether the current frame is the same as the
4510 // cached value because we want to step out from the frame where
4511 // "dbstep out" was evaluated, not from any functions called from
4512 // that frame.
4513
4514 if (is_end_of_fcn_or_script
4515 && m_call_stack.current_frame () == m_debug_frame)
4516 m_dbstep_flag = -1;
4517 }
4518
4519 if (! break_on_this_statement)
4520 break_on_this_statement = m_break_on_next_stmt;
4521
4522 m_break_on_next_stmt = false;
4523
4524 if (break_on_this_statement)
4525 {
4526 m_dbstep_flag = 0;
4527
4528 enter_debugger ();
4529 }
4530 }
4531
4532 bool
4533 tree_evaluator::is_logically_true (tree_expression *expr,
4534 const char *warn_for)
4535 {
4536 bool expr_value = false;
4537
4538 m_call_stack.set_location (expr->line (), expr->column ());
4539
4540 octave_value t1 = expr->evaluate (*this);
4541
4542 if (t1.is_defined ())
4543 return t1.is_true ();
4544 else
4545 error ("%s: undefined value used in conditional expression", warn_for);
4546
4547 return expr_value;
4548 }
4549
4550 octave_value
4551 tree_evaluator::max_recursion_depth (const octave_value_list& args,
4552 int nargout)
4553 {
4554 return set_internal_variable (m_max_recursion_depth, args, nargout,
4555 "max_recursion_depth", 0);
4556 }
4557
4558 symbol_info_list
4559 tree_evaluator::glob_symbol_info (const std::string& pattern) const
4560 {
4561 return m_call_stack.glob_symbol_info (pattern);
4562 }
4563
4564 symbol_info_list
4565 tree_evaluator::regexp_symbol_info (const std::string& pattern) const
4566 {
4567 return m_call_stack.regexp_symbol_info (pattern);
4568 }
4569
4570 symbol_info_list
4571 tree_evaluator::get_symbol_info (void)
4572 {
4573 return m_call_stack.get_symbol_info ();
4574 }
4575
4576 symbol_info_list
4577 tree_evaluator::top_scope_symbol_info (void) const
4578 {
4579 return m_call_stack.top_scope_symbol_info ();
4580 }
4581
4582 octave_map tree_evaluator::get_autoload_map (void) const
4583 {
4584 Cell fcn_names (dim_vector (m_autoload_map.size (), 1));
4585 Cell file_names (dim_vector (m_autoload_map.size (), 1));
4586
4587 octave_idx_type i = 0;
4588 for (const auto& fcn_fname : m_autoload_map)
4589 {
4590 fcn_names(i) = fcn_fname.first;
4591 file_names(i) = fcn_fname.second;
4592
4593 i++;
4594 }
4595
4596 octave_map m;
4597
4598 m.assign ("function", fcn_names);
4599 m.assign ("file", file_names);
4600
4601 return m;
4602 }
4603
4604 std::string tree_evaluator::lookup_autoload (const std::string& nm) const
4605 {
4606 std::string retval;
4607
4608 auto p = m_autoload_map.find (nm);
4609
4610 if (p != m_autoload_map.end ())
4611 {
4612 load_path& lp = m_interpreter.get_load_path ();
4613
4614 retval = lp.find_file (p->second);
4615 }
4616
4617 return retval;
4618 }
4619
4620 std::list<std::string> tree_evaluator::autoloaded_functions (void) const
4621 {
4622 std::list<std::string> names;
4623
4624 for (const auto& fcn_fname : m_autoload_map)
4625 names.push_back (fcn_fname.first);
4626
4627 return names;
4628 }
4629
4630 std::list<std::string>
4631 tree_evaluator::reverse_lookup_autoload (const std::string& nm) const
4632 {
4633 std::list<std::string> names;
4634
4635 for (const auto& fcn_fname : m_autoload_map)
4636 if (nm == fcn_fname.second)
4637 names.push_back (fcn_fname.first);
4638
4639 return names;
4640 }
4641
4642 void tree_evaluator::add_autoload (const std::string& fcn,
4643 const std::string& nm)
4644 {
4645 std::string file_name = check_autoload_file (nm);
4646
4647 m_autoload_map[fcn] = file_name;
4648 }
4649
4650 void tree_evaluator::remove_autoload (const std::string& fcn,
4651 const std::string& nm)
4652 {
4653 check_autoload_file (nm);
4654
4655 // Remove function from symbol table and autoload map.
4656 symbol_table& symtab = m_interpreter.get_symbol_table ();
4657
4658 symtab.clear_dld_function (fcn);
4659
4660 m_autoload_map.erase (fcn);
4661 }
4662
4663 octave_value
4664 tree_evaluator::whos_line_format (const octave_value_list& args, int nargout)
4665 {
4666 return set_internal_variable (m_whos_line_format, args, nargout,
4667 "whos_line_format");
4668 }
4669
4670 octave_value
4671 tree_evaluator::silent_functions (const octave_value_list& args, int nargout)
4672 {
4673 return set_internal_variable (m_silent_functions, args, nargout,
4674 "silent_functions");
4675 }
4676
4677 octave_value
4678 tree_evaluator::string_fill_char (const octave_value_list& args, int nargout)
4679 {
4680 return set_internal_variable (m_string_fill_char, args, nargout,
4681 "string_fill_char");
4682 }
4683
4684 // Final step of processing an indexing error. Add the name of the
4685 // variable being indexed, if any, then issue an error. (Will this also
4686 // be needed by pt-lvalue, which calls subsref?)
4687
4688 void tree_evaluator::final_index_error (index_exception& ie,
4689 const tree_expression *expr)
4690 {
4691 std::string extra_message;
4692
4693 if (is_variable (expr))
4694 {
4695 std::string var = expr->name ();
4696
4697 ie.set_var (var);
4698
4699 symbol_table& symtab = m_interpreter.get_symbol_table ();
4700
4701 octave_value fcn = symtab.find_function (var);
4702
4703 if (fcn.is_function ())
4704 {
4705 octave_function *fp = fcn.function_value ();
4706
4707 if (fp && fp->name () == var)
4708 extra_message
4709 = " (note: variable '" + var + "' shadows function)";
4710 }
4711 }
4712
4713 std::string msg = ie.message () + extra_message;
4714
4715 error_with_id (ie.err_id (), "%s", msg.c_str ());
4716 }
4717
4718 octave_value
4719 tree_evaluator::do_who (int argc, const string_vector& argv,
4720 bool return_list, bool verbose)
4721 {
4722 return m_call_stack.do_who (argc, argv, return_list, verbose);
4723 }
4724
4725 octave_value_list
4726 tree_evaluator::make_value_list (tree_argument_list *args,
4727 const string_vector& arg_nm)
4728 {
4729 octave_value_list retval;
4730
4731 if (args)
4732 {
4733 unwind_protect_var<const std::list<octave_lvalue> *>
4734 upv (m_lvalue_list, nullptr);
4735
4736 int len = args->length ();
4737
4738 unwind_protect_var<int> upv2 (m_index_position);
4739 unwind_protect_var<int> upv3 (m_num_indices);
4740
4741 m_num_indices = len;
4742
4743 std::list<octave_value> arg_vals;
4744
4745 int k = 0;
4746
4747 for (auto elt : *args)
4748 {
4749 // FIXME: is it possible for elt to be invalid?
4750
4751 if (! elt)
4752 break;
4753
4754 m_index_position = k++;
4755
4756 octave_value tmp = elt->evaluate (*this);
4757
4758 if (tmp.is_cs_list ())
4759 {
4760 octave_value_list tmp_ovl = tmp.list_value ();
4761
4762 for (octave_idx_type i = 0; i < tmp_ovl.length (); i++)
4763 arg_vals.push_back (tmp_ovl(i));
4764 }
4765 else if (tmp.is_defined ())
4766 arg_vals.push_back (tmp);
4767 }
4768
4769 retval = octave_value_list (arg_vals);
4770 }
4771
4772 octave_idx_type n = retval.length ();
4773
4774 if (n > 0)
4775 retval.stash_name_tags (arg_nm);
4776
4777 return retval;
4778 }
4779
4780 std::list<octave_lvalue>
4781 tree_evaluator::make_lvalue_list (tree_argument_list *lhs)
4782 {
4783 std::list<octave_lvalue> retval;
4784
4785 for (tree_expression *elt : *lhs)
4786 retval.push_back (elt->lvalue (*this));
4787
4788 return retval;
4789 }
4790
4791 void
4792 tree_evaluator::push_echo_state (int type, const std::string& file_name,
4793 int pos)
4794 {
4795 unwind_protect *frame = m_call_stack.curr_fcn_unwind_protect_frame ();
4796
4797 if (frame)
4798 {
4799 push_echo_state_cleanup (*frame);
4800
4801 set_echo_state (type, file_name, pos);
4802 }
4803 }
4804
4805 void
4806 tree_evaluator::set_echo_state (int type, const std::string& file_name,
4807 int pos)
4808 {
4809 m_echo_state = echo_this_file (file_name, type);
4810 m_echo_file_name = file_name;
4811 m_echo_file_pos = pos;
4812 }
4813
4814 void
4815 tree_evaluator::uwp_set_echo_state (bool state, const std::string& file_name,
4816 int pos)
4817 {
4818 m_echo_state = state;
4819 m_echo_file_name = file_name;
4820 m_echo_file_pos = pos;
4821 }
4822
4823 void
4824 tree_evaluator::maybe_set_echo_state (void)
4825 {
4826 octave_function *caller = caller_function ();
4827
4828 if (caller && caller->is_user_code ())
4829 {
4830 octave_user_code *fcn = dynamic_cast<octave_user_code *> (caller);
4831
4832 int type = fcn->is_user_function () ? ECHO_FUNCTIONS : ECHO_SCRIPTS;
4833
4834 std::string file_name = fcn->fcn_file_name ();
4835
4836 // We want the line where "echo" was called, not the line number
4837 // stored in the stack frame that was created for the echo
4838 // function (that will always be -1).
4839
4840 int pos = m_call_stack.current_user_code_line ();
4841
4842 if (pos < 0)
4843 pos = 1;
4844
4845 set_echo_state (type, file_name, pos);
4846 }
4847 }
4848
4849 void
4850 tree_evaluator::push_echo_state_cleanup (unwind_protect& frame)
4851 {
4852 frame.add (&tree_evaluator::uwp_set_echo_state, this,
4853 m_echo_state, m_echo_file_name, m_echo_file_pos);
4854 }
4855
4856 bool tree_evaluator::maybe_push_echo_state_cleanup (void)
4857 {
4858 // This function is expected to be called from ECHO, which would be
4859 // the top of the call stack. If the caller of ECHO is a
4860 // user-defined function or script, then set up unwind-protect
4861 // elements to restore echo state.
4862
4863 unwind_protect *frame = m_call_stack.curr_fcn_unwind_protect_frame ();
4864
4865 if (frame)
4866 {
4867 push_echo_state_cleanup (*frame);
4868 return true;
4869 }
4870
4871 return false;
4872 }
4873
4874
4875 octave_value
4876 tree_evaluator::echo (const octave_value_list& args, int)
4877 {
4878 bool cleanup_pushed = maybe_push_echo_state_cleanup ();
4879
4880 string_vector argv = args.make_argv ();
4881
4882 switch (args.length ())
4883 {
4884 case 0:
4885 if ((m_echo & ECHO_SCRIPTS) || (m_echo & ECHO_FUNCTIONS))
4886 {
4887 m_echo = ECHO_OFF;
4888 m_echo_files.clear ();
4889 }
4890 else
4891 m_echo = ECHO_SCRIPTS;
4892 break;
4893
4894 case 1:
2301 { 4895 {
2302 // FIXME: is it possible for elt to be invalid? 4896 std::string arg0 = argv[0];
2303 4897
2304 if (! elt) 4898 if (arg0 == "on")
2305 break; 4899 m_echo = ECHO_SCRIPTS;
2306 4900 else if (arg0 == "off")
2307 octave_value tmp = elt->evaluate (*this); 4901 m_echo = ECHO_OFF;
2308 4902 else
2309 if (tmp.is_cs_list ())
2310 { 4903 {
2311 octave_value_list tmp_ovl = tmp.list_value (); 4904 std::string file = fcn_file_in_path (arg0);
2312 4905 file = sys::env::make_absolute (file);
2313 for (octave_idx_type i = 0; i < tmp_ovl.length (); i++) 4906
2314 arg_vals.push_back (tmp_ovl(i)); 4907 if (file.empty ())
2315 } 4908 error ("echo: no such file %s", arg0.c_str ());
2316 else if (tmp.is_defined ()) 4909
2317 arg_vals.push_back (tmp); 4910 if (m_echo & ECHO_ALL)
2318 }
2319
2320 return octave_value_list (arg_vals);
2321 }
2322
2323 octave_value_list
2324 tree_evaluator::convert_return_list_to_const_vector
2325 (tree_parameter_list *ret_list, int nargout, const Matrix& ignored_outputs,
2326 const Cell& varargout)
2327 {
2328 octave_idx_type vlen = varargout.numel ();
2329 int len = ret_list->length ();
2330
2331 // Special case. Will do a shallow copy.
2332 if (len == 0)
2333 return varargout;
2334 else
2335 {
2336 int i = 0;
2337 int k = 0;
2338 int num_ignored = ignored_outputs.numel ();
2339 int ignored = num_ignored > 0 ? ignored_outputs(k) - 1 : -1;
2340
2341 if (nargout <= len)
2342 {
2343 int nout = nargout > 0 ? nargout : 1;
2344 octave_value_list retval (nout);
2345
2346 for (tree_decl_elt *elt : *ret_list)
2347 { 4911 {
2348 if (nargout == 0 && ! is_defined (elt->ident ())) 4912 // Echo is enabled for all functions, so turn it off
2349 break; 4913 // for this one.
2350 4914
2351 if (ignored >= 0 && i == ignored) 4915 m_echo_files[file] = false;
4916 }
4917 else
4918 {
4919 // Echo may be enabled for specific functions.
4920
4921 auto p = m_echo_files.find (file);
4922
4923 if (p == m_echo_files.end ())
2352 { 4924 {
2353 i++; 4925 // Not this one, so enable it.
2354 k++; 4926
2355 ignored = k < num_ignored ? ignored_outputs(k) - 1 : -1; 4927 m_echo |= ECHO_FUNCTIONS;
4928 m_echo_files[file] = true;
2356 } 4929 }
2357 else 4930 else
2358 retval(i++) = evaluate (elt);
2359
2360 if (i == nout)
2361 break;
2362 }
2363
2364 return retval;
2365 }
2366 else
2367 {
2368 octave_value_list retval (len + vlen);
2369
2370 for (tree_decl_elt *elt : *ret_list)
2371 {
2372 if (ignored >= 0 && i == ignored)
2373 { 4931 {
2374 i++; 4932 // This one is already in the list. Flip the
2375 k++; 4933 // status for it.
2376 ignored = k < num_ignored ? ignored_outputs(k) - 1 : -1; 4934
2377 } 4935 p->second = ! p->second;
2378 else
2379 retval(i++) = evaluate (elt);
2380 }
2381
2382 for (octave_idx_type j = 0; j < vlen; j++)
2383 retval(i++) = varargout(j);
2384
2385 return retval;
2386 }
2387 }
2388 }
2389
2390 bool
2391 tree_evaluator::eval_decl_elt (tree_decl_elt *elt)
2392 {
2393 bool retval = false;
2394
2395 tree_identifier *id = elt->ident ();
2396 tree_expression *expr = elt->expression ();
2397
2398 if (id && expr)
2399 {
2400 octave_lvalue ult = id->lvalue (*this);
2401
2402 octave_value init_val = expr->evaluate (*this);
2403
2404 ult.assign (octave_value::op_asn_eq, init_val);
2405
2406 retval = true;
2407 }
2408
2409 return retval;
2410 }
2411
2412 bool
2413 tree_evaluator::switch_case_label_matches (tree_switch_case *expr,
2414 const octave_value& val)
2415 {
2416 tree_expression *label = expr->case_label ();
2417
2418 octave_value label_value = label->evaluate (*this);
2419
2420 if (label_value.is_defined ())
2421 {
2422 if (label_value.iscell ())
2423 {
2424 Cell cell (label_value.cell_value ());
2425
2426 for (octave_idx_type i = 0; i < cell.rows (); i++)
2427 {
2428 for (octave_idx_type j = 0; j < cell.columns (); j++)
2429 {
2430 bool match = val.is_equal (cell(i,j));
2431
2432 if (match)
2433 return true;
2434 } 4936 }
2435 } 4937 }
2436 } 4938 }
2437 else
2438 return val.is_equal (label_value);
2439 } 4939 }
2440 4940 break;
2441 return false; 4941
2442 } 4942 case 2:
2443
2444 void tree_evaluator::push_stack_frame (const symbol_scope& scope)
2445 {
2446 m_call_stack.push (scope);
2447 }
2448
2449 void tree_evaluator::push_stack_frame (octave_user_function *fcn,
2450 const std::shared_ptr<stack_frame>& closure_frames)
2451 {
2452 m_call_stack.push (fcn, closure_frames);
2453 }
2454
2455 void tree_evaluator::push_stack_frame (octave_user_function *fcn,
2456 const stack_frame::local_vars_map& local_vars,
2457 const std::shared_ptr<stack_frame>& closure_frames)
2458 {
2459 m_call_stack.push (fcn, local_vars, closure_frames);
2460 }
2461
2462 void tree_evaluator::push_stack_frame (octave_user_script *script)
2463 {
2464 m_call_stack.push (script);
2465 }
2466
2467 void tree_evaluator::push_stack_frame (octave_function *fcn)
2468 {
2469 m_call_stack.push (fcn);
2470 }
2471
2472 void tree_evaluator::pop_stack_frame (void)
2473 {
2474 m_call_stack.pop ();
2475 }
2476
2477 int tree_evaluator::current_line (void) const
2478 {
2479 return m_call_stack.current_line ();
2480 }
2481
2482 int tree_evaluator::current_column (void) const
2483 {
2484 return m_call_stack.current_column ();
2485 }
2486
2487 int tree_evaluator::debug_user_code_line (void) const
2488 {
2489 return m_call_stack.debug_user_code_line ();
2490 }
2491
2492 int tree_evaluator::debug_user_code_column (void) const
2493 {
2494 return m_call_stack.debug_user_code_column ();
2495 }
2496
2497 void tree_evaluator::debug_where (std::ostream& os) const
2498 {
2499 std::shared_ptr<stack_frame> frm = m_call_stack.current_user_frame ();
2500
2501 frm->display_stopped_in_message (os);
2502 }
2503
2504 octave_user_code * tree_evaluator::current_user_code (void) const
2505 {
2506 return m_call_stack.current_user_code ();
2507 }
2508
2509 unwind_protect * tree_evaluator::curr_fcn_unwind_protect_frame (void)
2510 {
2511 return m_call_stack.curr_fcn_unwind_protect_frame ();
2512 }
2513
2514 octave_user_code * tree_evaluator::debug_user_code (void) const
2515 {
2516 return m_call_stack.debug_user_code ();
2517 }
2518
2519 octave_function * tree_evaluator::current_function (bool skip_first) const
2520 {
2521 return m_call_stack.current_function (skip_first);
2522 }
2523
2524 octave_function * tree_evaluator::caller_function (void) const
2525 {
2526 return m_call_stack.current_function (true);
2527 }
2528
2529 bool tree_evaluator::goto_frame (std::size_t n, bool verbose)
2530 {
2531 return m_call_stack.goto_frame (n, verbose);
2532 }
2533
2534 void tree_evaluator::goto_caller_frame (void)
2535 {
2536 m_call_stack.goto_caller_frame ();
2537 }
2538
2539 void tree_evaluator::goto_base_frame (void)
2540 {
2541 m_call_stack.goto_base_frame ();
2542 }
2543
2544 void tree_evaluator::restore_frame (std::size_t n)
2545 {
2546 return m_call_stack.restore_frame (n);
2547 }
2548
2549 std::string tree_evaluator::get_dispatch_class (void) const
2550 {
2551 return m_call_stack.get_dispatch_class ();
2552 }
2553
2554 void tree_evaluator::set_dispatch_class (const std::string& class_name)
2555 {
2556 m_call_stack.set_dispatch_class (class_name);
2557 }
2558
2559 bool
2560 tree_evaluator::is_class_method_executing (std::string& dclass) const
2561 {
2562 return m_call_stack.is_class_method_executing (dclass);
2563 }
2564
2565 bool
2566 tree_evaluator::is_class_constructor_executing (std::string& dclass) const
2567 {
2568 return m_call_stack.is_class_constructor_executing (dclass);
2569 }
2570
2571 std::list<std::shared_ptr<stack_frame>>
2572 tree_evaluator::backtrace_frames (octave_idx_type& curr_user_frame) const
2573 {
2574 return m_call_stack.backtrace_frames (curr_user_frame);
2575 }
2576
2577 std::list<std::shared_ptr<stack_frame>>
2578 tree_evaluator::backtrace_frames (void) const
2579 {
2580 return m_call_stack.backtrace_frames ();
2581 }
2582
2583 std::list<frame_info>
2584 tree_evaluator::backtrace_info (octave_idx_type& curr_user_frame,
2585 bool print_subfn) const
2586 {
2587 return m_call_stack.backtrace_info (curr_user_frame, print_subfn);
2588 }
2589
2590 std::list<frame_info> tree_evaluator::backtrace_info (void) const
2591 {
2592 return m_call_stack.backtrace_info ();
2593 }
2594
2595 octave_map
2596 tree_evaluator::backtrace (octave_idx_type& curr_user_frame,
2597 bool print_subfn) const
2598 {
2599 return m_call_stack.backtrace (curr_user_frame, print_subfn);
2600 }
2601
2602 octave_map tree_evaluator::backtrace (void) const
2603 {
2604 return m_call_stack.backtrace ();
2605 }
2606
2607 octave_map tree_evaluator::empty_backtrace (void) const
2608 {
2609 return m_call_stack.empty_backtrace ();
2610 }
2611
2612 std::string tree_evaluator::backtrace_message (void) const
2613 {
2614 std::list<frame_info> frames = backtrace_info ();
2615
2616 std::ostringstream buf;
2617
2618 for (const auto& frm : frames)
2619 { 4943 {
2620 buf << " " << frm.fcn_name (); 4944 std::string arg0 = argv[0];
2621 4945 std::string arg1 = argv[1];
2622 int line = frm.line (); 4946
2623 4947 if (arg1 == "on" || arg1 == "off")
2624 if (line > 0) 4948 std::swap (arg0, arg1);
4949
4950 if (arg0 == "on")
2625 { 4951 {
2626 buf << " at line " << line; 4952 if (arg1 == "all")
2627
2628 int column = frm.column ();
2629
2630 if (column > 0)
2631 buf << " column " << column;
2632
2633 buf << "\n";
2634 }
2635 }
2636
2637 return buf.str ();
2638 }
2639
2640 void tree_evaluator::push_dummy_scope (const std::string& name)
2641 {
2642 symbol_scope dummy_scope (name + "$dummy");
2643
2644 m_call_stack.push (dummy_scope);
2645 }
2646
2647 void tree_evaluator::pop_scope (void)
2648 {
2649 m_call_stack.pop ();
2650 }
2651
2652 symbol_scope tree_evaluator::get_top_scope (void) const
2653 {
2654 return m_call_stack.top_scope ();
2655 }
2656
2657 symbol_scope tree_evaluator::get_current_scope (void) const
2658 {
2659 return m_call_stack.current_scope ();
2660 }
2661
2662 void tree_evaluator::mlock (bool skip_first) const
2663 {
2664 octave_function *fcn = m_call_stack.current_function (skip_first);
2665
2666 if (! fcn)
2667 error ("mlock: invalid use outside a function");
2668
2669 if (fcn->is_builtin_function ())
2670 {
2671 warning ("mlock: locking built-in function has no effect");
2672 return;
2673 }
2674
2675 fcn->lock ();
2676 }
2677
2678 void tree_evaluator::munlock (bool skip_first) const
2679 {
2680 octave_function *fcn = m_call_stack.current_function (skip_first);
2681
2682 if (! fcn)
2683 error ("munlock: invalid use outside a function");
2684
2685 if (fcn->is_builtin_function ())
2686 {
2687 warning ("munlock: unlocking built-in function has no effect");
2688 return;
2689 }
2690
2691 fcn->unlock ();
2692 }
2693
2694 bool tree_evaluator::mislocked (bool skip_first) const
2695 {
2696 octave_function *fcn = m_call_stack.current_function (skip_first);
2697
2698 if (! fcn)
2699 error ("mislocked: invalid use outside a function");
2700
2701 return fcn->islocked ();
2702 }
2703
2704 octave_value
2705 tree_evaluator::max_stack_depth (const octave_value_list& args, int nargout)
2706 {
2707 return m_call_stack.max_stack_depth (args, nargout);
2708 }
2709
2710 void tree_evaluator::display_call_stack (void) const
2711 {
2712 m_call_stack.display ();
2713 }
2714
2715 octave_value tree_evaluator::find (const std::string& name)
2716 {
2717 std::shared_ptr<stack_frame> frame
2718 = m_call_stack.get_current_stack_frame ();
2719
2720 octave_value val = frame->varval (name);
2721
2722 if (val.is_defined ())
2723 return val;
2724
2725 // Subfunction. I think it only makes sense to check for
2726 // subfunctions if we are currently executing a function defined
2727 // from a .m file.
2728
2729 octave_value fcn = frame->find_subfunction (name);
2730
2731 if (fcn.is_defined ())
2732 return fcn;
2733
2734 symbol_table& symtab = m_interpreter.get_symbol_table ();
2735
2736 return symtab.fcn_table_find (name, ovl ());
2737 }
2738
2739 void tree_evaluator::clear_objects (void)
2740 {
2741 std::shared_ptr<stack_frame> frame
2742 = m_call_stack.get_current_stack_frame ();
2743
2744 frame->clear_objects ();
2745 }
2746
2747 void tree_evaluator::clear_variable (const std::string& name)
2748 {
2749 std::shared_ptr<stack_frame> frame
2750 = m_call_stack.get_current_stack_frame ();
2751
2752 frame->clear_variable (name);
2753 }
2754
2755 void tree_evaluator::clear_variable_pattern (const std::string& pattern)
2756 {
2757 std::shared_ptr<stack_frame> frame
2758 = m_call_stack.get_current_stack_frame ();
2759
2760 frame->clear_variable_pattern (pattern);
2761 }
2762
2763 void tree_evaluator::clear_variable_regexp (const std::string& pattern)
2764 {
2765 std::shared_ptr<stack_frame> frame
2766 = m_call_stack.get_current_stack_frame ();
2767
2768 frame->clear_variable_regexp (pattern);
2769 }
2770
2771 void tree_evaluator::clear_variables (void)
2772 {
2773 std::shared_ptr<stack_frame> frame
2774 = m_call_stack.get_current_stack_frame ();
2775
2776 frame->clear_variables ();
2777 }
2778
2779 void tree_evaluator::clear_global_variable (const std::string& name)
2780 {
2781 m_call_stack.clear_global_variable (name);
2782 }
2783
2784 void
2785 tree_evaluator::clear_global_variable_pattern (const std::string& pattern)
2786 {
2787 m_call_stack.clear_global_variable_pattern (pattern);
2788 }
2789
2790 void tree_evaluator::clear_global_variable_regexp(const std::string& pattern)
2791 {
2792 m_call_stack.clear_global_variable_regexp (pattern);
2793 }
2794
2795 void tree_evaluator::clear_global_variables (void)
2796 {
2797 m_call_stack.clear_global_variables ();
2798 }
2799
2800 void tree_evaluator::clear_all (bool force)
2801 {
2802 // FIXME: should this also clear objects?
2803
2804 clear_variables ();
2805 clear_global_variables ();
2806
2807 symbol_table& symtab = m_interpreter.get_symbol_table ();
2808
2809 symtab.clear_functions (force);
2810 }
2811
2812 void tree_evaluator::clear_symbol (const std::string& name)
2813 {
2814 // FIXME: are we supposed to do both here?
2815
2816 clear_variable (name);
2817
2818 symbol_table& symtab = m_interpreter.get_symbol_table ();
2819
2820 symtab.clear_function (name);
2821 }
2822
2823 void tree_evaluator::clear_symbol_pattern (const std::string& pattern)
2824 {
2825 // FIXME: are we supposed to do both here?
2826
2827 clear_variable_pattern (pattern);
2828
2829 symbol_table& symtab = m_interpreter.get_symbol_table ();
2830
2831 symtab.clear_function_pattern (pattern);
2832 }
2833
2834 void tree_evaluator::clear_symbol_regexp (const std::string& pattern)
2835 {
2836 // FIXME: are we supposed to do both here?
2837
2838 clear_variable_regexp (pattern);
2839
2840 symbol_table& symtab = m_interpreter.get_symbol_table ();
2841
2842 symtab.clear_function_regexp (pattern);
2843 }
2844
2845 std::list<std::string> tree_evaluator::global_variable_names (void) const
2846 {
2847 return m_call_stack.global_variable_names ();
2848 }
2849
2850 std::list<std::string> tree_evaluator::top_level_variable_names (void) const
2851 {
2852 return m_call_stack.top_level_variable_names ();
2853 }
2854
2855 std::list<std::string> tree_evaluator::variable_names (void) const
2856 {
2857 return m_call_stack.variable_names ();
2858 }
2859
2860 // Return a pointer to the user-defined function FNAME. If FNAME is empty,
2861 // search backward for the first user-defined function in the
2862 // current call stack.
2863
2864 octave_user_code *
2865 tree_evaluator::get_user_code (const std::string& fname,
2866 const std::string& class_name)
2867 {
2868 octave_user_code *user_code = nullptr;
2869
2870 if (fname.empty ())
2871 user_code = m_call_stack.debug_user_code ();
2872 else
2873 {
2874 std::string name = fname;
2875
2876 if (sys::file_ops::dir_sep_char () != '/' && name[0] == '@')
2877 {
2878 auto beg = name.begin () + 2; // never have @/method
2879 auto end = name.end () - 1; // never have trailing '/'
2880 std::replace (beg, end, '/', sys::file_ops::dir_sep_char ());
2881 }
2882
2883 std::size_t name_len = name.length ();
2884
2885 if (name_len > 2 && name.substr (name_len-2) == ".m")
2886 name = name.substr (0, name_len-2);
2887
2888 if (name.empty ())
2889 return nullptr;
2890
2891 symbol_table& symtab = m_interpreter.get_symbol_table ();
2892
2893 octave_value fcn;
2894 std::size_t p2 = std::string::npos;
2895
2896 if (name[0] == '@')
2897 {
2898 std::size_t p1 = name.find (sys::file_ops::dir_sep_char (), 1);
2899
2900 if (p1 == std::string::npos)
2901 return nullptr;
2902
2903 std::string dispatch_type = name.substr (1, p1-1);
2904
2905 p2 = name.find ('>', p1);
2906
2907 std::string method = name.substr (p1+1, p2-1);
2908
2909 fcn = symtab.find_method (method, dispatch_type);
2910 }
2911 else if (! class_name.empty ())
2912 {
2913 cdef_manager& cdm = m_interpreter.get_cdef_manager ();
2914
2915 fcn = cdm.find_method (class_name, name);
2916
2917 // If there is no classdef method, then try legacy classes.
2918 if (fcn.is_undefined ())
2919 fcn = symtab.find_method (name, class_name);
2920 }
2921 else
2922 {
2923 p2 = name.find ('>');
2924
2925 std::string main_fcn = name.substr (0, p2);
2926
2927 fcn = symtab.find_function (main_fcn);
2928 }
2929
2930 // List of function names sub1>sub2>...
2931 std::string subfuns;
2932
2933 if (p2 != std::string::npos)
2934 subfuns = name.substr (p2+1);
2935
2936 if (fcn.is_defined () && fcn.is_user_code ())
2937 user_code = fcn.user_code_value ();
2938
2939 if (! user_code || subfuns.empty ())
2940 return user_code;
2941
2942 fcn = user_code->find_subfunction (subfuns);
2943
2944 if (fcn.is_undefined ())
2945 return nullptr;
2946
2947 user_code = fcn.user_code_value ();
2948 }
2949
2950 return user_code;
2951 }
2952
2953 std::string
2954 tree_evaluator::current_function_name (bool skip_first) const
2955 {
2956 octave_function *curfcn = m_call_stack.current_function (skip_first);
2957
2958 if (curfcn)
2959 return curfcn->name ();
2960
2961 return "";
2962 }
2963
2964 bool
2965 tree_evaluator::in_user_code (void) const
2966 {
2967 return m_call_stack.current_user_code () != nullptr;
2968 }
2969
2970 void
2971 tree_evaluator::visit_decl_command (tree_decl_command& cmd)
2972 {
2973 if (m_echo_state)
2974 {
2975 int line = cmd.line ();
2976 if (line < 0)
2977 line = 1;
2978 echo_code (line);
2979 m_echo_file_pos = line + 1;
2980 }
2981
2982 if (m_debug_mode)
2983 do_breakpoint (cmd.is_active_breakpoint (*this));
2984
2985 // FIXME: tree_decl_init_list is not derived from tree, so should it
2986 // really have an accept method?
2987
2988 tree_decl_init_list *init_list = cmd.initializer_list ();
2989
2990 if (init_list)
2991 init_list->accept (*this);
2992 }
2993
2994 void
2995 tree_evaluator::visit_decl_elt (tree_decl_elt& elt)
2996 {
2997 tree_identifier *id = elt.ident ();
2998
2999 if (id)
3000 {
3001 if (elt.is_global ())
3002 m_call_stack.make_global (id->symbol ());
3003 else if (elt.is_persistent ())
3004 m_call_stack.make_persistent (id->symbol ());
3005 else
3006 error ("declaration list element not global or persistent");
3007
3008 octave_lvalue ult = id->lvalue (*this);
3009
3010 if (ult.is_undefined ())
3011 {
3012 tree_expression *expr = elt.expression ();
3013
3014 octave_value init_val;
3015
3016 if (expr)
3017 init_val = expr->evaluate (*this);
3018 else
3019 init_val = Matrix ();
3020
3021 ult.assign (octave_value::op_asn_eq, init_val);
3022 }
3023 }
3024 }
3025
3026 template <typename T>
3027 void
3028 tree_evaluator::execute_range_loop (const range<T>& rng, int line,
3029 octave_lvalue& ult,
3030 tree_statement_list *loop_body)
3031 {
3032 octave_idx_type steps = rng.numel ();
3033
3034 if (math::isinf (rng.limit ()))
3035 warning_with_id ("Octave:infinite-loop",
3036 "FOR loop limit is infinite, will stop after %"
3037 OCTAVE_IDX_TYPE_FORMAT " steps", steps);
3038
3039 for (octave_idx_type i = 0; i < steps; i++)
3040 {
3041 if (m_echo_state)
3042 m_echo_file_pos = line;
3043
3044 octave_value val (rng.elem (i));
3045
3046 ult.assign (octave_value::op_asn_eq, val);
3047
3048 if (loop_body)
3049 loop_body->accept (*this);
3050
3051 if (quit_loop_now ())
3052 break;
3053 }
3054 }
3055
3056 void
3057 tree_evaluator::visit_simple_for_command (tree_simple_for_command& cmd)
3058 {
3059 int line = cmd.line ();
3060 if (line < 0)
3061 line = 1;
3062
3063 if (m_echo_state)
3064 {
3065 echo_code (line);
3066 line++;
3067 }
3068
3069 if (m_debug_mode)
3070 do_breakpoint (cmd.is_active_breakpoint (*this));
3071
3072 // FIXME: need to handle PARFOR loops here using cmd.in_parallel ()
3073 // and cmd.maxproc_expr ();
3074
3075 unwind_protect_var<bool> upv (m_in_loop_command, true);
3076
3077 tree_expression *expr = cmd.control_expr ();
3078
3079 octave_value rhs = expr->evaluate (*this);
3080
3081 if (rhs.is_undefined ())
3082 return;
3083
3084 tree_expression *lhs = cmd.left_hand_side ();
3085
3086 octave_lvalue ult = lhs->lvalue (*this);
3087
3088 tree_statement_list *loop_body = cmd.body ();
3089
3090 if (rhs.is_range ())
3091 {
3092 // FIXME: is there a better way to dispatch here?
3093
3094 if (rhs.is_double_type ())
3095 {
3096 execute_range_loop (rhs.range_value (), line, ult, loop_body);
3097 return;
3098 }
3099
3100 // For now, disable all but range<double>.
3101
3102 #if 0
3103 if (rhs.is_int64_type ())
3104 {
3105 execute_range_loop (rhs.int64_range_value (), line, ult, loop_body);
3106 return;
3107 }
3108
3109 if (rhs.is_uint64_type ())
3110 {
3111 execute_range_loop (rhs.uint64_range_value (), line, ult, loop_body);
3112 return;
3113 }
3114
3115 if (rhs.is_int32_type ())
3116 {
3117 execute_range_loop (rhs.int32_range_value (), line, ult, loop_body);
3118 return;
3119 }
3120
3121 if (rhs.is_uint32_type ())
3122 {
3123 execute_range_loop (rhs.uint32_range_value (), line, ult, loop_body);
3124 return;
3125 }
3126
3127 if (rhs.is_int16_type ())
3128 {
3129 execute_range_loop (rhs.int16_range_value (), line, ult, loop_body);
3130 return;
3131 }
3132
3133 if (rhs.is_uint16_type ())
3134 {
3135 execute_range_loop (rhs.uint16_range_value (), line, ult, loop_body);
3136 return;
3137 }
3138
3139 if (rhs.is_int8_type ())
3140 {
3141 execute_range_loop (rhs.int8_range_value (), line, ult, loop_body);
3142 return;
3143 }
3144
3145 if (rhs.is_uint8_type ())
3146 {
3147 execute_range_loop (rhs.uint8_range_value (), line, ult, loop_body);
3148 return;
3149 }
3150
3151 if (rhs.is_single_type ())
3152 {
3153 execute_range_loop (rhs.float_range_value (), line, ult, loop_body);
3154 return;
3155 }
3156 #endif
3157 }
3158
3159 if (rhs.is_scalar_type ())
3160 {
3161 if (m_echo_state)
3162 m_echo_file_pos = line;
3163
3164 ult.assign (octave_value::op_asn_eq, rhs);
3165
3166 if (loop_body)
3167 loop_body->accept (*this);
3168
3169 // Maybe decrement break and continue states.
3170 quit_loop_now ();
3171
3172 return;
3173 }
3174
3175 // Also handle any range types not explicitly handled above, though
3176 // not as efficiently as the specialized code above.
3177
3178 if (rhs.is_range () || rhs.is_matrix_type () || rhs.iscell ()
3179 || rhs.is_string () || rhs.isstruct ())
3180 {
3181 // A matrix or cell is reshaped to 2 dimensions and iterated by
3182 // columns.
3183
3184 dim_vector dv = rhs.dims ().redim (2);
3185
3186 octave_idx_type nrows = dv(0);
3187 octave_idx_type steps = dv(1);
3188
3189 octave_value arg = rhs;
3190 if (rhs.ndims () > 2)
3191 arg = arg.reshape (dv);
3192
3193 if (nrows > 0 && steps > 0)
3194 {
3195 octave_value_list idx;
3196 octave_idx_type iidx;
3197
3198 // for row vectors, use single index to speed things up.
3199 if (nrows == 1)
3200 { 4953 {
3201 idx.resize (1); 4954 m_echo = (ECHO_SCRIPTS | ECHO_FUNCTIONS | ECHO_ALL);
3202 iidx = 0; 4955 m_echo_files.clear ();
3203 } 4956 }
3204 else 4957 else
3205 { 4958 {
3206 idx.resize (2); 4959 std::string file = fcn_file_in_path (arg1);
3207 idx(0) = octave_value::magic_colon_t; 4960 file = sys::env::make_absolute (file);
3208 iidx = 1; 4961
4962 if (file.empty ())
4963 error ("echo: no such file %s", arg1.c_str ());
4964
4965 m_echo |= ECHO_FUNCTIONS;
4966 m_echo_files[file] = true;
3209 } 4967 }
3210 4968 }
3211 for (octave_idx_type i = 1; i <= steps; i++) 4969 else if (arg0 == "off")
4970 {
4971 if (arg1 == "all")
3212 { 4972 {
3213 if (m_echo_state) 4973 m_echo = ECHO_OFF;
3214 m_echo_file_pos = line; 4974 m_echo_files.clear ();
3215 4975 }
3216 // index_op expects one-based indices. 4976 else
3217 idx(iidx) = i; 4977 {
3218 octave_value val = arg.index_op (idx); 4978 std::string file = fcn_file_in_path (arg1);
3219 4979 file = sys::env::make_absolute (file);
3220 ult.assign (octave_value::op_asn_eq, val); 4980
3221 4981 if (file.empty ())
3222 if (loop_body) 4982 error ("echo: no such file %s", arg1.c_str ());
3223 loop_body->accept (*this); 4983
3224 4984 m_echo_files[file] = false;
3225 if (quit_loop_now ())
3226 break;
3227 } 4985 }
3228 } 4986 }
3229 else 4987 else
4988 print_usage ();
4989 }
4990 break;
4991
4992 default:
4993 print_usage ();
4994 break;
4995 }
4996
4997 if (cleanup_pushed)
4998 maybe_set_echo_state ();
4999
5000 return octave_value ();
5001 }
5002
5003 bool tree_evaluator::in_debug_repl (void) const
5004 {
5005 return (m_debugger_stack.empty ()
5006 ? false : m_debugger_stack.top()->in_debug_repl ());
5007 }
5008
5009 void tree_evaluator::dbcont (void)
5010 {
5011 if (! m_debugger_stack.empty ())
5012 m_debugger_stack.top()->dbcont ();
5013 }
5014
5015 void tree_evaluator::dbquit (bool all)
5016 {
5017 if (! m_debugger_stack.empty ())
5018 m_debugger_stack.top()->dbquit (all);
5019 }
5020
5021 static octave_value end_value (const octave_value& value,
5022 octave_idx_type index_position,
5023 octave_idx_type num_indices)
5024 {
5025 dim_vector dv = value.dims ();
5026 int ndims = dv.ndims ();
5027
5028 if (num_indices < ndims)
5029 {
5030 for (int i = num_indices; i < ndims; i++)
5031 dv(num_indices-1) *= dv(i);
5032
5033 if (num_indices == 1)
5034 {
5035 ndims = 2;
5036 dv.resize (ndims);
5037 dv(1) = 1;
5038 }
5039 else
5040 {
5041 ndims = num_indices;
5042 dv.resize (ndims);
5043 }
5044 }
5045
5046 return (index_position < ndims
5047 ? octave_value (dv(index_position)) : octave_value (1.0));
5048 }
5049
5050 octave_value_list
5051 tree_evaluator::evaluate_end_expression (const octave_value_list& args)
5052 {
5053 int nargin = args.length ();
5054
5055 if (nargin != 0 && nargin != 3)
5056 print_usage ();
5057
5058 if (nargin == 3)
5059 {
5060 octave_idx_type index_position
5061 = args(1).xidx_type_value ("end: K must be integer value");
5062
5063 if (index_position < 1)
5064 error ("end: K must be greater than zero");
5065
5066 octave_idx_type num_indices
5067 = args(2).xidx_type_value ("end: N must be integer value");
5068
5069 if (num_indices < 1)
5070 error ("end: N must be greater than zero");
5071
5072 return end_value (args(0), index_position-1, num_indices);
5073 }
5074
5075 // If m_indexed_object is undefined, then this use of 'end' is
5076 // either appearing in a function call argument list or in an
5077 // attempt to index an undefined symbol. There seems to be no
5078 // reasonable way to provide a better error message. So just fail
5079 // with an invalid use message. See bug #58830.
5080
5081 if (m_indexed_object.is_undefined ())
5082 error ("invalid use of 'end': may only be used to index existing value");
5083
5084 octave_value expr_result;
5085
5086 if (m_index_list.empty ())
5087 expr_result = m_indexed_object;
5088 else
5089 {
5090 try
5091 {
5092 // When evaluating "end" with no arguments, we should have
5093 // been called from the built-in Fend function that appears
5094 // in the context of an argument list. Fend will be
5095 // evaluated in its own stack frame. But we need to
5096 // evaluate the partial expression that the special "end"
5097 // token applies to in the calling stack frame.
5098
5099 unwind_action act ([=] (std::size_t frm)
3230 { 5100 {
3231 // Handle empty cases, while still assigning to loop var. 5101 m_call_stack.restore_frame (frm);
3232 ult.assign (octave_value::op_asn_eq, arg); 5102 }, m_call_stack.current_frame ());
3233 } 5103
3234 5104 std::size_t n = m_call_stack.find_current_user_frame ();
3235 return; 5105 m_call_stack.goto_frame (n);
3236 } 5106
3237 5107 // End is only valid inside argument lists used for
3238 error ("invalid type in for loop expression near line %d, column %d", 5108 // indexing. The dispatch class is set by the function that
3239 cmd.line (), cmd.column ()); 5109 // evaluates the argument list.
3240 } 5110
3241 5111 // Silently ignore extra output values.
3242 void 5112
3243 tree_evaluator::visit_complex_for_command (tree_complex_for_command& cmd) 5113 octave_value_list tmp
3244 { 5114 = m_indexed_object.subsref (m_index_type, m_index_list, 1);
3245 int line = cmd.line (); 5115
3246 if (line < 0) 5116 expr_result = tmp.length () ? tmp(0) : octave_value ();
3247 line = 1; 5117
3248 5118 if (expr_result.is_cs_list ())
3249 if (m_echo_state) 5119 err_indexed_cs_list ();
3250 { 5120 }
3251 echo_code (line); 5121 catch (const index_exception&)
3252 line++; 5122 {
3253 } 5123 error ("error evaluating partial expression for END");
3254 5124 }
3255 if (m_debug_mode) 5125 }
3256 do_breakpoint (cmd.is_active_breakpoint (*this)); 5126
3257 5127 if (expr_result.isobject ())
3258 unwind_protect_var<bool> upv (m_in_loop_command, true); 5128 {
3259 5129 // FIXME: is there a better way to lookup and execute a method
3260 tree_expression *expr = cmd.control_expr (); 5130 // that handles all the details like setting the dispatch class
3261 5131 // appropriately?
3262 octave_value rhs = expr->evaluate (*this); 5132
3263 5133 std::string dispatch_class = expr_result.class_name ();
3264 if (rhs.is_undefined ()) 5134
3265 return; 5135 symbol_table& symtab = m_interpreter.get_symbol_table ();
3266 5136
3267 if (! rhs.isstruct ()) 5137 octave_value meth = symtab.find_method ("end", dispatch_class);
3268 error ("in statement 'for [X, Y] = VAL', VAL must be a structure"); 5138
3269 5139 if (meth.is_defined ())
3270 // Cycle through structure elements. First element of id_list 5140 return m_interpreter.feval
3271 // is set to value and the second is set to the name of the 5141 (meth, ovl (expr_result, m_index_position+1, m_num_indices), 1);
3272 // structure element. 5142 }
3273 5143
3274 tree_argument_list *lhs = cmd.left_hand_side (); 5144 return end_value (expr_result, m_index_position, m_num_indices);
3275 5145 }
3276 auto p = lhs->begin (); 5146
3277 5147 octave_value
3278 tree_expression *elt = *p++; 5148 tree_evaluator::PS4 (const octave_value_list& args, int nargout)
3279 5149 {
3280 octave_lvalue val_ref = elt->lvalue (*this); 5150 return set_internal_variable (m_PS4, args, nargout, "PS4");
3281 5151 }
3282 elt = *p; 5152
3283 5153 bool tree_evaluator::echo_this_file (const std::string& file, int type) const
3284 octave_lvalue key_ref = elt->lvalue (*this); 5154 {
3285 5155 if ((type & m_echo) == ECHO_SCRIPTS)
3286 const octave_map tmp_val = rhs.map_value (); 5156 {
3287 5157 // Asking about scripts and echo is enabled for them.
3288 tree_statement_list *loop_body = cmd.body (); 5158 return true;
3289 5159 }
3290 string_vector keys = tmp_val.keys (); 5160
3291 5161 if ((type & m_echo) == ECHO_FUNCTIONS)
3292 octave_idx_type nel = keys.numel (); 5162 {
3293 5163 // Asking about functions and echo is enabled for functions.
3294 for (octave_idx_type i = 0; i < nel; i++) 5164 // Now, which ones?
3295 { 5165
3296 if (m_echo_state) 5166 auto p = m_echo_files.find (file);
3297 m_echo_file_pos = line; 5167
3298 5168 if (m_echo & ECHO_ALL)
3299 std::string key = keys[i]; 5169 {
3300 5170 // Return true ulness echo was turned off for a specific
3301 const Cell val_lst = tmp_val.contents (key); 5171 // file.
3302 5172
3303 octave_idx_type n = val_lst.numel (); 5173 return (p == m_echo_files.end () || p->second);
3304 5174 }
3305 octave_value val = (n == 1) ? val_lst(0) : octave_value (val_lst); 5175 else
3306 5176 {
3307 val_ref.assign (octave_value::op_asn_eq, val); 5177 // Return true if echo is specifically enabled for this file.
3308 key_ref.assign (octave_value::op_asn_eq, key); 5178
3309 5179 return p != m_echo_files.end () && p->second;
3310 if (loop_body) 5180 }
3311 loop_body->accept (*this); 5181 }
3312 5182
3313 if (quit_loop_now ()) 5183 return false;
3314 break; 5184 }
3315 } 5185
3316 } 5186 void tree_evaluator::echo_code (int line)
3317 5187 {
3318 void tree_evaluator::visit_spmd_command (tree_spmd_command& cmd) 5188 std::string prefix = command_editor::decode_prompt_string (m_PS4);
3319 { 5189
3320 // For now, we just execute the commands serially. 5190 octave_function *curr_fcn = m_call_stack.current_function ();
3321 5191
3322 tree_statement_list *body = cmd.body (); 5192 if (curr_fcn && curr_fcn->is_user_code ())
3323 5193 {
3324 if (body) 5194 octave_user_code *code = dynamic_cast<octave_user_code *> (curr_fcn);
3325 body->accept (*this); 5195
3326 } 5196 int num_lines = line - m_echo_file_pos + 1;
3327 5197
3328 octave_value 5198 std::deque<std::string> lines
3329 tree_evaluator::evaluate_anon_fcn_handle (tree_anon_fcn_handle& afh) 5199 = code->get_code_lines (m_echo_file_pos, num_lines);
3330 { 5200
3331 // FIXME: should CMD_LIST be limited to a single expression? 5201 for (auto& elt : lines)
3332 // I think that is what Matlab does. 5202 octave_stdout << prefix << elt << std::endl;
3333 5203 }
3334 symbol_scope new_scope; 5204 }
3335 symbol_scope scope = afh.scope (); 5205
3336 if (scope) 5206 // Decide if it's time to quit a for or while loop.
3337 new_scope = scope.dup (); 5207 bool tree_evaluator::quit_loop_now (void)
3338 5208 {
3339 tree_parameter_list *param_list = afh.parameter_list (); 5209 octave_quit ();
3340 tree_parameter_list *param_list_dup 5210
3341 = param_list ? param_list->dup (new_scope) : nullptr; 5211 // Maybe handle 'continue N' someday...
3342 5212
3343 tree_parameter_list *ret_list = nullptr; 5213 if (m_continuing)
3344 5214 m_continuing--;
3345 tree_statement_list *stmt_list = nullptr; 5215
3346 5216 bool quit = (m_returning || m_breaking || m_continuing);
3347 symbol_scope parent_scope = get_current_scope (); 5217
3348 5218 if (m_breaking)
3349 new_scope.set_parent (parent_scope); 5219 m_breaking--;
3350 new_scope.set_primary_parent (parent_scope); 5220
3351 5221 return quit;
3352 tree_expression *expr = afh.expression (); 5222 }
3353 if (expr) 5223
3354 { 5224 void tree_evaluator::bind_auto_fcn_vars (const string_vector& arg_names,
3355 tree_expression *expr_dup = expr->dup (new_scope); 5225 const Matrix& ignored_outputs,
3356 tree_statement *stmt = new tree_statement (expr_dup, nullptr); 5226 int nargin, int nargout,
3357 stmt_list = new tree_statement_list (stmt); 5227 bool takes_varargs,
3358 } 5228 const octave_value_list& va_args)
3359 5229 {
3360 tree_anon_scopes anon_fcn_ctx (afh); 5230 set_auto_fcn_var (stack_frame::ARG_NAMES, Cell (arg_names));
3361 5231 set_auto_fcn_var (stack_frame::IGNORED, ignored_outputs);
3362 std::set<std::string> free_vars = anon_fcn_ctx.free_variables (); 5232 set_auto_fcn_var (stack_frame::NARGIN, nargin);
3363 5233 set_auto_fcn_var (stack_frame::NARGOUT, nargout);
3364 stack_frame::local_vars_map local_vars; 5234 set_auto_fcn_var (stack_frame::SAVED_WARNING_STATES, octave_value ());
3365 5235
3366 std::shared_ptr<stack_frame> frame 5236 if (takes_varargs)
3367 = m_call_stack.get_current_stack_frame (); 5237 assign ("varargin", va_args.cell_value ());
3368 5238 }
3369 for (auto& name : free_vars) 5239
3370 { 5240 std::string
3371 octave_value val = frame->varval (name); 5241 tree_evaluator::check_autoload_file (const std::string& nm) const
3372 5242 {
3373 if (val.is_defined ()) 5243 if (sys::env::absolute_pathname (nm))
3374 local_vars[name] = val; 5244 return nm;
3375 } 5245
3376 5246 std::string full_name = nm;
3377 octave_user_function *af 5247
3378 = new octave_user_function (new_scope, param_list_dup, ret_list, 5248 octave_user_code *fcn = m_call_stack.current_user_code ();
3379 stmt_list); 5249
3380 5250 bool found = false;
3381 octave_function *curr_fcn = m_call_stack.current_function (); 5251
3382 5252 if (fcn)
3383 bool is_nested = false; 5253 {
3384 5254 std::string fname = fcn->fcn_file_name ();
3385 if (curr_fcn) 5255
3386 { 5256 if (! fname.empty ())
3387 // FIXME: maybe it would be better to just stash curr_fcn 5257 {
3388 // instead of individual bits of info about it? 5258 fname = sys::env::make_absolute (fname);
3389 5259 fname = fname.substr (0, fname.find_last_of (sys::file_ops::dir_sep_str ()) + 1);
3390 // An anonymous function defined inside another nested function 5260
3391 // or parent of a nested function also behaves like a nested 5261 sys::file_stat fs (fname + nm);
3392 // function. 5262
3393 5263 if (fs.exists ())
3394 if (curr_fcn->is_parent_function () || curr_fcn->is_nested_function ())
3395 {
3396 is_nested = true;
3397 af->mark_as_nested_function ();
3398 new_scope.set_nesting_depth (parent_scope.nesting_depth () + 1);
3399 }
3400
3401 af->stash_dir_name (curr_fcn->dir_name ());
3402
3403 new_scope.cache_fcn_file_name (curr_fcn->fcn_file_name ());
3404 new_scope.cache_dir_name (curr_fcn->dir_name ());
3405
3406 // The following is needed so that class method dispatch works
3407 // properly for anonymous functions that wrap class methods.
3408
3409 if (curr_fcn->is_class_method () || curr_fcn->is_class_constructor ())
3410 af->stash_dispatch_class (curr_fcn->dispatch_class ());
3411
3412 af->stash_fcn_file_name (curr_fcn->fcn_file_name ());
3413 }
3414
3415 af->mark_as_anonymous_function ();
3416
3417 octave_value ov_fcn (af);
3418
3419 return (is_nested
3420 ? octave_value (new octave_fcn_handle (ov_fcn, local_vars, frame))
3421 : octave_value (new octave_fcn_handle (ov_fcn, local_vars)));
3422 }
3423
3424 octave_value_list
3425 tree_evaluator::execute_builtin_function (octave_builtin& builtin_function,
3426 int nargout,
3427 const octave_value_list& args)
3428 {
3429 octave_value_list retval;
3430
3431 if (args.has_magic_colon ())
3432 error ("invalid use of colon in function argument list");
3433
3434 profiler::enter<octave_builtin> block (m_profiler, builtin_function);
3435
3436 octave_builtin::fcn fcn = builtin_function.function ();
3437
3438 if (fcn)
3439 retval = (*fcn) (args, nargout);
3440 else
3441 {
3442 octave_builtin::meth meth = builtin_function.method ();
3443
3444 retval = (*meth) (m_interpreter, args, nargout);
3445 }
3446
3447 // Do not allow null values to be returned from functions.
3448 // FIXME: perhaps true builtins should be allowed?
3449
3450 retval.make_storable_values ();
3451
3452 // Fix the case of a single undefined value.
3453 // This happens when a compiled function uses
3454 //
3455 // octave_value retval;
3456 //
3457 // instead of
3458 //
3459 // octave_value_list retval;
3460 //
3461 // the idiom is very common, so we solve that here.
3462
3463 if (retval.length () == 1 && retval.xelem (0).is_undefined ())
3464 retval.clear ();
3465
3466 return retval;
3467 }
3468
3469 octave_value_list
3470 tree_evaluator::execute_mex_function (octave_mex_function& mex_function,
3471 int nargout,
3472 const octave_value_list& args)
3473 {
3474 octave_value_list retval;
3475
3476 if (args.has_magic_colon ())
3477 error ("invalid use of colon in function argument list");
3478
3479 profiler::enter<octave_mex_function> block (m_profiler, mex_function);
3480
3481 retval = call_mex (mex_function, args, nargout);
3482
3483 return retval;
3484 }
3485
3486 octave_value_list
3487 tree_evaluator::execute_user_script (octave_user_script& user_script,
3488 int nargout,
3489 const octave_value_list& args)
3490 {
3491 octave_value_list retval;
3492
3493 std::string file_name = user_script.fcn_file_name ();
3494
3495 if (args.length () != 0 || nargout != 0)
3496 error ("invalid call to script %s", file_name.c_str ());
3497
3498 tree_statement_list *cmd_list = user_script.body ();
3499
3500 if (! cmd_list)
3501 return retval;
3502
3503 // FIXME: Maybe this check belongs in the places where we push a new
3504 // stack frame? Or in the call_stack push method itself?
3505
3506 if (m_call_stack.size () >= static_cast<std::size_t> (m_max_recursion_depth))
3507 error ("max_recursion_depth exceeded");
3508
3509 unwind_protect_var<stmt_list_type> upv (m_statement_context, SC_SCRIPT);
3510
3511 profiler::enter<octave_user_script> block (m_profiler, user_script);
3512
3513 if (echo ())
3514 push_echo_state (tree_evaluator::ECHO_SCRIPTS, file_name);
3515
3516 // FIXME: Should we be using tree_evaluator::eval here?
3517
3518 cmd_list->accept (*this);
3519
3520 if (m_returning)
3521 m_returning = 0;
3522
3523 if (m_breaking)
3524 m_breaking--;
3525
3526 return retval;
3527 }
3528
3529 void
3530 tree_evaluator::visit_octave_user_script (octave_user_script&)
3531 {
3532 // ??
3533 panic_impossible ();
3534 }
3535
3536 octave_value_list
3537 tree_evaluator::execute_user_function (octave_user_function& user_function,
3538 int nargout,
3539 const octave_value_list& xargs)
3540 {
3541 octave_value_list retval;
3542
3543 // If this function is a classdef constructor, extract the first input
3544 // argument, which must be the partially constructed object instance.
3545
3546 octave_value_list args (xargs);
3547 octave_value_list ret_args;
3548
3549 int nargin = args.length ();
3550
3551 if (user_function.is_classdef_constructor ())
3552 {
3553 if (nargin > 0)
3554 {
3555 ret_args = args.slice (0, 1, true);
3556 --nargin;
3557 args = args.slice (1, nargin, true);
3558 }
3559 else
3560 panic_impossible ();
3561 }
3562
3563 // FIXME: this probably shouldn't be a double-precision matrix.
3564 Matrix ignored_outputs = ignored_fcn_outputs ();
3565
3566 tree_parameter_list *param_list = user_function.parameter_list ();
3567
3568 bool takes_varargs = false;
3569 int max_inputs = 0;
3570
3571 if (param_list)
3572 {
3573 takes_varargs = param_list->takes_varargs ();
3574 max_inputs = param_list->length ();
3575 }
3576
3577 if (! takes_varargs && nargin > max_inputs)
3578 {
3579 std::string name = user_function.name ();
3580
3581 if (name.empty ())
3582 name = "@<anonymous>";
3583
3584 error_with_id ("Octave:invalid-fun-call",
3585 "%s: function called with too many inputs",
3586 name.c_str ());
3587 }
3588
3589 define_parameter_list_from_arg_vector (param_list, args);
3590
3591 tree_parameter_list *ret_list = user_function.return_list ();
3592
3593 if (ret_list && ! ret_list->takes_varargs ())
3594 {
3595 int max_outputs = ret_list->length ();
3596
3597 if (nargout > max_outputs)
3598 {
3599 std::string name = user_function.name ();
3600
3601 error_with_id ("Octave:invalid-fun-call",
3602 "%s: function called with too many outputs",
3603 name.c_str ());
3604 }
3605 }
3606
3607 bind_auto_fcn_vars (xargs.name_tags (), ignored_outputs, nargin,
3608 nargout, user_function.takes_varargs (),
3609 user_function.all_va_args (args));
3610
3611 // For classdef constructor, pre-populate the output arguments
3612 // with the pre-initialized object instance, extracted above.
3613
3614 if (user_function.is_classdef_constructor ())
3615 {
3616 if (! ret_list)
3617 error ("%s: invalid classdef constructor, no output argument defined",
3618 user_function.dispatch_class ().c_str ());
3619
3620 define_parameter_list_from_arg_vector (ret_list, ret_args);
3621 }
3622
3623 // FIXME: Maybe this check belongs in the places where we push a
3624 // new stack frame? Or in the call_stack push method itself?
3625
3626 if (m_call_stack.size () >= static_cast<std::size_t> (m_max_recursion_depth))
3627 error ("max_recursion_depth exceeded");
3628
3629 unwind_action act2 ([&user_function] () {
3630 user_function.restore_warning_states ();
3631 });
3632
3633 // Evaluate the commands that make up the function.
3634
3635 unwind_protect_var<stmt_list_type> upv (m_statement_context, SC_FUNCTION);
3636
3637 tree_statement_list *cmd_list = user_function.body ();
3638
3639 if (cmd_list)
3640 {
3641 profiler::enter<octave_user_function>
3642 block (m_profiler, user_function);
3643
3644 if (echo ())
3645 push_echo_state (tree_evaluator::ECHO_FUNCTIONS,
3646 user_function.fcn_file_name ());
3647
3648 if (user_function.is_special_expr ())
3649 {
3650 panic_if (cmd_list->length () != 1);
3651
3652 tree_statement *stmt = cmd_list->front ();
3653
3654 tree_expression *expr = stmt->expression ();
3655
3656 if (expr)
3657 {
3658 m_call_stack.set_location (stmt->line (), stmt->column ());
3659
3660 retval = expr->evaluate_n (*this, nargout);
3661 }
3662 }
3663 else
3664 cmd_list->accept (*this);
3665
3666 if (m_returning)
3667 m_returning = 0;
3668
3669 if (m_breaking)
3670 m_breaking--;
3671 }
3672
3673 // Copy return values out.
3674
3675 if (ret_list && ! user_function.is_special_expr ())
3676 {
3677 Cell varargout;
3678
3679 if (ret_list->takes_varargs ())
3680 {
3681 octave_value varargout_varval = varval ("varargout");
3682
3683 if (varargout_varval.is_defined ())
3684 varargout = varargout_varval.xcell_value ("varargout must be a cell array object");
3685 }
3686
3687 retval = convert_return_list_to_const_vector (ret_list, nargout,
3688 ignored_outputs,
3689 varargout);
3690 }
3691
3692 return retval;
3693 }
3694
3695 void
3696 tree_evaluator::visit_octave_user_function (octave_user_function&)
3697 {
3698 // ??
3699 panic_impossible ();
3700 }
3701
3702 void
3703 tree_evaluator::visit_octave_user_function_header (octave_user_function&)
3704 {
3705 panic_impossible ();
3706 }
3707
3708 void
3709 tree_evaluator::visit_octave_user_function_trailer (octave_user_function&)
3710 {
3711 panic_impossible ();
3712 }
3713
3714 void
3715 tree_evaluator::visit_function_def (tree_function_def& cmd)
3716 {
3717 octave_value fcn = cmd.function ();
3718
3719 octave_function *f = fcn.function_value ();
3720
3721 if (f)
3722 {
3723 std::string nm = f->name ();
3724
3725 symbol_table& symtab = m_interpreter.get_symbol_table ();
3726
3727 symtab.install_cmdline_function (nm, fcn);
3728
3729 // Make sure that any variable with the same name as the new
3730 // function is cleared.
3731
3732 assign (nm);
3733 }
3734 }
3735
3736 void
3737 tree_evaluator::visit_identifier (tree_identifier&)
3738 {
3739 panic_impossible ();
3740 }
3741
3742 void
3743 tree_evaluator::visit_if_clause (tree_if_clause&)
3744 {
3745 panic_impossible ();
3746 }
3747
3748 void
3749 tree_evaluator::visit_if_command (tree_if_command& cmd)
3750 {
3751 if (m_echo_state)
3752 {
3753 int line = cmd.line ();
3754 if (line < 0)
3755 line = 1;
3756 echo_code (line);
3757 m_echo_file_pos = line + 1;
3758 }
3759
3760 // FIXME: tree_if_command_list is not derived from tree, so should it
3761 // really have an accept method?
3762
3763 tree_if_command_list *lst = cmd.cmd_list ();
3764
3765 if (lst)
3766 lst->accept (*this);
3767 }
3768
3769 void
3770 tree_evaluator::visit_if_command_list (tree_if_command_list& lst)
3771 {
3772 for (tree_if_clause *tic : lst)
3773 {
3774 tree_expression *expr = tic->condition ();
3775
3776 if (! (in_debug_repl ()
3777 && m_call_stack.current_frame () == m_debug_frame))
3778 m_call_stack.set_location (tic->line (), tic->column ());
3779
3780 if (m_debug_mode && ! tic->is_else_clause ())
3781 do_breakpoint (tic->is_active_breakpoint (*this));
3782
3783 if (tic->is_else_clause () || is_logically_true (expr, "if"))
3784 {
3785 tree_statement_list *stmt_lst = tic->commands ();
3786
3787 if (stmt_lst)
3788 stmt_lst->accept (*this);
3789
3790 break;
3791 }
3792 }
3793 }
3794
3795 void
3796 tree_evaluator::visit_index_expression (tree_index_expression&)
3797 {
3798 panic_impossible ();
3799 }
3800
3801 void
3802 tree_evaluator::visit_matrix (tree_matrix&)
3803 {
3804 panic_impossible ();
3805 }
3806
3807 void
3808 tree_evaluator::visit_cell (tree_cell&)
3809 {
3810 panic_impossible ();
3811 }
3812
3813 void
3814 tree_evaluator::visit_multi_assignment (tree_multi_assignment&)
3815 {
3816 panic_impossible ();
3817 }
3818
3819 void
3820 tree_evaluator::visit_no_op_command (tree_no_op_command& cmd)
3821 {
3822 if (m_echo_state)
3823 {
3824 int line = cmd.line ();
3825 if (line < 0)
3826 line = 1;
3827 echo_code (line);
3828 m_echo_file_pos = line + 1;
3829 }
3830
3831 if (m_debug_mode && cmd.is_end_of_fcn_or_script ())
3832 do_breakpoint (cmd.is_active_breakpoint (*this), true);
3833 }
3834
3835 void
3836 tree_evaluator::visit_constant (tree_constant&)
3837 {
3838 panic_impossible ();
3839 }
3840
3841 void
3842 tree_evaluator::visit_fcn_handle (tree_fcn_handle&)
3843 {
3844 panic_impossible ();
3845 }
3846
3847 void
3848 tree_evaluator::visit_parameter_list (tree_parameter_list&)
3849 {
3850 panic_impossible ();
3851 }
3852
3853 void
3854 tree_evaluator::visit_postfix_expression (tree_postfix_expression&)
3855 {
3856 panic_impossible ();
3857 }
3858
3859 void
3860 tree_evaluator::visit_prefix_expression (tree_prefix_expression&)
3861 {
3862 panic_impossible ();
3863 }
3864
3865 void
3866 tree_evaluator::visit_return_command (tree_return_command& cmd)
3867 {
3868 if (m_echo_state)
3869 {
3870 int line = cmd.line ();
3871 if (line < 0)
3872 line = 1;
3873 echo_code (line);
3874 m_echo_file_pos = line + 1;
3875 }
3876
3877 if (m_debug_mode)
3878 do_breakpoint (cmd.is_active_breakpoint (*this));
3879
3880 // Act like dbcont.
3881
3882 if (in_debug_repl () && m_call_stack.current_frame () == m_debug_frame)
3883 dbcont ();
3884 else if (m_statement_context == SC_FUNCTION
3885 || m_statement_context == SC_SCRIPT
3886 || m_in_loop_command)
3887 m_returning = 1;
3888 }
3889
3890 void
3891 tree_evaluator::visit_simple_assignment (tree_simple_assignment&)
3892 {
3893 panic_impossible ();
3894 }
3895
3896 void
3897 tree_evaluator::visit_statement (tree_statement& stmt)
3898 {
3899 tree_command *cmd = stmt.command ();
3900 tree_expression *expr = stmt.expression ();
3901
3902 if (cmd || expr)
3903 {
3904 if (! (in_debug_repl ()
3905 && m_call_stack.current_frame () == m_debug_frame))
3906 m_call_stack.set_location (stmt.line (), stmt.column ());
3907
3908 try
3909 {
3910 if (cmd)
3911 {
3912 unwind_protect_var<const std::list<octave_lvalue> *>
3913 upv (m_lvalue_list, nullptr);
3914
3915 cmd->accept (*this);
3916 }
3917 else
3918 {
3919 if (m_echo_state)
3920 {
3921 int line = stmt.line ();
3922 if (line < 0)
3923 line = 1;
3924 echo_code (line);
3925 m_echo_file_pos = line + 1;
3926 }
3927
3928 if (m_debug_mode)
3929 do_breakpoint (expr->is_active_breakpoint (*this));
3930
3931 // FIXME: maybe all of this should be packaged in
3932 // one virtual function that returns a flag saying whether
3933 // or not the expression will take care of binding ans and
3934 // printing the result.
3935
3936 // FIXME: it seems that we should just have to
3937 // evaluate the expression and that should take care of
3938 // everything, binding ans as necessary?
3939
3940 octave_value tmp_result = expr->evaluate (*this, 0);
3941
3942 if (tmp_result.is_defined ())
3943 {
3944 bool do_bind_ans = false;
3945
3946 if (expr->is_identifier ())
3947 do_bind_ans = ! is_variable (expr);
3948 else
3949 do_bind_ans = ! expr->is_assignment_expression ();
3950
3951 if (do_bind_ans)
3952 bind_ans (tmp_result, expr->print_result ()
3953 && statement_printing_enabled ());
3954 }
3955 }
3956 }
3957 catch (const std::bad_alloc&)
3958 {
3959 // FIXME: We want to use error_with_id here so that give users
3960 // control over this error message but error_with_id will
3961 // require some memory allocations. Is there anything we can
3962 // do to make those more likely to succeed?
3963
3964 error_with_id ("Octave:bad-alloc",
3965 "out of memory or dimension too large for Octave's index type");
3966 }
3967 catch (const interrupt_exception&)
3968 {
3969 // If we are debugging, then continue with next statement.
3970 // Otherwise, jump out of here.
3971
3972 if (m_debug_mode)
3973 m_interpreter.recover_from_exception ();
3974 else
3975 throw;
3976 }
3977 catch (const execution_exception& ee)
3978 {
3979 error_system& es = m_interpreter.get_error_system ();
3980
3981 if ((m_interpreter.interactive ()
3982 || application::forced_interactive ())
3983 && ((es.debug_on_error ()
3984 && m_bp_table.debug_on_err (es.last_error_id ()))
3985 || (es.debug_on_caught ()
3986 && m_bp_table.debug_on_caught (es.last_error_id ())))
3987 && in_user_code ())
3988 {
3989 es.save_exception (ee);
3990 es.display_exception (ee);
3991
3992 enter_debugger ();
3993
3994 // It doesn't make sense to continue execution after an
3995 // error occurs so force the debugger to quit all debug
3996 // levels and return the the top prompt.
3997
3998 throw quit_debug_exception (true);
3999 }
4000 else
4001 throw;
4002 }
4003 }
4004 }
4005
4006 void
4007 tree_evaluator::visit_statement_list (tree_statement_list& lst)
4008 {
4009 // FIXME: commented out along with else clause below.
4010 // static octave_value_list empty_list;
4011
4012 auto p = lst.begin ();
4013
4014 if (p != lst.end ())
4015 {
4016 while (true)
4017 {
4018 tree_statement *elt = *p++;
4019
4020 if (! elt)
4021 error ("invalid statement found in statement list!");
4022
4023 octave_quit ();
4024
4025 elt->accept (*this);
4026
4027 if (m_breaking || m_continuing)
4028 break;
4029
4030 if (m_returning)
4031 break;
4032
4033 if (p == lst.end ())
4034 break;
4035 else
4036 {
4037 // Clear previous values before next statement is
4038 // evaluated so that we aren't holding an extra
4039 // reference to a value that may be used next. For
4040 // example, in code like this:
4041 //
4042 // X = rand (N); # refcount for X should be 1
4043 // # after this statement
4044 //
4045 // X(idx) = val; # no extra copy of X should be
4046 // # needed, but we will be faked
4047 // # out if retval is not cleared
4048 // # between statements here
4049
4050 // result_values = empty_list;
4051 }
4052 }
4053 }
4054 }
4055
4056 void
4057 tree_evaluator::visit_switch_case (tree_switch_case&)
4058 {
4059 panic_impossible ();
4060 }
4061
4062 void
4063 tree_evaluator::visit_switch_case_list (tree_switch_case_list&)
4064 {
4065 panic_impossible ();
4066 }
4067
4068 void
4069 tree_evaluator::visit_switch_command (tree_switch_command& cmd)
4070 {
4071 if (m_echo_state)
4072 {
4073 int line = cmd.line ();
4074 if (line < 0)
4075 line = 1;
4076 echo_code (line);
4077 m_echo_file_pos = line + 1;
4078 }
4079
4080 if (m_debug_mode)
4081 do_breakpoint (cmd.is_active_breakpoint (*this));
4082
4083 tree_expression *expr = cmd.switch_value ();
4084
4085 if (! expr)
4086 error ("missing value in switch command near line %d, column %d",
4087 cmd.line (), cmd.column ());
4088
4089 octave_value val = expr->evaluate (*this);
4090
4091 tree_switch_case_list *lst = cmd.case_list ();
4092
4093 if (lst)
4094 {
4095 for (tree_switch_case *t : *lst)
4096 {
4097 if (t->is_default_case () || switch_case_label_matches (t, val))
4098 {
4099 tree_statement_list *stmt_lst = t->commands ();
4100
4101 if (stmt_lst)
4102 stmt_lst->accept (*this);
4103
4104 break;
4105 }
4106 }
4107 }
4108 }
4109
4110 void
4111 tree_evaluator::visit_try_catch_command (tree_try_catch_command& cmd)
4112 {
4113 if (m_echo_state)
4114 {
4115 int line = cmd.line ();
4116 if (line < 0)
4117 line = 1;
4118 echo_code (line);
4119 m_echo_file_pos = line + 1;
4120 }
4121
4122 bool execution_error = false;
4123 octave_scalar_map err_map;
4124
4125 tree_statement_list *try_code = cmd.body ();
4126
4127 if (try_code)
4128 {
4129 // unwind frame before catch block
4130
4131 unwind_protect frame;
4132
4133 interpreter_try (frame);
4134
4135 // The catch code is *not* added to unwind_protect stack; it
4136 // doesn't need to be run on interrupts.
4137
4138 try
4139 {
4140 try_code->accept (*this);
4141 }
4142 catch (const execution_exception& ee)
4143 {
4144 execution_error = true;
4145
4146 error_system& es = m_interpreter.get_error_system ();
4147
4148 es.save_exception (ee);
4149
4150 err_map.assign ("message", es.last_error_message ());
4151 err_map.assign ("identifier", es.last_error_id ());
4152 err_map.assign ("stack", es.last_error_stack ());
4153
4154 m_interpreter.recover_from_exception ();
4155 }
4156
4157 // Actions attached to unwind_protect frame will run here, prior
4158 // to executing the catch block.
4159 }
4160
4161 if (execution_error)
4162 {
4163 tree_statement_list *catch_code = cmd.cleanup ();
4164
4165 if (catch_code)
4166 {
4167 tree_identifier *expr_id = cmd.identifier ();
4168
4169 if (expr_id)
4170 {
4171 octave_lvalue ult = expr_id->lvalue (*this);
4172
4173 ult.assign (octave_value::op_asn_eq, err_map);
4174 }
4175
4176 // perform actual "catch" block
4177 catch_code->accept (*this);
4178 }
4179 }
4180 }
4181
4182 void
4183 tree_evaluator::do_unwind_protect_cleanup_code (tree_statement_list *list)
4184 {
4185 unwind_protect frame;
4186
4187 frame.protect_var (octave_interrupt_state);
4188 octave_interrupt_state = 0;
4189
4190 // We want to preserve the last location info for possible
4191 // backtracking.
4192
4193 frame.add (&call_stack::set_line, &m_call_stack,
4194 m_call_stack.current_line ());
4195
4196 frame.add (&call_stack::set_column, &m_call_stack,
4197 m_call_stack.current_column ());
4198
4199 // Similarly, if we have seen a return or break statement, allow all
4200 // the cleanup code to run before returning or handling the break.
4201 // We don't have to worry about continue statements because they can
4202 // only occur in loops.
4203
4204 frame.protect_var (m_returning);
4205 m_returning = 0;
4206
4207 frame.protect_var (m_breaking);
4208 m_breaking = 0;
4209
4210 try
4211 {
4212 if (list)
4213 list->accept (*this);
4214 }
4215 catch (const execution_exception& ee)
4216 {
4217 error_system& es = m_interpreter.get_error_system ();
4218
4219 es.save_exception (ee);
4220 m_interpreter.recover_from_exception ();
4221
4222 if (m_breaking || m_returning)
4223 frame.discard (2);
4224 else
4225 frame.run (2);
4226
4227 frame.discard (2);
4228
4229 throw;
4230 }
4231
4232 // The unwind_protects are popped off the stack in the reverse of
4233 // the order they are pushed on.
4234
4235 // FIXME: these statements say that if we see a break or
4236 // return statement in the cleanup block, that we want to use the
4237 // new value of the breaking or returning flag instead of restoring
4238 // the previous value. Is that the right thing to do? I think so.
4239 // Consider the case of
4240 //
4241 // function foo ()
4242 // unwind_protect
4243 // fprintf (stderr, "1: this should always be executed\n");
4244 // break;
4245 // fprintf (stderr, "1: this should never be executed\n");
4246 // unwind_protect_cleanup
4247 // fprintf (stderr, "2: this should always be executed\n");
4248 // return;
4249 // fprintf (stderr, "2: this should never be executed\n");
4250 // end_unwind_protect
4251 // endfunction
4252 //
4253 // If we reset the value of the breaking flag, both the returning
4254 // flag and the breaking flag will be set, and we shouldn't have
4255 // both. So, use the most recent one. If there is no return or
4256 // break in the cleanup block, the values should be reset to
4257 // whatever they were when the cleanup block was entered.
4258
4259 if (m_breaking || m_returning)
4260 frame.discard (2);
4261 else
4262 frame.run (2);
4263 }
4264
4265 void
4266 tree_evaluator::visit_unwind_protect_command (tree_unwind_protect_command& cmd)
4267 {
4268 if (m_echo_state)
4269 {
4270 int line = cmd.line ();
4271 if (line < 0)
4272 line = 1;
4273 echo_code (line);
4274 m_echo_file_pos = line + 1;
4275 }
4276
4277 tree_statement_list *cleanup_code = cmd.cleanup ();
4278
4279 tree_statement_list *unwind_protect_code = cmd.body ();
4280
4281 if (unwind_protect_code)
4282 {
4283 try
4284 {
4285 unwind_protect_code->accept (*this);
4286 }
4287 catch (const execution_exception& ee)
4288 {
4289 error_system& es = m_interpreter.get_error_system ();
4290
4291 // FIXME: Maybe we should be able to temporarily set the
4292 // interpreter's exception handling state to something "safe"
4293 // while the cleanup block runs instead of just resetting it
4294 // here?
4295 es.save_exception (ee);
4296 m_interpreter.recover_from_exception ();
4297
4298 // Run the cleanup code on exceptions, so that it is run even
4299 // in case of interrupt or out-of-memory.
4300 do_unwind_protect_cleanup_code (cleanup_code);
4301
4302 // If an error occurs inside the cleanup code, a new
4303 // exception will be thrown instead of the original.
4304 throw;
4305 }
4306 catch (const interrupt_exception&)
4307 {
4308 // The comments above apply here as well.
4309 m_interpreter.recover_from_exception ();
4310 do_unwind_protect_cleanup_code (cleanup_code);
4311 throw;
4312 }
4313
4314 // Also execute the unwind_protect_cleanump code if the
4315 // unwind_protect block runs without error.
4316 do_unwind_protect_cleanup_code (cleanup_code);
4317 }
4318 }
4319
4320 void
4321 tree_evaluator::visit_while_command (tree_while_command& cmd)
4322 {
4323 int line = cmd.line ();
4324 if (line < 0)
4325 line = 1;
4326
4327 if (m_echo_state)
4328 {
4329 echo_code (line);
4330 line++;
4331 }
4332
4333 unwind_protect_var<bool> upv (m_in_loop_command, true);
4334
4335 tree_expression *expr = cmd.condition ();
4336
4337 if (! expr)
4338 panic_impossible ();
4339
4340 for (;;)
4341 {
4342 if (m_echo_state)
4343 m_echo_file_pos = line;
4344
4345 if (m_debug_mode)
4346 do_breakpoint (cmd.is_active_breakpoint (*this));
4347
4348 if (is_logically_true (expr, "while"))
4349 {
4350 tree_statement_list *loop_body = cmd.body ();
4351
4352 if (loop_body)
4353 loop_body->accept (*this);
4354
4355 if (quit_loop_now ())
4356 break;
4357 }
4358 else
4359 break;
4360 }
4361 }
4362
4363 void
4364 tree_evaluator::visit_do_until_command (tree_do_until_command& cmd)
4365 {
4366 int line = cmd.line ();
4367 if (line < 0)
4368 line = 1;
4369
4370 if (m_echo_state)
4371 {
4372 echo_code (line);
4373 line++;
4374 }
4375
4376 unwind_protect_var<bool> upv (m_in_loop_command, true);
4377
4378 tree_expression *expr = cmd.condition ();
4379
4380 if (! expr)
4381 panic_impossible ();
4382
4383 for (;;)
4384 {
4385 if (m_echo_state)
4386 m_echo_file_pos = line;
4387
4388 tree_statement_list *loop_body = cmd.body ();
4389
4390 if (loop_body)
4391 loop_body->accept (*this);
4392
4393 if (quit_loop_now ())
4394 break;
4395
4396 if (m_debug_mode)
4397 do_breakpoint (cmd.is_active_breakpoint (*this));
4398
4399 if (is_logically_true (expr, "do-until"))
4400 break;
4401 }
4402 }
4403
4404 void
4405 tree_evaluator::visit_superclass_ref (tree_superclass_ref&)
4406 {
4407 panic_impossible ();
4408 }
4409
4410 void
4411 tree_evaluator::visit_metaclass_query (tree_metaclass_query&)
4412 {
4413 panic_impossible ();
4414 }
4415
4416 void tree_evaluator::bind_ans (const octave_value& val, bool print)
4417 {
4418 static std::string ans = "ans";
4419
4420 if (val.is_defined ())
4421 {
4422 if (val.is_cs_list ())
4423 {
4424 octave_value_list lst = val.list_value ();
4425
4426 for (octave_idx_type i = 0; i < lst.length (); i++)
4427 bind_ans (lst(i), print);
4428 }
4429 else
4430 {
4431 // FIXME: Maybe assign could also return the assigned value,
4432 // just for convenience?
4433
4434 assign (ans, val);
4435
4436 if (print)
4437 {
4438 // Use varval instead of displaying VAL directly so that
4439 // we get the right type and value for things like
4440 // magic_int values that may mutate when stored.
4441
4442 octave_value_list args = ovl (varval (ans));
4443 args.stash_name_tags (string_vector (ans));
4444 feval ("display", args);
4445 }
4446 }
4447 }
4448 }
4449
4450 void
4451 tree_evaluator::do_breakpoint (tree_statement& stmt)
4452 {
4453 do_breakpoint (stmt.is_active_breakpoint (*this),
4454 stmt.is_end_of_fcn_or_script ());
4455 }
4456
4457 void
4458 tree_evaluator::do_breakpoint (bool is_breakpoint,
4459 bool is_end_of_fcn_or_script)
4460 {
4461 bool break_on_this_statement = false;
4462
4463 if (is_breakpoint)
4464 break_on_this_statement = true;
4465 else if (m_dbstep_flag > 0)
4466 {
4467 if (m_call_stack.current_frame () == m_debug_frame)
4468 {
4469 if (m_dbstep_flag == 1 || is_end_of_fcn_or_script)
4470 {
4471 // We get here if we are doing a "dbstep" or a "dbstep N" and
4472 // the count has reached 1 so that we must stop and return to
4473 // debug prompt. Alternatively, "dbstep N" has been used but
4474 // the end of the frame has been reached so we stop at the last
4475 // line and return to prompt.
4476
4477 break_on_this_statement = true;
4478 }
4479 else
4480 {
4481 // Executing "dbstep N". Decrease N by one and continue.
4482
4483 m_dbstep_flag--;
4484 }
4485
4486 }
4487 else if (m_dbstep_flag == 1
4488 && m_call_stack.current_frame () < m_debug_frame)
4489 {
4490 // We stepped out from the end of a function.
4491
4492 m_debug_frame = m_call_stack.current_frame ();
4493
4494 break_on_this_statement = true;
4495 }
4496 }
4497 else if (m_dbstep_flag == -1)
4498 {
4499 // We get here if we are doing a "dbstep in".
4500
4501 break_on_this_statement = true;
4502
4503 m_debug_frame = m_call_stack.current_frame ();
4504 }
4505 else if (m_dbstep_flag == -2)
4506 {
4507 // We get here if we are doing a "dbstep out". Check for end of
4508 // function and whether the current frame is the same as the
4509 // cached value because we want to step out from the frame where
4510 // "dbstep out" was evaluated, not from any functions called from
4511 // that frame.
4512
4513 if (is_end_of_fcn_or_script
4514 && m_call_stack.current_frame () == m_debug_frame)
4515 m_dbstep_flag = -1;
4516 }
4517
4518 if (! break_on_this_statement)
4519 break_on_this_statement = m_break_on_next_stmt;
4520
4521 m_break_on_next_stmt = false;
4522
4523 if (break_on_this_statement)
4524 {
4525 m_dbstep_flag = 0;
4526
4527 enter_debugger ();
4528 }
4529 }
4530
4531 bool
4532 tree_evaluator::is_logically_true (tree_expression *expr,
4533 const char *warn_for)
4534 {
4535 bool expr_value = false;
4536
4537 m_call_stack.set_location (expr->line (), expr->column ());
4538
4539 octave_value t1 = expr->evaluate (*this);
4540
4541 if (t1.is_defined ())
4542 return t1.is_true ();
4543 else
4544 error ("%s: undefined value used in conditional expression", warn_for);
4545
4546 return expr_value;
4547 }
4548
4549 octave_value
4550 tree_evaluator::max_recursion_depth (const octave_value_list& args,
4551 int nargout)
4552 {
4553 return set_internal_variable (m_max_recursion_depth, args, nargout,
4554 "max_recursion_depth", 0);
4555 }
4556
4557 symbol_info_list
4558 tree_evaluator::glob_symbol_info (const std::string& pattern) const
4559 {
4560 return m_call_stack.glob_symbol_info (pattern);
4561 }
4562
4563 symbol_info_list
4564 tree_evaluator::regexp_symbol_info (const std::string& pattern) const
4565 {
4566 return m_call_stack.regexp_symbol_info (pattern);
4567 }
4568
4569 symbol_info_list
4570 tree_evaluator::get_symbol_info (void)
4571 {
4572 return m_call_stack.get_symbol_info ();
4573 }
4574
4575 symbol_info_list
4576 tree_evaluator::top_scope_symbol_info (void) const
4577 {
4578 return m_call_stack.top_scope_symbol_info ();
4579 }
4580
4581 octave_map tree_evaluator::get_autoload_map (void) const
4582 {
4583 Cell fcn_names (dim_vector (m_autoload_map.size (), 1));
4584 Cell file_names (dim_vector (m_autoload_map.size (), 1));
4585
4586 octave_idx_type i = 0;
4587 for (const auto& fcn_fname : m_autoload_map)
4588 {
4589 fcn_names(i) = fcn_fname.first;
4590 file_names(i) = fcn_fname.second;
4591
4592 i++;
4593 }
4594
4595 octave_map m;
4596
4597 m.assign ("function", fcn_names);
4598 m.assign ("file", file_names);
4599
4600 return m;
4601 }
4602
4603 std::string tree_evaluator::lookup_autoload (const std::string& nm) const
4604 {
4605 std::string retval;
4606
4607 auto p = m_autoload_map.find (nm);
4608
4609 if (p != m_autoload_map.end ())
4610 {
4611 load_path& lp = m_interpreter.get_load_path ();
4612
4613 retval = lp.find_file (p->second);
4614 }
4615
4616 return retval;
4617 }
4618
4619 std::list<std::string> tree_evaluator::autoloaded_functions (void) const
4620 {
4621 std::list<std::string> names;
4622
4623 for (const auto& fcn_fname : m_autoload_map)
4624 names.push_back (fcn_fname.first);
4625
4626 return names;
4627 }
4628
4629 std::list<std::string>
4630 tree_evaluator::reverse_lookup_autoload (const std::string& nm) const
4631 {
4632 std::list<std::string> names;
4633
4634 for (const auto& fcn_fname : m_autoload_map)
4635 if (nm == fcn_fname.second)
4636 names.push_back (fcn_fname.first);
4637
4638 return names;
4639 }
4640
4641 void tree_evaluator::add_autoload (const std::string& fcn,
4642 const std::string& nm)
4643 {
4644 std::string file_name = check_autoload_file (nm);
4645
4646 m_autoload_map[fcn] = file_name;
4647 }
4648
4649 void tree_evaluator::remove_autoload (const std::string& fcn,
4650 const std::string& nm)
4651 {
4652 check_autoload_file (nm);
4653
4654 // Remove function from symbol table and autoload map.
4655 symbol_table& symtab = m_interpreter.get_symbol_table ();
4656
4657 symtab.clear_dld_function (fcn);
4658
4659 m_autoload_map.erase (fcn);
4660 }
4661
4662 octave_value
4663 tree_evaluator::whos_line_format (const octave_value_list& args, int nargout)
4664 {
4665 return set_internal_variable (m_whos_line_format, args, nargout,
4666 "whos_line_format");
4667 }
4668
4669 octave_value
4670 tree_evaluator::silent_functions (const octave_value_list& args, int nargout)
4671 {
4672 return set_internal_variable (m_silent_functions, args, nargout,
4673 "silent_functions");
4674 }
4675
4676 octave_value
4677 tree_evaluator::string_fill_char (const octave_value_list& args, int nargout)
4678 {
4679 return set_internal_variable (m_string_fill_char, args, nargout,
4680 "string_fill_char");
4681 }
4682
4683 // Final step of processing an indexing error. Add the name of the
4684 // variable being indexed, if any, then issue an error. (Will this also
4685 // be needed by pt-lvalue, which calls subsref?)
4686
4687 void tree_evaluator::final_index_error (index_exception& ie,
4688 const tree_expression *expr)
4689 {
4690 std::string extra_message;
4691
4692 if (is_variable (expr))
4693 {
4694 std::string var = expr->name ();
4695
4696 ie.set_var (var);
4697
4698 symbol_table& symtab = m_interpreter.get_symbol_table ();
4699
4700 octave_value fcn = symtab.find_function (var);
4701
4702 if (fcn.is_function ())
4703 {
4704 octave_function *fp = fcn.function_value ();
4705
4706 if (fp && fp->name () == var)
4707 extra_message
4708 = " (note: variable '" + var + "' shadows function)";
4709 }
4710 }
4711
4712 std::string msg = ie.message () + extra_message;
4713
4714 error_with_id (ie.err_id (), "%s", msg.c_str ());
4715 }
4716
4717 octave_value
4718 tree_evaluator::do_who (int argc, const string_vector& argv,
4719 bool return_list, bool verbose)
4720 {
4721 return m_call_stack.do_who (argc, argv, return_list, verbose);
4722 }
4723
4724 octave_value_list
4725 tree_evaluator::make_value_list (tree_argument_list *args,
4726 const string_vector& arg_nm)
4727 {
4728 octave_value_list retval;
4729
4730 if (args)
4731 {
4732 unwind_protect_var<const std::list<octave_lvalue> *>
4733 upv (m_lvalue_list, nullptr);
4734
4735 int len = args->length ();
4736
4737 unwind_protect_var<int> upv2 (m_index_position);
4738 unwind_protect_var<int> upv3 (m_num_indices);
4739
4740 m_num_indices = len;
4741
4742 std::list<octave_value> arg_vals;
4743
4744 int k = 0;
4745
4746 for (auto elt : *args)
4747 {
4748 // FIXME: is it possible for elt to be invalid?
4749
4750 if (! elt)
4751 break;
4752
4753 m_index_position = k++;
4754
4755 octave_value tmp = elt->evaluate (*this);
4756
4757 if (tmp.is_cs_list ())
4758 {
4759 octave_value_list tmp_ovl = tmp.list_value ();
4760
4761 for (octave_idx_type i = 0; i < tmp_ovl.length (); i++)
4762 arg_vals.push_back (tmp_ovl(i));
4763 }
4764 else if (tmp.is_defined ())
4765 arg_vals.push_back (tmp);
4766 }
4767
4768 retval = octave_value_list (arg_vals);
4769 }
4770
4771 octave_idx_type n = retval.length ();
4772
4773 if (n > 0)
4774 retval.stash_name_tags (arg_nm);
4775
4776 return retval;
4777 }
4778
4779 std::list<octave_lvalue>
4780 tree_evaluator::make_lvalue_list (tree_argument_list *lhs)
4781 {
4782 std::list<octave_lvalue> retval;
4783
4784 for (tree_expression *elt : *lhs)
4785 retval.push_back (elt->lvalue (*this));
4786
4787 return retval;
4788 }
4789
4790 void
4791 tree_evaluator::push_echo_state (int type, const std::string& file_name,
4792 int pos)
4793 {
4794 unwind_protect *frame = m_call_stack.curr_fcn_unwind_protect_frame ();
4795
4796 if (frame)
4797 {
4798 push_echo_state_cleanup (*frame);
4799
4800 set_echo_state (type, file_name, pos);
4801 }
4802 }
4803
4804 void
4805 tree_evaluator::set_echo_state (int type, const std::string& file_name,
4806 int pos)
4807 {
4808 m_echo_state = echo_this_file (file_name, type);
4809 m_echo_file_name = file_name;
4810 m_echo_file_pos = pos;
4811 }
4812
4813 void
4814 tree_evaluator::uwp_set_echo_state (bool state, const std::string& file_name,
4815 int pos)
4816 {
4817 m_echo_state = state;
4818 m_echo_file_name = file_name;
4819 m_echo_file_pos = pos;
4820 }
4821
4822 void
4823 tree_evaluator::maybe_set_echo_state (void)
4824 {
4825 octave_function *caller = caller_function ();
4826
4827 if (caller && caller->is_user_code ())
4828 {
4829 octave_user_code *fcn = dynamic_cast<octave_user_code *> (caller);
4830
4831 int type = fcn->is_user_function () ? ECHO_FUNCTIONS : ECHO_SCRIPTS;
4832
4833 std::string file_name = fcn->fcn_file_name ();
4834
4835 // We want the line where "echo" was called, not the line number
4836 // stored in the stack frame that was created for the echo
4837 // function (that will always be -1).
4838
4839 int pos = m_call_stack.current_user_code_line ();
4840
4841 if (pos < 0)
4842 pos = 1;
4843
4844 set_echo_state (type, file_name, pos);
4845 }
4846 }
4847
4848 void
4849 tree_evaluator::push_echo_state_cleanup (unwind_protect& frame)
4850 {
4851 frame.add (&tree_evaluator::uwp_set_echo_state, this,
4852 m_echo_state, m_echo_file_name, m_echo_file_pos);
4853 }
4854
4855 bool tree_evaluator::maybe_push_echo_state_cleanup (void)
4856 {
4857 // This function is expected to be called from ECHO, which would be
4858 // the top of the call stack. If the caller of ECHO is a
4859 // user-defined function or script, then set up unwind-protect
4860 // elements to restore echo state.
4861
4862 unwind_protect *frame = m_call_stack.curr_fcn_unwind_protect_frame ();
4863
4864 if (frame)
4865 {
4866 push_echo_state_cleanup (*frame);
4867 return true;
4868 }
4869
4870 return false;
4871 }
4872
4873
4874 octave_value
4875 tree_evaluator::echo (const octave_value_list& args, int)
4876 {
4877 bool cleanup_pushed = maybe_push_echo_state_cleanup ();
4878
4879 string_vector argv = args.make_argv ();
4880
4881 switch (args.length ())
4882 {
4883 case 0:
4884 if ((m_echo & ECHO_SCRIPTS) || (m_echo & ECHO_FUNCTIONS))
4885 {
4886 m_echo = ECHO_OFF;
4887 m_echo_files.clear ();
4888 }
4889 else
4890 m_echo = ECHO_SCRIPTS;
4891 break;
4892
4893 case 1:
4894 {
4895 std::string arg0 = argv[0];
4896
4897 if (arg0 == "on")
4898 m_echo = ECHO_SCRIPTS;
4899 else if (arg0 == "off")
4900 m_echo = ECHO_OFF;
4901 else
4902 { 5264 {
4903 std::string file = fcn_file_in_path (arg0); 5265 full_name = fname + nm;
4904 file = sys::env::make_absolute (file); 5266 found = true;
4905
4906 if (file.empty ())
4907 error ("echo: no such file %s", arg0.c_str ());
4908
4909 if (m_echo & ECHO_ALL)
4910 {
4911 // Echo is enabled for all functions, so turn it off
4912 // for this one.
4913
4914 m_echo_files[file] = false;
4915 }
4916 else
4917 {
4918 // Echo may be enabled for specific functions.
4919
4920 auto p = m_echo_files.find (file);
4921
4922 if (p == m_echo_files.end ())
4923 {
4924 // Not this one, so enable it.
4925
4926 m_echo |= ECHO_FUNCTIONS;
4927 m_echo_files[file] = true;
4928 }
4929 else
4930 {
4931 // This one is already in the list. Flip the
4932 // status for it.
4933
4934 p->second = ! p->second;
4935 }
4936 }
4937 } 5267 }
4938 } 5268 }
4939 break; 5269 }
4940 5270
4941 case 2: 5271 if (! found)
4942 { 5272 warning_with_id ("Octave:autoload-relative-file-name",
4943 std::string arg0 = argv[0]; 5273 "autoload: '%s' is not an absolute filename",
4944 std::string arg1 = argv[1]; 5274 nm.c_str ());
4945 5275
4946 if (arg1 == "on" || arg1 == "off") 5276 return full_name;
4947 std::swap (arg0, arg1); 5277 }
4948
4949 if (arg0 == "on")
4950 {
4951 if (arg1 == "all")
4952 {
4953 m_echo = (ECHO_SCRIPTS | ECHO_FUNCTIONS | ECHO_ALL);
4954 m_echo_files.clear ();
4955 }
4956 else
4957 {
4958 std::string file = fcn_file_in_path (arg1);
4959 file = sys::env::make_absolute (file);
4960
4961 if (file.empty ())
4962 error ("echo: no such file %s", arg1.c_str ());
4963
4964 m_echo |= ECHO_FUNCTIONS;
4965 m_echo_files[file] = true;
4966 }
4967 }
4968 else if (arg0 == "off")
4969 {
4970 if (arg1 == "all")
4971 {
4972 m_echo = ECHO_OFF;
4973 m_echo_files.clear ();
4974 }
4975 else
4976 {
4977 std::string file = fcn_file_in_path (arg1);
4978 file = sys::env::make_absolute (file);
4979
4980 if (file.empty ())
4981 error ("echo: no such file %s", arg1.c_str ());
4982
4983 m_echo_files[file] = false;
4984 }
4985 }
4986 else
4987 print_usage ();
4988 }
4989 break;
4990
4991 default:
4992 print_usage ();
4993 break;
4994 }
4995
4996 if (cleanup_pushed)
4997 maybe_set_echo_state ();
4998
4999 return octave_value ();
5000 }
5001
5002 bool tree_evaluator::in_debug_repl (void) const
5003 {
5004 return (m_debugger_stack.empty ()
5005 ? false : m_debugger_stack.top()->in_debug_repl ());
5006 }
5007
5008 void tree_evaluator::dbcont (void)
5009 {
5010 if (! m_debugger_stack.empty ())
5011 m_debugger_stack.top()->dbcont ();
5012 }
5013
5014 void tree_evaluator::dbquit (bool all)
5015 {
5016 if (! m_debugger_stack.empty ())
5017 m_debugger_stack.top()->dbquit (all);
5018 }
5019
5020 static octave_value end_value (const octave_value& value,
5021 octave_idx_type index_position,
5022 octave_idx_type num_indices)
5023 {
5024 dim_vector dv = value.dims ();
5025 int ndims = dv.ndims ();
5026
5027 if (num_indices < ndims)
5028 {
5029 for (int i = num_indices; i < ndims; i++)
5030 dv(num_indices-1) *= dv(i);
5031
5032 if (num_indices == 1)
5033 {
5034 ndims = 2;
5035 dv.resize (ndims);
5036 dv(1) = 1;
5037 }
5038 else
5039 {
5040 ndims = num_indices;
5041 dv.resize (ndims);
5042 }
5043 }
5044
5045 return (index_position < ndims
5046 ? octave_value (dv(index_position)) : octave_value (1.0));
5047 }
5048
5049 octave_value_list
5050 tree_evaluator::evaluate_end_expression (const octave_value_list& args)
5051 {
5052 int nargin = args.length ();
5053
5054 if (nargin != 0 && nargin != 3)
5055 print_usage ();
5056
5057 if (nargin == 3)
5058 {
5059 octave_idx_type index_position
5060 = args(1).xidx_type_value ("end: K must be integer value");
5061
5062 if (index_position < 1)
5063 error ("end: K must be greater than zero");
5064
5065 octave_idx_type num_indices
5066 = args(2).xidx_type_value ("end: N must be integer value");
5067
5068 if (num_indices < 1)
5069 error ("end: N must be greater than zero");
5070
5071 return end_value (args(0), index_position-1, num_indices);
5072 }
5073
5074 // If m_indexed_object is undefined, then this use of 'end' is
5075 // either appearing in a function call argument list or in an
5076 // attempt to index an undefined symbol. There seems to be no
5077 // reasonable way to provide a better error message. So just fail
5078 // with an invalid use message. See bug #58830.
5079
5080 if (m_indexed_object.is_undefined ())
5081 error ("invalid use of 'end': may only be used to index existing value");
5082
5083 octave_value expr_result;
5084
5085 if (m_index_list.empty ())
5086 expr_result = m_indexed_object;
5087 else
5088 {
5089 try
5090 {
5091 // When evaluating "end" with no arguments, we should have
5092 // been called from the built-in Fend function that appears
5093 // in the context of an argument list. Fend will be
5094 // evaluated in its own stack frame. But we need to
5095 // evaluate the partial expression that the special "end"
5096 // token applies to in the calling stack frame.
5097
5098 unwind_action act ([=] (std::size_t frm)
5099 {
5100 m_call_stack.restore_frame (frm);
5101 }, m_call_stack.current_frame ());
5102
5103 std::size_t n = m_call_stack.find_current_user_frame ();
5104 m_call_stack.goto_frame (n);
5105
5106 // End is only valid inside argument lists used for
5107 // indexing. The dispatch class is set by the function that
5108 // evaluates the argument list.
5109
5110 // Silently ignore extra output values.
5111
5112 octave_value_list tmp
5113 = m_indexed_object.subsref (m_index_type, m_index_list, 1);
5114
5115 expr_result = tmp.length () ? tmp(0) : octave_value ();
5116
5117 if (expr_result.is_cs_list ())
5118 err_indexed_cs_list ();
5119 }
5120 catch (const index_exception&)
5121 {
5122 error ("error evaluating partial expression for END");
5123 }
5124 }
5125
5126 if (expr_result.isobject ())
5127 {
5128 // FIXME: is there a better way to lookup and execute a method
5129 // that handles all the details like setting the dispatch class
5130 // appropriately?
5131
5132 std::string dispatch_class = expr_result.class_name ();
5133
5134 symbol_table& symtab = m_interpreter.get_symbol_table ();
5135
5136 octave_value meth = symtab.find_method ("end", dispatch_class);
5137
5138 if (meth.is_defined ())
5139 return m_interpreter.feval
5140 (meth, ovl (expr_result, m_index_position+1, m_num_indices), 1);
5141 }
5142
5143 return end_value (expr_result, m_index_position, m_num_indices);
5144 }
5145
5146 octave_value
5147 tree_evaluator::PS4 (const octave_value_list& args, int nargout)
5148 {
5149 return set_internal_variable (m_PS4, args, nargout, "PS4");
5150 }
5151
5152 bool tree_evaluator::echo_this_file (const std::string& file, int type) const
5153 {
5154 if ((type & m_echo) == ECHO_SCRIPTS)
5155 {
5156 // Asking about scripts and echo is enabled for them.
5157 return true;
5158 }
5159
5160 if ((type & m_echo) == ECHO_FUNCTIONS)
5161 {
5162 // Asking about functions and echo is enabled for functions.
5163 // Now, which ones?
5164
5165 auto p = m_echo_files.find (file);
5166
5167 if (m_echo & ECHO_ALL)
5168 {
5169 // Return true ulness echo was turned off for a specific
5170 // file.
5171
5172 return (p == m_echo_files.end () || p->second);
5173 }
5174 else
5175 {
5176 // Return true if echo is specifically enabled for this file.
5177
5178 return p != m_echo_files.end () && p->second;
5179 }
5180 }
5181
5182 return false;
5183 }
5184
5185 void tree_evaluator::echo_code (int line)
5186 {
5187 std::string prefix = command_editor::decode_prompt_string (m_PS4);
5188
5189 octave_function *curr_fcn = m_call_stack.current_function ();
5190
5191 if (curr_fcn && curr_fcn->is_user_code ())
5192 {
5193 octave_user_code *code = dynamic_cast<octave_user_code *> (curr_fcn);
5194
5195 int num_lines = line - m_echo_file_pos + 1;
5196
5197 std::deque<std::string> lines
5198 = code->get_code_lines (m_echo_file_pos, num_lines);
5199
5200 for (auto& elt : lines)
5201 octave_stdout << prefix << elt << std::endl;
5202 }
5203 }
5204
5205 // Decide if it's time to quit a for or while loop.
5206 bool tree_evaluator::quit_loop_now (void)
5207 {
5208 octave_quit ();
5209
5210 // Maybe handle 'continue N' someday...
5211
5212 if (m_continuing)
5213 m_continuing--;
5214
5215 bool quit = (m_returning || m_breaking || m_continuing);
5216
5217 if (m_breaking)
5218 m_breaking--;
5219
5220 return quit;
5221 }
5222
5223 void tree_evaluator::bind_auto_fcn_vars (const string_vector& arg_names,
5224 const Matrix& ignored_outputs,
5225 int nargin, int nargout,
5226 bool takes_varargs,
5227 const octave_value_list& va_args)
5228 {
5229 set_auto_fcn_var (stack_frame::ARG_NAMES, Cell (arg_names));
5230 set_auto_fcn_var (stack_frame::IGNORED, ignored_outputs);
5231 set_auto_fcn_var (stack_frame::NARGIN, nargin);
5232 set_auto_fcn_var (stack_frame::NARGOUT, nargout);
5233 set_auto_fcn_var (stack_frame::SAVED_WARNING_STATES, octave_value ());
5234
5235 if (takes_varargs)
5236 assign ("varargin", va_args.cell_value ());
5237 }
5238
5239 std::string
5240 tree_evaluator::check_autoload_file (const std::string& nm) const
5241 {
5242 if (sys::env::absolute_pathname (nm))
5243 return nm;
5244
5245 std::string full_name = nm;
5246
5247 octave_user_code *fcn = m_call_stack.current_user_code ();
5248
5249 bool found = false;
5250
5251 if (fcn)
5252 {
5253 std::string fname = fcn->fcn_file_name ();
5254
5255 if (! fname.empty ())
5256 {
5257 fname = sys::env::make_absolute (fname);
5258 fname = fname.substr (0, fname.find_last_of (sys::file_ops::dir_sep_str ()) + 1);
5259
5260 sys::file_stat fs (fname + nm);
5261
5262 if (fs.exists ())
5263 {
5264 full_name = fname + nm;
5265 found = true;
5266 }
5267 }
5268 }
5269
5270 if (! found)
5271 warning_with_id ("Octave:autoload-relative-file-name",
5272 "autoload: '%s' is not an absolute filename",
5273 nm.c_str ());
5274
5275 return full_name;
5276 }
5277 5278
5278 DEFMETHOD (max_recursion_depth, interp, args, nargout, 5279 DEFMETHOD (max_recursion_depth, interp, args, nargout,
5279 doc: /* -*- texinfo -*- 5280 doc: /* -*- texinfo -*-
5280 @deftypefn {} {@var{val} =} max_recursion_depth () 5281 @deftypefn {} {@var{val} =} max_recursion_depth ()
5281 @deftypefnx {} {@var{old_val} =} max_recursion_depth (@var{new_val}) 5282 @deftypefnx {} {@var{old_val} =} max_recursion_depth (@var{new_val})