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 declar daniel_yokomiso@yahoo.com.br (Daniel Yokomiso) (2003-10-06) |
From: | "Daniel Yokomiso" <daniel_yokomiso@yahoo.com.br> |
Newsgroups: | comp.compilers |
Date: | 6 Oct 2003 21:22:11 -0400 |
Organization: | Compilers Central |
References: | 03-10-004 |
Keywords: | design, code |
Posted-Date: | 06 Oct 2003 21:22:11 EDT |
"Gabriele Farina" <mrfaro@libero.it> escreveu na mensagem
> 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?? ...
> [The only way I know to handle anonymous functions is to give each on a
> secret private name at compile time. As far as adding new methods at
> runtime, it's doable but it means that you have to keep most of the
> compiler symbol table at runtime and possibly the whole compiler if
> you're adding new code on the fly. -John]
Hi,
There's another way to handle anonymous functions. You just emit bytecode,
or real assembly, or whatever that looks like this:
f = function (x) return x + 1
f(1)
becomes:
assign f, <address of anonymous function>
push 1
push f
call
In other words compile every anonymous function to a memory location,
as you would do with named functions, push the parameters into a stack
(or your parameter placeholders) and tell the computer to jump to the
memory address holding the anonymous function, execute it and after it
ends go back. If you're using a interpreter you can turn all method
calls, named or anonymous, into this model. Ditto for a compiled
language. You'll just need the compiler on runtime if you want to
"eval" code dinamically.
For your class model you can make your vtables writable, so you can
either bind old function names to new implementations (as long as you
keep the same interface) or even bind new function names. In the
latter you'll probably better of using a message map (a la smalltalk),
in other words a hashtable from function interfaces (name + number of
parameters) to method implementations.
Best regards,
Daniel Yokomiso.
Return to the
comp.compilers page.
Search the
comp.compilers archives again.