How to unify first class functions / lexical subs and pir builtins in HLL ?
Austin Hastings
Austin_Hastings at Yahoo.com
Fri Jun 5 18:23:37 UTC 2009
Gabriele,
I'm in a similar situation with a plain old procedural language. I want
to have builtins for things that parrot provides as pir instructions.
However, I've noticed that the pir-provided builtins are generally
primitive. For example, the "print" opcode only accepts one argument,
the push and unshift opcodes only move one value to the deque, etc.
I think (and have implemented) that the solution is to define "builtins"
that mimic the names of the pir opcodes,
but which do "the right thing" vis-a-vis multiple args, etc.
In my case, that makes the builtins into language-magic. In your case,
if you want them to be first class functions you will have to code some
functions around them.
What you might consider is building in an "inline pir" function, and
defining everything else in terms of that.
(I put an asm(args) {{ block }} in my language, but my builtins were
handled by having the compiler generate the inline code.)
Cheers,
=Austin
gabriele renzi wrote:
> Hi everyone,
>
> I'm not sure this is the right place but I don't remember a
> parrot-users mailing list (and at least it's not listed on the parrot
> website).
>
> Anyway, I'm writing a small article about parrot and I was
> implementing a tiny language with lexical scope and first class
> functions.
> Although I know how to create closures in my language and I can call
> them ok, I still have a problem:
>
> if I use variable lookup to access my functions, it doesn't find the
> pir-defined routines, while if I use the normal function lookup it
> does not consider the lexical functions defined as variables.
>
> I did solve this once managing scopes explicitly in the grammar
> (following the Squaak example) and having something like
>
> for (scopes() )
> if $_.symbol($<name>)
> call closure
> found = true
> if not found
> call fun $<name>
>
> but I don't like this and it still does not allow passing the builtins
> around (moreover I was happily avoiding the whole manual scope
> management :).
>
>
> It seems to me there are a few options,
> * install the symbols explicitly in the top level namespace (should
> work, but requires duplicated effort when defining a new builtin)
> * have some trap function in the variable lookup mechanic that fetches
> the function if all else fails (nice, seems to be python's behaviour,
> but is it possible?)
> * register the namespace where functions are defined (package?) as the
> parent namespace for the top level scope (a PAST::Block/immediate
> defined in TOP)
>
> All of these, though, I have no clue on how to implement :)
> As this seems a problem common to many lisp-1-ish languages (python,
> ecmascript, scheme) I guess there is a proper way of doing it but I
> don't know how to approach it, is there a documented way or an obvious
> way I'm overlooking? Is this something worth documenting for future
> implementors?
>
> Thanks in advance, and sorry if I ask dumb questions.
> _______________________________________________
> http://lists.parrot.org/mailman/listinfo/parrot-dev
>
>
More information about the parrot-dev
mailing list