home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / pascal / 5175 < prev    next >
Encoding:
Internet Message Format  |  1992-09-02  |  4.4 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnewsm!cbnewsl!att-out!oucsboss!oucsace!tswingle
  2. From: tswingle@oucsace.cs.ohiou.edu (Tom Swingle)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Re: Procedural Variables
  5. Message-ID: <1992Sep2.233128.8426@oucsace.cs.ohiou.edu>
  6. Date: 2 Sep 92 23:31:28 GMT
  7. References: <1992Aug31.145029.16564@doug.cae.wisc.edu>
  8. Organization: Ohio University CS Dept,. Athens
  9. Lines: 113
  10.  
  11. In article <1992Aug31.145029.16564@doug.cae.wisc.edu> chris@castlab.engr.wisc.edu (Christian Rohrmeier) writes:
  12. >Thanks to all those who sent me replies to my question about dynamically calling
  13. >a procedure via a variable.
  14. >
  15. >It seems that is comes down to procedural variables.
  16. >
  17. >The problem is, I can't seem to assign a variable to the proc. variable!
  18. >
  19. >Here sample code given to me by Kjell:
  20. >
  21. >Program TestProc;
  22. >
  23. >Uses Crt;
  24. >
  25. >Var Proc: procedure;
  26. >
  27. >Begin
  28. >  Proc:= ClrScr;
  29. >  Proc;
  30. >End;
  31. >
  32. >This works. However, if I try Proc:='name_of_my_procedure' or to try and assign a
  33. >string varibale to it I get a type mismatch.
  34. >
  35. >Any ideas from the net?
  36. >
  37. >-Chris
  38.  
  39. This solution is "borrowed" from the comp.lang.c newsgroup, where I have seen
  40. this question posted many times before.
  41.  
  42. The problem is that, except for debugging information that may or may not be
  43. included in the executable file that you compile to, the names of your 
  44. procedures or functions no longer exist.  Your program must provide the 
  45. correlation between the name of the subroutine being called and its location.
  46. The best way to do this is to bind them together as fields in an array of
  47. records as follows:
  48.  
  49. -- Begin included program --
  50.  
  51. program showprocnames;
  52.  
  53. uses crt;
  54.  
  55. const
  56.   numcommands=4;
  57.   commandlist:array[1..numcommands] of record
  58.     proc:procedure;
  59.     name:string;
  60.   end=((proc:clrscr;name:'clrscr'),
  61.        (proc:clreol;name:'clreol'),
  62.        (proc:insline;name:'insline'),
  63.        (proc:delline;name:'delline'));
  64.  
  65. var
  66.   command:string;
  67.   i,line:integer;
  68.  
  69. begin
  70.   repeat
  71.     clrscr;
  72.     writeln('Enter a command below from the following list:');
  73.     writeln;
  74.     for i:=1 to numcommands do writeln(i:3,'.  ',commandlist[i].name);
  75.     writeln;
  76.     writeln('I''ll put the cursor on the line below and execute it:');
  77.     line:=wherey;  { Figure out what line to put the cursor on }
  78.     writeln('Your command will be executed here.');
  79.     writeln('We are trained professionals.  Don''t try this at home :-).');
  80.     writeln('This is just filler to show the effect of the command.');
  81.     writeln;
  82.     write('Enter your command or "quit" to quit:  ');
  83.     readln(command);
  84.     if command<>'quit' then
  85.       for i:=1 to numcommands do
  86.         if commandlist[i].name=command then begin
  87.           gotoxy(1,line);  { Go to the appropriate line }
  88.           commandlist[i].proc;  { Do the command }
  89.           gotoxy(1,25);
  90.           write('Command executed.  Press any key...');
  91.           while readkey=#0 do;  { Empty keypress read }
  92.         end;
  93.   until command='quit';
  94. end.
  95.  
  96. -- End included program --
  97.  
  98. The body of the program is really unimportant, it is just to show that it
  99. works.  The important thing to notice is the way I have declared the record to
  100. be a sort of "lookup table" so you can find what procedure to call when you
  101. have put your command together.  
  102.  
  103. Note, however, that all the procedures I used here have no parameters.  If you
  104. want to call procedures with parameters, you need to also do the following:
  105.  
  106.   - Declare a procedural type for each combination of number and type of
  107.     parameter you will be encountering, and either declare separate arrays of
  108.     records to hold each type, or use a variant record so you can store all
  109.     of these types in the same data structure.
  110.   - In addition to the name of the procedure, also store the number and types
  111.     of parameters in the data structure somewhere.
  112.   - Be sure you can reference any variables that your procedure call requires,
  113.     possibly by doing the same trick; i.e., keep a list of variable names and
  114.     pointers to those variables.
  115.  
  116. What you actually end up doing is writing a little mini-interpreter with only
  117. a subset of commands that are necessary to what you are doing.  A pretty 
  118. formidable project if it grows to significant complexity.
  119. -- 
  120. "The problem you are experiencing is  |  "To be sure of hitting the target, 
  121. not with the network nor with the     |  shoot first and, whatever you hit, 
  122. station.  Please adjust your set."    |  call it the target." 
  123. More ramblings from: tswingle@oucsace.cs.ohiou.edu/tswingle@bigbird.cs.ohiou.edu
  124.