comparison src/parse.y @ 5484:2ff5363a16bd

[project @ 2005-10-06 17:12:12 by jwe]
author jwe
date Thu, 06 Oct 2005 17:12:13 +0000
parents e2f85b298a74
children 61d6cebd243b
comparison
equal deleted inserted replaced
5483:d2df058c4319 5484:2ff5363a16bd
36 #include <cstdio> 36 #include <cstdio>
37 37
38 #ifdef YYBYACC 38 #ifdef YYBYACC
39 #include <cstdlib> 39 #include <cstdlib>
40 #endif 40 #endif
41
42 #include <map>
41 43
42 #include "Cell.h" 44 #include "Cell.h"
43 #include "Matrix.h" 45 #include "Matrix.h"
44 #include "cmd-edit.h" 46 #include "cmd-edit.h"
45 #include "cmd-hist.h" 47 #include "cmd-hist.h"
143 std::stack<symbol_table*> symtab_context; 145 std::stack<symbol_table*> symtab_context;
144 146
145 // Name of parent function when parsing function files that might 147 // Name of parent function when parsing function files that might
146 // contain nested functions. 148 // contain nested functions.
147 std::string parent_function_name; 149 std::string parent_function_name;
150
151 // TRUE means we are in the process of autoloading a function.
152 static bool autoloading = false;
153
154 // List of autoloads (function -> file mapping).
155 static std::map<std::string, std::string> autoload_map;
148 156
149 // Forward declarations for some functions defined at the bottom of 157 // Forward declarations for some functions defined at the bottom of
150 // the file. 158 // the file.
151 159
152 // Generic error messages. 160 // Generic error messages.
2508 // If input is coming from a file, issue a warning if the name of 2516 // If input is coming from a file, issue a warning if the name of
2509 // the file does not match the name of the function stated in the 2517 // the file does not match the name of the function stated in the
2510 // file. Matlab doesn't provide a diagnostic (it ignores the stated 2518 // file. Matlab doesn't provide a diagnostic (it ignores the stated
2511 // name). 2519 // name).
2512 2520
2513 if (reading_fcn_file) 2521 if (reading_fcn_file || autoloading)
2514 { 2522 {
2515 if (! lexer_flags.parsing_nested_function 2523 if (! (lexer_flags.parsing_nested_function || autoloading)
2516 && curr_fcn_file_name != id_name) 2524 && curr_fcn_file_name != id_name)
2517 { 2525 {
2518 if (Vwarn_function_name_clash) 2526 if (Vwarn_function_name_clash)
2519 warning ("function name `%s' does not agree with function\ 2527 warning ("function name `%s' does not agree with function\
2520 file name `%s'", id_name.c_str (), curr_fcn_file_full_name.c_str ()); 2528 file name `%s'", id_name.c_str (), curr_fcn_file_full_name.c_str ());
3165 3173
3166 return help_txt; 3174 return help_txt;
3167 } 3175 }
3168 3176
3169 std::string 3177 std::string
3170 get_help_from_file (const std::string& path) 3178 get_help_from_file (const std::string& nm, bool& symbol_found,
3179 bool include_file_info)
3171 { 3180 {
3172 std::string retval; 3181 std::string retval;
3173 3182
3174 if (! path.empty ()) 3183 std::string file = fcn_file_in_path (nm);
3175 { 3184
3176 FILE *fptr = fopen (path.c_str (), "r"); 3185 if (! file.empty ())
3186 {
3187 symbol_found = true;
3188
3189 FILE *fptr = fopen (file.c_str (), "r");
3177 3190
3178 if (fptr) 3191 if (fptr)
3179 { 3192 {
3180 unwind_protect::add (safe_fclose, (void *) fptr); 3193 unwind_protect::add (safe_fclose, (void *) fptr);
3181 3194
3182 retval = gobble_leading_white_space (fptr, true, true, false); 3195 retval = gobble_leading_white_space (fptr, true, true, false);
3196
3197 if (! retval.empty () && include_file_info)
3198 retval = nm + " is the file: " + file + "\n\n" + retval;
3183 3199
3184 unwind_protect::run (); 3200 unwind_protect::run ();
3185 } 3201 }
3186 } 3202 }
3187 3203
3353 unwind_protect::run_frame ("parse_fcn_file"); 3369 unwind_protect::run_frame ("parse_fcn_file");
3354 3370
3355 return script_file_executed; 3371 return script_file_executed;
3356 } 3372 }
3357 3373
3374 std::string
3375 lookup_autoload (const std::string& nm)
3376 {
3377 return
3378 octave_env::make_absolute (Vload_path_dir_path.find (autoload_map[nm]),
3379 octave_env::getcwd ());
3380 }
3381
3358 bool 3382 bool
3359 load_fcn_from_file (const std::string& nm, bool exec_script) 3383 load_fcn_from_file (const std::string& nm, bool exec_script)
3360 { 3384 {
3385 unwind_protect::begin_frame ("load_fcn_from_file");
3386
3361 bool script_file_executed = false; 3387 bool script_file_executed = false;
3362 3388
3363 string_vector names (2); 3389 string_vector names (2);
3364 3390
3365 int nm_len = nm.length (); 3391 int nm_len = nm.length ();
3372 { 3398 {
3373 file = nm; 3399 file = nm;
3374 } 3400 }
3375 else 3401 else
3376 { 3402 {
3377 names[0] = nm + ".oct"; 3403 file = lookup_autoload (nm);
3378 names[1] = nm + ".m"; 3404
3379 3405 if (! file.empty ())
3380 file 3406 {
3381 = octave_env::make_absolute (Vload_path_dir_path.find_first_of (names), 3407 unwind_protect_bool (autoloading);
3382 octave_env::getcwd ()); 3408
3409 autoloading = true;
3410 exec_script = true;
3411 }
3412 else
3413 {
3414 names[0] = nm + ".oct";
3415 names[1] = nm + ".m";
3416
3417 file = octave_env::make_absolute (Vload_path_dir_path.find_first_of (names),
3418 octave_env::getcwd ());
3419 }
3383 } 3420 }
3384 3421
3385 int len = file.length (); 3422 int len = file.length ();
3386 3423
3387 if (len > 4 && file.substr (len-4, len-1) == ".oct") 3424 if (len > 4 && file.substr (len-4, len-1) == ".oct")
3391 } 3428 }
3392 else if (len > 2) 3429 else if (len > 2)
3393 { 3430 {
3394 // These are needed by yyparse. 3431 // These are needed by yyparse.
3395 3432
3396 unwind_protect::begin_frame ("load_fcn_from_file");
3397
3398 unwind_protect_str (curr_fcn_file_name); 3433 unwind_protect_str (curr_fcn_file_name);
3399 unwind_protect_str (curr_fcn_file_full_name); 3434 unwind_protect_str (curr_fcn_file_full_name);
3400 3435
3401 curr_fcn_file_name = nm; 3436 curr_fcn_file_name = nm;
3402 curr_fcn_file_full_name = file; 3437 curr_fcn_file_full_name = file;
3403 3438
3404 script_file_executed = parse_fcn_file (file, exec_script); 3439 script_file_executed = parse_fcn_file (file, exec_script, autoloading);
3405 3440
3406 if (! (error_state || script_file_executed)) 3441 if (! error_state)
3407 force_link_to_function (nm); 3442 {
3408 3443 if (autoloading)
3409 unwind_protect::run_frame ("load_fcn_from_file"); 3444 {
3410 } 3445 script_file_executed = false;
3446 force_link_to_function (nm);
3447 }
3448 else if (! script_file_executed)
3449 force_link_to_function (nm);
3450 }
3451 }
3452
3453 unwind_protect::run_frame ("load_fcn_from_file");
3411 3454
3412 return script_file_executed; 3455 return script_file_executed;
3413 } 3456 }
3414 3457
3415 bool 3458 bool
3416 load_fcn_from_file (symbol_record *sym_rec, bool exec_script) 3459 load_fcn_from_file (symbol_record *sym_rec, bool exec_script)
3417 { 3460 {
3418 return load_fcn_from_file (sym_rec->name (), exec_script); 3461 return load_fcn_from_file (sym_rec->name (), exec_script);
3462 }
3463
3464 DEFCMD (autoload, args, ,
3465 "-*- texinfo -*-\n\
3466 @deftypefn {Built-in Function} {} autoload (@var{function}, @var{file})\n\
3467 Define @var{function} to autoload from @var{file}.\n\
3468 @end deftypefn")
3469 {
3470 octave_value_list retval;
3471
3472 int nargin = args.length ();
3473
3474 if (nargin == 2)
3475 {
3476 string_vector argv = args.make_argv ("autoload");
3477
3478 if (! error_state)
3479 autoload_map[argv[1]] = argv[2];
3480 }
3481 else
3482 print_usage ("autoload");
3483
3484 return retval;
3419 } 3485 }
3420 3486
3421 void 3487 void
3422 source_file (const std::string file_name) 3488 source_file (const std::string file_name)
3423 { 3489 {