comparison src/symtab.cc @ 2975:b9c107cbe388

[project @ 1997-05-15 20:01:00 by jwe]
author jwe
date Thu, 15 May 1997 20:02:55 +0000
parents ca7d3625ee01
children a3556d2adec9
comparison
equal deleted inserted replaced
2974:ebbc34ff7f66 2975:b9c107cbe388
32 32
33 #include "glob-match.h" 33 #include "glob-match.h"
34 #include "str-vec.h" 34 #include "str-vec.h"
35 35
36 #include "error.h" 36 #include "error.h"
37 #include "oct-fcn.h"
38 #include "oct-sym.h"
39 #include "oct-var-ref.h" 37 #include "oct-var-ref.h"
38 #include "ov.h"
40 #include "symtab.h" 39 #include "symtab.h"
41 #include "utils.h" 40 #include "utils.h"
42 #include "variables.h" 41 #include "variables.h"
43 42
44 // Variables and functions. 43 // Variables and functions.
45 44
46 symbol_def::symbol_def (octave_symbol *sym, unsigned int sym_type) 45 symbol_def::symbol_def (void)
47 { 46 {
48 init_state (); 47 init_state ();
49 definition = sym; 48 }
49
50 symbol_def::symbol_def (const octave_value& val, unsigned int sym_type)
51 {
52 init_state ();
53 definition = val;
50 type = sym_type; 54 type = sym_type;
51 } 55 }
52 56
53 void 57 void
54 symbol_def::init_state (void) 58 symbol_def::init_state (void)
55 { 59 {
56 type = UNKNOWN; 60 type = UNKNOWN;
57 eternal = 0; 61 eternal = 0;
58 read_only = 0; 62 read_only = 0;
59 63
60 definition = 0;
61 next_elem = 0; 64 next_elem = 0;
62 count = 0; 65 count = 0;
63 } 66 }
64 67
65 symbol_def::~symbol_def (void)
66 {
67 delete definition;
68 }
69
70 bool 68 bool
71 symbol_def::is_variable (void) const 69 symbol_def::is_variable (void) const
72 { 70 {
73 return (type & USER_VARIABLE || type & BUILTIN_VARIABLE); 71 return (type & USER_VARIABLE || type & BUILTIN_VARIABLE);
74 } 72 }
121 { 119 {
122 return false; 120 return false;
123 } 121 }
124 122
125 void 123 void
126 symbol_def::define (octave_symbol *s, unsigned int sym_type) 124 symbol_def::define (const octave_value& val, unsigned int sym_type)
127 { 125 {
128 definition = s; 126 definition = val;
129 127
130 type = sym_type; 128 type = sym_type;
131 } 129 }
132 130
133 void 131 void
147 symbol_def::make_eternal (void) 145 symbol_def::make_eternal (void)
148 { 146 {
149 eternal = 1; 147 eternal = 1;
150 } 148 }
151 149
152 octave_symbol * 150 octave_value&
153 symbol_def::def (void) const 151 symbol_def::def (void)
154 { 152 {
155 return definition; 153 return definition;
156 } 154 }
157 155
158 string 156 string
219 if (definition) 217 if (definition)
220 retval = definition->help (); 218 retval = definition->help ();
221 return retval; 219 return retval;
222 } 220 }
223 221
224 octave_symbol * 222 octave_value&
225 symbol_record::def (void) const 223 symbol_record::def (void)
226 { 224 {
227 return definition ? definition->def () : false; 225 static octave_value foo;
226
227 return definition ? definition->def () : foo;
228 } 228 }
229 229
230 void 230 void
231 symbol_record::rename (const string& new_name) 231 symbol_record::rename (const string& new_name)
232 { 232 {
295 } 295 }
296 296
297 bool 297 bool
298 symbol_record::is_defined (void) const 298 symbol_record::is_defined (void) const
299 { 299 {
300 return definition ? (definition->def () != 0) : false; 300 return (definition != 0);
301 } 301 }
302 302
303 bool 303 bool
304 symbol_record::is_read_only (void) const 304 symbol_record::is_read_only (void) const
305 { 305 {
369 } 369 }
370 370
371 if (definition->symbol_type () == symbol_def::BUILTIN_VARIABLE) 371 if (definition->symbol_type () == symbol_def::BUILTIN_VARIABLE)
372 sym_type = symbol_def::BUILTIN_VARIABLE; 372 sym_type = symbol_def::BUILTIN_VARIABLE;
373 373
374 definition->define (new octave_value (v), sym_type); 374 definition->define (v, sym_type);
375 } 375 }
376 376
377 return retval; 377 return retval;
378 } 378 }
379 379
404 { 404 {
405 symbol_def *old_def = pop_def (); 405 symbol_def *old_def = pop_def ();
406 maybe_delete (old_def); 406 maybe_delete (old_def);
407 } 407 }
408 408
409 push_def (new symbol_def (new octave_value (v), 409 push_def (new symbol_def (v, symbol_def::BUILTIN_FUNCTION));
410 symbol_def::BUILTIN_FUNCTION));
411 410
412 definition->count = 1; 411 definition->count = 1;
413 412
414 return 1; 413 return 1;
415 } 414 }
430 { 429 {
431 symbol_def *old_def = pop_def (); 430 symbol_def *old_def = pop_def ();
432 maybe_delete (old_def); 431 maybe_delete (old_def);
433 } 432 }
434 433
435 push_def (new symbol_def (f, sym_type)); 434 octave_value tmp (f);
435
436 push_def (new symbol_def (tmp, sym_type));
436 437
437 definition->count = 1; 438 definition->count = 1;
438 439
439 return 1; 440 return 1;
440 } 441 }
500 } 501 }
501 502
502 void 503 void
503 symbol_record::mark_as_linked_to_global (void) 504 symbol_record::mark_as_linked_to_global (void)
504 { 505 {
505 linked_to_global = 1; 506 if (is_formal_parameter ())
507 error ("can't make function parameter `%s' global", nm.c_str ());
508 else if (is_static ())
509 error ("can't make static variable `%s' global", nm.c_str ());
510 else
511 linked_to_global = 1;
506 } 512 }
507 513
508 bool 514 bool
509 symbol_record::is_linked_to_global (void) const 515 symbol_record::is_linked_to_global (void) const
510 { 516 {
513 519
514 void 520 void
515 symbol_record::mark_as_static (void) 521 symbol_record::mark_as_static (void)
516 { 522 {
517 if (is_linked_to_global ()) 523 if (is_linked_to_global ())
518 error ("can't make global variable static"); 524 error ("can't make global variable `%s' static", nm.c_str ());
519 else if (is_formal_parameter ()) 525 else if (is_formal_parameter ())
520 error ("can't make formal parameter static"); 526 error ("can't make formal parameter `%s' static", nm.c_str ());
521 else 527 else
522 tagged_static = 1; 528 tagged_static = 1;
523 } 529 }
524 530
525 bool 531 bool
526 symbol_record::is_static (void) const 532 symbol_record::is_static (void) const
527 { 533 {
528 return tagged_static; 534 return tagged_static;
529 } 535 }
530 536
531 octave_value 537 octave_value&
532 symbol_record::variable_value (void) const 538 symbol_record::variable_value (void)
533 { 539 {
534 octave_value retval; 540 static octave_value foo;
535 541
536 if (is_variable ()) 542 return is_variable () ? def () : foo;
537 {
538 octave_symbol *tmp = def ();
539 retval = tmp->eval ();
540 }
541
542 return retval;
543 } 543 }
544 544
545 octave_variable_reference 545 octave_variable_reference
546 symbol_record::variable_reference (void) 546 symbol_record::variable_reference (void)
547 { 547 {
552 { 552 {
553 if (! (is_formal_parameter () || is_linked_to_global ())) 553 if (! (is_formal_parameter () || is_linked_to_global ()))
554 link_to_builtin_variable (this); 554 link_to_builtin_variable (this);
555 555
556 if (! is_defined ()) 556 if (! is_defined ())
557 define (octave_value ()); 557 {
558 } 558 octave_value tmp;
559 559 define (tmp);
560 return octave_variable_reference 560 }
561 (static_cast<octave_value *> (def ()), sv_fcn); 561 }
562
563 return octave_variable_reference (&(def ()), sv_fcn);
562 } 564 }
563 565
564 symbol_record * 566 symbol_record *
565 symbol_record::next (void) const 567 symbol_record::next (void) const
566 { 568 {
660 symbol_record_info::symbol_record_info (void) 662 symbol_record_info::symbol_record_info (void)
661 : initialized (0), nr (-1), nc (-1), type (symbol_def::UNKNOWN), 663 : initialized (0), nr (-1), nc (-1), type (symbol_def::UNKNOWN),
662 hides (SR_INFO_NONE), eternal (0), read_only (0), nm (), 664 hides (SR_INFO_NONE), eternal (0), read_only (0), nm (),
663 const_type () { } 665 const_type () { }
664 666
665 symbol_record_info::symbol_record_info (const symbol_record& sr) 667 symbol_record_info::symbol_record_info (symbol_record& sr)
666 : initialized (0), nr (-1), nc (-1), type (sr.type ()), 668 : initialized (0), nr (-1), nc (-1), type (sr.type ()),
667 hides (SR_INFO_NONE), eternal (0), read_only (0), nm (), 669 hides (SR_INFO_NONE), eternal (0), read_only (0), nm (),
668 const_type () 670 const_type ()
669 { 671 {
670 if (sr.is_variable () && sr.is_defined ()) 672 if (sr.is_variable () && sr.is_defined ())
671 { 673 {
672 // Would be nice to avoid this cast. XXX FIXME XXX 674 octave_value tmp = sr.def ();
673 675
674 octave_symbol *tmp = sr.def (); 676 const_type = tmp.type_name ();
675 677
676 octave_value vtmp = tmp->eval (); 678 nr = tmp.rows ();
677 679 nc = tmp.columns ();
678 const_type = vtmp.type_name ();
679
680 nr = vtmp.rows ();
681 nc = vtmp.columns ();
682 680
683 symbol_def *sr_def = sr.definition; 681 symbol_def *sr_def = sr.definition;
684 symbol_def *hidden_def = sr_def->next_elem; 682 symbol_def *hidden_def = sr_def->next_elem;
685 683
686 if (hidden_def) 684 if (hidden_def)