!!!Re: Re: routine_id help
- Posted by Ralf Nieuwenhuijsen <nieuwen at XS4ALL.NL> Feb 16, 1999
- 424 views
>After posting, I experimented a bit; it indeed appears that "routine_id" can't >access ANY forward routines, even in the >same file. This doesn't sound right... the documentation states that forward >access can be achieved, and even notes that >mutual recursion is possible using the function. I had been working under the >assumption that visibility was only limited by >global/local/private scope. Well, about scope, Robert has promised us to fix it. Im at least assuming he fixes it in such way direct forward calling of a routine in *another* include file is possible. (as long as the includes statements match). However, dont take it the wrong way, routine_id is suppose to be used differently. Look at this.. -- run a forward routine a number of times procedure repeat_procedure (integer rp, integer count, sequence arg) for index = 1 to count do call_proc (rp,arg) end for end procedure -- just a procedure procedure barf (sequence s) puts (1, "Barf!! " & s & " Barf!!\n") end procedure repeat_procedure (routine_id ("barf"), 10, " You can make a forward call !! ") What does this do ? This prints out "Barf!! You can make a forward call !! Barf!!" exactly 10 times. A useles example, I know. Look at this, an example, you are likely to see somewhere: ------- A library called procces.e or something sequence filters -- used to store the different routine pointers global function register_filter (integer rp) filters = append(filters, rp) end function global function procces (sequence s) for index = 1 to length(filters) do s = call_func (filters[index], {s}) end for return s end function --- A program include procces.e function pos (object x) return x + ( x < 0 * -2) -- makes all of x positive end function function upper_case (object x) x = x + ( ( 'a' - 'A' ) * ( x >= 'a' and x <= 'z' )) -- make all of x uppercase end function -- registering filters register_filter (routine_id ("pos")) register_filter (routine_id ("upper_case")) puts (1, procces ( -32 & -32 & "two spaces surround this" & -32 & -32 & '\n' ) ) -- prints out: " TWO SPACES SURROUND THIS \n" Do you now see how they are meant to be used ? If you want to use them in the way you did, use this library: -- routine.e sequence names, pointers global procedure add_routine (sequence name, integer rp) integer place place = find (name, names) if place then pointers[place] = rp else names = append(names, name) pointers = append(pointers, rp) end if end procedure global funciton lookup_routine (sequence name) integer place place = find (name, names) if place then return pointers[place] else return -1 -- 'none' equals -1 (the third boolean IMO) end if end function -- end of routine.e How do you use this library ? Well, after the routine that it should be able to call, you say: (you could just do this right after *every* routine and type) procedure indent () puts (1, repeat (' ', 6)) end procedure add_routine ("indent", routine_id ("indent")) Then a library included before is allowed to say lookup_routine ("indent') and get the routine pointer associated. (actually, except for the integer and sequence type restriction, the library is just a general key-association library) So, include the library above, and use lookup_routine () were you previously used routine_id () and use add_routine after each routine you want lookup_routine to be able to find. >Just out of curiosity, if "routine_id" can't call a forward routine, how on >earth can Euphoria implement mutual recursion? >Perhaps my copy of refman.doc is out of date? This is how: -- note: do not try to run this: infinite recursion created (see how Euphoria uses up all your memory, rather than just -- the stack to contiue the recursion as long as possible integer proc_b procedure proc_a () call_proc (proc_b, {}) end procedure procedure proc_b () proc_a () end procedure proc_b = routine_id ("proc_a") -- Simple, not ? Just dont try to run it. It doesnt do anything anyway. I hope this clears things up. Lots of code, but it saves me from having to strangle english to make it say what I want. Ralf N.