Board index » delphi » Executing a procedure by its name as String...

Executing a procedure by its name as String...

Quote
"nILS Podewski" <n...@brennsoft.net> wrote in message

news:3a9baf6b$0$131$9b622d9e@news.freenet.de...

Quote
> My question is fairly simple i guess... I have got a string that
holds the
> name of a procedure. How do I execute that procedure?

The only language I've used that did this directly was dBASE III (and
it's relatives).  I suspect a non-compiled non-tokenized BASIC could
do it, but I never used one that did.

You'll have to provide some kind of procedure-to-textual_name binding
at compile time, then look up the textual name to execute the
procedure.

    - Bill

 

Re:Executing a procedure by its name as String...


In article <%7Xm6.12791$CW1.10439...@typhoon.ne.mediaone.net>,
William J. Leary Jr. <Bill_Le...@msn.com> wrote:

Quote
> I suspect a non-compiled non-tokenized BASIC could
>do it, but I never used one that did.

At least Microsoft BASIC could execute any string. You just had to
store it on disk with proper line number and then merge it to your code.
In compiled languages such does not work.

Osmo

Re:Executing a procedure by its name as String...


On 28 Feb 2001 16:05:14 GMT, Osmo Ronkanen scribbled:

Quote
>In article <%7Xm6.12791$CW1.10439...@typhoon.ne.mediaone.net>,
>William J. Leary Jr. <Bill_Le...@msn.com> wrote:
>> I suspect a non-compiled non-tokenized BASIC could
>>do it, but I never used one that did.

>At least Microsoft BASIC could execute any string. You just had to
>store it on disk with proper line number and then merge it to your code.
>In compiled languages such does not work.

Well, there is REXX's INTERPRET instruction.
And, don't forget ForTran's computed GOTO's.

Jonesy
--
  | Marvin L Jones       | jonz           |  W3DHJ   |  OS/2
  |  Gunnison, Colorado  |  @             |  Jonesy  |  linux   __
  |   7,703' -- 2,345m   |   frontier.net |  DM68mn             SK

Re:Executing a procedure by its name as String...


Quote
"Osmo Ronkanen" <ronka...@cc.helsinki.fi> wrote in message

news:97j7jq$k7n$1@oravannahka.helsinki.fi...

Quote
> At least Microsoft BASIC could execute any string. You just had to
> store it on disk with proper line number and then merge it to your
code.
> In compiled languages such does not work.

I never encountered that feature in an MSB program I worked on myself.
I used MERGE a few times, but always with pre-tokenized code, not
plain text.

    - Bill

Re:Executing a procedure by its name as String...


"William J. Leary Jr." <Bill_Le...@msn.com> wrote in message
news:Jlqn6.283$3F2.631467@typhoon.ne.mediaone.net...

Quote
> "Osmo Ronkanen" <ronka...@cc.helsinki.fi> wrote in message
> news:97j7jq$k7n$1@oravannahka.helsinki.fi...
> > At least Microsoft BASIC could execute any string. You just had to
> > store it on disk with proper line number and then merge it to your
> code.
> > In compiled languages such does not work.

> I never encountered that feature in an MSB program I worked on
myself.
> I used MERGE a few times, but always with pre-tokenized code, not
> plain text.

IIRC on the Commodore PET one could also execute a string. Just clear
the screen, print the line containing the string, followed by an
unnumbered RUN or GOTO on the next line; move the cursor to the top of
the screen; stuff a couple of carriage returns in the keyboard buffer
and let the screen editor have its way with your code.

Sometimes it's nice that we've moved on a bit since then.

FP

Re:Executing a procedure by its name as String...


Marco van de Voort wrote:

Quote
> There is a different way though, so called procedure variables. These are
> variables that can hold the location of a procedure of a certain type. (the
> type defined with the variable):

> procedure testprocedure1(param1 :longint; var param2:longint);

> begin
>  writeln('hello procedure1');
> end;

> procedure testprocedure2(param1 :longint; var param2:longint);

> begin
>  writeln('hello procedure2');
> end;

> type Aproceduretype=procedure (param1 :longint; var param2:longint);

> var myprocedure : AProcedureType;
>     SomeValue   : Longint;
> begin
>  MyProcedure:=Testprocedure1;
>  MyProcedure(1,SomeValue);
>  MyProcedure:=Testprocedure2;
>  MyProcedure(1,SomeValue);
> end.

> NOTE: (just to be complete) The above is TP style, some compilers (like some
> modi of Free Pascal) will require you to use a @ when assigning:

For anyone new to this (like me!): I've just realised why I couldn't get this to
work under TP7: the procedures need to be declared "far", eg:
    procedure testprocedure1(param1 :longint; var param2:longint); far;

David.

Re:Executing a procedure by its name as String...


JRS:  In article <97lilt$9u...@kermit.esat.net>, seen in
news:comp.lang.pascal.borland, Frank Peelo
<fpe...@portablesolutions.com> wrote at Thu, 1 Mar 2001 13:15:48 :-

Quote

>IIRC on the Commodore PET one could also execute a string. Just clear
>the screen, print the line containing the string, followed by an
>unnumbered RUN or GOTO on the next line; move the cursor to the top of
>the screen; stuff a couple of carriage returns in the keyboard buffer
>and let the screen editor have its way with your code.

It has its uses : this is part of the javascript in my js-maths.htm :

function TestF(Fn, p) { var N = 'Frm' + Private++
 document.writeln('<form name='+N+'><tt>', Fn, '( ',
  '<input type=text name=InA size=12> ')
 if (p>1) document.writeln(', &#160;<input type=text name=InB size=3> ')
 if (p>2) document.writeln(', &#160;<input type=text name=InC size=3> ')
 document.writeln(') &#160; <input type=button name=DoIt value="="',
   ' onClick="'+N+'.Ans.value='+
   Fn+'('+N+'.InA.value-0, '+N+'.InB.value-0, '+N+'.InC.value-0)',
  '">',
  ' &#160; <input type=text name=Ans size=22 readonly>',
  '<\/tt><\/form><p>') }

Note that the string parameter Fn is, in the 2nd line, written as a
string, and in the 8th line is executed as a function.  A typical call is
        TestF('OKStrOfMN', 3)

Note also that sequential calls generate forms Frm1, Frm2, ...

In Pascal one would need
        TestF('OKStrOfMN', OKStrOfMN, 3) ;

The above is a tester for emulators of Str(X:m:n, S) and similar.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

Re:Executing a procedure by its name as String...


Quote
"Frank Peelo" <fpe...@portablesolutions.com> wrote in message

news:97lilt$9up$1@kermit.esat.net...

Quote
> IIRC on the Commodore PET one could also execute a string. Just
clear
> the screen, print the line containing the string, followed by an
> unnumbered RUN or GOTO on the next line; move the cursor to the top
of
> the screen; stuff a couple of carriage returns in the keyboard
buffer
> and let the screen editor have its way with your code.

Now that you've pinged my memory, I seem to recall using some trick
like that once or twice.  I can't recall the machine or the BASIC
version, but I do recall doing it.

Quote
> Sometimes it's nice that we've moved on a bit since then.

Amazing "features" we used to find.

    - Bill

Re:Executing a procedure by its name as String...


In article <QT3o6.6262$3F2.1949...@typhoon.ne.mediaone.net>, William J.

Quote
Leary Jr. wrote:
>"Frank Peelo" <fpe...@portablesolutions.com> wrote in message
>news:97lilt$9up$1@kermit.esat.net...
>> IIRC on the Commodore PET one could also execute a string. Just
>clear
>> the screen, print the line containing the string, followed by an
>> unnumbered RUN or GOTO on the next line; move the cursor to the top
>of
>> the screen; stuff a couple of carriage returns in the keyboard
>buffer
>> and let the screen editor have its way with your code.

>Now that you've pinged my memory, I seem to recall using some trick
>like that once or twice.  I can't recall the machine or the BASIC
>version, but I do recall doing it.

The Commodore 64 also could do this, to create a kind of self modifying
basic.

Re:Executing a procedure by its name as String...


Quote
nILS Podewski wrote:

> My question is fairly simple i guess... I have got a string
> that holds the
> name of a procedure. How do I execute that procedure?

OT:

In addition to all the interesting solutions prepared by the
experts here: I suggest that you have a look at FORTH

There is an older page about ATLAN 4th on my homepage with
interesting links, if you do not want to ask the search
engines.

Greetings!
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria +43-7219-7035-0
Muehlviertler Elektronik Glaser.  Industrial control and instrumentation
http://members.eunet.at/meg-glaser/

Re:Executing a procedure by its name as String...


Le Tue, 27 Feb 2001 23:55:39 GMT, "William J. Leary Jr."
<Bill_Le...@msn.com> crivait:

Quote
>The only language I've used that did this directly was dBASE III (and
>it's relatives).  I suspect a non-compiled non-tokenized BASIC could
>do it, but I never used one that did.

Consider also APL and J...

Re:Executing a procedure by its name as String...


I can do something close to what you want with pointers. Try this out for size.

Program

Var   DoSomeProc: Pointer;

Procedure First; Far;
Begin
End;

Procedure Second; Far;
Begin
End;

Procedure DoWhatever; Assembler;
Asm
     Call DoSomeProc;
End;

Begin
DoSomeProc:=@First;
DoWhatever;
DoSomeProc:=@Second;
DoWhatever;
End.

Quote
nILS Podewski wrote:
> My question is fairly simple i guess... I have got a string that holds the
> name of a procedure. How do I execute that procedure?

> Example:

> const
>   Proc = 'DoSomething(1);

> procedure DoSomething(howoften : byte);
> begin
>   ...
> end;

> begin
>   { Here I'd like to run that procedure, but I only have the String with its
> name...
>     so I'm looking for something like: }
>   ExecProcFromString(Proc);
> end.

> TIA,

> nILS Podewski

Re:Executing a procedure by its name as String...


"Marco van de Voort" <mar...@toad.stack.nl> wrote in message

Quote
> The Commodore 64 also could do this, to create a kind of self
modifying
> basic.

Yes, that was it.  I never owned a C64 myself, but a friend did and he
demonstrated this to me.  I wasn't sure if I was more surprised or
impressed.  It was interesting, though.

    - Bill

Other Threads