Re: Questions about anonymous functions and classes/functions declarations

Uli Kusterer <witness@t-online.de>
13 Oct 2003 00:18:36 -0400

          From comp.compilers

Related articles
Questions about anonymous functions and classes/functions declarations mrfaro@libero.it (Gabriele Farina) (2003-10-04)
Re: Questions about anonymous functions and classes/functions declarat derkgwen@HotPOP.com (Derk Gwen) (2003-10-06)
Re: Questions about anonymous functions and classes/functions declarat lsantil@calstatela.edu (Louis Paul Santillan) (2003-10-06)
Re: Questions about anonymous functions and classes/functions declarat haberg@matematik.su.se (2003-10-06)
Re: Questions about anonymous functions and classes/functions declarat joachim.durchholz@web.de (Joachim Durchholz) (2003-10-06)
Re: Questions about anonymous functions and classes/functions declarat geoff@wozniak.ca (Geoff Wozniak) (2003-10-08)
Re: Questions about anonymous functions and classes/functions declarat joachim.durchholz@web.de (Joachim Durchholz) (2003-10-08)
Re: Questions about anonymous functions and classes/functions declarat witness@t-online.de (Uli Kusterer) (2003-10-13)
Re: Questions about anonymous functions and classes/functions declarat marcov@stack.nl (Marco van de Voort) (2003-10-13)
| List of all articles for this month |

From: Uli Kusterer <witness@t-online.de>
Newsgroups: comp.compilers
Date: 13 Oct 2003 00:18:36 -0400
Organization: T-Online
References: 03-10-004
Keywords: design
Posted-Date: 13 Oct 2003 00:18:36 EDT

  "Gabriele Farina" <mrfaro@libero.it> wrote:
> I'm planning to try to implemente a programming language. I got a lot
> of ideas, and I know how to implements them except for anonymous
> functions. Where I have to store them?? I have to have a table only
> for anonymous functions?? Someone asks to me to treat them as
> constants, but I can't understanda how to develop it.


  Since usually functions aren't called directly by anyone, but rather
through some sort of jump address, I'd personally just store the
function anywhere and make the jump address a constant that is
assigned to whatever variable you want to assign the anonymous
function's pointer to. I.e., pseudocode:


      myFuncPtr = &myProc;


      fun myProc( a, b )
      {
            dosomething;
      }


generates the following (pseudo-assembler) code:


  30: moveaddr myProc, [1]
      . . .
100: myProc dosomething;
110: rts;


where [1] is some slot on the stack, and myProc is a label for the jump
address 100, which is entered in your symbol tabel under the name
"myProc". Now, what would the code


      myFuncPtr = &function(a,b) { dosomething; };


generate? Pretty much the same code, just that there is no label myProc.
Instead, you just generate the code as:


  30: moveaddr 100, [1]
      . . .
100: dosomething;
110: rts;


Which is pretty much the same. You just make the jump address a constant
in your code, and you don't enter it in the symbol table.


> Now there is another questions: I'd like to give the users the ability
> to edit a class definition at runtime. For example:
>
> Do you think this can be useful and can be implemented?? There could be any
> problems??


  I think what you're doing is called "prototype-based programming" and
is supported by a number of languages, like Io and SELF.


  It is pretty useful for certain kinds of projects, especially when you
have a domain where you have many classes from which only one object
will be created. Text adventures are a good example, where almost each
item implements its own behavior, and you'd spend half your life writing
subclass definitions and code that instantiates one object of that type.


  You have to be careful in your object layout, though. Since in
prototype-based programming, you can modify a class, you can end up with
a situation where you add a data member to a class, and suddenly all of
its subclasses have to adapt to the new size of the base class.


Cheers,
-- M. Uli Kusterer
http://www.zathras.de


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.