create function at runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • invincible

    create function at runtime

    hi friends , how can I declare / create function during runtime similiar to
    lambda in lisp.

    thanks

    Mohan


  • Richard Bos

    #2
    Re: create function at runtime

    "invincible " <mohan.bhakri@i n.bosch.com> wrote:
    [color=blue]
    > hi friends , how can I declare / create function during runtime similiar to
    > lambda in lisp.[/color]

    You cannot. Not in ISO C, anyway; and not in any dialect of C that I'm
    aware of without resorting to the most hairy of hacks. It would probably
    involve writing machine code directly, and you really don't want to do
    that unless you absolutely must. Note that "absolutely must" includes
    "cannot possibly use a language which is better suited for this; at a
    pinch, even using Clipper will be better than munging your own code
    space."

    Richard

    Comment

    • DeltaOne

      #3
      Re: create function at runtime

      Well there is no facility in C language for this. Well if you want to
      create something like this you can try ony way that is giving a feel
      that actually you are creating a function at run time but in actual you
      dont...You take input and parse the input yourself and the if a
      statement like this is there::
      printf("hello world")
      then make it str="hello World";
      and puts(str);

      Comment

      • Chris McDonald

        #4
        Re: create function at runtime

        "DeltaOne" <shakti.misra@w ipro.com> writes:
        [color=blue]
        >Well there is no facility in C language for this. Well if you want to
        >create something like this you can try ony way that is giving a feel
        >that actually you are creating a function at run time but in actual you
        >dont...You take input and parse the input yourself and the if a
        >statement like this is there::
        >printf("hell o world")
        >then make it str="hello World";
        >and puts(str);[/color]


        <OT>
        .... or your process writes the C text to a file, spawns other
        processes to compile and link the C, then dynamically load the resulting
        shared object into the running process, if your operating system
        supports it.
        </OT>

        --
        Chris.

        Comment

        • Keith Thompson

          #5
          Re: create function at runtime

          "invincible " <mohan.bhakri@i n.bosch.com> writes:[color=blue]
          > hi friends , how can I declare / create function during runtime similiar to
          > lambda in lisp.[/color]

          Probably by implementing a Lisp interpreter in C.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Chris McDonald

            #6
            Re: create function at runtime

            Keith Thompson <kst-u@mib.org> writes:
            [color=blue]
            >"invincible " <mohan.bhakri@i n.bosch.com> writes:[color=green]
            >> hi friends , how can I declare / create function during runtime similiar to
            >> lambda in lisp.[/color][/color]
            [color=blue]
            >Probably by implementing a Lisp interpreter in C.[/color]

            or a C interpreter in C:



            --
            Chris.

            Comment

            • Mark McIntyre

              #7
              Re: create function at runtime

              On Thu, 19 May 2005 06:29:33 GMT, in comp.lang.c ,
              rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
              [color=blue]
              >"invincible " <mohan.bhakri@i n.bosch.com> wrote:
              >[color=green]
              >> hi friends , how can I declare / create function during runtime similiar to
              >> lambda in lisp.[/color]
              >
              >You cannot. Not in ISO C, anyway; and not in any dialect of C that I'm
              >aware of without resorting to the most hairy of hacks.[/color]

              You could write an interpreter, and your C programme could write the
              fn out to file / into memory, invoke the interpreter on it, and
              process the results.

              --
              Mark McIntyre
              CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
              CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

              ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
              http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
              ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

              Comment

              • Flash Gordon

                #8
                Re: create function at runtime

                Chris McDonald wrote:[color=blue]
                > "DeltaOne" <shakti.misra@w ipro.com> writes:
                >[color=green]
                >>Well there is no facility in C language for this. Well if you want to
                >>create something like this you can try ony way that is giving a feel
                >>that actually you are creating a function at run time but in actual you
                >>dont...You take input and parse the input yourself and the if a
                >>statement like this is there::
                >>printf("hel lo world")
                >>then make it str="hello World";
                >>and puts(str);[/color]
                >
                > <OT>
                > ... or your process writes the C text to a file, spawns other
                > processes to compile and link the C, then dynamically load the resulting
                > shared object into the running process, if your operating system
                > supports it.
                > </OT>[/color]

                Or you embed a C interpreter written in standard C in your program.
                --
                Flash Gordon
                Living in interesting times.
                Although my email address says spam, it is real and I read it.

                Comment

                • Richard Bos

                  #9
                  Re: create function at runtime

                  Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                  [color=blue]
                  > On Thu, 19 May 2005 06:29:33 GMT, in comp.lang.c ,
                  > rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                  >[color=green]
                  > >"invincible " <mohan.bhakri@i n.bosch.com> wrote:
                  > >[color=darkred]
                  > >> hi friends , how can I declare / create function during runtime similiar to
                  > >> lambda in lisp.[/color]
                  > >
                  > >You cannot. Not in ISO C, anyway; and not in any dialect of C that I'm
                  > >aware of without resorting to the most hairy of hacks.[/color]
                  >
                  > You could write an interpreter, and your C programme could write the
                  > fn out to file / into memory, invoke the interpreter on it, and
                  > process the results.[/color]

                  Sure, but that's a whole different thing. Then you're creating _another_
                  programming environment within the larger program, the language of which
                  may be deceptively similar to the one your main program is written in.
                  AFAIAA all lambda functions in all Lisp-alikes create new functions as a
                  part of the main program itself.

                  Richard

                  Comment

                  • Arafangion

                    #10
                    Re: create function at runtime

                    Chris McDonald wrote:[color=blue]
                    > Keith Thompson <kst-u@mib.org> writes:
                    >
                    >[color=green]
                    >>"invincible " <mohan.bhakri@i n.bosch.com> writes:
                    >>[color=darkred]
                    >>>hi friends , how can I declare / create function during runtime similiar to
                    >>>lambda in lisp.[/color][/color]
                    >
                    >[color=green]
                    >>Probably by implementing a Lisp interpreter in C.[/color]
                    >
                    >
                    > or a C interpreter in C:
                    >
                    > http://www.softintegration.com/products/chstandard/
                    >[/color]
                    www.enlightenment.org has a nice implementation of Small, a C-like
                    language, called Embryo.
                    Apparently it's very fast, and contains a small subset of C.

                    Comment

                    • SM Ryan

                      #11
                      Re: create function at runtime

                      "invincible " <mohan.bhakri@i n.bosch.com> wrote:
                      # hi friends , how can I declare / create function during runtime similiar to
                      # lambda in lisp.

                      One way is to write the function to a file; call a compiler using system("cc ....")
                      to write a .so or .dll or .dylib or some other operating system specific format;
                      and then use operating system specific dynamic linking libraries to load it into
                      your address space.

                      Another way is to malloc a byte vector and then store the machine instruction code
                      in the vector. You then convert the data vector address to a function address and
                      call the function. However function addresses are not always the same as a data
                      address and you may need to combine a system specific linkage address to the code
                      address to get a function pointer. Also if you're using virtual memory, this would
                      often get a page protection fault unless you change the page characterisitic s
                      in a system specific fashion.

                      Another possibility is to use an interpretter, perhaps a threaded interpretter.
                      Threaded interpretters are fast and once you have the internal interpretter written
                      (or simply find a Forth implementation written in C), easy to do. Threaded
                      interpretters don't run afoul of vm page protections.

                      --
                      SM Ryan http://www.rawbw.com/~wyrmwif/
                      OOOOOOOOOO! NAVY SEALS!

                      Comment

                      • Mark McIntyre

                        #12
                        Re: create function at runtime

                        On Thu, 19 May 2005 09:07:53 GMT, in comp.lang.c ,
                        rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                        [color=blue]
                        >Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                        >[color=green]
                        >> On Thu, 19 May 2005 06:29:33 GMT, in comp.lang.c ,
                        >> rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                        >>[color=darkred]
                        >> >"invincible " <mohan.bhakri@i n.bosch.com> wrote:
                        >> >
                        >> >> hi friends , how can I declare / create function during runtime similiar to
                        >> >> lambda in lisp.
                        >> >
                        >> >You cannot. Not in ISO C, anyway; and not in any dialect of C that I'm
                        >> >aware of without resorting to the most hairy of hacks.[/color]
                        >>
                        >> You could write an interpreter, and your C programme could write the
                        >> fn out to file / into memory, invoke the interpreter on it, and
                        >> process the results.[/color]
                        >
                        >Sure, but that's a whole different thing.[/color]

                        He said he wanted to create a function during runtime, the above meets
                        the requirement. YMMV.
                        [color=blue]
                        > Then you're creating _another_ programming environment within the larger program,[/color]

                        So what? He didn't say this was forbidden.
                        [color=blue]
                        >the language of which may be deceptively similar to the one your main program is written in.[/color]

                        Again, so what?
                        [color=blue]
                        >AFAIAA all lambda functions in all Lisp-alikes create new functions as a
                        >part of the main program itself.[/color]

                        I've no clue about lisp, you may be right, but IMO its irrelevant.
                        --
                        Mark McIntyre
                        CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                        CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                        ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
                        http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
                        ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

                        Comment

                        • Richard Bos

                          #13
                          Re: create function at runtime

                          Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                          [color=blue]
                          > On Thu, 19 May 2005 09:07:53 GMT, in comp.lang.c ,
                          > rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                          >[color=green]
                          > >Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                          > >[color=darkred]
                          > >> On Thu, 19 May 2005 06:29:33 GMT, in comp.lang.c ,
                          > >> rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                          > >>
                          > >> >"invincible " <mohan.bhakri@i n.bosch.com> wrote:
                          > >> >
                          > >> >> hi friends , how can I declare / create function during runtime similiar to
                          > >> >> lambda in lisp.
                          > >> >
                          > >> >You cannot. Not in ISO C, anyway; and not in any dialect of C that I'm
                          > >> >aware of without resorting to the most hairy of hacks.
                          > >>
                          > >> You could write an interpreter, and your C programme could write the
                          > >> fn out to file / into memory, invoke the interpreter on it, and
                          > >> process the results.[/color]
                          > >
                          > >Sure, but that's a whole different thing.[/color]
                          >
                          > He said he wanted to create a function during runtime, the above meets
                          > the requirement. YMMV.[/color]

                          He said he wanted it to be similar to lambda-functions in Lisp. I'm not
                          a Lisp expert, but TTBOMK Lisp lambda functions are fully functional
                          functions, equivalent to compile-time functions in all regards except
                          their time of creation. In C terms this would imply, for example, being
                          able to pass its address to qsort() and getting the right results.

                          Richard

                          Comment

                          • Mark McIntyre

                            #14
                            Re: create function at runtime

                            On Fri, 20 May 2005 08:13:40 GMT, in comp.lang.c ,
                            rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote:
                            [color=blue]
                            >Mark McIntyre <markmcintyre@s pamcop.net> wrote:
                            >[color=green]
                            >>
                            >> He said he wanted to create a function during runtime, the above meets
                            >> the requirement. YMMV.[/color]
                            >
                            >He said he wanted it to be similar to lambda-functions in Lisp.[/color]

                            IME 'similar to' doesn't mean 'identical to' but YMMV. Anyhoo, we're
                            approximately in agreement I suspect.


                            --
                            Mark McIntyre
                            CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
                            CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt >

                            Comment

                            Working...