Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2007-10

Calling a function by name (as string) read from a text file

20 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jilani Khaldi

Hi All,

I have this program:

program mainfun;
{$mode objfpc}
uses
   SysUtils;
type
   SetupFun = function(const vect: array of double): double;
MyRec = record
   x: array of double;
   myFunName: ShortString; // function name to evoke
end;
function SetMeUp(const x1: array of double): double;
var
   i: integer;
   sum: double;
begin
   sum := 0.0;
   for i := 0 to High(x1) do
     sum := sum + x1[i];
   result := sum;
end;
// main
var
   MR: MyRec;
   y: double;
   st: string;
   f: text;
   i, count: integer;
   evokeFun: SetupFun;
begin
   // read MR from a text file...
   assign(f, 'data.txt');
   {$I-}
   reset(f);
   {$I+}
   readln(f, MR.myFunName);
   readln(f, count);
   setlength(MR.x,count+1);
   for i := 0 to count do
     readln(f, MR.x[i]);
   closefile(f);
   writeln(MR.myFunName);
   writeln(High(MR.x));
   for i := 0 to High(MR.x) do
     writeln(MR.x[i]);
{////////////////////////////////////////////////
Is there a way to "connect" the name of the function as a string "evoke"
to the true function "evokeFun: SetupFun;" getting a new function
"newEvokeFun: SetupFun;" so I can write:
////////////////////////////////////////////////}
   newEvokeFun := @SetMeUp;
   y := newEvokeFun(MR.x);
   st := Format('Value: %6.5f', [y]);
   writeln(st);
   setlength(MR.x,0);
   readln;
end.
{
"data.txt" file contains:
evokeFun
1
  3.141590000000000E+000
  2.718280000000000E+000
}
I hope I have been clear in my request.
Thank you very much.

Jilani

Reply by email

Daniël Mantione
to Jilani Khaldi

Op Wed, 10 Oct 2007, schreef Jilani Khaldi:

Show quoted text 5 lines
{//////////////////////////////////////////////// Is there a way to "connect" the name of the function as a string "evoke" to the true function "evokeFun: SetupFun;" getting a new function "newEvokeFun: SetupFun;" so I can write: ////////////////////////////////////////////////}

Create a table with function names and the actual function, for example:

type  Tsetupfunc_mapping=record
        name:string;
        func:setupfunc;
      end;

const setupfunc_mapping:array[0..2] of Tsetupfunc_mapping=( (name:'setup_function_1';),
(name:'setup_function_2';),

       (name:'setup_function_3';));

Then search the name of the function you want to call in the table, then call the function in the "func" field.

Daniël

Reply by email

Jilani Khaldi
to Daniël Mantione
Show quoted text 12 lines
type Tsetupfunc_mapping=record name:string; func:setupfunc; end; const setupfunc_mapping:array[0..2] of Tsetupfunc_mapping=( (name:'setup_function_1';), (name:'setup_function_2';), (name:'setup_function_3';)); Then search the name of the function you want to call in the table, then call the function in the "func" field.

Thanks, but the consts should be:
const setupfunc_mapping: array[0..2] of Tsetupfunc_mapping=( (name: 'setup_function_1'; func: setup_function_1), (name: 'setup_function_2'; func: setup_function_2),

         (name: 'setup_function_3'; func: setup_function_3));

However compiling with {$mode objfpc} I get the error: ** Error: Some fields coming before "name" weren't initialized and it dosen't compile.
While with {$mode delphi} it compiles but with a simple warning: ** Warning: Some fields coming before "name" weren't initialized The question now is how to get it compiling (and executing) without error using {$mode objfpc}?
Thanks!

ps
the code compiles and runs without warnings using Delphi.

Jilani KHALDI

Reply by email

Daniël Mantione
to Jilani Khaldi

y

Op Wed, 10 Oct 2007, schreef Jilani Khaldi:

Show quoted text 20 lines
> type Tsetupfunc_mapping=record > name:string; > func:setupfunc; > end; > const setupfunc_mapping:array[0..2] of Tsetupfunc_mapping=( > (name:'setup_function_1';), > (name:'setup_function_2';), > (name:'setup_function_3';)); > > Then search the name of the function you want to call in the table, then > call the function in the "func" field. Thanks, but the consts should be: const setupfunc_mapping: array[0..2] of Tsetupfunc_mapping=( (name: 'setup_function_1'; func: setup_function_1), (name: 'setup_function_2'; func: setup_function_2), (name: 'setup_function_3'; func: setup_function_3)); However compiling with {$mode objfpc} I get the error: ** Error: Some fields coming before "name" weren't initialized and it dosen't compile.

Did you implement the record exactly as shown? This error normally occurs if you have fields in your record that you don't mention in your typed const declaration.

Daniël

Reply by email

Jilani Khaldi
to Jilani Khaldi: Thanks, but the consts should be: const setupfunc_mapping: array[0..2] of…

Hi All,
just curious about the D language
(http://www.digitalmars.com/d/index.html), I read some articles on the site, downloaded the compiler... and wrote some little examples. Well, many of the things that the author presented as new and hot features are already present in Turbo Pascal or in Delphi for a decade (objects by reference, strings...).
The only thing I see it has more than Object Pascal is Generics (templates) and they seems to be well implemented there. I think, with templates, Object Pascal will be the only "human" high level complete and mature language.

Jilani KHALDI

Reply by email

Florian Klaempfl
to Jilani Khaldi
Show quoted text 12 lines
Hi All, just curious about the D language (http://www.digitalmars.com/d/index.html), I read some articles on the site, downloaded the compiler... and wrote some little examples. Well, many of the things that the author presented as new and hot features are already present in Turbo Pascal or in Delphi for a decade (objects by reference, strings...). The only thing I see it has more than Object Pascal is Generics (templates) and they seems to be well implemented there. I think, with templates, Object Pascal will be the only "human" high level complete and mature language.

What's wrong with
http://www.freepascal.org/docs-html/ref/refch8.html#x79-860008 ?

Reply by email

Leonardo M. Ram�
to Florian Klaempfl

See http://www.freepascal.org/docs-html/ref/refse39.html

I was reading the document and noted this hughe image with blanks http://www.freepascal.org/docs-html/ref/ref100x.png that really looks bad.

Anyway, nothing related to the subject.

Leonardo M. Ramé
http://leonardorame.blogspot.com

____________________________________________________________________________________ Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out. http://answers.yahoo.com/dir/?link=list&sid=396545469

Reply by email

Matt Emson
to Jilani Khaldi: just curious about the D language (http://www.digitalmars.com/d/index.html), I read some…
Jilani Khaldi wrote 7 lines
Hi All, just curious about the D language (http://www.digitalmars.com/d/index.html), I read some articles on the site, downloaded the compiler... and wrote some little examples. Well, many of the things that the author presented as new and hot features are already present in Turbo Pascal or in Delphi for a decade (objects by reference, strings...).

I only with that Walter would take on-board Sets ala Object Pascal. They get all these crazy, crazy set implementations popping up in the D forums, but if you demonstrate how OP does it, we get a "that's nice" and it's totally ignored... well last time I looked, so maybe he did implement sets now.

M

Reply by email

Jilani Khaldi
to Daniël Mantione: y Op Wed, 10 Oct 2007, schreef Jilani Khaldi: Did you implement the record exactly as…
Did you implement the record exactly as shown? This error normally occurs if you have fields in your record that you don't mention in your typed const declaration.
{$mode objfpc} -> Error!
{$mode delphi} -> Ok!
...
type
   TSetupFuncMapping = record
     x: array of double;
     name:string;
     func: SetupFunc;
   end;

const
SFM: array[0..2] of TSetupFuncMapping = (
(x: nil; name: 'SetupFunction_1'; func: SetupFunction_1), (x: nil; name: 'SetupFunction_2'; func: SetupFunction_2),

   (x: nil; name: 'SetupFunction_3'; func: SetupFunction_3));

Jilani

Jilani KHALDI

Reply by email

Florian Klaempfl
to Mattias Gaertner
Show quoted text 10 lines
On Wed, 10 Oct 2007 16:12:03 +0200 Florian Klaempfl wrote: > Jilani Khaldi schrieb: >>> What's wrong with >>> http://www.freepascal.org/docs-html/ref/refch8.html#x79-860008 ? >> Nothing, once completed. > Well, it's completed or do you miss things? but still under testing ;) Is there already a generic tree example?

Not a tree, but a list and a map:
<http://svn.freepascal.org/svn/fpc/trunk/rtl/objpas/fgl.pp>

Reply by email

Adrian Veith
to Jilani Khaldi: just curious about the D language (http://www.digitalmars.com/d/index.html), I read some…
Show quoted text 7 lines
Hi All, just curious about the D language (http://www.digitalmars.com/d/index.html), I read some articles on the site, downloaded the compiler... and wrote some little examples. Well, many of the things that the author presented as new and hot features are already present in Turbo Pascal or in Delphi for a decade (objects by reference, strings...).

oh D has more interesting things for a compiled language:
- dynamic closures or delegates to nested functions
- lazy evaluation
- template mixins
- garbage collection

I am not sure if all these things are really useful - but it looks interesting. especially the delegates stuff.

What I don't understand is, that they haven't changed the idiotic switch statement.

cheers,

Adrian.

Reply by email

Tomas Hajny
to Jilani Khaldi: {$mode objfpc} -> Error! {$mode delphi} -> Ok! ... type TSetupFuncMapping = record x:…
Jilani Khaldi wrote 20 lines
> Did you implement the record exactly as shown? This error normally > occurs > if you have fields in your record that you don't mention in your typed > const declaration. {$mode objfpc} -> Error! {$mode delphi} -> Ok! ... type TSetupFuncMapping = record x: array of double; name:string; func: SetupFunc; end; const SFM: array[0..2] of TSetupFuncMapping = ( (x: nil; name: 'SetupFunction_1'; func: SetupFunction_1), (x: nil; name: 'SetupFunction_2'; func: SetupFunction_2), (x: nil; name: 'SetupFunction_3'; func: SetupFunction_3));

I only get a RTE when compiling and running the following with 2.0.4, not a compile time error.

Tomas

{$MODE OBJFPC}
uses
   SysUtils;
type
   SetupFun = function(const vect: array of double): double;
MyRec = record
   x: array of double;
   myFunName: ShortString; // function name to evoke
end;
type
   TSetupFuncMapping = record
     x: array of double;
     name:string;
     func: SetupFun;
   end;
function SetupFunction_1 (const vect: array of double): double;
begin
 SetupFunction_1 := 1;
end;
function SetupFunction_2 (const vect: array of double): double;
begin
 SetupFunction_2 := 2;
end;
function SetupFunction_3 (const vect: array of double): double;
begin
 SetupFunction_3 := 3;
end;
function SetMeUp(const x1: array of double): double;
var
   i: integer;
   sum: double;
begin
   sum := 0.0;
   for i := 0 to High(x1) do
     sum := sum + x1[i];
   result := sum;
end;

const
SFM: array[0..3] of TSetupFuncMapping = (
(x: nil; name: 'SetupFunction_1'; func: @SetupFunction_1), (x: nil; name: 'SetupFunction_2'; func: @SetupFunction_2), (x: nil; name: 'SetMeUp'; func: @SetMeUp),

   (x: nil; name: 'SetupFunction_3'; func: @SetupFunction_3));
// main
var
   MR: MyRec;
   y: double;
   st: string;
   f: text;
   i, j, count: integer;
begin
   // read MR from a text file...
   assign(f, 'data.txt');
   {$I-}
   reset(f);
   {$I+}
   readln(f, MR.myFunName);
   readln(f, count);
   setlength(MR.x,count+1);
   for i := 0 to count do
     readln(f, MR.x[i]);
   closefile(f);
   writeln(MR.myFunName);
   writeln(High(MR.x));
   for i := 0 to High(MR.x) do
     begin
      writeln(MR.x[i]);
      for J := 0 to 3 do
       if SFM [J].Name = MR.MyFunName [J] then
        y := SFM [J].Func (MR.X);
      st := Format('Value: %6.5f', [y]);
      writeln(st);
      setlength(MR.x,0);
     end;
   readln;
end.

Reply by email

Mattias Gaertner
to Florian Klaempfl: Not a tree, but a list and a map: <http://svn.freepascal.org/svn/fpc/trunk/rtl/objpas/fgl.…
Show quoted text 15 lines
Mattias Gaertner schrieb: > On Wed, 10 Oct 2007 16:12:03 +0200 > Florian Klaempfl wrote: > >> Jilani Khaldi schrieb: >>>> What's wrong with >>>> http://www.freepascal.org/docs-html/ref/refch8.html#x79-860008 ? >>> Nothing, once completed. >> Well, it's completed or do you miss things? but still under >> testing ;) > > Is there already a generic tree example? Not a tree, but a list and a map: <http://svn.freepascal.org/svn/fpc/trunk/rtl/objpas/fgl.pp>

It seems the map works with a list instead of a hash or a tree. Does the STL map work the same?

Mattias

Reply by email

Jilani Khaldi
to Adrian Veith: oh D has more interesting things for a compiled language: - dynamic closures or delegates…
Show quoted text 8 lines
oh D has more interesting things for a compiled language: - dynamic closures or delegates to nested functions - lazy evaluation - template mixins - garbage collection I am not sure if all these things are really useful - but it looks interesting. especially the delegates stuff.

I think the main problem of Java, D, C++... (and even Object Pascal) is the "syndrome of giantism". They are getting bigger and bigger all the time. C, instead, doesn't suffer of this and it is always a main language. I think we have to restart from Turbo Pascal 3.0.

Jilani KHALDI

Reply by email

Marco Ciampa
to Jilani Khaldi: just curious about the D language (http://www.digitalmars.com/d/index.html), I read some…
Jilani Khaldi wrote 7 lines
Hi All, just curious about the D language (http://www.digitalmars.com/d/index.html), I read some articles on the site, downloaded the compiler... and wrote some little examples. Well, many of the things that the author presented as new and hot features are already present in Turbo Pascal or in Delphi for a decade (objects by reference, strings...).

Yes, it's so sad to see people that waste time reinventing the wheel...

Marco Ciampa +--------------------+ | Linux User #78271 | | FSFE fellow #364 | +--------------------+

Reply by email

Matt Emson
to Marco Ciampa
Marco Ciampa wrote
Yes, it's so sad to see people that waste time reinventing the wheel...

To be fair to Walter, you have to look at his history. He wrote a lot of different compiler implementations over the years - this is pretty much what he's been doing *since* the time of Turbo Pascal. He was responsible for the Symantec C++ compiler, which has a lineage back to some old DOS compiler that was either the first or at least one of the first to implement C++ outside of the original implementation (the parser that parsed to plain C.) He also wrote the native Java compiler from the Visual Cafe for Windows product line (very nice compiler for its time.) Digital Mars C++ has a direct line back to Symantec C++ IIRC.

So D is Walter's, "done a lot of C style syntaxed compilers, but now I want one I actually like myself" trip. You need to forgive him for that. He was writing compilers when Florian was at Grade school ;-)

M

Reply by email