From bug-octave-request at bevo dot che dot wisc dot edu Mon Nov 20 16:02:27 2000 Subject: Fw: curr_sym_tab? From: "John W. Eaton" To: "Stef Pillaert (KAHO)" Cc: "bug-octave" Date: Mon, 20 Nov 2000 16:01:53 -0600 On 21-May-2000, Stef Pillaert (KAHO) wrote: | Sorry to post this again (it was on the help-list), but I didn't find a | solution yet for using "eval" in a .cc function, without having problems | with local-global scopes....Perhaps someone on the bug-list can help me | out...I really don't know if it is a bug, or if I'm doing something wrong... | Any help appreciated a lot | Stef. | (Oh yes, octave is compiled with atlas (static lib), but I don't think that | matters, does it?) | | > In octave, I have a function tst, X stays local in the | > function tst, and doesn't have an effect on X outside the function: | | > octave:1> function tst(A,Str) | > > X=A/2; | > > tst=eval(Str); | > > endfunction | > octave:2> tst(5,"X*6") | > ans = 15 | > octave:3> X=12; | > octave:4> tst(X,"X*6") | > ans = 36 | > octave:5> tst(X+2,"X*6") | > ans = 42 | > octave:6> X | > X = 12 | > | > This all seems normal. | > Now, I try to do the same in an .oct function: | > | > # include | > | > extern octave_value_list Feval (const octave_value_list&, int); | > | > DEFUN_DLD (test,args,nargout, | > " ") | > | > { | > const Matrix A = args(0).matrix_value(); | > const string Str = args(1).string_value(); | > const Matrix X = A/2; | > octave_value_list tmp; | > tmp(0)=octave_value(Str); | > const octave_value_list retval = Feval(tmp,1); | > return retval; | > } | > | > This is what I get: | > | > octave:1> test(5,"X*6") | > error: undefined near line 0 column 1 | > error: evaluating binary operator *' near line 0, column 2 | > octave:1> X=12; | > octave:2> test(X,"X*6") | > ans = 72 | > ans = 72 | > octave:3> test(X+2,"X*6") | > ans = 72 | > ans = 72 | > octave:4> X | > X = 12 | > | > It seems logical that the first call doesn't work (X isn't known as a | local | > symbol), but is it logical that X seems to be known, when defined outside | > the function (as if it was global?). | > | > I changed the .oct function (adding the set_local_value function): | > | > # include | > #include | > #include | > | > extern octave_value_list Feval (const octave_value_list&, int); | > | > void set_local_value (const string& nm, const octave_value& val) | > { | > symbol_record *const sr = curr_sym_tab->lookup (nm, true); | > | > if (sr) | > sr->define (val); | > else | > panic_impossible (); | > } | > | > DEFUN_DLD (test,args,nargout, | > " ") | > | > | | > Matrix A = args(0).matrix_value(); | > string Str = args(1).string_value(); | > Matrix X = A/2; | > set_local_value ("X",X); | > octave_value_list tmp; | > tmp(0)=octave_value(Str); | > octave_value_list retval = Feval(tmp,1); | > return retval; | > } | > | > The results I get now: | > | > octave:1> test(5,"X*6") | > ans = 15 | > ans = 15 | > octave:2> X=12; | > octave:3> test(X,"X*6") | > ans = 36 | > ans = 36 | > octave:4> test(X+2,"X*6") | > ans = 24 | > ans = 24 | > octave:5> X | > X = 4 | > | > I always believed that the function set_local_value (I think I found it in | > the archives somewhere long ago...) only declared the symbol | > to be local, but the above shows that it seems to be globally defined. Is | > this me misusing the function set_local_value, or is there something | wrong? | > | > Or is there another way to translate my tst function into an .oct-file (I | > remember that there is a function "feval" instead of Ffeval that can be | used in .oct files, | > but is there also a "eval" that can be used in an .oct file instead of | > Feval? And will the above problems be avoided? I looked in parse.y, but | > didn't find my way in it, sorry... | > | > Thanks for any help or suggestions... Sorry for the long delay. This is not a bug. Octave does not automatically introduce a new scope for dynamically linked functions, so your expression is evaluated with curr_sym_tab == top_level_sym_tab. If you want to introduce a new scope, you'll have to do it yourself. Look at octave_user_function::do_multi_index_op in src/ov-usr-fcn.cc to see some of the things you might want to do to make sure that your local symbol table will work correctly if the function you are defining happens to be called recursively, etc. jwe ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------